February 26 2014

Yii accessRules filter access in controllers for user roles.

Tagged Under :

Yii
I had install Rights module inside my application. The Rights module itself wouldn’t auto integrate with the controller.

So I need to integrate myself, at each of the controller we need to add 2 extra function they are:
public function filters() {
  return array(
    'accessControl', 
  );
}
and
public function accessRules() {
  return array(
    array('allow',
          'action' => array('Index'),
          'users' => array('@'),
    ),
  );
}

If both function already exist, you no need to add it again. And then add below example new function in Controller.php.
public function checkAccess() {
  return Yii::app()->user->checkAccess(
    Yii::app()->controller->id.".".Yii::app()->controller->action->id
  )
}

After that. add expression inside the accessRules. And it will look like below example:
'action' => array('Index'),
'users' => array('@'),
'expression' => 'Yii::app()->controller->checkAccess()'

Now you can use Right Module to set the access right.

Make a Comment

You must be logged in to post a comment.