Login.php
00001 <?php
00012 class Dsao_Model_User_Login extends Dsao_Model_Abstract
00013 {
00017 protected $_auth = null;
00018
00022 protected $_user = null;
00023
00029 protected function _cookieRegenerate()
00030 {
00031
00032 $hashAutologin = Dsao_Hash_User::factory(null, '/user/login/autologin',
00033 array('idUser' => $this->_user->getId()))->insert()->deleteDuplicates();
00034
00035
00036 setrawcookie('autologin',
00037 $hashAutologin->getUri()->getUriString(),
00038 time()+(60*60*24*30),
00039 '/',
00040 $_SERVER['SERVER_NAME'],
00041 false,
00042 true);
00043 }
00044
00050 protected function _init()
00051 {
00052 $this->_auth = Zend_Auth::getInstance();
00053 }
00054
00060 protected function _writeUserToSession()
00061 {
00062
00063 $this->_user->refresh();
00064
00065
00066 $this->_user->setValid()->setReadOnly();
00067
00068
00069 $this->_auth->getStorage()->write(serialize($this->_user));
00070
00071
00072 $defaultNamespace = new Zend_Session_Namespace();
00073
00074
00075 if (!isset($defaultNamespace->initialized))
00076 {
00077 Zend_Session::regenerateId();
00078 $defaultNamespace->initialized = true;
00079 }
00080 }
00081
00087 public function cookieLogin()
00088 {
00089
00090 if (!isset($_COOKIE['autologin']))
00091 {
00092 return false;
00093 }
00094
00095
00096 $hashAutologin = Dsao_Hash_User::factory(null, $_COOKIE['autologin']);
00097
00098
00099 if ($hashAutologin->isValid())
00100 {
00101 $this->_user = new Dsao_User(array('id' => $hashAutologin->idUser));
00102
00103 $this->_writeUserToSession();
00104
00105 $this->_cookieRegenerate();
00106
00107 return true;
00108 }
00109
00110 return false;
00111 }
00112
00120 public function login(Dsao_User $user, $autologin = false)
00121 {
00122
00123 $authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Registry::get('db'));
00124 $authAdapter->setTableName('user_data')
00125 ->setIdentityColumn('username')
00126 ->setCredentialColumn('password')
00127 ->setIdentity($user->username)
00128 ->setCredential($user->password);
00129
00130
00131 $result = $this->_auth->authenticate($authAdapter);
00132
00133
00134 if ($result->isValid())
00135 {
00136
00137 $this->_user = new Dsao_User(array(
00138 'id' => $authAdapter->getResultRowObject('id')->id));
00139
00140 $this->_writeUserToSession();
00141
00142
00143 if ($autologin)
00144 {
00145 $this->_cookieRegenerate();
00146 }
00147
00148 return true;
00149 }
00150
00151 return false;
00152 }
00153
00159 public function logout()
00160 {
00161
00162 if (isset($_COOKIE['autologin']))
00163 {
00164 setrawcookie('autologin',
00165 '',
00166 time()-3600,
00167 '/',
00168 '.'.$_SERVER['SERVER_NAME'],
00169 false,
00170 true);
00171 }
00172
00173
00174 Zend_Auth::getInstance()->clearIdentity();
00175
00176
00177 Zend_Session::destroy(true);
00178 }
00179
00186 public function sendNewPasswordMail(Dsao_User $user)
00187 {
00188
00189 $content = Dsao_View_Smarty::sprintf
00190 ('user_login_password_email_content',
00191 array
00192 (
00193 'password' => $user->passwordNew->getPassword(),
00194 'uri' => $user->hash->getUri()
00195 ->getUriString(true, false, true),
00196 ));
00197
00198
00199 $subject = Zend_Registry::get('translate')->
00200 _('user_login_password_email_subject');
00201
00202 $mail = new Dsao_Mail($user, $subject, $content);
00203
00204
00205 return $mail->send();
00206 }
00207 }