如何为用户登录YII添加更多错误代码?

如何为用户登录YII添加更多错误代码?,yii,Yii,在yii中,我可以使用: self::ERROR_USERNAME_INVALID; 我想要另一个: self::ERROR_USER_BANNED; 这必须给出错误: Sorry, but you cannot login because you account has been blocked. 如何设置它?将其直接添加到受保护的/components/UserIdentity.php: LoginForm.php模型的默认方式: 添加新的验证器规则,对您的username字段说: p

在yii中,我可以使用:

self::ERROR_USERNAME_INVALID;
我想要另一个:

self::ERROR_USER_BANNED;
这必须给出错误:

Sorry, but you cannot login because you account has been blocked.
如何设置它?

将其直接添加到受保护的/components/UserIdentity.php:

LoginForm.php模型的默认方式:

添加新的验证器规则,对您的
username
字段说:

public function rules() {
    return array(
        // ... other rules ...
        array('username','isBanned')
    );
}

// the isbanned validator
public function isBanned($attribute,$params) {
    if($this->_identity===null)
        $this->_identity=new UserIdentity($this->username,$this->password);

    if($this->_identity->authenticate() === UserIdentity::ERROR_USER_BANNED){
        $this->addError($attribute,$this->_identity->errorMessage);
}
当然,您可以在UserIdentity中声明另一个函数来检查是否禁用,并从
isbanked
验证器调用该函数,而不是在
authenticate
函数中进行验证。

将其直接添加到受保护的/components/UserIdentity.php:

LoginForm.php模型的默认方式:

添加新的验证器规则,对您的
username
字段说:

public function rules() {
    return array(
        // ... other rules ...
        array('username','isBanned')
    );
}

// the isbanned validator
public function isBanned($attribute,$params) {
    if($this->_identity===null)
        $this->_identity=new UserIdentity($this->username,$this->password);

    if($this->_identity->authenticate() === UserIdentity::ERROR_USER_BANNED){
        $this->addError($attribute,$this->_identity->errorMessage);
}

当然,您可以在UserIdentity中声明另一个函数来检查是否禁止,并从
isbanding
验证器调用该函数,而不是在
authenticate
函数中进行验证。

在UserIdentity.php中添加以下内容

const ERROR_USER_BANNED = 12 ; #or whateve int value you prefer


public function getErrorMessageX() #or whatever method name
{
    switch ($this->errorCode)
    {
        case self::ERROR_USER_BANNED:
            return 'sorry, your account has been banned'; # custom error msg

        case self::ERROR_USERNAME_INVALID:
            return 'User does not exists';

        case self::ERROR_PASSWORD_INVALID:
            return 'Password does not match';

        case self::ERROR_ACCOUNT_NOT_CONFIRMED:           #this one is mine:)
            return 'This Account needs confirmation';
    }
}
现在在LoginForm.php中

public function authenticate( )
{
    $this->_identity = new UserIdentity($this->username,$this->password);

    if( $this->_identity->authenticate() === FALSE )
        $this->addError('username', $this->_identity->errorMessageX); #here

            #some more code

    return !$this->_identity->errorCode;
}

在UserIdentity.php中添加以下内容

const ERROR_USER_BANNED = 12 ; #or whateve int value you prefer


public function getErrorMessageX() #or whatever method name
{
    switch ($this->errorCode)
    {
        case self::ERROR_USER_BANNED:
            return 'sorry, your account has been banned'; # custom error msg

        case self::ERROR_USERNAME_INVALID:
            return 'User does not exists';

        case self::ERROR_PASSWORD_INVALID:
            return 'Password does not match';

        case self::ERROR_ACCOUNT_NOT_CONFIRMED:           #this one is mine:)
            return 'This Account needs confirmation';
    }
}
现在在LoginForm.php中

public function authenticate( )
{
    $this->_identity = new UserIdentity($this->username,$this->password);

    if( $this->_identity->authenticate() === FALSE )
        $this->addError('username', $this->_identity->errorMessageX); #here

            #some more code

    return !$this->_identity->errorCode;
}

这将取决于您登录用户的方式,默认方式是在LoginForm.php模型文件中设置错误,您正在使用它吗?是的。但不确定它到底在哪里说:“如果(错误用户禁止)$message='Bla Bla Bla'…这是我无法匹配的部分。谢谢bool.dev!你绝对是StackOverflow上的YII人!:-)谢谢你这么想,但是还有很多其他的好答案,所以我不是那个人。无论如何,更新了答案。编辑:再次阅读整个答案,不仅仅是添加的部分,还更改了以前发布的代码有一点取决于你是如何登录用户的,默认的方式是在LoginForm.php模型文件中设置错误,你在使用它吗?我是的。但不确定它到底在哪里说:“如果(错误用户被禁止)$message='blablablablabla'。。。这是我无法匹配的部分。谢谢bool.dev!你绝对是StackOverflow上的YII人!:-)谢谢你这么想,但是还有很多其他的好答案,所以我不是那个人。无论如何,更新答案。编辑:再次阅读整个答案,不仅仅是添加的部分,更改了之前发布的代码,也有一点遗漏不:记住根据method重命名属性不:记住根据method重命名属性