Cakephp 具有子组的ACL组

Cakephp 具有子组的ACL组,cakephp,permissions,grouping,acl,Cakephp,Permissions,Grouping,Acl,我有工作ACL组 -客户 -成员 -经理 并且需要将它们作为子组放到所有组中 全部 -客户 -成员 -经理 若要将权限添加到所有,则其他3个将继承该权限, 正确的方法是什么?我假设您拥有必要的用户和组模型,并且将使用CakePHP的ACL行为。我还假设您有“acos”、“aros”和“aros_acos”表 您需要将组设置为树类型: class Groups extends AppModel { var $actsAs = array('Tree', 'Acl' => ar

我有工作ACL组

-客户
-成员
-经理

并且需要将它们作为子组放到所有组中

全部
-客户
-成员
-经理

若要将权限添加到所有,则其他3个将继承该权限,

正确的方法是什么?

我假设您拥有必要的用户和组模型,并且将使用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)。前四个字段是树行为正常工作所必需的

在组_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\u controller.php中,然后每次添加/删除用户/组时都运行/groups/build\u 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在线文档