ivanamat/cakephp3-aclmanager

CakePHP 3.x 的 AclManager 插件

安装数: 13,199

依赖: 2

建议者: 0

安全性: 0

星标: 27

关注者: 9

分支: 27

开放问题: 1

类型:cakephp-plugin

1.3 2018-02-18 14:16 UTC

This package is auto-updated.

Last update: 2024-09-04 15:20:14 UTC


README

安装

Composer

您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。

安装 composer 包的推荐方式

composer require ivanamat/cakephp3-aclmanager

Git 子模块

git submodule add git@github.com:ivanamat/cakephp3-aclmanager.git plugins/AclManager
git submodule init
git submodule update

手动安装

下载 .zip 或 .tar.gz 文件,解压缩并将插件文件夹 "cakephp3-aclmanager" 重命名为 "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 中包含并配置 AuthComponentAclComponent

    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 3.x Acl Manager

CakePHP 3.x - AclManager 是一个用于管理 CakePHP 3.x 权限控制(ACL)的单个插件,基于 原版想法,该想法由 Frédéric Massart (FMCorz) 为 CakePHP 2.x 提出。

本项目将因 CakePHP 4.x - AclManager 的推出而弃用。

所有代码将迁移到以下仓库以继续未来的版本:https://github.com/ivanamat/cakephp-aclmanager

作者

Iván Amat 在 GitHub 上
www.ivanamat.es

许可

MIT 许可证