How To Customize Accessrules() To Be Related By Database

Hi All

I create users area and I need to customize accessRules() by 'status column ’

For Example when status=2 that mean this user is admin and can access to all actions …etc , I save user status when user login

$this->setState(‘status’, $user->status);

UserIdentity.php





<?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

{


	 // Need to store the user's ID:

	 private $_id;

 	public $password;

 	public $status;




	/**

	 * Authenticates a user.

	 * The example implementation makes sure if the username and password

	 * are both 'demo'.

	 * In practical applications, this should be changed to authenticate

	 * against some persistent user identity storage (e.g. database).

	 * @return boolean whether authentication succeeds.

	 */

	public function authenticate()

	{

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


		if ($user===null) { // No user found!

			$this->errorCode=self::ERROR_USERNAME_INVALID;

		} else if ($user->password !== SHA1($this->password) ) { // Invalid password!

			$this->errorCode=self::ERROR_PASSWORD_INVALID;

     	

		} else { // Okay!

			$this->errorCode=self::ERROR_NONE;

			// Store the role in a session:

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

         	$this->setState('id', $user->user_id);

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

		}

		return !$this->errorCode;

	}

	

	public function getId()

	{

	 return $this->_id;

	}


	

}



I need to change this Code to be related to status :





	public function accessRules()

	{

		return array(

			array('allow',  // allow all users to perform 'index' and 'view' actions

				'actions'=>array('index','view'),

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

			),

			array('allow', // allow authenticated user to perform 'create' and 'update' actions

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

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

			),

			array('allow', // allow admin user to perform 'admin' and 'delete' actions

				'actions'=>array('admin','delete'),

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

			),

			array('deny',  // deny all users

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

			),

		);

	}




Thanks in Advance

Hi Samilo,

You can use expression validator like that

array(‘allow’,

            'actions'=&gt;array('update'),


            'expression'=&gt;(&#036;this-&gt;getState('status')==1)

));

Thank you Kona but I have this error message Why ?





AdminarticleController and its behaviors do not have a method or closure named "getState".	



I fixed it :D , Thank u konapaz For your useful notes

[b]

[/b]

this new code





	public function accessRules()

	{

		return array(

			array('allow',  // allow all users to perform 'index' and 'view' actions

				'actions'=>array('index','view'),

				'expression'=>'$user->user_biasa == "user"',

			),

			array('allow', // allow authenticated user to perform 'create' and 'update' actions

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

			'expression'=>'$user->user_biasa == "user"',

			),

			array('allow', // allow admin user to perform 'admin' and 'delete' actions

				'actions'=>array('admin','delete'),

				'expression'=>'$user->status == 2',

			),

			array('deny',  // deny all users

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

			),

		);

	}




the CUserIdentity has getState method

http://www.yiiframework.com/doc/api/1.1/CBaseUserIdentity#getState-detail

In which controller has this accessRules ?

Thank u I Fixed it , The problem is in calling way :

‘expression’=>($this->getState(‘status’)==1)

return error :





AdminarticleController and its behaviors do not have a method or closure named "getState".      




‘expression’=>’$user->status == 2’,

Work

fine! :)