具有子组的ACL组

时间:2011-04-21 14:43:03

标签: cakephp permissions grouping acl

我有工作的ACL组 -client
-member
-manager

并且需要将它们作为子组放到所有组中 ALL
-client
-member
-manager

将权限添加到ALL,所以其他3个将继承它,
什么是正确的方法呢?

1 个答案:

答案 0 :(得分:0)

我假设你有必要的用户和群组模型,您将使用CakePHP的ACL行为。我还假设你有'acos','aros'和& 'aros_acos'表。

您需要将您的群组设为树类型:

class Groups extends AppModel {
    var $actsAs = array('Tree', 'Acl' => array('type' => 'requester'));

    function parentNode() {
        return null;
    }

}

在MySQL中,你的组表应该包含这些字段 - id,parent_id,lft,rght,name(或description)。前四个字段是必要的,以便树行为起作用。

在groups_controller.php中:

function add($parentId = null){
    if(!empty($this->data)){
        if($this->Group->save($this->data)) {
            $this->Session->setFlash(__('The group has been saved.', true));
            $this->redirect(array('action'=>'index'));
        } else {
            $this->Session->setFlash(__('The group could not be saved. Please try again.', true));
        }
    }
    $this->set(compact('parentId'));
}

在用户模型中:

class User extends AppModel {

var $name = 'User';
var $belongsTo = array('Group');
var $actsAs = array('Acl' => array('type' => 'requester'));

function parentNode() {
    if (!$this->id && empty($this->data)) {
        return null;
    }
    if (isset($this->data['User']['group_id'])) {
    $groupId = $this->data['User']['group_id'];
    } else {
        $groupId = $this->field('group_id');
    }
    if (!$groupId) {
    return null;
    } else {
        return array('Group' => array('id' => $groupId));
    }
}


}

现在每次添加新组或用户时,AROS表都会自动更新。 然后,您需要为AROS_ACOS表上的每个节点设置权限。不幸的是,在CakePHP中没有简单的方法。

您可以将此代码放在groups_controller.php中,然后在每次添加/删除用户/组时运行/ groups / build_acl:

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

    //allow managers to posts and widgets
    $group->id = 2;
    $this->Acl->deny($group, 'controllers');
    $this->Acl->allow($group, 'controllers/Posts');
    $this->Acl->allow($group, 'controllers/Widgets');

    //allow client to only add and edit on posts and widgets
    $group->id = 3;
    $this->Acl->deny($group, 'controllers');        
    $this->Acl->allow($group, 'controllers/Posts/add');
    $this->Acl->allow($group, 'controllers/Posts/edit');        
    $this->Acl->allow($group, 'controllers/Widgets/add');
    $this->Acl->allow($group, 'controllers/Widgets/edit');
    //we add an exit to avoid an ugly "missing views" error message
    echo "all done";
    exit;
}

我希望这会有所帮助。大多数代码都来自CakePHP在线文档。