livelyworks/laravel-yes-authority

YesAuthority - Laravel 路由权限库

2.9.16 2023-02-28 18:22 UTC

README

YesAuthority 是一个灵活的 Laravel 授权系统,它通过检查 route 权限来访问网站的特定部分或应用程序。可以通过 User-basedRole-basedConditionally 添加权限。它使用 authority.checkpost 中间件来过滤当前访问路由的权限,在这个中间件中会检查用户登录的每一个权限。

安装

在您的 composer.json 中要求此包,或者通过运行以下命令进行安装:

    composer require livelyworks/laravel-yes-authority

现在,将此行插入到您的 config/app.php 文件中的 provider 数组下。

    LivelyWorks\YesAuthority\YesAuthorityServiceProvider::class

现在,在 config/yes-authority.phpapp/Http/Middleware/YesAuthorityCheckpostMiddleware.php 文件发布后运行此命令。

    php artisan vendor:publish  --tag="yesauthority"

现在,将此行插入到您的 app/Http/Kernel.php 文件中的 $routeMiddleware 数组下。

    'authority.checkpost'  => \App\Http\Middleware\YesAuthorityCheckpostMiddleware::class

使用 authority.checkpost 中间件来处理基于权限的路由。

    Route::group(['middleware' => 'authority.checkpost'], function () {
        // Place all those routes here which needs authentication and authorization.
    });

现在,基本设置就绪了。您需要使用 config/yes-authority 配置权限规则。

配置

以下列出了权限结构,但强烈建议您阅读更多关于 文档 的内容。

    [
        'allow' => ['*'], // Allowed permission to user. Priority is less than deny.
        'deny'  => ['temp1'], // Deny permission to user. Priority is higher than allow.
    ]

    canAccess('temp1');
    // false 

用法 - 助手

  • canAccess($accessId = null);
    检查访问权限,默认情况下检查当前路由,并以布尔值返回响应。
    canAccess('temp1');
    // true or false
  • canPublicAccess($accessId = null); - Authentication not required
    检查公共访问权限,默认情况下检查当前路由,并以布尔值返回响应。
    canPublicAccess();
    // true or false

用法 - 门面

  • YesAuthority::check($accessId = null, $requestForUserId = null)
    检查 $accessId 的访问权限,默认情况下检查当前路由,并以布尔值返回响应。它还可以通过传递用户 ID ($requestForUserId) 参数来检查特定用户的访问权限。
    YesAuthority::check('temp1');
    // true or false
  • YesAuthority::isPublicAccess($accessId = null); - Authentication not required
    检查 $accessId 的访问权限,默认情况下检查当前路由,并以布尔值返回响应。
    YesAuthority::isPublicAccess('temp1');
    // true or false

用法 - 指令

  • @canAccess($accessId = null);
    检查访问权限,默认情况下检查当前路由,并以布尔值返回响应。
    @canAccess()
       // your logic here.
    @endAccess;
  • @canPublicAccess($accessId = null); - Authentication not required
    检查公共访问权限,默认情况下检查当前路由,并以布尔值返回响应。
    @canPublicAccess()
       // your logic here.
    @endAccess;