Monday, August 29, 2016

Yii User Setting isAdmin

The Yii framework has CUserIdentity as the basis for logging into an application.

It is possible to add state to the class, so an identity can be tested easily; for example, testing if a menu option should displayed for the current user.

array(
    'label'=>'List OfficeVisit', 
    'url'=>array('index'), 
    'visible'=>yii::app()->user->isAdmin)

The CUserIdentity class has support for adding state with setState. The state can be retrieved with getState, or by accessing the state directly as a property.

In the UserIdentity class in the application, which extends CUserIdentity, add the following in the authenticate method, after authenticating:

if ($this->username==='dbadmin') {
     $this->setState('isAdmin', true);

}

The state can be accessed later with either:

yii::app()->user->getState('isAdmin')

or

yii::app()->user->isAdmin

Refer to http://www.yiiframework.com/wiki/6/how-to-add-more-information-to-yii-app-user/ for more information.

 

No comments:

Post a Comment

Followers