kawsarjoy / rolepermission
为 Laravel 开发的角色权限包
This package is auto-updated.
Last update: 2024-09-24 16:09:35 UTC
README
一个用于处理 Laravel 中角色和权限的简单包。
安装
此包非常易于设置。只需几个步骤。
Composer
通过 Composer 拉取此包
composer require kawsarjoy/rolepermission
服务提供者
-
Laravel 5.5 及以上版本使用包自动发现功能,无需编辑
config/app.php
文件。 -
Laravel 5.4 及以下版本,请将包添加到您的应用程序服务提供者在
config/app.php
文件中。
'providers' => [ ... /** * Third Party Service Providers... */ KawsarJoy\RolePermission\RolePermissionServiceProvider::class, ],
配置文件
将包配置文件发布到您的应用程序。在您的终端中运行以下命令。
php artisan vendor:publish --provider="KawsarJoy\RolePermission\RolePermissionServiceProvider" --tag="config"
you may set `rolepermission-enable` to `false` to disable the feature.
you may set `do-migration` to `false` in config file to disable the migration.
Note: database must have all the necessary tables and columns if you disable migration.
迁移文件
将包迁移文件发布到您的应用程序。在您的终端中运行以下命令。
php artisan vendor:publish --provider="KawsarJoy\RolePermission\RolePermissionServiceProvider" --tag="migration"
可授权特性
-
在您的
User
模型中包含Permissible
特性和实现Permissible
合同。以下是一个示例。 -
在
User
模型的顶部包含use KawsarJoy\RolePermission\Permissible;
,并在命名空间下方实现Permissible
特性。以下是一个示例。
示例 User
模型特性和合同
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Illuminate\Notifications\Notifiable; use KawsarJoy\RolePermission\Permissible; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; use Permissible; // rest of your model ... }
迁移和种子
这使用的是 Laravel 中的默认用户表。您应该已经有用户表的迁移文件,并且已经迁移。
-
设置所需的表
php artisan migrate
-
更新
database\seeds\DatabaseSeeder.php
以包含种子。以下是一个示例。
<?php use Illuminate\Database\Seeder; use Illuminate\Database\Eloquent\Model; class DatabaseSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { Model::unguard(); $this->call('RolesTableSeeder'); $this->call('PermissionsTableSeeder'); $this->call('ConnectRelationshipsSeeder'); //$this->call('UsersTableSeeder'); Model::reguard(); } }
- 使用角色创建一组初始权限、角色和用户。
composer dump-autoload
php artisan db:seed
角色已种植
权限已种植
就这样!
用法
创建角色
use KawsarJoy\RolePermission\Models\Role; $adminRole = Role::create([ 'name' => 'admin', 'description' => 'Admin User', ]); $authorRole = Role::create([ 'name' => 'author', 'description' => 'Author User', ]);
附加、分离和同步角色
非常简单。从数据库中获取用户并调用 attachRole
方法。在 User
和 Role
模型之间存在 BelongsToMany
关系。
use App\User; $user = User::find($id); $user->roles()->sync([1,2]); // you have to pass array of ids
为新注册用户分配用户角色
您可以在用户注册时通过更新 app\Http\Controllers\Auth\RegisterController.php
文件将角色分配给用户。您可以通过包含所需模型并修改 create()
方法来附加用户角色,在注册时将角色分配给用户。以下是一个示例
- 更新
app\Http\Controllers\Auth\RegisterController.php
的顶部
<?php namespace App\Http\Controllers\Auth; use App\User; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Validator; use KawsarJoy\RolePermission\Models\Role; use KawsarJoy\RolePermission\Models\Permission; use Illuminate\Foundation\Auth\RegistersUsers;
检查角色
现在您可以通过以下方式检查用户是否有所需的角色。
if ($user->hasRole('admin')) { // you have to pass a name // }
当然,还有检查多个角色的方法
if ($user->hasRole(['admin', 'moderator'])) { // The user has at least one of the roles }
创建权限
多亏了 Permission
模型,这非常简单。
use KawsarJoy\RolePermission\Models\Permission; $createUsersPermission = Permission::create([ 'name' => 'create-user', 'description' => 'Can create user', // optional ]); $deleteUsersPermission = Permission::create([ 'name' => 'delete-user', 'description' => 'Can delete user', // optional ]);
附加、分离和同步权限
您可以将权限附加到角色上
use App\User; use KawsarJoy\RolePermission\Models\Role; $role = Role::find($roleId); $role->permissions()->sync([1,2,3]); // permissions attached to a role
检查权限
if ($user->hasPermission('create-user') { // you have to pass a name // }
您可以像角色一样检查多个权限。
Blade 扩展
有四个 Blade 扩展。基本上,它是对经典 if 语句的替代。
@role('admin') // @if(Auth::check() && Auth::user()->hasRole('admin')) // user has admin role @endrole @role(['admin', 'author']) // @if(Auth::check() && Auth::user()->hasRole('admin|author')) // user has admin or author role @endrole @permission('edit-articles') // @if(Auth::check() && Auth::user()->hasPermission('edit-articles')) // user has edit articles permissison @endpermission @permission(['edit-articles','add-articles']) // @if(Auth::check() && Auth::user()->hasPermission('edit-articles|add-articles')) // user has edit or add articles permissison @endpermission
权限门
此包包含 roles
、permissions
权限门。现在您可以轻松地保护您的控制器方法。
public function create() { if(Gate::allows('roles', 'admin')) { // user has admin role } } // you can pass multiple role public function edit() { if(Gate::allows('roles', 'admin|author')) { // user has admin or author role } } // same goes for permission // Gate::allows('permissions', 'add-articles') // Gate::allows('permissions', 'add-articles|edit-articles') // you can also use public function create() { if(Gate::denies('roles', 'admin')) { // user has not admin role } } public function edit() { if(Gate::denies('roles', 'admin|author')) { // user neither has admin nor author role } } // same goes for permission // Gate::denies('permissions', 'add-articles') // user do not have add article permission // Gate::denies('permissions', 'add-articles|edit-articles') // user neither have add article or edit article permission
中间件
此包包含 CheckRole
、CheckPermission
中间件。这些中间件将由 RolePermissionServiceProvider 自动加载。您无需再次将其添加到您的 Http\Kernel 中
现在您可以轻松地保护您的路由。
Route::get('/', function () { // })->middleware('roles:admin'); Route::get('/', function () { // })->middleware('permissions:edit-user'); //Permissions middleware will chack the permissions based on the resource route names eg. [posts/create => [posts.create | [prefix].posts.create]] // you should have laravel resource route name as same as in permissions table. Like below // posts.index // posts.create // posts.show // posts.edit // posts.update // posts.destroy Route::Resource('posts','PostController')->middleware('permissions'); Route::group(['middleware' => ['roles:admin']], function () { // }); Route::group(['middleware' => ['roles:admin|author']], function () { // }); Route::group(['middleware' => ['permissions:create-user']], function () { // }); Route::group(['middleware' => ['permissions:create-user|edit-user']], function () { // });
系统抛出403 HTTP错误,并加载随包一起提供的403.blade.php视图文件。如果您想修改视图,您需要通过运行此命令来发布视图。
php artisan vendor:publish --provider="KawsarJoy\RolePermission\RolePermissionServiceProvider" --tag="views"
信用凭证
本包的readme文件受到了laravel-roles的启发。
配置文件
您可以在config目录下的default-user.php中更改模型名称和主键。更多信息请查看配置文件。
更多信息
有关更多信息,请参阅HasRoleAndPermission特质。
提交问题
在提出问题之前,有几个考虑因素
- 本项目上的一个星号表示支持,也是对所有贡献者的感谢方式。如果您在没有星号的情况下打开一个问题,您的提问可能会被忽略。感谢您的理解和支持。
- 阅读说明并确保所有步骤都正确执行。
- 检查问题是否与您的开发环境设置特定。
- 提供复现步骤。
- 尝试查找问题,如果您有解决方案,请发起拉取请求。
- 表明您已尝试查找问题。
- 检查您报告的问题是否是先前报告过的重复问题。
- 遵循这些说明表明您已经尝试过。
- 如果您有任何问题,请发送电子邮件至engabukawsar@gmail.com
- 请理解这是一个开源项目,我在打开问题时免费提供给社区。