Chorn Sokun's Weblog

Conquer inner fear, push it to the limit!

Yii overwrite accessRules in base class

leave a comment »

Suppose you defined a base class Controller with basic accessRules

class Controller extends CController
{
  public function filters()
  {
    return array(
      'accessControl',
      'postOnly + delete',
    );
  }

  public function accessRules() {
    return array(
      // allow authenticated user to perform 'create' and 'update' actions
      array('allow', 'actions'=>; array('index', 'view'), 'users' => '@'),

      // deny all users
      array('deny', 'users'=>'*'),
    );
  }
}

and you want to add (overwrite) rules in derived class, using array_merge here how you do it:

class Site extends Controller
{
  public function accessRules()
  {
    return array_merge(
      // allow all user to perform 'help' actions
      array( array('allow', 'actions'=> array('help'), 'users' => '*') ),

      // parent go last
      parent::accessRules()
    );
  }
}

As best practice your base class default access rule is deny all by default.

Advertisements

Written by Chorn Sokun

October 22, 2012 at 3:34 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: