質問

I am developing an application in YII modules based

Need to restrict users on certain actions as well as on entire module

I was able to restrict the users using 'expression' + callback in rules but now I want to use the same callback function for two different actions i.e. I have a callback function which I want to get the parameter value and depending on that evaluate what action to perform, here is what I have done as yet

public function accessRules()
    {
        return array(
            array('allow',  // allow all users to perform 'index' and 'view' actions
                'actions'=>array('index'),
                'users'=>array('*'),
                'expression'=>array($this, "checkAccessRule"),
            ),
            array('allow',  // allow all users to perform 'index' and 'view' actions
                'actions'=>array('login'),
                'users'=>array('?'),
            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions'=>array('view','create','update','admin','delete'),
                'users'=>array('@'),
                'expression'=>array($this, "checkAccessRule"), 
            ),
            array('deny',  // deny all users
                'users'=>array('*'),

            ),
        );
    }

the callback function

function checkAccessRule($op){ 
        if($op == 1){
            if(in_array($this->module->getName(), Yii::app()->user->getState("companyModules")))
                return true;
            return false;
        }elseif($op == 2){
            if((Yii::app()->user->getState("user_role") == 1) && (in_array($this->module->getName(), Yii::app()->user->getState("companyModules"))))
                return true;
            return false;
        }
    }

unable to get this '$op' from the callback if i send it 'expression'=>array($this, "checkAccessRule(1)"),

any help would be appreciated

役に立ちましたか?

解決

That won't work, when you state the function name it will be called via Yii as a string so the (1) will be taken as part of the function name. Lucky for you, the expression parameter accepts an anonymous function too (function(){}). so:

public function accessRules()
{
    return array(
        array('allow',  // allow all users to perform 'index' and 'view' actions
            'actions'=>array('index'),
            'users'=>array('*'),
            'expression'=>function(){$this->checkAccessRule(1)},
        ),
        array('allow',  // allow all users to perform 'index' and 'view' actions
            'actions'=>array('login'),
            'users'=>array('?'),
        ),
        array('allow', // allow authenticated user to perform 'create' and 'update' actions
            'actions'=>array('view','create','update','admin','delete'),
            'users'=>array('@'),
            'expression'=>function(){$this->checkAccessRule(1)},, 
        ),
        array('deny',  // deny all users
            'users'=>array('*'),

        ),
    );
}

Should work.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top