blpraveen / laravel-mongodb-permission-spatie
基于Spatie包使用MongoDB (Moloquent) 对Laravel进行权限处理
Requires
- blpraveen/moloquent: ^0.3.0
- illuminate/container: ^8.0
- spatie/laravel-permission: ^4.2
README
此包允许在数据库中保存权限和角色。它基于 Spatie Laravel Permission 构建。
安装后,您可以执行如下操作:
//adding permissions to a user $user->givePermissionTo('edit articles'); //adding permissions via a role $user->assignRole('writer'); $user2->assignRole('writer'); $role->givePermissionTo('edit articles');
您可以使用Laravel的默认 can
函数测试用户是否具有权限。
$user->can('edit articles');
安装
您可以通过composer安装此包
$ composer require fahmiardi/laravel-mongodb-permission
必须安装此服务提供者以及Spatie提供者。
// config/app.php 'providers' => [ ... Spatie\Permission\PermissionServiceProvider::class, Fahmiardi\Mongodb\Permissions\PermissionServiceProvider::class, ];
您可以使用以下命令发布配置文件:
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="config"
这是发布配置文件的内容
// config/laravel-permission.php return [ /* |-------------------------------------------------------------------------- | Authorization Models |-------------------------------------------------------------------------- */ 'models' => [ /* |-------------------------------------------------------------------------- | Permission Model |-------------------------------------------------------------------------- | | When using the "HasRoles" trait from this package, we need to know which | Eloquent model should be used to retrieve your permissions. Of course, it | is often just the "Permission" model but you may use whatever you like. | | The model you want to use as a Permission model needs to implement the | `Spatie\Permission\Contracts\Permission` contract. | */ 'permission' => Spatie\Permission\Models\Permission::class, /* |-------------------------------------------------------------------------- | Role Model |-------------------------------------------------------------------------- | | When using the "HasRoles" trait from this package, we need to know which | Eloquent model should be used to retrieve your roles. Of course, it | is often just the "Role" model but you may use whatever you like. | | The model you want to use as a Role model needs to implement the | `Spatie\Permission\Contracts\Role` contract. | */ 'role' => Spatie\Permission\Models\Role::class, ], /* |-------------------------------------------------------------------------- | Authorization Tables |-------------------------------------------------------------------------- */ 'table_names' => [ /* |-------------------------------------------------------------------------- | Roles Table |-------------------------------------------------------------------------- | | When using the "HasRoles" trait from this package, we need to know which | table should be used to retrieve your roles. We have chosen a basic | default value but you may easily change it to any table you like. | */ 'roles' => 'roles', /* |-------------------------------------------------------------------------- | Permissions Table |-------------------------------------------------------------------------- | | When using the "HasRoles" trait from this package, we need to know which | table should be used to retrieve your permissions. We have chosen a basic | default value but you may easily change it to any table you like. | */ 'permissions' => 'permissions', /* |-------------------------------------------------------------------------- | User Permissions Table |-------------------------------------------------------------------------- | | When using the "HasRoles" trait from this package, we need to know which | table should be used to retrieve your users permissions. We have chosen a | basic default value but you may easily change it to any table you like. | */ 'user_has_permissions' => 'user_has_permissions', /* |-------------------------------------------------------------------------- | User Roles Table |-------------------------------------------------------------------------- | | When using the "HasRoles" trait from this package, we need to know which | table should be used to retrieve your users roles. We have chosen a | basic default value but you may easily change it to any table you like. | */ 'user_has_roles' => 'user_has_roles', /* |-------------------------------------------------------------------------- | Role Permissions Table |-------------------------------------------------------------------------- | | When using the "HasRoles" trait from this package, we need to know which | table should be used to retrieve your roles permissions. We have chosen a | basic default value but you may easily change it to any table you like. | */ 'role_has_permissions' => 'role_has_permissions', ], ];
调整上面的 table_names
配置以支持MongoDB的许多对多关系(使用EmbedsMany)
'user_has_permissions' => Fahmiardi\Mongodb\Permissions\Models\EmbedPermission::class, 'user_has_roles' => Fahmiardi\Mongodb\Permissions\Models\EmbedRole::class, 'role_has_permissions' => Fahmiardi\Mongodb\Permissions\Models\EmbedPermission::class,
用法
首先将 Spatie\Permission\Traits\HasRoles
-特性添加到您的User模型中。
use Illuminate\Foundation\Auth\User as Authenticatable; use Spatie\Permission\Traits\HasRoles; class User extends Authenticatable { use HasRoles; // ... }
此包允许用户与角色相关联。权限可以与角色相关联。Role和Permission是常规Eloquent模型。它们可以有名称,可以按以下方式创建:
use Spatie\Permission\Models\Role; use Spatie\Permission\Models\Permission; $role = Role::create(['name' => 'writer']); $permission = Permission::create(['name' => 'edit articles']);
HasRoles
为您的模型添加Eloquent关系,可以直接访问或用作基础查询。
$permissions = $user->permissions; $roles = $user->roles()->pluck('name'); // returns a collection
###使用权限 您可以将权限授予用户
$user->givePermissionTo('edit articles'); //you can also give multiple permission at once $user->givePermissionTo('edit articles', 'delete articles'); //you may also pass an array $user->givePermissionTo(['edit articles', 'delete articles']);
您可以从用户中撤销权限
$user->revokePermissionTo('edit articles');
您可以测试用户是否具有权限
$user->hasPermissionTo('edit articles');
保存的权限将注册到 Illuminate\Auth\Access\Gate
-类中。因此,您可以使用Laravel的默认 can
函数测试用户是否具有权限。
$user->can('edit articles');
###使用角色和权限 您可以将角色分配给用户
$user->assignRole('writer'); // you can also assign multiple roles at once $user->assignRole('writer', 'admin'); $user->assignRole(['writer', 'admin']);
您可以从用户中移除角色
$user->removeRole('writer');
角色也可以同步
//all current roles will be removed from the user and replace by the array given $user->syncRoles(['writer', 'admin']);
您可以确定用户是否具有某个特定角色
$user->hasRole('writer');
您还可以确定用户是否具有给定列表中的任何角色
$user->hasAnyRole(Role::all());
您还可以确定用户是否具有给定列表中的所有角色
$user->hasAllRoles(Role::all());
assignRole
、hasRole
、hasAnyRole
、hasAllRoles
和 removeRole
-函数可以接受字符串、Spatie\Permission\Models\Role
-对象或 \Illuminate\Support\Collection
-对象。
您可以将权限授予角色
$role->givePermissionTo('edit articles');
您可以确定角色是否具有某个特定权限
$role->hasPermissionTo('edit articles');
您可以从角色中撤销权限
$role->revokePermissionTo('edit articles');
givePermissionTo
和 revokePermissionTo
-函数可以接受字符串或 Spatie\Permission\Models\Permission
-对象。
保存的权限和角色也注册到了 Illuminate\Auth\Access\Gate
-类中。
$user->can('edit articles');
###使用Blade指令 此包还添加了Blade指令,用于验证当前登录用户是否具有所有或给定列表中的任何角色。
@role('writer') I'm a writer! @else I'm not a writer... @endrole
@hasrole('writer') I'm a writer! @else I'm not a writer... @endhasrole
@hasanyrole(Role::all()) I have one or more of these roles! @else I have none of these roles... @endhasanyrole
@hasallroles(Role::all()) I have all of these roles! @else I don't have all of these roles... @endhasallroles
您可以使用Laravel的本地 @can
指令检查用户是否具有某个权限。
使用中间件
此包不包含检查权限的中间件,但您可以非常容易地自己添加。
$ php artisan make:middleware RoleMiddleware
这将为您创建一个RoleMiddleware,您可以在其中处理角色和权限检查。
// app/Http/Middleware/RoleMiddleware.php use Auth; ... public function handle($request, Closure $next, $role, $permission) { if (Auth::guest()) { return redirect($urlOfYourLoginPage); } if (! $request->user()->hasRole($role)) { abort(403); } if (! $request->user()->can($permission)) { abort(403); } return $next($request); }
别忘了将路由中间件添加到您的Kernel
// app/Http/Kernel.php protected $routeMiddleware = [ ... 'role' => \App\Http\Middleware\RoleMiddleware::class, ... ];
现在,您可以使用您刚刚设置的中间件来保护路由
Route::group(['middleware' => ['role:admin,access_backend']], function () { // });
扩展
如果您需要扩展或替换现有的 Role
或 Permission
模型,只需记住以下事项:
- 您的
Role
模型需要实现Spatie\Permission\Contracts\Role
接口 - 您的
Permission
模型需要实现Spatie\Permission\Contracts\Permission
接口 - 您必须使用以下命令发布配置:
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="config"
并更新models.role
和models.permission
的值
单元测试
很快。