简体   繁体   中英

Yii showing user type in view using user identity object

In my web application I need to show the type of user in the view in protected/views/layouts/main.php .

But I am getting this error:

"CException" ."Property "CWebUser.type" is not defined."

I am unable to get rid of this error , how to resolve this issue?

I am using this line of code to display the type of user

array('label'=>'Logout ('.Yii::app()->user->type.')', 'url'=>array('/site/logout'), 
'visible'=>!Yii::app()->user->isGuest)

I tried by using user->user_type also but not working

My code for the UserIdentity class

class UserIdentity extends CUserIdentity
{
    private $_id;


    public function authenticate()
    {
        $user = User::model()->findByAttributes(array(
                'email'=>$this->username));
        if ($user === null) {

            $this->errorCode=self::ERROR_USERNAME_INVALID;
        } else if ($user->pass !==
        hash_hmac('sha256', $this->password,
                Yii::app()->params['encryptionKey']) ) {

            $this->errorCode=self::ERROR_PASSWORD_INVALID;
        } else { 
            $this->errorCode=self::ERROR_NONE;
            $this->setState('type', $user->user_type);
            $this->setState('id', $user->id);
            $this->_id = $user->id;

        }
        return !$this->errorCode;

    }

    public function getId() {
        return $this->_id;

    }

}

Also since I am using Role based access control I have changed the code in user.php for assigning roles to users

My code to assign users type.

public function afterSave() {
        if (!Yii::app()->authManager->isAssigned(
                $this->type,$this->id)) {
            Yii::app()->authManager->assign($this->type,
            $this->id);
        }
        return parent::afterSave();
        }

And I have used this code in my SiteController for assigning roles to users

$auth->assign($user->type,$user->id);

If I;m right in what's happening, there may be times when you're not logged in that Yii is trying to access the user settings. As you're not logged in you can't access them, hence the error. So in the label, check that user isset()

'label' => (isset(Yii::app()->user->type) ? Yii::app()->user->type : '')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM