matiosfree/l-rbac

Laravel 的 RBAC 实现。基于 Laravel Abilities\Gates。

v0.1.1 2021-07-22 21:23 UTC

This package is auto-updated.

Last update: 2024-09-23 04:02:04 UTC


README

Laravel 的 RBAC 实现。基于 Laravel Abilities\Gates (v5+)。本包实现了一个通用分层 RBAC,遵循 Yii2 中的实现

Latest Version on Packagist Software License Total Downloads

安装

  1. 安装此包的首选方法是使用 composer。可以运行
php composer require matiosfree/l-rbac "*"

或者将以下内容添加到 composer.json 的 require 部分。

"matiosfree/l-rbac": "*"

  1. 将服务提供者添加到 config/app.php。
MatiosFree\LRbac\RbacServiceProvider::class,
  1. 使用命令发布服务提供者
php artisan vendor:publish --provider="MatiosFree\LRbac\RbacServiceProvider"
  1. 创建一个扩展 MatiosFree\LRbac\RbacAuthorization 的授权类
<?php
namespace App\Classes;

use App\Classes\Rules\OwnPostRule;
use App\Classes\Rules\RoleRule;
use MatiosFree\LRbac\RbacAuthorization;

class Authorization extends RbacAuthorization {

    public function getDefaultRoles(): array {
        return ['user', 'manager'];
    }

    public function getRoles(): array {
        return [
            'manager' => [
                'description' => 'Manager Role', // optional property
                'ruleName' => RoleRule::class, // optional property that contains the rule for the role\action
                'children' => [ //optional property that contains chaining rules
                    'updatePost',
                    'deletePost',
                ]
            ],
            'user' => [
                'description' => 'User Role',
                'ruleName' => RoleRule::class,
                'children' => [
                    'updateOwnPost'
                ]
            ],
        ];
    }

    public function getPermissions(): array {
        return [
            'updatePost' => [
                'description' => 'Edit any posts'
            ],
            'updateOwnPost' => [
                'description' => 'Edit own post',
                'ruleName' => OwnPostRule::class,
                'children' => [
                    'updatePost' //updateOwnPost is part of updatePost action
                ],
            ],
            'deletePost' => [
                'description' => 'Delete any posts'
            ],
        ];
    }

}

注意!你可能注意到 updatePost 动作是 updateOwnPost 动作的一部分。这意味着如果 updatePost 不被允许,系统将尝试检查对 updateOwnPost 的访问权限。因为用户可能没有更新所有帖子的权限,但他应该能够更新他自己的帖子。此类实现了以下层次结构:RBAC hierarchy

  1. 为所有需要的操作创建特定的规则。你的规则必须实现 MatiosFree\LRbac\Contracts\IRbacRuleContract

此规则检查用户角色

<?php
namespace App\Classes\Rules;


use MatiosFree\LRbac\Contracts\IRbacRuleContract;

class RoleRule implements IRbacRuleContract {

    public function execute($user, $item, $arguments): bool {
        return $user->role === $item->getName();
    }

}

此规则检查用户是否为帖子的作者

<?php
namespace App\Classes\Rules;


use MatiosFree\LRbac\Contracts\IRbacRuleContract;

class OwnPostRule implements IRbacRuleContract {

    public function execute($user, $item, $arguments): bool {
        return $user->id === $arguments['post']->author_id;
    }

}

用法

在代码中,你可以像在官方 Laravel 文档中描述的那样完全一样地检查访问权限 (官方 Laravel 文档)

if (Gate::allows('updatePost', ['post' => $post])) {
    // The current user can update the post...
}


if (Gate::denies('updatePost', ['post' => $post])) {
    // The current user can't update the post...
}


if (Gate::forUser($user)->allows('updatePost', ['post' => $post])) {
    // The user can update the post...
}

//In user model

if ($request->user()->can('updatePost', ['post' => $post])) {
    // The current user can update the post...
}

if ($request->user()->cannot('updatePost', ['post' => $post])) {
    // The current user can't update the post...
}

//In controller:

$this->authorize('updatePost', ['post' => $post]);

// In blade templates


@can('updatePost', ['post' => $post])
    <!-- // The current user can update the post... -->
@else
    <!-- The current user can't update the post... -->
@endcan

默认角色是一个 隐式 分配给 所有 用户的角色。默认角色通常与一个规则相关联,该规则确定该角色是否适用于正在检查的用户。

许可证

MIT 许可证(MIT)。请参阅 许可证文件 获取更多信息。