softvalley/multi-auth-role-permission
Composer 安装命令:
composer require softvalley/multi-auth-role-permission
包简介
Multi auth role permission where user easily assign role permission
README 文档
README
Table of Contents
Description
Multi-Auth Role Permission is a versatile Laravel package designed to create a robust and flexible access control system. This package enables you to define user roles with specific permissions and allows users to customize their permissions beyond their assigned roles. It's a user-friendly solution to enhance the security and control of your Laravel project.
Installation
👉 To install this package, you'll need to have Laravel version 9 or higher and PHP version 8.0.0 or higher installed on your machine. You can download the latest version of PHP from the official PHP resource: https://www.php.net/downloads.php. Ensure your environment meets these requirements before proceeding with the installation.
-
Install auth package in laravel project (JWT, laravel/ui etc).
-
Install package:
composer require Softvalley/multi-auth-role-permission -
Run migrate command:
php artisan migrate -
Add auth guards:
php artisan add:auth {your-guard-name} -
After add auth guard, hit
applicatoin_url/guardsget api for guard list.GEThttp://localhost:8000/guards or use bellow routeuse Softvalley\MultiAuthRolePermission\Http\Controllers\RolePermissionController; use Symfony\Component\HttpFoundation\Response; // auth guards Route::get('/guards', function () { return sendResponse( 'Data fetch successfully.', \Softvalley\MultiAuthRolePermission\Models\AuthGuard::all(), Response::HTTP_OK ); })->name('guards');Resonse:
{ "code": 200, "success": true, "message": "Data fetch successfully.", "data": [ { "id": 1, "name": "admin", "created_at": "2023-10-12T08:27:57.000000Z", "updated_at": "2023-10-12T08:27:57.000000Z", }, { "id": 2, "name": "customer", "created_at": "2023-10-12T08:27:51.000000Z", "updated_at": "2023-10-12T08:27:51.000000Z", } ], "errors": [] } -
Follow bellow route middleware pattern for permissions. User must be login for access the routes.
Route::middleware('check.auth:{your-guard}')->group(function () { // users can access this block routes after login Route::middleware('permission:{your-guard}')->group(function () { // user can access this block routes if user logged in and if user have permission // demo route Route::group(['prefix' => 'test', 'as' => 'test.'], function () { Route::get('/index', function () { return response()->json([ 'code' => 200, 'success' => true, 'message' => 'Permission testing', 'data' => [], 'errors' => [] ]); })->name('index'); }); }); });
-
Suppose you have two
auth guardslikeadminandcustomer. Then you can use bellow route pattern for permission.- For admin.
Route::middleware('check.auth:admin')->group(function () { // users can access this block routes after login Route::middleware('permission:admin')->group(function () { // user can access this block routes if user logged in and if user have permission // demo routes Route::group(['prefix' => 'test', 'as' => 'test.'], function () { Route::get('/admin/dashboard', function () { return response()->json([ 'code' => 200, 'success' => true, 'message' => 'Admin Permission testing', 'data' => [], 'errors' => [] ]); })->name('admin.dashboard'); }); }); });
- For customer
Route::middleware('check.auth:customer')->group(function () { // users can access this block routes after login Route::middleware('permission:customer')->group(function () { // user can access this block routes if user loggedin and if user have permission // demo routes Route::group(['prefix' => 'test', 'as' => 'test.'], function () { Route::get('/profile', function () { return response()->json([ 'code' => 200, 'success' => true, 'message' => 'Customer Permission testing', 'data' => [], 'errors' => [] ]); })->name('customer.profile'); }); }); });
-
For guard wise route permission list use bellow route.
use Softvalley\MultiAuthRolePermission\Http\Controllers\RolePermissionController; Route::get('permission/module/{guard}', [RolePermissionController::class, 'module_permission'])->name('permission.route.list');
{ "code": 200, "success": true, "message": "Data fetch successfully.", "data": [ { "module": "test", "permission": [ { "auth_guard_id": 0, "role_id": 0, "auth_user_id": 0, "module": "test", "operation": " a", "route": "test/a", "is_permit": 0, "route_name": "test.a", "method": "GET" }, { "auth_guard_id": 0, "role_id": 0, "auth_user_id": 0, "module": "test", "operation": " b", "route": "test/b", "is_permit": 0, "route_name": "test.b", "method": "GET" } ] } ], "errors": [] } -
Role CRUD route
use Softvalley\MultiAuthRolePermission\Http\Controllers\RolePermissionController; Route::group(['prefix' => 'role', 'as' => 'role.'], function () { // use `guard` params for gurad wise role list // use `search` params for search role Route::get('/list', [RolePermissionController::class, 'index'])->name('list'); Route::post('/create', [RolePermissionController::class, 'store'])->name('create'); Route::get('/show/{id}', [RolePermissionController::class, 'show'])->name('show'); Route::put('/update/{id}', [RolePermissionController::class, 'update'])->name('update'); Route::delete('/delete/{id}', [RolePermissionController::class, 'destroy'])->name('delete'); });
// example: Request data for role create and update { "auth_guard_id" : 1, // from '{base_url}/gurds' api. "name" : "admin", "is_admin" : 0, // 1 means admin role and 0 means other role. If admin then there is no permission for this role. "role_permissions" : [ // permission data from '{base_url}/permission/{guard}' api. { "module": "test", "permission": [ { "auth_guard_id": 0, "role_id": 0, "auth_user_id": 0, "module": "test", "operation": " a", "route": "test/a", "is_permit": 1, // 1 means add permission for test.a route "route_name": "test.a", "method": "GET" }, { "auth_guard_id": 0, "role_id": 0, "auth_user_id": 0, "module": "test", "operation": " b", "route": "test/b", "is_permit": 0, // 0 means permission not added "route_name": "test.b", "method": "GET" } ] } ] }
-
user permission list from bellow route
use Softvalley\MultiAuthRolePermission\Http\Controllers\RolePermissionController; Route::post('/user/permission/list', [RolePermissionController::class, 'get_user_permission_list'])->name('user.permission.list');
// example: Request data { "auth_user_id" : 1, // your user id "role_id" : 1 }
-
Assaign user permission using bellow route
use Softvalley\MultiAuthRolePermission\Http\Controllers\RolePermissionController; Route::post('/user/permission/add', [RolePermissionController::class, 'user_permission'])->name('user.permission.add');
// example: Request data { "auth_user_id" : 1, // your user id "role_id" : 2, "role_permissions" : [ // permission data from 'user/permission/list' api. { "module": "test", "permission": [ { "auth_guard_id": 0, "role_id": 0, "auth_user_id": 0, "module": "test", "operation": " a", "route": "test/a", "is_permit": 1, // 1 means permission added "route_name": "test.a", "method": "GET" }, { "auth_guard_id": 0, "role_id": 0, "auth_user_id": 0, "module": "test", "operation": " b", "route": "test/b", "is_permit": 0, // 0 means permission not added "route_name": "test.b", "method": "GET" } ] } ] }
-
For user role use
userRole($guard_name, $user_id)function .// response like bellow { "role_id": 1, "role: "Admin", "is_admin": 1 }
softvalley/multi-auth-role-permission 适用场景与选型建议
softvalley/multi-auth-role-permission 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 11 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「permission」 「role」 「role permission」 「Multi Auth」 「multi-auth-role-permission」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 softvalley/multi-auth-role-permission 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 softvalley/multi-auth-role-permission 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 softvalley/multi-auth-role-permission 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Multiauth package
Theme and asset management for laravel
This package provides Laravel bindings and a Eloquent/Model trait for pragmarx/recovery package.
Provide a way to secure accesses to all routes of an symfony application.
The library provides a flexible way to add role-based access control management to CakePHP 3.x
This package provides a flexible way to add Role-based Permissions to Laravel 6.x
统计信息
- 总下载量: 21
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-11-24