Access Rules defined in base controller


(sudsbangs@gmail.com) #1

Hi,

I have an accessRules which validates for access Rights for each user using the following syntax defined in the method of each controller. Now i have many controllers, so i have to define this expression( ‘expression’ => ‘Yii::app()->controller->isValidAccess()’,) in every controller inorder to place access restriction for all the actions.

Is there any ways i can define this expression in base controller i.e (components/controller.php), so that i dont have to paste this expression in all my controllers.

Note: ‘Yii::app()->controller->isValidAccess()’ ---- exists in components/controller.php

/* expression defined in all my controllers */

public function accessRules() {

    return array(


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


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


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


             'expression' => 'Yii::app()->controller->isValidAccess()',


        ),


       


    );


}

(Amjad Mghl) #2

Hi,

As Your each controller extens from CController, so if you define anything there in CContoller can be accessed and works fine for all of your controllers.

So, you need to simply place the code in base controller.

Thanks


(sudsbangs@gmail.com) #3

Hi,

Thanks for the quick help, sorry but i cant define the accessRules() in a base controller, as i have different set of actions defined in my own controller,

I just need my expression in accessRules() ie(‘expression’ => ‘Yii::app()->controller->isValidAccess()’,) to be placed on the base controller , so that i dont have to include this expression in all my controller for accessRules().

Many Thanks


(Amjad Mghl) #4

First,

You need to extend base controller first and then put access control code there and after that extend all of your controllers which you want to have these rules.

Thanks