/** * This function needs to be overriden because user * might get deactivated by admin, or some states * might be changed by either admin or user himself. * TODO: finish working on this function and test it */ protected function restoreFromCookie() { $app = Yii::app(); $cookie = $app->getRequest()->getCookies()->itemAt($this->getStateKeyPrefix()); if ($cookie && !empty($cookie->value) && ($data = $app->getSecurityManager()->validateData($cookie->value)) !== false) { $data = unserialize($data); if (isset($data[0], $data[1], $data[2], $data[3])) { list($id, $name, $duration, $states) = $data; // this code is being rewritten: $this->changeIdentity($id,$name,$states); // below is the new code $identity = new _CUserIdentity($id, ''); $identity->authenticateByCookie(); switch ($identity->errorCode) { case _CUserIdentity::ERROR_NONE: $this->login($identity); // LOOKS LIKE MAIN CONTROLLER IS NOT INITIALIZED YET /*// set user preferences (for welcome message, and so on) if(isset(Yii::app()->user->interface) && !empty(Yii::app()->user->interface)) // set user preferred interface W3::setInterface(Yii::app()->user->interface); if(isset(Yii::app()->user->language) && !empty(Yii::app()->user->language)) // set user preferred language W3::setLanguage(Yii::app()->user->language); // set the welcome-back message MUserFlash::setTopSuccess(Yii::t('hint', '{screenName}, welcome back! Automatic authentication has been successfully passed.', array('{screenName}'=>'<strong>'.$this->getState('screenName').'</strong>') ));*/ break; case _CUserIdentity::ERROR_ACCOUNT_IS_INACTIVE: // set the error message /*MUserFlash::setTopError(Yii::t('hint', 'We are sorry, but your member account is marked as "inactive". Inactive member accounts are temporarely inaccessible. {contactLink}.', array('{contactLink}'=>CHtml::link(Yii::t('link','Contact us'),array('site/contact'))) ));*/ break; case _CUserIdentity::ERROR_UNKNOWN_IDENTITY: default: // should we call logout() here? //throw new CHttpException(401,Yii::t('yii','Unknown Identity')); break; } if ($this->autoRenewCookie) { $cookie->expire = time() + $duration; $app->getRequest()->getCookies()->add($cookie->name, $cookie); } } } }
/** * Authenticates the password. * This is the 'authenticate' validator as declared in rules(). */ public function authenticate($attribute, $params) { if (!$this->hasErrors()) { $identity = new _CUserIdentity($this->{self::getLoggingWithField()}, $this->password); $identity->authenticate(); switch ($identity->errorCode) { case _CUserIdentity::ERROR_NONE: // if user is already logged in if (!Yii::app()->user->isGuest) { // log user out from the current account. i want to sleep well, do you? ;) Yii::app()->user->logout(); if (!Yii::app()->getSession()->getIsStarted()) { // restore http session. this is necessary for login Yii::app()->getSession()->open(); } } // remember for 30 days. makes sence only if auto-login is allowed $duration = Yii::app()->user->allowAutoLogin && $this->rememberMe ? 3600 * 24 * 30 : 0; // log user in and save in session all appended data Yii::app()->user->login($identity, $duration); // set user preferences (for welcome message, and so on) if (isset(Yii::app()->user->interface) && !empty(Yii::app()->user->interface)) { // set user preferred interface W3::setInterface(Yii::app()->user->interface); } if (isset(Yii::app()->user->language) && !empty(Yii::app()->user->language)) { // set user preferred language W3::setLanguage(Yii::app()->user->language); } break; case _CUserIdentity::ERROR_USERNAME_INVALID: if (self::getLoggingWithField() === 'username') { $this->addError('username', Yii::t('t', 'Username is incorrect.')); } else { if (self::getLoggingWithField() === 'email') { $this->addError('email', Yii::t('t', 'Email is incorrect.')); } else { if (self::getLoggingWithField() === 'usernameOrEmail') { $this->addError('usernameOrEmail', Yii::t('t', 'Username or email is incorrect.')); } } } break; case _CUserIdentity::ERROR_ACCOUNT_IS_INACTIVE: // set the error message MUserFlash::setTopError(Yii::t('hint', 'We are sorry, but your member account is marked as "inactive". Inactive member accounts are temporarely inaccessible. {contactLink}.', array('{contactLink}' => CHtml::link(Yii::t('link', 'Contact us'), array('site/contact'))))); // add to username (first field in the login form) error css class // and make the validate() to fail $attribute = self::getLoggingWithField(); $attribute !== 'username' && $attribute !== 'email' && $attribute !== 'usernameOrEmail' && ($attribute = 'username'); $this->addError($attribute, ''); break; case _CUserIdentity::ERROR_IS_NOT_ADMINISTRATOR: // set the error message MUserFlash::setTopError(Yii::t('hint', 'We are sorry, but your access type is {accessType}. Required access type: {requiredAccessType}.', array('{accessType}' => Yii::app()->controller->var->userAccessType, '{requiredAccessType}' => Yii::t('t', User::ADMINISTRATOR_T)))); unset(Yii::app()->controller->var->userAccessType); // we do not need this any more // add to username (first field in the login form) error css class // and make the validate() to fail $attribute = self::getLoggingWithField(); $attribute !== 'username' && $attribute !== 'email' && $attribute !== 'usernameOrEmail' && ($attribute = 'username'); $this->addError($attribute, ''); break; case _CUserIdentity::ERROR_PASSWORD_INVALID: default: $this->addError('password', Yii::t('t', 'Password is incorrect.')); break; } } }