Package Data | |
---|---|
Maintainer Username: | w3ns0n |
Maintainer Contact: | w3ns0n@gmail.com (w3ns0n) |
Package Create Date: | 2021-10-08 |
Package Last Update: | 2021-10-08 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-18 03:00:49 |
Package Statistics | |
---|---|
Total Downloads: | 81 |
Monthly Downloads: | 1 |
Daily Downloads: | 0 |
Total Stars: | 0 |
Total Watchers: | 1 |
Total Forks: | 1 |
Total Open Issues: | 0 |
简体中文 | English
第一步,composer
安装
composer require w3ns0n/lumen-permission
安装成功后,复制以下文件
cp vendor/w3ns0n/lumen-permission/config/permission.php config/permission.php
cp vendor/w3ns0n/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
, 根据需要注册PermissionMiddleware
,PermissionRouteMiddleware
,RoleMiddleware
(用法情况使用说明)
$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);
接着,在.env
或config/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 current roles and set the given ones.
$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');
// sync => remove all current permissions and set the given ones.
$role->syncPermissionTo(1,2,3)
角色查看权限
$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')
也可以通过懒加载进行获取角色及相关权限
$user->load('roles.permissions');
PermissionRouteMiddleware
Route::group(['middleware' => 'permission_route'], function () {
//
});
PermissionMiddleware
Route::group(['middleware' => ['permission:view.user']], function () {
//
});
RoleMiddleware
Route::group(['middleware' => ['role:test']], function () {
//
});
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
......
public function register()
{
// super-admin no need to verify permissions
Gate::before(function ($user, string $ability) {
return $user->hasRole('super-admin') ? true : null;
});
}
......
}
本扩展基于 spatie/laravel-permission 进行更改
permission
表结构
route
,method
,display_name
,pid
,is_menu
字段guard_name
字段role
表结构
display_name
字段guard_name
model_has_permission
表格,移出model
的直接权限The MIT License (MIT). Please see License File for more information.