inani/ maravel-permissions
一个用于处理用户角色和权限的 Laravel 扩展包
2.1
2021-02-20 15:38 UTC
Requires
- illuminate/support: ~5|~6|~7|~8
Requires (Dev)
- fzaninotto/faker: ~1.4
- phpunit/phpunit: ~4.0
This package is auto-updated.
Last update: 2024-09-20 23:39:14 UTC
README
下载
composer require inani/maravel-permissions
安装
然后,在 config/app.php
中包含服务提供者。(如果是 Laravel 5.5 或更高版本,可以跳过此步骤)
'providers' => [ ... Inani\Maravel\Providers\MaravelServiceProvider::class, ... ];
发布资源并迁移
php artisan vendor:publish
PS:您可以编辑 2020_05_27_221346_add_role_id_to_users
迁移来将其与正确的用户表关联
使用正确的值编辑 config/maravels.php
<?php return [ // Define the list of actions to be checked against 'actions' => [ 'add', 'delete', 'create', 'search', 'update' ], // define the class path for the entities 'entities' => [ \App\Models\User::class, ], ];
然后迁移
php artisan migrate
设置模型
要设置用户模型,您只需添加(并导入)IsMarvel
特性。
use Inani\Maravel\HasRole; class User extends Model { use HasRole; ... }
用法
所有角色都是角色,权限是权力
因为每个用户都值得成为英雄,Maravel API 基于漫威术语,以下是使用方法
// Having a user $user = User::first(); // Create a new role, description is not mandotary $userManager = RoleBuilder::create('User Manager', 'The role to manage users') ->havingPower([ 'name' => 'can_update', 'description' => 'The abilitiy to update a user', 'action' => 'update', 'entity' => \App\Models\User::class, ]); // we can grant a power to it $userManager = RoleBuilder::of($userManager) ->grant([ 'name' => 'can_create', 'description' => 'The abilitiy to create a user', 'action' => 'create', 'entity' => \App\Models\User::class, ]); // Or take it off $ability = Ability::first(); $storm = RoleBuilder::of($userManager)->takeOff($ability); // bless the user with the abilities of the role $user->roleManager()->blessWith($storm); // check if it has the ability $user->roleManager()->owns($ability); // check if it has one of the provided abilities $user->roleManager()->ownsOneOf([$ability, $anOtherAbility]); // make it human again (remove its role) $user->roleManager()->humanize();
您也可以直接管理实例
// Create Ability $ability = Ability::create([ 'name' => 'post_write', 'description' => 'Abitlity to create new Posts', 'action' => 'add', 'entity' => \App\Models\Post::class, ]); // Create a Marvel $writer = Role::create([ 'name' => 'Webmaster', 'description' => 'A Role that allows you create new posts' ]); // Grant the ability $writer->grant($ability); // remove a certain ability $writer->takeOff($ability); // remove all and keep only those $abilities = [1, 2]; // or the models $writer->keep($abilities); // bless it to our user $user = \App\Models\User::first(); $user->roleManager()->blessWith($writer);
我遗漏了什么吗?
提交带有详细信息的 PR 或问题!