jeffersonsimaogoncalves/cakephp-permission
Composer 安装命令:
composer require jeffersonsimaogoncalves/cakephp-permission
包简介
The library provides a flexible way to add role-based access control management to CakePHP 3.x
关键字:
README 文档
README
The library provides a flexible way to add role-based access control management to CakePHP 3.x
Inspired by Laravel Permission
Quick example
//Creats a role $role = Role::create('editor'); //Givs a permission to the role $role->givePermission('edit article'); //Adds the role to the user $user->assignRole($role); // You can also give it directly by its name $user->assignRole('editor'); //Checks whether the user has the permission var_dump($user->hasPermission('edit article')) //output "true"
Installation
- Install via composer
composer require jeffersonsimaogoncalves/cakephp-permission
- Load the plugin in
config/bootstrap.php:
// Load the plugin. Plugin::load('JeffersonSimaoGoncalves/CakePermission');
- Add the following configuration to your
app.php
'Permission' => [ 'tableNameMap' => [ /** * Your users table, remember to modify it */ 'users' => 'your users table name', /** * Your roles table;If you want to use the default configuration. you don't need to change. */ //'roles' => 'roles', /** * Your permissions table;If you want to use the default configuration. you don't need to change. */ //'permissions' => 'permissions', /** * The join table between users and roles;If you want to use the default configuration. you don't need to change. */ //'users_roles' => 'users_roles', /** * The join table between roles and permissions;If you want to use the default configuration. you don't need to change. */ //'roles_permissions' => 'roles_permissions', ], 'tableClassMap' => [ /** * The Users model class, remember to modify it */ 'Users' => App\Model\Table\YourUsersTable::class, /** * The Roles model class;If you want to use the default configuration. you don't need to change. */ //'Roles' => JeffersonSimaoGoncalves\CakePermission\Model\Table\RolesTable::class, /** * The Permissions model class;If you want to use the default configuration. you don't need to change. */ //'Permissions' => JeffersonSimaoGoncalves\CakePermission\Model\Table\PermissionsTable::class ] ]
- Generate the permission migration
./cake permission_migrate
If ok, now run the migrate command
./cake migrations migrate
Usage
Models
Open your User entity, use UserTrait like this:
namespace App\Model\Entity; use Cake\ORM\Entity; use JeffersonSimaoGoncalves\CakePermission\Model\Entity\UserTrait; class User extends Entity { use UserTrait; //Use trait provied by CakePermission protected $_accessible = [ '*' => true, 'id' => false ]; // ... }
Open your UsersTable, use UserTableTrait like this:
namespace App\Model\Table; use Cake\ORM\Table; use JeffersonSimaoGoncalves\CakePermission\Model\Table\UsersTableTrait; class UsersTable extends Table { use UsersTableTrait; // Use `UsersTableTrait` public function initialize(array $config) { parent::initialize($config); $this->setTable('users'); $this->setDisplayField('id'); $this->setPrimaryKey('id'); $this->buildPermissionRelationship(); // Creats the relationship } // ... }
Using permissions
Creates the permissions with PermissionTrait::create or PermissionTrait::findOrCreate
$addPermission = Permission::findOrCreate('add article'); $editPermission = Permission::create('edit article');
Using roles and permissions
Creates a role to the database with the RoleTrait::create or RoleTrait::findOrCreate
$role = Role::create('editor'); //You can also use the following method. $role = Role::findOrCreate('editor');
Give the permission to the role; You must confirm that the permission exists.
$role->givePermission($addPermission); $role->givePermission($editPermission); //You can also directly give them by thier name $role->givePermission('add article'); $role->givePermission('edit article'); //You can also give multiple permissions at once $role->givePermission(['add article', 'edit article']);
Gets all permissions of the role
$role->getAllPermissions();
Checks whether the role has permission to do something:
$role->hasPermission('edit article'); //true $role->hasPermission(['edit artic;e', 'add article']); //true $role->hasPermission(['edit article', 'drop article']); // false $role->hasAnyPermission('edit article', 'drop article'); // true
Removes the permission
$role->revokePermission($addPermission); //Or by its name $role->revokePermission('add article'); //Revokes all permissions $role->revokeAllPermissions();
User's roles and permissions
Add the role to the user:
$user->assignRole($role); $user->assignRole('editor'); //You can also assign multiple roles at once $user->assignRole(['editor', 'other role']);
Gets all the roles of user
$user->getAllRoles();
Gets all permissions of user:
$user->getAllPermissions();
Checks whether the user has permission to do something:
$user->hasPermission('edit article'); //true $user->hasPermission(['edit artic;e', 'add article']); //true $user->hasPermission(['edit article', 'drop article']); // false $user->hasAnyPermission('edit article', 'drop article'); // true
Removes the role of the user:
$user->removeRole('editor'); //Or removes all roles of the user $user->removeAllRoles();
Extending
You can extends all existing Entity or Table. Do not forget to modify the default configuration in your app.php
Requirements
- CakePHP >=3.6
- PHP 7.0+
LICENSE
The MIT license. See MIT
jeffersonsimaogoncalves/cakephp-permission 适用场景与选型建议
jeffersonsimaogoncalves/cakephp-permission 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 10 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「security」 「cakephp」 「permission」 「rbac」 「role based」 「manage users」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jeffersonsimaogoncalves/cakephp-permission 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jeffersonsimaogoncalves/cakephp-permission 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jeffersonsimaogoncalves/cakephp-permission 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CakePHP 4.x AdminLTE Theme.
Provide a way to secure accesses to all routes of an symfony application.
This package provides a flexible way to add Role-based Permissions to Laravel 6.x
It's a barebone security class written on PHP
Email Toolkit Plugin for CakePHP
This package provides a flexible way to add Role-based Permissions to Laravel
统计信息
- 总下载量: 20
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-20