leafwrap / role-sanctions

角色制裁是一种简洁灵活的方法,用于将基于角色的权限添加到Laravel

0.0.3 2023-11-04 07:43 UTC

This package is auto-updated.

Last update: 2024-09-04 09:24:08 UTC


README

角色制裁是一种简洁灵活的方法,用于将基于角色的权限添加到Laravel

安装

使用包管理器composer安装leafwrap/role-sanctions。

composer require leafwrap/role-sanctions

您可以使用以下命令发布配置文件:

php artisan vendor:publish --tag=role-sanctions

用法

步骤1

在config/role-sanctions.php中添加所有模块

'modules' => [
    ...modules
],

步骤2

添加所有模块后,在AuthServiceProviders中演示所有门

if(auth()->check() && auth()->user()->role){
    RoleSanction::demonstrate(auth()->user()->role);
}

步骤3

然后在控制器方法中验证您的角色能力

use Leafwrap\RoleSanctions\Facades\RoleSanction;

public function index()
{
    try {
        # if use api
        $certify = RoleSanction::certify('{module}-{action}');
        # e.g. $certify = RoleSanction::certify('user-read');

        if(!$certify){
            return response()->json(['message' => 'Unauthorized'], 403);
        }

        # if use general purpose
        RoleSanction::certify('{module}-{action}');
        # e.g. $certify = RoleSanction::certify('user-read');

        ... your code
    } catch (Exception $e) {
        return $e;
    }
}