Extending The Cwebuser

Bonjour,

Thanks to everyone for making my experience with Yii an enjoyable one.

Now check this out.

I was successfully able to add information to Yii::app()->user using this page.

http://www.yiiframework.com/wiki/60/add-information-to-yii-app-user-by-extending-cwebuser/

Now I have implemented mine in this manner




<?php


/**

 * UserIdentity represents the data needed to identity a user.

 * It contains the authentication method that checks if the provided

 * data can identity the user.

 */

class UserIdentity extends CUserIdentity

{

    private $_id;

 

    public function authenticate()

    {

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

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

        if($user===null)

            $this->errorCode=self::ERROR_USERNAME_INVALID;

        else if(!$user->validatePassword($this->password))

            $this->errorCode=self::ERROR_PASSWORD_INVALID;

        else

        {

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

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

			$this->setState("user_rank",$user->user_rank);

            $this->errorCode=self::ERROR_NONE;

        }

        return $this->errorCode==self::ERROR_NONE;

    }

 

    public function getId()

    {

        return $this->_id;

    }

}



Unfortunately I get the error

" CException;Property "CWebUser.user_rank" is not defined" when the session /state is lost

How do I go around this problem to maybe at worst send the user to the login page or at best continue to save the state

Merci beaucoup

Sry I didn’t understand your problem.

Can you provide the error stack and more code related to the error.

@kokomo

Thanks for the quick response,

I get this error (or can replicate it anyway)


Property "CWebUser.user_rank" is not defined

whenever I open 2 / more tabs of my web app and then log out from one of those tabs and then tries to continue browsing normally in the other tab.

Logically I should have lost the Yii::app()->user->user_rank, which is true.

But with the error being displayed in that manner,i would wish to force the user to the login page when it has been released that the state no more exists

And for the error stack, this below is from

C:\wamp\www\framework\web\auth\CWebUser.php(141)


 /**

131      * PHP magic method.

132      * This method is overriden so that persistent states can be accessed like properties.

133      * @param string $name property name

134      * @return mixed property value

135      */

136     public function __get($name)

137     {

138         if($this->hasState($name))

139             return $this->getState($name);

140         else

141             return parent::__get($name);

142     }

Merci again for your time!

I’m still confused about your error description but in general you have to check if user_rank exist at the correct place to avoid this error.




if (!isset($your_var->user_rank)) {

  $this->redirect(Yii::app()->createUrl("user/login"));

}



Can you please provide the full error stack (not only the implementation of the core __get() method) and the relevant code where the error stack hits your custom code.