Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii::app()->user->id; is returning username instead of id number

Tags:

php

yii

I have a very strange problem in that on one machine Yii::app()->user->id; returns the username, but on another machine running identical code I'm getting the id number as expected. How could Yii::app()->user->id be getting the username? What have I missed?

like image 279
hurshid Avatar asked May 27 '11 11:05

hurshid


2 Answers

first at all,Let's take a look at what happened after user login.

after

    $identity->authenticate();

if

    $identity->errorCode===UserIdentity::ERROR_NONE

then we will go the login action

    Yii::app()->user->login($identity,$duration)

and what behind the login?

I have scan the source of yii the main idea is the

    $this->changeIdentity($id,$identity->getName(),$states);

in login function of CWebUser class.

below is changeIdentity function

    protected function changeIdentity($id,$name,$states)
{ 
    Yii::app()->getSession()->regenerateID(true);
    $this->setId($id);
    $this->setName($name);
    $this->loadIdentityStates($states);
}

secondly: Let's look back the problem

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

It means to run the getId() method of you class which extends CWebUser, and you config it in sites(the root of you application)/protected/config/main.php like this:

    'components'=>array(
    'user'=>array(
        'class'=>'WebUser',
        // enable cookie-based authentication
        'allowAutoLogin'=>true,
    ),

In my case ,the class extends CWebUser is WebUser.

In WebUser ,there is not getId() method, the method will be inherited from CWebUser, because WebUser extends from CWebUser. so this is getId() method from CWebUser.

https://github.com/yiisoft/yii/blob/1.1.13/framework/web/auth/CWebUser.php#LC287

     public function getId()
{
    return $this->getState('__id');
}

so when does the '__id' set at? obviously, it is set at the statement of changeIdentity function:

    $this->setId($id);

and where do the arguement $id value come from? we know it at below code in class CWebUser:

    public function login($identity,$duration=0)
{ 
    $id=$identity->getId();


    $states=$identity->getPersistentStates();



    if($this->beforeLogin($id,$states,false))
    {
        $this->changeIdentity($id,$identity->getName(),$states);

        if($duration>0)
        {
            if($this->allowAutoLogin)
                $this->saveToCookie($duration);
            else
                throw new CException(Yii::t('yii','{class}.allowAutoLogin must be set true in order to use cookie-based authentication.',
                    array('{class}'=>get_class($this))));
        }

        $this->afterLogin(false);
    }
    return !$this->getIsGuest();
}

the

    $id = $identity->getId();

so we can only add a getId function in $idenity, it means we add getId funciton in UserIdentity which extend CUserIdentity,like this:

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

and

public function setId($id)
{
    $this->_id = $id;
    return;
}

when user login successfully, we can pass the user_id to setId($id) in authenticate function of UserIdentity which extends CUserIdentity ,like this: public function authenticate() {

    $record=User::model()->findByAttributes(array('user_name'=>$this->username));

    if($record===null)
        $this->errorCode=self::ERROR_USERNAME_INVALID;
    else if($record->password!==md5($this->password))
        $this->errorCode=self::ERROR_PASSWORD_INVALID;
    else
    {
        $this->setId($record->user_id);


        $this->errorCode=self::ERROR_NONE;
    }
    return !$this->errorCode;
}
like image 135
fucus on Avatar answered Oct 16 '22 11:10

fucus on


You have to override method getId() in your UserIdentity class.

In your identity class, simply add this:

private $_id;

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

and then finally you could set id by using

$this->_id = $user->id

More on it, please refer this link: http://www.yiiframework.com/doc/guide/1.1/en/topics.auth#defining-identity-class

like image 30
Syakur Rahman Avatar answered Oct 16 '22 13:10

Syakur Rahman