alescx/cakephp3-aclmanager

此包的最新版本(dev-master)没有提供许可证信息。

CakePhp3 的 acl-manager 插件

安装: 12

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 9

类型:cakephp-plugin

dev-master 2018-04-28 08:55 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:20:00 UTC


README

Software License CakePHP 3 Build Status

安装

    Composer require alescx/cakephp3-aclmanager

或修改 composer.json

    "alescx/cakephp3-aclmanager": "dev-master"

要求

CakePhp ACL

如何使用

构建 Acos

首先,您需要构建自己的 acos,为此,您需要在想要的位置添加以下行。有两种方法

  • 通过事件

        use Alescx\AclManager\Event\PermissionsEditor;
    
        $this->eventManager()->on(new PermissionsEditor());
        $acosBuilder = new Event('Permissions.buildAcos', $this);
        $this->eventManager()->dispatch($acosBuilder);
    
  • 通过组件

        $this->loadComponent('JcPires/AclManager.AclManager');
        $this->AclManager->acosBuilder();
    

注意:!!! 在构建后,不要忘记删除这些行!!!

在创建新组时添加权限

!!! 请注意,为了正常工作,您首先需要一个一级 ARO,其基础节点拥有与超级管理员类似的完全授权,如 aros_acos 表中的此示例:create:1 read: 1 update: 1 delete: 1!!!

在您的 Admin/GroupsController.php 中

    use Alescx\AclManager\Event\PermissionsEditor;

添加基本权限,在您的 action add 中添加

    if ($this->Groups->save($group)) {

        if (isset($this->request->data['parent_id'])) {
            $parent = $this->request->data['parent_id'];
        } else {
            $parent = null;
        }

        $this->eventManager()->on(new PermissionsEditor());
        $perms = new Event('Permissions.addAro', $this, [
            'Aro' => $group,
            'Parent' => $parent,
            'Model' => 'Groups'
        ]);
        $this->eventManager()->dispatch($perms);
    }

编辑权限

  1. 在您的 action edit() 中
we need to get all acos "not really necessary is just an automatic array builder":

```
    $this->loadComponent('Alescx/AclManager.AclManager');
    $EditablePerms = $this->AclManager->getFormActions();
```

If you to exclude some actions for the form like ajax actions, you have to add a static property

On the specified controller like PostController or BlogController, ...:

```
    public static $AclActionsExclude = [
        'action1',
        'action2',
        '...'
    ];
```

You will have an array with all acos's alias indexed by the controller aco path like:

```
    'Blog' => [
        'add',
        'edit',
        'delete
    ],
    'Post' => [
        'add',
        'edit',
        'delete'
    ],
    'Admin/Post' => [
        'add',
        'edit',
        'delete'
    ]
```
  1. 构建表单
First if you want to use the AclManager Helper

```
    public $helpers = [            
            'AclManager' => [
                        'className' => 'Alescx/AclManager.AclManager'
                    ]
        ];

    // on your action in your controllerPath
    $EditablePerms = $this->AclManager->getFormActions();
```

an exemple with an Acl helper for checking if permissions are allowed or denied:

```
<?php foreach ($EditablePerms as $Acos) :?>
    <?php foreach ($Acos as $controllerPath => $actions) :?>
        <?php if (!empty($actions)) :?>
            <h4><?= $controllerPath ;?></h4>
            <?php foreach ($actions as $action) :?>
                <?php ($this->AclManager->checkGroup($group, $controllerPath.'/'.$action)) ? $val = 1 : $val = 0 ?>
                <?= $this->Form->label($controllerPath.'/'.$action, $action);?>
                <?= $this->Form->select($controllerPath.'/'.$action, [0 => 'No', 1 => 'Yes'], ['value' => $val]) ;?>
            <?php endforeach ;?>
        <?php endif;?>
    <?php endforeach ;?>
<?php endforeach ;?>
```

render:

```
    <select name="App/Blog/add">
        <option value="0">No</option>
        <option value="1" selected>Yes</option>
    </select>
```

如果您不使用 Array Builder,则需要指定您的输入名称,例如 aco 路径:App/Blog/add 或 App/Admin/Blog/add ... :base/:folder/:subfolder/:controller/:action "文件夹和子文件夹可以是空的"

  1. 更新新权限
```

    if ($this->request->is('post')) {

        $this->eventManager()->on(new PermissionsEditor());
        $perms = new Event('Permissions.editPerms', $this, [
            'Aro' => $group,
            'datas' => $this->request->data
        ]);
        $this->eventManager()->dispatch($perms);
    }

```

data need to be like this 'aco path' => value "0 deny / 1 allow"

```
    'App/Blog/add' => 0
    'App/Blog/edit' => 1
    ...
```