Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ( 
[allowAutoLogin] => true 
[guestName] => 'Guest' 
[loginUrl] => array ( '0' => '/site/login' ) 
[identityCookie] => null 
[authTimeout] => null 
[autoRenewCookie] => false 
[autoUpdateFlash] => true 
[CWebUser:_keyPrefix] => '0f4431ceed8f17883650835e575b504b' 
[CWebUser:_access] => array() 
[behaviors] => array() 
[CApplicationComponent:_initialized] => true 
[CComponent:_e] => null 
[CComponent:_m] => null 
)

Any help is much appreciated!

like image 362
Soph Avatar asked Jun 02 '11 03:06

Soph


2 Answers

Maybe you can try to debug harder: change messages to something like this:

if(Yii::app()->user->isGuest) {
    print("Not logged");
} else {
    print_r(Yii::app()->user);
    print("Welcome ".Yii::app()->user->name);
    print("Your id is ".Yii::app()->user->id);

}

And check session variable in your config/main.php file

...
    'session' => array(
        'autoStart'=>true,
    ),
...
like image 178
briiC Avatar answered Nov 16 '22 01:11

briiC


The error is in the following line

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

As seen in the official yii documentation regarding auth & auth, setState should be used for anything but the id field. In order to implement the key Yii will use to identify your user, return a unique value per user in the Identity getId() function.

In your case, this means you simply have to change the above line into the following:

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

Regarding the actual inner working of the login procedure, I'd recommend a look at the CWebUser class, and especially at its login function, which is responsible for the actual storage of the Identity getId() return value.

like image 31
Andor Goetzendorff Avatar answered Nov 16 '22 00:11

Andor Goetzendorff