开发者

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

I have a very strange problem in that on one machine Yii::app()->user->id; returns the username, but on another开发者_StackOverflow中文版 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?


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;
}


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


Try this, set username:

$this->_id=$user->id;
$this->username=$user->username;


you have two way to solve this:

1) Yii::app()->user->setState("id",$user->id);
2) Yii::app()->user->id = $user->id;

both way uses $_SESSION in order to save the value.

then you can retriwe the value like that:

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


try this Yii::app()->user->getId()


Even I got the same problem, everything is ok but rather id I got username. You check your Useridentity file. In the else part you may miss something like pointer(->).

    {
        $this->_id = $user->id; /*here u might miss pointer -> */
        // print_r($this->_id);exit;
        $this->errorCode = self::ERROR_NONE;
    }

Hope it'll help you hurshid.


yii default installation sets user id as the name of the user (admin or demo by default) . If you change the user script and use sql and do the recommended modifications for that, id is returned as the id field in the table .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜