mingzaily / lumen-permission by mingzaily

Permission handling for lumen 6.x and up
84
2
2
Package Data
Maintainer Username: mingzaily
Maintainer Contact: mingzaily@163.com (mingzaily)
Package Create Date: 2020-08-12
Package Last Update: 2020-11-10
Language: PHP
License: MIT
Last Refreshed: 2024-12-17 03:07:06
Package Statistics
Total Downloads: 84
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 2
Total Watchers: 2
Total Forks: 2
Total Open Issues: 0

lumen-permission (RBAC Frame)

Build StatusGitHub license

简体中文 | English

开始

安装

第一步,composer安装

composer require mingzaily/lumen-permission

安装成功后,复制以下文件

cp vendor/mingzaily/lumen-permission/config/permission.php config/permission.php
cp vendor/mingzaily/lumen-permission/database/migrations/create_permission_tables.php.stub database/migrations/2020_0 1_01_000000_create_permission_tables.php

同时也需要把lumen-framework核心框架的auth.php配置文件复制出来

cp vendor/laravel/lumen-framework/config/auth.php config/auth.php

然后在 bootstrap/app.php, 根据需要注册PermissionMiddlewarePermissionRouteMiddlewareRoleMiddleware(用法情况使用说明)

$app->routeMiddleware([
    'auth'       => App\Http\Middleware\Authenticate::class,
    'permission' => Mingzaily\Permission\Middlewares\PermissionMiddleware::class,
    'permission_route' => Mingzaily\Permission\Middlewares\PermissionRouteMiddleware::class,
    'role'       => Mingzaily\Permission\Middlewares\RoleMiddleware::class,
]);

在同个文件下, 注册扩展包的配置文件,lumen-permission的服务器提供类,和cache别名

// register permission config file
$app->configure('permission');
// register frame's cacheManager
$app->alias('cache', \Illuminate\Cache\CacheManager::class);  // if you don't have this already
// register lumen-permission ServiceProvider
$app->register(Mingzaily\Permission\PermissionServiceProvider::class);
// register AuthServiceProvider
$app->register(App\Providers\AuthServiceProvider::class);

接着,在.envconfig/database.php配置数据库连接参数

运行迁移文件为扩展包创建表:

php artisan migrate

使用

用户分配,删除,更换角色

$user = Auth::user();
// assign role, also can be written as role id
$user->assignRole('test');
// if deploy multiple roles
$user->assignRole('test1','test2');
// remove role
$user->removeRole('test');
// sync role => remove all assigned role,and assign give role
$user->syncRole('test2');

获取第一个角色

$role = $user->getFirstRole();

获取所有角色

$role = $user->roles;
// or
$role = $user->getAllRoles();

判断是否有该角色

$user->hasRole('test');
$user->hasAnyRole('test','test2');// Return true as long as one exists
$user->hasAllRoles('test','test2');// All roles exist before returning true

角色分配,删除权限

$role->givePermissionTo('view.user');
// or be written as permission id
$role->givePermissionTo(1);
// revoke
$role->revokePermissionTo('view.user');

角色查看权限

$role->getAllPermissions();
// also support tree
$role->getTreePermissions();

判断角色是否有该权限

$role->hasPermissionTo('view.user'); // $role->checkPermissionTo('view.user')
$role->hasAnyPermission('view.user','edit.user');
$role->hasAllPermissions('view.user','edit.user')

中间件

PermissionRouteMiddleware

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

PermissionMiddleware

Route::group(['middleware' => ['permission:view.user']], function () {
    //
});

RoleMiddleware

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

感谢

本扩展基于 spatie/laravel-permission 进行更改

不同点

  • 支持配置一用户单角色或者一用户多角色
  • 修改permission表结构
    • 添加route,method,display_name,pid,is_menu字段
    • 删除 guard_name字段
  • 修改role表结构
    • 添加 display_name字段
    • Delete filed guard_name
  • 移出model_has_permission表格,移出model的直接权限
  • 移出Laravel blade模板支持(如需要,请使用 spatie/laravel-permission
  • 移出Guard看守器配置
  • 移出通配符权限设置

License

The MIT License (MIT). Please see License File for more information.