Yii ::获取actions()指定的控制器动作

时间:2012-10-01 08:18:43

标签: php yii

我有下一个accessRules的控制器:

  public function accessRules()
  {
    return array(
      array('allow',
        'actions'=>array('login','logout'),
        'users'=>array('*'),
      ),
      array('allow',
        'actions'=>array('index'),
        'users'=>array('@'),
      ),
      array('allow',
        'actions'=>array('users'),
        'expression'=>'$user->getState(\'role\')==0',
      ),
      array('deny',
        'users'=>array('*'),
      ),
    );
  }

actions()方法指定的所有操作(在所有控制器中):

  public function actions()
  {
    return array(
      'index'=>$this->module->getName().'.controllers.main.IndexAction',
      'login'=>$this->module->getName().'.controllers.main.LoginAction',
      'logout'=>$this->module->getName().'.controllers.main.LogoutAction',
    );
  }

是否有机会获得控制器/操作列表取决于当前用户权限? 我想构建一个包含所有控制器列表及其操作的导航菜单,如下所示:

  1. Controler1 (show only if current user have permissions to access it)
    • Controler1 / action1 (show only if current user have permissions to access it)
    • Controler1 / action2 (show only if current user have permissions to access it)
  2. Controller2 (show only if current user have permissions to access it)
    • Controler2 / action1 (show only if current user have permissions to access it)
    • Controler2 / action2 (show only if current user have permissions to access it)

1 个答案:

答案 0 :(得分:0)

据我所知,如果在控制器中声明了访问规则,则无法获取访问规则。

您可能必须使用Yii的基于角色的访问控制(RBAC)并在外部保存您的访问规则,可能在数据库中。请阅读此处以获取更多信息:Yii Documentation - role based access control

还有一个非常强大的Yii扩展名为Rights,它为RBAC提供后端。

目前这似乎有些过分,但RBAC在灵活性方面无与伦比。您可以非常精细地创建用户角色并为角色分配操作。

如果您使用它,您可以检查Yii::app->user->checkAccess('post.create')之类的访问权限,以确切了解此类菜单所需的内容。但我不认为你可以获得开箱即用的所有可用操作的列表,但这可能相对容易扩展。