当前位置: 动力学知识库 > 问答 > 编程问答 >

adding error codes in yii login

问题描述:

I am trying to add new custom error in my login, if user_status is '0' then it should give error, I am proceeding as follow :

UserIdentity.php

const ERROR_USERNAME_INACTIVE=68;

public function authenticate()

{

$user = Users::model()->findByAttributes(array('email'=>$this->username));

if ($user===null)

{

$this->errorCode=self::ERROR_USERNAME_INVALID;

}

else if ($user->password !== md5($this->password))

{

$this->errorCode=self::ERROR_PASSWORD_INVALID;

}

else if($user->user_status !='1')

{

$this->errorCode=self::ERROR_USERNAME_INACTIVE;

}

else

{

$this->errorCode=self::ERROR_NONE;

$this->_id = $user->id;

}

return !$this->errorCode;

}

In login.php

case UserIdentity::ERROR_NONE:

Yii::app()->user->login($identity);

break;

case UserIdentity::ERROR_USERNAME_INVALID:

$this->addError('username','No Such user is associated with us .');

break;

case UserIdentity::ERROR_USERNAME_INACTIVE:

$this->addError('user_status','Sorry, this user is not activated yet');

default: // UserIdentity::ERROR_PASSWORD_INVALID

$this->addError('password','Password is incorrect.');

break;

Now I am getting problem that when the user_Status is !=1 then it is giving the error correctly but also giving, password incorrect while the password is correct

网友答案:

You forgot to add a break to the last case statement. Without a break the default will be executed.

...
    break;
case UserIdentity::ERROR_USERNAME_INACTIVE:
    $this->addError('user_status','Sorry, this user is not activated yet');
    break;
default: // UserIdentity::ERROR_PASSWORD_INVALID
...
网友答案:

Sorry, I was making a silly mistake, after switch case, i forget to put break statement so the next default statement was also executing.

 case UserIdentity::ERROR_USERNAME_INACTIVE:
             $this->addError('user_status','Sorry, this user is not activated yet');
             **break;**
                default: // UserIdentity::ERROR_PASSWORD_INVALID

                    $this->addError('password','Password is incorrect.');
                    break;
分享给朋友:
您可能感兴趣的文章:
随机阅读: