L
L
Leonid2016-09-27 11:36:55
Yii
Leonid, 2016-09-27 11:36:55

In Yii 1.x, hang a hook to check the access rights to the controller action?

Is it possible in Yii 1.x to hang your hook specifically for checking access rights to the controller action?
For example, we have a certain controller:

class MyController extends Controller
{
    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
            'postOnly + delete', // we only allow deletion via POST request
        );
    }

    public function accessRules()
    {
        return array(
            array('allow',
                'roles'=>array('root', 'admin', 'manager'),
            ),
            array('allow', 
                'actions'=>array('send', 'list'),
                'users'=>array('*'),
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }

so I need to execute my code, only for requests that require certain access rights (all actions except: send and list), while not reacting to cases when such rights are not required (actions: send and list) - attention: execute your own code before checking those action permissions.
Is it possible and how?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolai Konyukhov, 2016-09-27
@heahoh

You can define override the beforeControllerAction method, it is called before the filters are executed

/**
   * The pre-filter for controller actions.
   * This method is invoked before the currently requested controller action and all its filters
   * are executed. You may override this method in the following way:
   * <pre>
   * if(parent::beforeControllerAction($controller,$action))
   * {
   *     // your code
   *     return true;
   * }
   * else
   *     return false;
   * </pre>
   * @param CController $controller the controller
   * @param CAction $action the action
   * @return boolean whether the action should be executed.
   */
  public function beforeControllerAction($controller,$action)
  {
    if(($parent=$this->getParentModule())===null)
      $parent=Yii::app();
    return $parent->beforeControllerAction($controller,$action);
  }

in $action you can get the action id and perform the necessary checks for the type and operations

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question