Difference between #2 and #3 of
How to add more information to Yii::app()->user

Revision #3 has been created by qiang on Feb 10, 2009, 4:33:19 PM with the memo:

Removed getter and setter for lastLoginTime
« previous (#2) next (#4) »

Changes

Title unchanged

How to add more information to Yii::app()->user

Category unchanged

Tutorials

Yii version unchanged

Tags unchanged

Content changed

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 storrepresents the information that are closely related with the current user and should. Some information can be persistent throughout the current user session. For example, [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](http://www.yiiframework.com/doc/guide/topics.auth).
[...]
{
$this->_id=$user->id;
$this->
setState('lastLoginTime=', $user->lastLoginTime);
$this->errorCode=self::ERROR_NONE;
}
[...]
return $this->_id;
}
} public function getLastLoginTime()
 
{
 
return $this->getState('
```
 
 
In the above, during authentication we retrieve the ID and the 
lastL loginT time');
 
}
 

 
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
 information of the authenticated user. We save the ID in a private variable `$_id` and save `lastLoginTime` in a state by calling `gsetId()` method to return a private variable. The reason that the `id` property is not defined likeState()`. The reason that we use different approaches to save `id` and `lastLoginTime` is because `id` is a pre-defined property in [CUserIdentity] andthat is recognized by [CWebUser]. If we need to addwant to store 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 th
use `setState()`, like we do with `lastLoginTime`.
 
 
We also override the `getId()` method to return the private variabl
e `$_id` and `lastLoginTime` properties if we find such a user record whose password matches the provided password (meaning successful authentication). The parent implementation is to return the username.

That's all we need. Now if we want to retrieve the `id` or `lastLoginTime` information in our code, we can do the following:
[...]
```php
$id=Yii::app()->user->id;
$lastLoginTime=Yii::app()->user->
getState('lastLoginTime'); // starting from 1.0.3 you canIf you are using version 1.0.2 or earlier, you should use the following: // $lastLoginTime=Yii::app()->user->getState('lastLoginTime'); ``` > Note: When cookie-based authentication is enabled (by setting [CWebUser::allowAutoLogin] to be true), these persistent information will be stored in cookie. Therefore, you should NOT store sensitive information (e.g. password) like we do in the above.
21 0
28 followers
Viewed: 226 679 times
Version: 1.1
Category: Tutorials
Written by: qiang
Last updated by: Yang He
Created on: Feb 9, 2009
Last updated: 11 years ago
Update Article

Revisions

View all history