denismitr / laravel-permissions
Laravel 角色和权限包,支持团队
Requires
- php: >=7.1.0
- illuminate/auth: ~5.5.0|~5.6.0|~5.7.0|~5.8.0
- illuminate/console: ~5.5.0|~5.6.0|~5.7.0|~5.8.0
- illuminate/container: ~5.5.0|~5.6.0|~5.7.0|~5.8.0
- illuminate/contracts: ~5.5.0|~5.6.0|~5.7.0|~5.8.0
- illuminate/database: ~5.5.0|~5.6.0|~5.7.0|~5.8.0
- illuminate/events: ~5.5.0|~5.6.0|~5.7.0|~5.8.0
- illuminate/support: ~5.5.0|~5.6.0|~5.7.0|~5.8.0
Requires (Dev)
- orchestra/testbench: 3.*
- phpunit/phpunit: 7.*
README
Laravel Permissions
这是一个用于集成 Laravel 5.5 - 5.8 的包
安装
使用 composer 安装此包
composer require denismitr/laravel-permissions
更新 composer 后,将 PermissionsServiceProvider
添加到 config/app.php
中的 providers 数组,如下所示
Denismitr\Permissions\PermissionsServiceProvider::class,
然后,如果您需要使用提供的中间件之一,您可以将 auth.group
中间件添加到您的 Http Kernel.php
中,如下所示
'auth.group.all' => \Denismitr\Permissions\Middleware\AuthGroupAllMiddleware::class, 'auth.group.any' => \Denismitr\Permissions\Middleware\AuthGroupAnyMiddleware::class,
此中间件确保用户属于所有必需的认证组
发布配置和迁移
php artisan vendor:publish
并从列表中选择 Provider: Denismitr\Permissions\PermissionsServiceProvider
迁移
然后运行 php artisan migrate
,将创建以下 5 个表
- auth_groups
- permissions
- auth_group_users
- auth_group_permissions
创建 CRUD 并填充这些表由您自己决定。
用法
首先将 InteractsWithAuthGroups
特性包含到 User
模型中,如下所示
use InteractsWithAuthGroups;
要将用户添加到认证组并授予他们组权限
// Given we have AuthGroup::create(['name' => 'superusers']); // To find an auth group by name AuthGroup::named('superusers')->addUser($userA)->addUser($userB); $userA->isOneOf('superusers'); //true $userB->isOneOf('superusers'); // true // Gives permission to the choosen group AuthGroup::named('superusers')->givePermissionTo($editArticlesPermission); AuthGroup::named('superusers')->givePermissionTo($editBlogPermission); // These methods check if user has a permission through any auth group, // to which user belongs $userA->hasPermissionTo('edit-articles'); // true $userA->isAllowedTo('edit-blog'); // true $userB->hasPermissionTo('edit-blog'); // true $userB->isAllowedTo('edit-articles'); // true
检查认证组是否已存在
AuthGroup::existsWithName('accountants'); // returns true or false
私有组和/或团队
用户可以创建私有组或基本上是团队。请注意,在 InteractsWithAuthGroups
特性中有一个 canOwnAuthGroups
方法,默认返回 true
。如果您想定义一些自定义规则来决定是否允许某个或某些用户创建认证组,这可能是您需要做的,您需要在您的用户模型中重写此方法。
$privateGroup = $this->owner->createNewAuthGroup('My private group', 'My private group description'); $privateGroup ->addUser($this->userA) ->addUser($this->userB); // Custome role can be specified ->addUser($this->userB, 'accountant'); $authGroup->hasUser($this->userA); // true $authGroup->isOwnedBy($this->owner); // true $this->owner->ownsAuthGroup($authGroup); // true $authGroup->forUser($this->userA)->allowTo('edit-articles');
角色
角色只是字符串,它们应该仅作为辅助工具使用。
$user->onAuthGroup($privateGroup)->getRole(); // Owner (this one can be setup in config of the package) $user->joinAuthGroup($bloggers, 'Invited user'); $user->joinAuthGroup($editors, 'Supervisor'); $user->onAuthGroup($editors)->getRole(); // 'Invited user' $user->onAuthGroup($privateGroup)->getRole(); // 'Supervisor' $user->onAuthGroup($bloggers)->hasRole('Invited user'); // true $user->onAuthGroup($editors)->hasRole('Supervisor'); // true $user->onAuthGroup($privateGroup)->hasRole('Pinguin'); // false
为了撤销权限
$authGroup->revokePermissionTo('delete post', 'edit post');
通过认证组授予权限
$admin->joinAuthGroup('admins'); // group must already exist $admin->onAuthGroup('admins')->grantPermissionTo('administrate-blog'); // permission must already exist // same as $admin->onAuthGroup('admins')->allowTo('administrate-blog'); // permission must already exist // or $admin->onAuthGroup('admins')->givePermissionTo('administrate-blog'); // later $blogAdminPermission->isGrantedFor($this->admin);
为了检查权限
$user->hasPermissionTo('edit post', 'delete post'); $user->can('delete post');
注意!!! 由于兼容性原因,can
方法只能支持单个能力参数
当前认证组
用户可以通过添加到 users
表的 current_auth_group_id
列来拥有一个当前认证组,该列由包迁移添加。此功能可以用于模拟在不同 团队 之间切换。
// Given $user = User::create(['email' => 'new@user.com']); $authGroupA = AuthGroup::create(['name' => 'Auth group A']); $authGroupB = AuthGroup::create(['name' => 'Auth group B']); // Do that $user->joinAuthGroup($authGroupA); $user->joinAuthGroup($authGroupB); // Expect user is on two authGroups $user->isOneOf($authGroupA); // true $user->isOneOf($authGroupB); // true // Do switch to authGroupB $user->switchToAuthGroup($authGroupB); // currentAuthGroup() method returns a current AuthGroup model or null in case user is // not a member of any group // currentAuthGroupName() works in the same way and can be used to display current team or group name $user->currentAuthGroup(); // $authGroupB $user->currentAuthGroupName(); // Auth group B
请注意,如果用户属于一个或多个 认证组,则 currentAuthGroup()
方法将自动选择并设置用户的一个认证组为当前组,通过 current_auth_group_id
列持久化在 User
模型上,并返回它。同样适用于 currentAuthGroupName()
。
以及一个额外的奖励:一个 blade authgroup
和 team
指令
@authgroup('staff') // do stuff @endauthgroup
以及它的别名
@team('some team') // do stuff @endteam
其他一些指令
@isoneof('admins') ... @endisoneof @isoneofany('writers|bloggers') ... @endisoneofany
@isoneofall('authors,writers,bloggers') ... @endisoneofall
作者
Denis Mitrofanov