Yii身份验证错误:登录成功后Yii :: app() - > user-> isGuest()始终返回true

时间:2013-05-30 18:07:35

标签: php yii

我开始在经过身份验证的用户和来宾用户之间创建差异。但总是当我识别用户并且它成功登录时,当我使用Yii :: app() - > user-> isGuest时它仍然返回true。这是我正在使用的代码:

if (Yii::app()->user->isGuest){
            echo 'Welcome back Guest';
    } else {
            echo 'Welcome back '.Yii::app()->user->name;
    }

我总是得到'欢迎回来的客人',无论我是否已登录(成功)。如果我再次登录,我收到此消息。这是用户识别代码。

class UserIdentity extends CUserIdentity {

    /**
     * Authenticates a user.
     */
    private $_id;

    public function authenticate() {
        $user = Users::model()->findByAttributes(array('username' => $this->username));
        if ($user === null)
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        else if ($user->password !== $this->password)
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        else {
            $this->_id = $user->id;
            $this->setState('title', $this->username);
            $this->setState('id', $user->id);   
            $this->errorCode = self::ERROR_NONE;
        }
        return !$this->errorCode;
    }

    public function getId() {
        return $this->_id;
    }

} 

请帮我摆脱这个问题。

4 个答案:

答案 0 :(得分:3)

您应该修复LoginForm

/**
 * Authenticates the password.
 * This is the 'authenticate' validator as declared in rules().
 */
public function authenticate($attribute, $params)
{
    // we only want to authenticate when no input errors
    if (! $this->hasErrors()) {
        $identity = new UserIdentity($this->email, $this->password);
        $identity->authenticate();
        switch ($identity->errorCode) {
            case UserIdentity::ERROR_NONE:
                $duration = ($this->rememberMe)
                    ? 3600*24*14 // 14 days
                    : 0; // login till the user closes the browser
                Yii::app()->user->login($identity, $duration);
                break;

            default:
                // UserIdentity::ERROR_USERNAME_INVALID
                // UserIdentity::ERROR_PASSWORD_INVALID
                // UserIdentity::ERROR_MEMBER_NOT_APPOVED
                $this->addError('', Yii::t('auth',
                    'Incorrect username/password combination.'));
                break;
        }
    }
}


class UsersController extends Controller 
{
    /**
     * Displays the login page
     */
    public function actionLogin() 
    {
        $model = new LoginForm;

        if (isset($_POST['LoginForm'])) {
            $model->attributes = $_POST['LoginForm'];

            $valid = $model->validate();
            if ($valid) {
                $this->redirect(Yii::app()->user->returnUrl);
            }
        }
        $this->render('login', array('model' => $model));
    }
}

你能展示你的配置吗?

答案 1 :(得分:1)

答案可以在yii authentication error

找到

您还可以在此处阅读有关yii身份验证的更多信息authentication and authorisation

答案 2 :(得分:0)

尝试此操作并查看使用Not(!)运算符

if (!Yii::app()->user->isGuest){
            echo 'Welcome back Guest';
    } else {
            echo 'Welcome back '.Yii::app()->user->name;
    }

答案 3 :(得分:0)

你应该这样做:

$this->setState('id', $user[0]->id);  

setState不应该用于'id'。要返回'id',您已经覆盖了getId()方法。

如果您确实需要进行设置,请更改为:

$this->_id = $user[0]->id;

希望这会有所帮助。

此致