Yii Framework Forum

Why the accessRules doesen't work in my yii module, the yii version is 1.1


(George_lu) #1

I want to make the authenticated users can access my

admin(module)/sysMessage(controller)/index(action)

My accessRules is as below:

public function accessRules()
    {
        return array(
            array('allow',  // allow only users in the 'admin' role access to our actions
                'actions'=>array('index','view', 'create', 'update', 'admin', 'delete'),
                'roles'=>array('admin'),
            ),
                array('allow',  
                        'actions'=>array('index','view'),
                        'roles'=>array('@'),
                ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }

But, when the authenticated users tried to access my

admin(module)/sysMessage(controller)/index(action), they got this message:

“Error 403 You are not authorized to perform this action.”

Could you tell me why?


(George_lu) #2

When we use the module/controller/action, we should check the

/yiiroot/trackstar/protected/modules/admin/yourModule.php

I changed the “public function beforeControllerAction” as below, so the problem be solved.

refer:Create AccessRules in modules Yii Framework

public function beforeControllerAction($controller, $action)
     {
      if(parent::beforeControllerAction($controller, $action))
     {
      // this method is called before any module controller action is performed
       // you may place customized code here

     if(Yii::app()->user->isGuest){
     $url = Yii::app()->createUrl(Yii::app()->user->loginUrl);
     Yii::app()->user->returnUrl = Yii::app()->createUrl('/admin/');
     Yii::app()->request->redirect($url);
    }
     else {
       return true;
     }


     }
     else
     return false;
     }