mmorinaga / cakephp3-aclmanager
此软件包最新版本(dev-master)没有可用的许可证信息。
为 CakePhp3 开发的 acl-manager 插件
dev-master
2020-01-19 16:49 UTC
Requires
- php: >=5.4.16
- cakephp/acl: dev-master
- cakephp/cakephp: ~3.4
Requires (Dev)
- cakephp/cakephp-codesniffer: dev-master
This package is auto-updated.
Last update: 2024-09-20 02:36:41 UTC
README
安装
Composer require mmorinaga/cakephp3-aclmanager
或添加到 composer.json 中
"mmorinaga/cakephp3-aclmanager": "dev-master"
需求
如何使用
构建您的 Acos
首先,您需要构建您的 acos,为了做到这一点,您需要添加以下行到您想要的位置。有两种方式:
-
通过事件
use AclManager\Event\PermissionsEditor;
$this->eventManager()->on(new PermissionsEditor()); $acosBuilder = new Event('Permissions.buildAcos', $this); $this->eventManager()->dispatch($acosBuilder);
-
通过组件
$this->loadComponent('AclManager.AclManager'); $this->AclManager->acosBuilder();
注意:!!! 在构建后,不要忘记删除这些行 !!!
在创建新组时添加权限
!!! 请注意,为了工作,您首先需要一个具有完全授权的顶级 ARO,类似于超级管理员,在 aros_acos 表中如下所示:create:1 read: 1 update: 1 delete: 1!!!
在您的 Admin/GroupsController.php 中
use AclManager\Event\PermissionsEditor;
添加基本权限,在您的动作 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);
}
编辑权限
- 在您的动作 edit() 中
we need to get all acos "not really necessary is just an automatic array builder":
```
$this->loadComponent('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'
]
```
- 构建您的表单
First if you want to use the AclManager Helper
```
public $helpers = [
'AclManager' => [
'className' => '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 "文件夹和子文件夹可以是空的"
- 更新新权限
```
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
...
```