I cannot add a new property to CWebUser

Hello…

I’m having problems adding a new property to CWebUser object. I have followed the document at http://www.yiiframework.com/doc/cookbook/6/ but even if I follow the instructions, it does not help.

Inside authenticate method of UserIdentity class I wrote:


$this->setState('administrador', $user->administrador);

where $user->administrador has actually a value of 1.

After that instruction, I wrote




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



but an error is shown telling that property is undefined.

If I use getState method instead, an empty value is returned.

Framework version is yii-1.0.7.r1212

Thanks in advance for your help…

Jaime

Jaime, take a look here:

http://www.yiiframework.com/forum/index.php?/topic/4579-adding-more-info-to-yiiapp-user/

Hello Polydoro…

That doesn’t work because he really didn’t show the solution.

This is the whole authenticate method if you can discover what is happening:




	public function authenticate()

	{

	    $username=strtolower($this->username);

	    

	    // Si el nombre de usuario es 'admin', verifica la password del superadministrador

	    if ($username == 'admin')

	    {

	        if (md5($this->password) == '65334ae9c30abb1f0c45e4041925c504')

	        {

	            $this->_id = -1;

	            $this->setState('administrador', 1);

	            $this->errorCode=self::ERROR_NONE;

	        }

	        else

	            $this->errorCode=self::ERROR_PASSWORD_INVALID;

	    }

	    else

	    {

            $user=Usuario::model()->find('LOWER(username)=?',array($username));

            if($user===null)

                $this->errorCode=self::ERROR_USERNAME_INVALID;

            else if(md5(md5($this->password).Yii::app()->params["salt"])!==$user->password)

                $this->errorCode=self::ERROR_PASSWORD_INVALID;

            else

            {

                $this->_id=$user->id;

                $this->username=$user->username;

                $this->setState('administrador', $user->administrador);

                $this->errorCode=self::ERROR_NONE;

            }

	    }


	    if ($this->errorCode == self::ERROR_NONE)

	    {

	        $auth=Yii::app()->authManager;

	        $auth->createRole('admin', 'Usuario Administrador', 'return Yii::app()->user->administrador');

	        $auth->assign('admin', $username);

	        //echo "-->" . Yii::app()->user->getState('administrador');

	        //exit();

	    }

	    

	    return !$this->errorCode;

	}