fahmiardi / laravel-mongodb-permission
基于 Spatie 包,使用 Mongodb (Moloquent) 处理 Laravel 5.3 的权限
Requires
- moloquent/moloquent: ^0.3.0
- spatie/laravel-permission: ~1.5.2
This package is not auto-updated.
Last update: 2024-09-28 19:45:25 UTC
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
- trait 添加到您的用户模型中。
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
的值
单元测试
即将推出。