Add two parameters to Yii::app()->user

I modified UserIdentity() class to authenticate the user against a MySQL database. I am able to verify the entered username and password.

However, I also get two more values back from the database which I’d like to store with the user.

Accessing the username with Yii::app()->user->name works as expected. I’d like to add the other two parameters to Yii::app()->user so I can access the values the same way I access the username: Yii::app()->user->parameterOne, Yii::app()->user->parameterTwo.

What is the best way to accomplish this? Do I have to modify or extend CWebUser for this?

Try this:


class UserIdentity extends CUserIdentity

{

    public $parameter1;

    public $parameter2;

...


    public getParameter1()

    {

      return $parameter1;

    }

}

Yii::app()->user->getParameter1()

OR

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

This is a bad idea overall.

For instance, after login, the username is saved in the “user state” and if the user change his username, then the change won’t get reflected.

Instead of doing that, extend CWebUser, something like:




class WebUser extends CWebUser

{

    

    protected static $_userModel;

    

    public function getUserModel()

    {

        if(self::$_userModel===null)

            self::$_userModel=User::getCachedUserDataById($this->getId());

        if(self::$_userModel===null)

            self::$_userModel=new User;

        return self::$_userModel;

    }

    

    public function getAttribute($attribute)

    {

        return $this->getUserModel()->getAttribute($attribute);

    }


}



Then you can do smth like:




Yii::app()->user->getAttribute('username');

Yii::app()->user->getAttribute('last_login');



Or you can just implement methods like




public function getUsername()

{

   return $this->getAttribute('username');

}



And call it like:




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



Well, you get the point here.

Great, thank you very much!! :)

This username behavior is the standard one within yii. I didn’t modify this. Or are you saying that it is bad to store two parameters as rookie84 suggested?

It doesn’t matter if it is standard Yii or not, you will get to the same problem as i described, so just DO NOT use it, instead do as i advised you, let Yii store only the user_id(because this will not change) in the user state and based on that user id, write methods that will bring you the extra data you need from the user table.

You can also set the state, $this->setState(‘somevar’, ‘someval’);

Non-rbac roles can be set that way, and even in an array.

Hi,

When you do something like you proposed you also have to remember to clear cached user model. This is the same as changing username and assigning its new value, unless you don’t have setter implemented.

Using CWebUser::setState is a good idea. With CWebUser::__get you can use it like this:




$user=Yii::app()->getUser();

$user->setState('myProperty','myValue');

echo $user->myProperty; // 'myValue'



But with CWebUser::__get you can only get valid states (which were previously set with CWebUser::setState)

d

Think twice, the cache will use a dependency, and you don’t have to remember to clear the cache at all, is automatically.

setSate() is still a bad idea if the user info changes and you need to show it.