2012-10-01 24 views
1

我有未來accessRules控制器指定控制器動作:的Yii ::獲得通過的行動()

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('*'), 
    ), 
    ); 
    } 

的所有操作(在所有控制器)的行爲規定()方法:

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 /動作1(show only if current user have permissions to access it)
    • Controler1 /動作2(show only if current user have permissions to access it)
  2. 控制器2(show only if current user have permissions to access it)
    • Controler2 /動作1(show only if current user have permissions to access it)
    • Controler2 /動作2(show only if current user have permissions to access it)

回答

0

據我所知,目前還沒有辦法,如果得到的訪問規則,他們在您的控制器中聲明。

您可能不得不使用Yii的基於角色的訪問控制(RBAC)並將訪問規則從外部保存到數據庫中。請閱讀此處以獲取更多信息:Yii Documentation - role based access control

還有一個稱爲Rights的相當能幹的Yii擴展,它爲RBAC提供了一個後端。

目前看起來似乎有些過火,但RBAC在靈活性方面無與倫比。您可以創建用戶角色並非常細緻地將操作分配給角色。

如果你使用它,你可以檢查像Yii::app->user->checkAccess('post.create')這樣的訪問,所以你需要這樣一個菜單。但我不認爲你可以得到所有可用的動作列表,但這可能是你可以相對容易地擴展的東西。