我有一个非常奇怪的问题,在一台机器上Yii::app()->user->id;返回用户名,但在运行相同代码的另一台机器上,我按预期获得了id号.如何Yii::app()->user->id获得用户名?我错过了什么?
首先,让我们来看看用户登录后发生的事情.
后
$identity->authenticate();
Run Code Online (Sandbox Code Playgroud)
如果
$identity->errorCode===UserIdentity::ERROR_NONE
Run Code Online (Sandbox Code Playgroud)
然后我们将进行登录操作
Yii::app()->user->login($identity,$duration)
Run Code Online (Sandbox Code Playgroud)
登录后面的内容是什么?
我扫描了yii的来源,主要的想法是
$this->changeIdentity($id,$identity->getName(),$states);
Run Code Online (Sandbox Code Playgroud)
在CWebUser类的登录功能中.
下面是changeIdentity函数
protected function changeIdentity($id,$name,$states)
{
Yii::app()->getSession()->regenerateID(true);
$this->setId($id);
$this->setName($name);
$this->loadIdentityStates($states);
}
Run Code Online (Sandbox Code Playgroud)
其次:让我们回顾一下这个问题
Yii::app()->user->id;
Run Code Online (Sandbox Code Playgroud)
这意味着运行扩展CWebUser的类的getId()方法,并在站点(应用程序的根目录)/protected/config/main.php中配置它,如下所示:
'components'=>array(
'user'=>array(
'class'=>'WebUser',
// enable cookie-based authentication
'allowAutoLogin'=>true,
),
Run Code Online (Sandbox Code Playgroud)
在我的例子中,类扩展CWebUser是WebUser.
在WebUser中,没有getId()方法,该方法将继承自CWebUser,因为WebUser是从CWebUser扩展而来的.所以这是来自CWebUser的getId()方法.
https://github.com/yiisoft/yii/blob/1.1.13/framework/web/auth/CWebUser.php#LC287
public function getId()
{
return $this->getState('__id');
}
Run Code Online (Sandbox Code Playgroud)
所以'__id'什么时候设置?显然,它设置在changeIdentity函数的声明中:
$this->setId($id);
Run Code Online (Sandbox Code Playgroud)
争论$ id值来自哪里?我们在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();
}
Run Code Online (Sandbox Code Playgroud)
该
$id = $identity->getId();
Run Code Online (Sandbox Code Playgroud)
所以我们只能在$ idenity中添加一个getId函数,这意味着我们在UserIdentity中添加了扩展CUserIdentity的getId函数,如下所示:
public function getId()
{
return $this->_id;
}
Run Code Online (Sandbox Code Playgroud)
和
public function setId($id)
{
$this->_id = $id;
return;
}
Run Code Online (Sandbox Code Playgroud)
当用户成功登录时,我们可以将user_id传递给UserIdentity的authenticate函数中的setId($ id),该函数扩展了CUserIdentity,如下所示: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;
}
Run Code Online (Sandbox Code Playgroud)
试试这个,设置用户名:
$this->_id=$user->id;
$this->username=$user->username;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12326 次 |
| 最近记录: |