zorca / roles-and-permissions
在您的 Laravel 应用程序中实现角色和权限,支持多对多关系(枢纽表)。
1.1.2
2022-01-24 12:20 UTC
Requires
- php: ^8.0
- bensampo/laravel-enum: ^4.1
- illuminate/contracts: ^8.73
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- nunomaduro/collision: ^5.10
- orchestra/testbench: ^6.22
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpunit/phpunit: ^9.5
- spatie/laravel-ray: ^1.26
This package is auto-updated.
Last update: 2024-09-25 19:39:39 UTC
README
介绍
此包允许您将角色和权限分配给任何 Laravel 模型,或分配给枢纽表(多对多关系)。
由 Ajimoti Ibukun 构建
文档
您可以在此处阅读正确的文档。
快速示例
在模型上
以下示例说明如何在安装后使用该包在模型上。
use App\Enums\Role; use App\Enums\Permission; // Assign a 'Super Admin' role to this user $user->assign(Role::SuperAdmin); // Check if the user has the role $user->hasRole(Role::SuperAdmin); // Or $user->isSuperAdmin(); // returns true // Check if the user can perform a operation $user->can(Permission::DeleteTransactions); // Or $user->canDeleteTransactions(); // Check if the user has multiple permissions $user->holds(Permission::DeleteTransactions, Permission::BlockUsers);
枢纽表(多对多关系)
这展示了如何在多对多关系上使用该包。在本例中,我们假设在我们的 User
模型中有一个 merchant
关系。这个关系返回 Laravel 的 BelongsToMany
类的一个实例。
导入 App\Enums\Role
和 App\Enums\Permission
类。
use App\Enums\Role; use App\Enums\Permission; // Sample merchant $merchant = Merchant::where('name', 'wallmart')->first(); // Assign a 'Super Admin' role to this user on the selected merchant (wallmart) $user->of($merchant)->assign(Role::SuperAdmin); // Check if the user has a super admin role on the selected merchant (wallmart) $user->of($merchant)->hasRole(Role::SuperAdmin); // Check if the user can 'delete transactions' on the selected merchant (wallmart) $user->of($merchant)->can(Permission::DeleteTransactions); // Check if the user has multiple permissions on the selected merchant (wallmart) $user->of($merchant)->holds(Permission::DeleteTransactions, Permission::BlockUsers);
我们使用了
user
模型来制作示例,类似于包上面的示例,该包将适用于任何模型类。
需求
- PHP 8.0 或更高版本
- Laravel 8.0 或更高版本
- 安装后,该包会发布一个
config/roles-and-permissions.php
文件,请确保您的配置目录中没有同名文件。
优点
- 该包可用于任何模型,即任何模型都可以分配角色和权限。
- 角色可以分配多个权限。
- 模型通过角色拥有权限。
- 模型可以分配多个角色。
- 可以分配角色给多对多关系。(即该包可用于枢纽表)。
- 支持角色层次结构。(高级别的角色可以配置为具有低级别角色的权限)。
定时任务
- 无法直接在多对多关系上分配权限。
安装
您可以通过 composer 安装该包
composer require ajimoti/roles-and-permissions
如果您想在现有的枢纽表上应用该包,您可以将表名添加到 config/roles-and-permissions.php
配置文件中的 pivot.tables
数组中。以下命令将为数组中提供的每个枢纽表添加一个 role
列。
运行以下命令后,您就可以使用该包了。
php artisan roles:install
文档
访问https://roles.ajimoti.com/docs/intro 以更好地了解如何使用该包。
测试
composer test
变更日志
请参阅CHANGELOG 以获取有关最近更改的更多信息。
贡献
请参阅CONTRIBUTING 以获取详细信息。
安全漏洞
有关报告安全漏洞的详细信息,请参阅我们的安全策略。
鸣谢
许可
MIT 许可证(MIT)。有关更多信息,请参阅许可文件。