0

我正在使用蛋糕 2.x

我也在使用 Auth 和 Acl 组件。

我想允许对所有登录用户执行单个操作。

但这导致我多次编写此代码,然后运行 ​​initDB。

public function initDB() {
    $group = $this->User->Group;
    //Allow ADMINISTRATORS to everything
    $group->id = ADMINISTRATORS;
    $this->Acl->allow($group, 'controllers');

    //allow SALES_MANAGERS to upload SOW file at `products`
    $group->id = SALES_MANAGERS;
    $this->Acl->deny($group, 'controllers');
    $this->Acl->allow($group, 'controllers/Pages');


    //allow SOLUTION_ARCHITECTS to only add and edit on posts and widgets
    $group->id = SOLUTION_ARCHITECTS;
    $this->Acl->deny($group, 'controllers');
    $this->Acl->allow($group, 'controllers/Pages');

    //allow IMPLEMENTATION_MANAGERS to only add and edit on posts and widgets
    $group->id = IMPLEMENTATION_MANAGERS;
    $this->Acl->deny($group, 'controllers');
    $this->Acl->allow($group, 'controllers/Pages');

    //we add an exit to avoid an ugly "missing views" error message
    echo "all done";
    exit;
}

正如您所注意到的,我需要允许 Pages 访问所有不同的组。

我更喜欢类似于 Auth->allow 的简单方法,它始终允许所有登录用户执行某些操作。

谢谢你。

更新

这是我的解决方法。有更好的解决方案吗?

public function initDB() {
    $group = $this->User->Group;

  ... // didn't want to repeat this part which  is same as above.

  // we allow all groups the following actions
    $onlyForLoggedInUsers = array(
        'controllers/Users/logout',
        'controllers/Pages',
    );
    $this->_allowAllGroupsThisAction($onlyForLoggedInUsers);

    //we add an exit to avoid an ugly "missing views" error message
    echo "all done";
    exit;
}

protected function _allowAllGroupsThisAction($actions) {
    $groups = array(SALES_MANAGERS, SOLUTION_ARCHITECTS, IMPLEMENTATION_MANAGERS);
    $actions = (array)$actions;
    $group = $this->User->Group;
    foreach ($groups as $id) {
        $group->id = $id;
        foreach($actions as $action) {
            $this->Acl->allow($group, $action);
        }
    }
}
4

1 回答 1

0

如果您将组创建为分层的,则可以。创建一个充当树的组结构,并像这样构造您的数据:

  • 用户
    • 管理员
    • 经理人
      • 销售经理
      • 实施经理
    • 开发者
      • 解决方案架构师

使用这种结构,分配给父 ARO 的任何权限都将由所有后代继承。关于如何设置父行为的说明可以在这里找到:http: //book.cakephp.org/2.0/en/tutorials-and-examples/simple-acl-controlled-application/simple-acl-controlled-application。 html#acts-as-a-requester

于 2013-02-25T04:04:18.743 回答