Yii Framework Forum: Enhancement to access control filter - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Enhancement to access control filter Rate Topic: -----

#1 User is offline   datashaman 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 56
  • Joined: 27-September 09
  • Location:Cape Town, South Africa

Posted 28 September 2009 - 02:34 AM

Hi,

I've been using the Yii framework for a few days now, and I f-ing love it. It's the best framework I've used (and I've used MANY).

There are a few issues and enhancements I've encountered while setting up my apps, which I'd like to share.

The access control filter lacks a mechanism to send parameters to the access check, so I subclassed CAccessControlFilter and added the following override method:

class CNewAccessControlFilter extends CAccessControlFilter
{
  public function isRoleMatched($user)
  {
    if(empty($this->roles))
      return true;

    foreach($this->roles as $role) {
      if(is_array($role)) {
        list($role, $params) = $role;
      } else {
        $params = array();
      }
      if($user->checkAccess($role, $params)) {
        return true;
      }
    }

    return false;
  }
}


This allows you to specify roles with params for the bizRule (if needed).

So your access rule definitions in the controller now look like this:

public function accessRules()
{
  return array(
    array('allow', 'roles' => array(
      array('admin', array('paramname' => 'paramvalue')), // Specify the role as an array with the parameters as the second element
      'guest', // Specify the role as a string if you do not need parameters for the bizRule
    ));
  );
}


I hope someone finds this useful.
0

#2 User is offline   Ivo Roper 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 22-September 09
  • Location:Seattle, WA, USA

Posted 01 October 2009 - 10:49 PM

I was just about to work on that very class extension, then thought I should try here first. Excellent timing, thank you! Looks pretty simple, but I'll let you know if I run into any quirks. <waves cheerfully>

Ivo in Seattle
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users