luongtran/laravel-mongodb-permission-spatie

基于 Spatie 包使用 Mongodb (Moloquent) 对 Laravel 进行权限处理

v1.0.3 2023-09-24 15:47 UTC

This package is not auto-updated.

Last update: 2024-09-09 18:54:31 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 添加到您的 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');

可以测试用户是否有权限

$user->can('edit articles');

保存的权限将注册到 Illuminate\Auth\Access\Gate-类中。因此,您可以使用 Laravel 的默认 can 函数测试用户是否有权限。

$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());

您也可以确定用户是否具有给定列表中的任何角色

您还可以确定用户是否具有给定列表中的所有角色

$role->givePermissionTo('edit articles');

函数 assignRolehasRolehasAnyRolehasAllRolesremoveRole 可以接受字符串、Spatie\Permission\Models\Role 对象或 \Illuminate\Support\Collection 对象。

$role->hasPermissionTo('edit articles');

可以将权限授予角色

$role->revokePermissionTo('edit articles');

可以确定角色是否具有特定的权限

可以从角色中撤销权限

$user->can('edit articles');

函数 givePermissionTorevokePermissionTo 可以接受字符串或 Spatie\Permission\Models\Permission 对象。

@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

保存的权限和角色也注册到 Illuminate\Auth\Access\Gate-类中。

###使用 blade 指令

此包还添加了 Blade 指令以验证当前登录用户是否具有所有或任何给定的角色列表。

$ php artisan make:middleware RoleMiddleware

您可以使用 Laravel 的原生 @can 指令检查用户是否具有特定权限。

// 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);
}

使用中间件

// app/Http/Kernel.php
protected $routeMiddleware = [
    ...
    'role' => \App\Http\Middleware\RoleMiddleware::class,
    ...
];

此包不包含检查权限的中间件,但很容易自行添加。

Route::group(['middleware' => ['role:admin,access_backend']], function () {
    //
});

这将为您创建一个 RoleMiddleware,您可以在其中处理角色和权限检查。

不要忘记将路由中间件添加到您的 Kernel

  • 您的 Role 模型需要实现 Spatie\Permission\Contracts\Role 接口
  • 您的 Permission 模型需要实现 Spatie\Permission\Contracts\Permission 接口
  • 您必须使用以下命令发布配置:php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="config" 并更新 models.rolemodels.permission 的值

单元测试

即将推出。