todi1979 / cakephp3-aclmanager
CakePHP 3.x 的 AclManager 插件
Requires
- php: >=5.4.16
- cakephp/acl: ~0.2
- cakephp/cakephp: ~3.0
- cakephp/plugin-installer: ~1.0
- composer/installers: *
README
安装
Composer
您可以使用 composer 将此插件安装到 CakePHP 应用程序中。
安装 composer 包的推荐方式是
composer require todi1979/cakephp3-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 隐藏 ACLs 列表中拒绝的插件、控制器和操作。
Configure::write('AclManager.hideDenied', true);
- AclManager.ignoreActions 忽略所有不希望添加到 ACLs 中的插件、控制器和操作。此参数的值必须是一个数组。
# 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() 并享受!