Q:

Yii - Creating a "Master" user account linked with sub accounts

I am trying to figure out how I would go about making my user authentication architecture. Instead of the normal one account per user, I'm developing a system where you have one account (email and password), which is linked to various smaller accounts (which have different IDs, display names, access roles... basically another table in the database). These smaller, or 'sub' accounts, are completely independent of each other.  
I currently have it working to where I can log into the "Master" account, and I have a dropdown list in the user menu that will be used for switching between the various sub accounts. However, I'm stuck on just how I go about achieving switching account and keeping it persistent.
I have tried extending CWebUser and adding a setSub($id) method that takes the ID of the sub account to switch to, and setting the $currentSub variable to the sub account model, but that doesn't seem to be working... I have no idea where to start with this.
All I really need is the displayName and id of the sub account to persist, and a way of changing these values on any page of the site. I can then use the ID on personalized pages and go from there (as opposed to using the id of the "master" account, which is set as Yii::app()->user->id). I guess I just don't know how to set the variables and have them persist.

A:

Looks like you are doing everything right as you describe it, but without your actual code, I don't know what you might have mixed up. Here is what I just verified works for me:
In your components folder, place a new file named WebUser.php with the following code:
class WebUser extends CWebUser{

    public function getSubId(){
        return Yii::app()->user->getState('subId');
    }

    public function setSubId($id){
        Yii::app()->user->setState('subId',$id);
    }
}

Then in your config/main.php file add the 'class'=>'WebUser' line to your user component.
// application components
'components'=>array(
    'user'=>array(
        // enable cookie-based authentication
        'class'=>'WebUser',
        'allowAutoLogin'=>true,
    ),
...

With the above, I verified it working for me with the following code:
Yii::app()->user->subId = 10;
echo Yii::app()->user->subId;

And it indeed echo'd 10 to the screen. You can duplicate the logic in WebUser to do the same for the sub's displayName attribute. Then just have your script set those values wherever appropriate and you are all set.
UPDATE: Fixed my code for later viewers. The above should work and persist now. Previously I had tried to define a variable in the WebUser object, but obviously that could not persist. Instead, using the WebUser object as a wrapper for the setState/getState methods works as desired.

