2012-03-05 60 views
2

比方說,我的控制器中有一個函數isAuthorized(),用於檢查用戶是否有權執行控制器動作addedit。現在讓我們說我在控制器動作my_custom_action。如何檢查我的用戶有權限使用的東西來執行我的控制器操作my_custom_action內的行動addedit像:cakephp isauthorized check in other controller action

$this->Auth->isAuthorized(); 

謝謝

+0

這取決於你的'isAuthorized'方法是如何實現的。你試圖解決的實際問題是什麼?也許像ACL或類似的「真正的」許可系統是你正在尋找的東西。 – deceze 2012-03-05 03:04:33

回答

0

在我創建這個app_controller(種)功能。首先檢查正在使用的控制器,然後檢查控制器的動作。你也可以嘗試看看這個'Simple Model-Based Access Control'麪包店的文章。

function isAuthorized() { 
    switch($this->name) { 
     case 'Everyone': /* EveryoneController */ 
      return true; 
      break; 
     case 'Banned': /* BannedController */ 
      $this->Auth->authError = __('You do not have permission to ', true).$this->action.' '.strtolower($this->name); 
      return false; 
      break; 
     default: 
      switch($this->action) { 
       case 'index': 
        return true; 
        break; 
       case 'add': 
       case 'edit': 
       case 'delete': 
        if (/* permission check */) { 
         return true; 
        } else { 
         $this->Auth->authError = __('You do not have permission to ', true).$this->action.' '.strtolower($this->name); 
         return false; 
        } 
        break; 
       default: 
        return true; 
        break; 
      } 
      break; 
    } 
} 
+0

對於Cake 2.x,這是如此嗎? – 472084 2012-07-04 20:36:39

+0

沒有這是蛋糕1.2,我懷疑它的變化很大,雖然 – icc97 2012-07-04 21:19:06

+0

糟糕,抱歉沒有看到問題上的1.3標籤。 – 472084 2012-07-04 22:13:18

0

這會檢查當前請求'my_custom_action'的用戶是否是admin,如果是,則授予訪問權限。

public function isAuthorized($user) { 

     if (in_array($this->action, array('my_custom_action'))) { 

      if ($this->Auth->user('is_admin')) 
       return true; 
      else 
       return false; 
     } 
} 
相關問題