Yii AccessRules在表达式中将参数传递给回调函数
我正在开发基于YII模块的应用程序 需要限制用户的某些操作以及整个模块 我能够在规则中限制用户使用Yii AccessRules在表达式中将参数传递给回调函数,yii,yii-components,Yii,Yii Components,我正在开发基于YII模块的应用程序 需要限制用户的某些操作以及整个模块 我能够在规则中限制用户使用'expression'+callback,但现在我想对两个不同的操作使用相同的回调函数,即我有一个回调函数,我想获取参数值,并根据该函数评估要执行的操作,以下是我迄今为止所做的 public function accessRules() { return array( array('allow', // allow all users to per
'expression'+callback
,但现在我想对两个不同的操作使用相同的回调函数,即我有一个回调函数,我想获取参数值,并根据该函数评估要执行的操作,以下是我迄今为止所做的
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('*'),
),
);
}
回调函数
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;
}
}
如果我将此“$op”发送到数组($this,“checkAccessRule(1)”,
当您声明函数名时,将通过Yii以字符串的形式调用它,因此
(1)
将作为函数名的一部分,任何无效的帮助都将不胜感激。幸运的是,expression参数也接受匿名函数(function(){}
)。因此:
应该有用
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('*'),
),
);
}