我的Yii项目中有一些模块,我想只有当前用户有权使用这个模块时才启动它们。
这就是我所做的:在SiteController中,我使用用户模块关系获取db表,并获得一组模块名称。然后我想在config中没有定义moudules时加载每个可用的模块。我找不到任何操作员或方法来做到这一点。有没有办法在没有解决方法的情况下实现这一目标?
我想到的另一件事是在每个模块的beforeControllerAction中取消模块init,但我仍然无法解决这个问题。
答案 0 :(得分:1)
您最好使用beforeControllerAction
检查访问权限,如下所示:
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->checkAccess('getMyModule')){
throw new CHttpException(403, Yii::t('yii','You do not have sufficient permissions to access.'));
}
return true;
}
else
return false;
}