Alex Rivera | Logout

Yii::app()->user->isGuest always returns true even though login was successful

Asked 2011-06-02T03:39:36.773
10

I started to make some differences between those users which have authenticated and those that not. For this, i am using

Yii::app()->user->id;

However, in a determined view i put the following code:

<?php 
    if(Yii::app()->user->isGuest) {
        print("Welcome back Guest!");
        print("Your id is ".Yii::app()->user->id);
    } else {
        print("Welcome back ".Yii::app()->user->name);
        print("Your id is ".Yii::app()->user->id);
}?>

And i always get the "welcome back guest!", whether i have logged in (successfully) or not. And if i have logged in, then it displays the welcome message together with the user's id!

EDIT

@briiC.lv Hey.. sorry for the late reply, I hope you are still following this! I am not extending the given UserIdentity class. Is this mandatory? Since i still dont get very well the whole authorization issue, i thought it would be best to give a try with the class they provide, and then extend with my own functionality.. Anyway, next i post my UserIdentity class with its small tweaks.. maybe the problem lies here??

<?php class UserIdentity extends CUserIdentity{
private $_id;

public function authenticate()
{   
    $user = Users::model()->findAll('username=\''.$this->username.'\' AND password=\''.$this->encryptedPassword.'\'');
    if(!isset($user[0]))
    {
        return false;
    }
    else 
    {   
        $this->setState('id', $user[0]->id);            
        $this->username = $user[0]->username;
        $this->errorCode=self::ERROR_NONE;
        return true;
    }
}

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

}

Here is the output i got when i started to log as you suggested; i got this output immediately after successfully logging in.

[05:23:21.833][trace][vardump] CWebUser#1 ( 
[allo
Edit
Report

1 Answer

0

First of all, you need to know condition that sets guest and logged-in user apart.

Based on Yii master's branch CWebUser::getIsGuest():

public function getIsGuest()
{
    return $this->getState('__id')===null;
}

Compared to your code:

$user = Users::model()->findAll('username=\''.$this->username.'\' AND password=\''.$this->encryptedPassword.'\'');
if(!isset($user[0])) {
    // false
} else {   
    $this->setState('id', $user[0]->id); // this is for persistent state sakes
    ...
}

}

In short: you did supply 'id' to Identity persistent state but Yii CWebUser expecting '__id' based on UserIdentity::getId().

Solution is pretty dead simple. You just need to set $this->_id

$user = Users::model()->findAll('username=\''.$this->username.'\' AND password=\''.$this->encryptedPassword.'\'');
if(!isset($user[0])) {
    // false
} else {   
    $this->setState('id', $user[0]->id); // this is for persistent state sakes
    $this->_id = $user[0]->id; // this is UserIdentity's ID that'll be fetch by CWebUser
    ...
}

}

This routine explains how CWebUser get UserIdentity's ID: https://github.com/yiisoft/yii/blob/master/framework/web/auth/CWebUser.php#L221

Please do test it out.

answered 2012-11-23T03:36:12.387

Your Answer