1

yii では、以下を使用できます。

self::ERROR_USERNAME_INVALID;

もう1つ欲しい:

self::ERROR_USER_BANNED;

それはエラーを与える必要があります:

Sorry, but you cannot login because you account has been blocked.

これを設定するにはどうすればよいですか?

4

2 に答える 2

5

protected/components/UserIdentity.phpに直接追加します。

class UserIdentity extends CUserIdentity {
    const ERROR_USER_BANNED = -1; // say -1 you have to give some int value

    public function authenticate() {
        // ... code ...
        if (/* condition to check for banning */) { // you might want to put this check right after any other username checks, and before password checks
            $this->errorCode=self::ERROR_USER_BANNED;
            $this->errorMessage='Sorry, but you cannot login because your account has been blocked.'
            return $this->errorCode;
        }
    }
}

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 で別の関数を宣言して禁止をチェックし、その関数をisBannedバリデーターから呼び出すこともできauthenticateます。

于 2012-12-03T10:49:48.243 に答える
0

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;
}
于 2012-12-08T06:46:54.400 に答える