Exemplo n.º 1
0
 /**
  * Authenticates the password.
  * This is the 'authenticate' validator as declared in rules().
  */
 public function authenticate($attribute, $params)
 {
     if (!$this->hasErrors()) {
         $identity = new UserIdentity($this->username, $this->password);
         if (0 == $this->isapi) {
             $identity->authenticate();
         } else {
             $identity->apiAuthenticate();
         }
         switch ($identity->errorCode) {
             case UserIdentity::ERROR_NONE:
                 $accessableProducts = TestUserService::getAccessableProduct($identity->getId());
                 if (empty($accessableProducts)) {
                     $this->addError('username', Yii::t('LoginForm', 'no accessable product'));
                 } else {
                     $duration = 0;
                     if ($this->rememberMe) {
                         // keep login state duration
                         $duration = LoginForm::DURATION;
                     }
                     Yii::app()->user->login($identity, $duration);
                     UserLogService::createUserLog(array('created_by' => Yii::app()->user->id, 'created_at' => date(CommonService::DATE_FORMAT), 'ip' => $_SERVER['REMOTE_ADDR']));
                     LoginService::setLanguageCookie($this->language);
                 }
                 break;
             case UserIdentity::ERROR_USERNAME_INVALID:
                 $this->addError('username', Yii::t('LoginForm', 'username is incorrect'));
                 break;
             case UserIdentity::ERROR_CONNECT:
                 $this->addError('username', Yii::t('LoginForm', 'ldap connect failed'));
                 break;
             case UserIdentity::ERROR_USER_DISABLED:
                 $this->addError('username', Yii::t('LoginForm', 'user disabled'));
                 break;
             case UserIdentity::ERROR_LDAP_MISS:
                 $this->addError('username', Yii::t('LoginForm', 'ldap module disabled'));
                 break;
             case UserIdentity::ERROR_USER_NOT_FOUND:
                 $this->addError('username', Yii::t('LoginForm', 'user not found') . '&nbsp;<a href="' . Yii::app()->createUrl('site/permission') . '">' . Yii::t('LoginForm', 'permission tips') . '</a>');
                 break;
             default:
                 // UserIdentity::ERROR_PASSWORD_INVALID {
                 $this->addError('password', Yii::t('LoginForm', 'password is incorrect'));
                 break;
         }
     }
 }