albertotain / acl-manager
CakePHP 4 的 AclManager 插件
Requires
- php: >=7.4
- cakephp/acl: ^0.7.1
- cakephp/cakephp: ^4.0
- cakephp/plugin-installer: ~1.2
- composer/installers: *
Requires (Dev)
- phpunit/phpunit: ^5.7.14|^6.0
README
安装
Composer
您可以使用 composer 将此插件安装到 CakePHP 应用程序中。
安装 composer 包的推荐方法是
composer require albertotain/cakephp-aclmanager
入门
- 通过运行 composer require cakephp/acl 安装 CakePHP ACL 插件。 阅读 ACL 插件文档。
- 设置 AclManager 配置。 AclManager.aros。必须在加载插件之前指定。
- 在 app/config/bootstrap.php 中加载 Acl 和 AclManager 插件。
# Example configuration for an schema based on Groups, Roles and Users Configure::write('AclManager.aros', array('Groups', 'Roles', 'Users')); Plugin::load('Acl', ['bootstrap' => true]); Plugin::load('AclManager', ['bootstrap' => true, 'routes' => true]);
警告:不建议使用 Plugin::loadAll();。如果您使用 Plugin::loadAll();,请确保不会使用 Plugin::load('PluginName') 重复加载任何插件。
配置参数
必须在加载插件之前指定。
- AclManager.aros 必需。设置要使用的 AROs。此参数的值必须是一个包含要使用 AROs 名称的数组。
# Example configuration for an schema based on Groups, Roles and Users Configure::write('AclManager.aros', array('Groups', 'Roles', 'Users'));
- AclManager.admin 可选。设置 'admin' 前缀。此参数的值必须是布尔值。
# Set prefix admin ( http://www.domain.com/admin/AclManager ) Configure::write('AclManager.admin', true);
- AclManager.hideDenied 隐藏 ACL 列表中被拒绝的插件、控制器和动作。
Configure::write('AclManager.hideDenied', true);
- AclManager.ignoreActions 忽略您不想添加到 ACL 中的所有插件、控制器和动作。此参数的值必须是一个数组。
# Ecample: Configure::write('AclManager.ignoreActions', array( 'actionName', // ignore action 'Plugin.*', // Ignore the plugin 'Plugin.Controller/*', // Ignore the plugin controller 'Plugin.Controller/Action', // Ignore specific action from the plugin. 'Error/*' // Ignore the controller 'Error/Action' // Ignore specifc action from controller ));
创建 ACL 表
要创建与 ACL 相关的表,请运行以下迁移命令。
bin/cake migrations migrate -p Acl
示例模式
基于组、角色和用户的示例模式。
CREATE TABLE `groups` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(100) COLLATE utf8_unicode_ci NOT NULL, `created` datetime DEFAULT NULL, `modified` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; CREATE TABLE `roles` ( `id` int(11) NOT NULL AUTO_INCREMENT, `group_id` int(11) DEFAULT NULL, `name` varchar(100) COLLATE utf8_unicode_ci NOT NULL, `created` datetime DEFAULT NULL, `modified` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `group_id` int(11) NOT NULL, `role_id` int(11) NOT NULL, `username` varchar(50) COLLATE utf8_unicode_ci NOT NULL, `password` char(255) COLLATE utf8_unicode_ci NOT NULL, `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `created` datetime DEFAULT NULL, `modified` datetime DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `username` (`username`), UNIQUE KEY `email` (`email`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
认证
在 AppController 中包含并配置 AuthComponent 和 AclComponent。
public $components = [ 'Acl' => [ 'className' => 'Acl.Acl' ] ]; $this->loadComponent('Auth', [ 'authorize' => [ 'Acl.Actions' => ['actionPath' => 'controllers/'] ], 'loginAction' => [ 'plugin' => false, 'controller' => 'Users', 'action' => 'login' ], 'loginRedirect' => [ 'plugin' => false, 'controller' => 'Posts', 'action' => 'index' ], 'logoutRedirect' => [ 'plugin' => false, 'controller' => 'Pages', 'action' => 'display' ], 'unauthorizedRedirect' => [ 'plugin' => false, 'controller' => 'Users', 'action' => 'login', 'prefix' => false ], 'authError' => 'You are not authorized to access that location.', 'flash' => [ 'element' => 'error' ] ]);
模型设置
作为请求者操作
将 $this->addBehavior('Acl.Acl', ['type' => 'requester']); 添加到 src/Model/Table/GroupsTable.php、src/Model/Table/RolesTable.php 和 src/Model/Table/UsersTable.php 文件中的 initialize 函数中。
public function initialize(array $config) { parent::initialize($config); $this->addBehavior('Acl.Acl', ['type' => 'requester']); }
在 Group 实体中实现 parentNode 函数
将以下 parentNode 实现添加到 src/Model/Entity/Group.php 文件中。
public function parentNode() { return null; }
在 Role 实体中实现 parentNode 函数
将以下 parentNode 实现添加到 src/Model/Entity/Role.php 文件中。
public function parentNode() { if (!$this->id) { return null; } if (isset($this->group_id)) { $groupId = $this->group_id; } else { $Users = TableRegistry::get('Users'); $user = $Users->find('all', ['fields' => ['group_id']])->where(['id' => $this->id])->first(); $groupId = $user->group_id; } if (!$groupId) { return null; } return ['Groups' => ['id' => $groupId]]; }
在 User 实体中实现 parentNode 函数
将以下 parentNode 实现添加到 src/Model/Entity/User.php 文件中。
public function parentNode() { if (!$this->id) { return null; } if (isset($this->role_id)) { $roleId = $this->role_id; } else { $Users = TableRegistry::get('Users'); $user = $Users->find('all', ['fields' => ['role_id']])->where(['id' => $this->id])->first(); $roleId = $user->role_id; } if (!$roleId) { return null; } return ['Roles' => ['id' => $roleId]]; }
创建一个组、角色和用户。
允许所有。添加到 AppController.php 中。
public function initialize() { parent::initialize(); ... $this->Auth->allow(); }
现在创建一个组、角色和用户。
访问插件
现在导航到 http://www.domain.com/AclManager(或 http://www.domain.com/admin/AclManager,如果 AclManager.admin 设置为 true),只需单击 "更新 ACOs 和 AROs 并设置默认值",更新 ACOs 和 AROs 后,从 AppController.php 中删除 $this->Auth->allow(),然后享受吧!
已知问题
- 未知。
变更日志
v1.3
添加
- AclManager.hideDenied 隐藏 ACL 列表中被拒绝的插件、控制器和动作。
更改
- AclManager.ignoreActions 忽略您不想添加到 ACL 中的所有插件、控制器和动作。
Configure::write('AclManager.ignoreActions', array( 'actionName', // ignore action 'Plugin.*', // Ignore the plugin 'Plugin.Controller/*', // Ignore the plugin controller 'Plugin.Controller/Action', // Ignore specific action from the plugin 'Error/*' // Ignore the controller 'Error/Action' // Ignore specifc action from controller ));
- 更新 indexctp 和 permissioins.ctp:显示或隐藏 ACL 列表中没有权限的 ACL。在操作面板下方显示闪存消息。
- 修复了 acoUpdate 同步。
关于 CakePHP Acl Manager
CakePHP - AclManager 是一个用于管理 CakePHP 权限控制 (ACL) 的单一插件,基于 Frédéric Massart (FMCorz) 为 CakePHP 2.x 提出的 原始想法。