accessRules

Hi Guys

I am trying to get the user access permission from database.

I have a field call userRoler which contains an integer. Suppose I want to give create and update access to whoever logs in with userRole==3.

I dont know why the following does not work:

public function accessRules()

{

return array(

array('allow',


      'actions'=>array('create','update'),


      'users'=>array('@'),


      'expression'=>'$user->userRole===3',


  ),

I get the following error:

CException

Property "CWebUser.userRole" is not defined.

Any idea?

Thank you

userRole is not defined within the core CWebUser class. You have to either extend the framework class and define userRole or you can set userRole to be a session variable such as

UserIdentity.php


Yii::app()->user->setState('userRole', $user->userRole);

Controller




array('allow',

'actions'=>array('create','update'),

'expression'=>'!$user->isGuest && $user->getState(\'userRole\')==3',



not tested but should work!

If you decide to extend cwebuser.php read this wiki:

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

Thank you.It solved the problem but there was more evolved.It didnt know what $user is. So I had to define $user in LoginForm.php inside

public function login()

{

if($this->_identity->errorCode===UserIdentity::ERROR_NONE)

	{


		$user=User::model()->findByAttributes(array('username'=>$this->username));


		Yii::app()->user->setState('userRole', $user->userRole);

}

Then use the expression as you said

That’s what I was trying to understand.

Thank you anyway. Now I am back to learn how to record user’s last Logout date/time into the Database