Yii 1.1: Add information to Yii::app()->user by extending CWebUser (better version)


I found a better solution.

(Your login credentials may differ from my version so you will have to fit the script to your needs. I'm only telling what is the point)

Steps to follow: 1. Add $user field to UserIdentity class 2. Add getUser() method - getter for above property 3. Add setUser($user) method - setter for above property which will assign user's properties info $user attribute.

My example UserIdentity class:

class UserIdentity extends CUserIdentity
    * User's attributes
    * @var array
    public $user;
    public function authenticate()
        if ($user)
            if ($user->password === md5($user->salt.$this->password)) {
        return !$this->errorCode;
    public function getUser()
        return $this->user;
    public function setUser(CActiveRecord $user)

Now user's attributes has been set, create WebUser class and place it under /protected/components

class WebUser extends CWebUser
    public function __get($name)
        if ($this->hasState('__userInfo')) {
            if (isset($user[$name])) {
                return $user[$name];
        return parent::__get($name);
    public function login($identity, $duration) {
        $this->setState('__userInfo', $identity->getUser());
        parent::login($identity, $duration);
    * Required to checkAccess function
    * Yii::app()->user->checkAccess('operation')
    public function getId()
        return $this->id;

remember to set that class as Yii::app()->user class:


Should work now :) You can now access user's database fields as properties.

Total 5 comments

#17870 report it
letsjump at 2014/08/03 08:35am
Yii Password Helper

Keep in mind that, starting from version 1.1.14 Yii has a password helper class that usees the strong blowfish algorithm. Take a look at http://www.yiiframework.com/doc/api/1.1/CPasswordHelper

#16632 report it
Anas AbuDayah at 2014/03/13 05:41am
Good Job

Good job man, this is awesome, You make my life easier :)

#56 report it
horizons at 2010/09/30 04:23am
problem with using login in soap requests

public function login($identity, $duration) must be public function login($identity, $duration=0)

tested with yii-1.1.4.r2429 otherwise you will get the soap error "looks like we got no XML document" when you using Yii::app()->user->login($identity); without duration.

#181 report it
Spear at 2010/08/17 09:24am
Not right

To my way of thinking, it is important to remember that WebUser (which is user recognized by application) is not always related to Database user profile, and that your AR User is not your WebUser. Hence that, I prefer to call Yii::app()->user->model to get associated User model for current WebUser (if there is one).

Anyway, it is always nice to see a new cookbook article, thank you!

#183 report it
DarkNSF at 2010/08/16 05:55pm

$user does not need to be declared public if you have a getter and a setter.

Leave a comment

Please to leave your comment.

Write new article
  • Written by: emix
  • Updated by: Anas AbuDayah
  • Category: Tutorials
  • Yii Version: 1.1
  • Votes: +5 / -2
  • Viewed: 38,807 times
  • Created on: Aug 15, 2010
  • Last updated: Mar 13, 2014
  • Tags: Yii, user, rbac