【问题标题】:check permission against group not users using Auth->authorize="actions"使用 Auth->authorize="actions" 检查组而不是用户的权限
【发布时间】:2010-08-05 10:17:40
【问题描述】:

谁能解释一下Auth->authorize = "actions"的工作原理
在我的项目中,我计划给这个。
正如this 教我的那样,授权将调用$this->Aro->check($user,"controllers/:controller/:action")

这将检查对用户的权利??
这意味着用户应该在 aros 表中。
但我不需要这个来检查用户,但我需要检查一个组
我怎样才能做到这一点。

现在当用户不在 Aro 表中时,它会显示

这样 Aro 将只是组,并且需要将用户添加到 Aros

提前致谢

【问题讨论】:

    标签: cakephp authorization cakephp-1.3 authorize


    【解决方案1】:

    找到解决方案
    使用这个reference
    我将 AuthComponent 扩展为 CustomAuth 并覆盖了 AuthComponent 中的isAutorized() 方法,如下所示

    在控制器/组件/custom_auth.php 中

        <?php
    App::import('Component','Auth');
    class CustomAuthComponent extends AuthComponent {
    
        public function isAuthorized($type = null, $object = null, $user = null) {
    
            $actions  = $this->__authType($type);
            if( $actions['type'] != 'actions' ){
                return parent::isAuthorized($type, $object, $user);
            }
            if (empty($user) && !$this->user()) {
                return false;
            } elseif (empty($user)) {
                $user = $this->user();
            }
    
    
            $group = array('model' => 'Group','foreign_key' =>$user['Login']['group_id']);
            $valid = $this->Acl->check($group, $this->action());
            return $valid;
        }
    }
    ?>
    

    在 app_controller.php 中

    function beforeFilter()
    {
    $this->CustomAuth->userModel = 'Login';
    $this->CustomAuth->allowedActions = array('display');
    $this->CustomAuth->actionPath = 'controllers/';
    $this->CustomAuth->authorize = 'actions';
    }
    

    这解决了我的问题:)

    【讨论】:

      【解决方案2】:

      看看这个chapter。要检查组权限,请执行此操作('model' 和 'foreign_key' 值来自 aros 表):

      $this->Acl->check(
           array('model' => 'Group', 'foreign_key' => 2),
          'controller/action'
      );
      

      【讨论】:

      • 但是由于 Auth->authorize = "actions" 给出了检查将自动完成对吗?
      • 没错。您将需要使用$this-&gt;Auth-&gt;authorize = 'controller';isAuthorized() 方法(book.cakephp.org/view/396/authorize)。
      • 但如果我给$this-&gt;Auth-&gt;authorize = 'controller';,我需要转到每个控制器并覆盖isAuthorized()。如何在每个控制器中避免这种覆盖??
      • 那么我还需要从每个控制器调用isAuthorized() 已经尝试过但失败了
      猜你喜欢
      • 1970-01-01
      • 2013-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      • 1970-01-01
      相关资源
      最近更新 更多