2012-03-05 136 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; 
     } 
}