You are viewing revision #1 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.
By default, the expression Yii::app()->user returns a [CWebUser] [application component](http://www.yiiframework.com/doc/guide/basics.application#application-component) which can be used to store information that are closely related with the current user and should be persistent throughout the current user session. [CWebUser] already comes with a [name|CWebUser::name] property that stores the username of the current user.
In order to store more information, we need to modify the [identity|IUserIdentity] class used together with [CWebUser]. Each application may have one or several identity classes which are mainly responsible to provide ways of performing user authentication.
Here we use the UserIdentity class included in the testdrive application as an example, assuming our goal is to add the ID and the last login time of the user to [CWebUser]. We would modify UserIdentity as follows,
class UserIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
$user=User::model()->findByAttributes(array('username'=>$this->username));
if($user===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($user->password!==md5($this->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id=$user->id;
$this->lastLoginTime=$user->lastLoginTime;
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
}
public function getLastLoginTime()
{
return $this->getState('lastLoginTime');
}
public function setLastLoginTime($value)
{
return $this->setState('lastLoginTime',$value);
}
}
In the above we define a lastLoginTime property with getter/setter methods. We also override the getId() method to return a private variable. The reason that the id property is not defined like lastLoginTime is because id is a pre-defined property in [CUserIdentity] and is recognized by [CWebUser]. If we need to add more information, we should follow the way of defining lastLoginTime.
In the authenticate() method, we retrieve the user record according to the provided username. We populate the id and lastLoginTime properties if we find such a user record whose password matches the provided password (meaning successful authentication).
That's all we need. Now if we want to retrieve the id or lastLoginTime information in our code, we can do the following:
$id=Yii::app()->user->id;
$lastLoginTime=Yii::app()->user->getState('lastLoginTime');
// starting from 1.0.3 you can use the following:
// $lastLoginTime=Yii::app()->user->lastLoginTime;
Setting are stored in cookies!
Please, pay close attention, to what article author said: "When cookie-based authentication is enabled, these persistent information will be stored IN COOKIE".
It means, that if you're doing some changes to authentication area, you MUST logout before. If not, application will be taking these persistent information out of cookie, and you will be flooded with messages that CWebUser.lastLoginTime (in this example) is not defined!
Logout, login again and all problems auto-magically are gone! :]
Qiang please help
Hi qiang,
I got the error the Trejder mentioned above , but we are not changed anything , can you please advice what happened ? is anything related to browser ?
Topic Link http://www.yiiframework.com/forum/index.php/topic/57670-where-we-use-setstate/
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.