0

我正在尝试在我的登录名中添加新的自定义错误,如果 user_status 为 '0' 那么它应该给出错误,我按如下方式进行:

用户身份.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;
    }

在 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;

现在我遇到的问题是,当 user_Status 为 !=1 时,它会正确给出错误,但也会给出,密码不正确,而密码正确

4

2 回答 2

2

您忘记break在最后一个 case 语句中添加 a。没有休息,default将被执行。

...
    break;
case UserIdentity::ERROR_USERNAME_INACTIVE:
    $this->addError('user_status','Sorry, this user is not activated yet');
    break;
default: // UserIdentity::ERROR_PASSWORD_INVALID
...
于 2013-01-07T12:10:23.300 回答
0

抱歉,我犯了一个愚蠢的错误,在 switch case 之后,我忘记放 break 语句,所以下一个默认语句也在执行。

 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;
于 2013-01-07T12:09:49.430 回答