gpt4 book ai didi

php - Laravel : Setting dynamic routes based on access control list

转载 作者:行者123 更新时间:2023-12-01 15:14:16 25 4
gpt4 key购买 nike

我在建 REST API JWT具有自己的逻辑的身份验证和授权。它工作得很好。现在,我想根据角色和权限动态设置路由。假设我有这样的数据库结构:

角色:

id  |   name
1 | school
2 | transport

权限:
id  |   name                   |  controller         | routes
1 | view-class-result | ApiController | getClassResult
2 | view-student-result | ApiController | studentResult
3 | download-student-result | ApiController | donwloadSchoolTemplate

Permission_role
role_id |  permission_id
1 1
1 2
1 3

现在,我想根据数据库中的角色和权限创建路由。

目前我的路线看起来像:
//All JWT authentication API goes here
Route::group(['middleware' => 'jwt.auth'], function() {
Route::get('user', 'ApiController@getAuthUser');
Route::get('invalidate', 'ApiController@invalidate');

//All authorized API goes here
Route::group(['middleware' => 'ability:school,view-class-result,true'], function() {
Route::post('classResult', 'ApiController@getClassResult');
});
Route::group(['middleware' => 'ability:school,view-student-result,true'], function() {
Route::post('studentResult', 'ApiController@studentResult');
});
Route::group(['middleware' => 'ability:school,download-student-result,true'], function() {
Route::post('getStudentExamResult', 'ApiController@downloadSchoolTemplate');
});
});

我不希望以上路线被硬编码。如何从数据库中获取此路线。像下面这样的东西。但不知道该怎么做。

在路由文件中,
$a = User:all();
foreach($a->roles as $value){
foreach($value->permission as $val){

Route::group(['middleware' => 'ability:{$value->name},{$val->name},true'], function() {
Route::post('{$val->controller}', '{$val->controller}@{$val->method}');
});

}
}

谢谢。

最佳答案

最好的主意是使用中间件参数
创建 中间件 调用 检查权限 那么您必须将该中间件注册到您的 中app/Http/kernel.php 只有您需要检查以下代码的文件

您的 内核.php 文件

protected $routeMiddleware = [    
'checkPermission' => \App\Http\Middleware\CheckPermission::class,
];

CheckPermission.php
    <?php

namespace App\Http\Middleware;
use Closure;
use DB;

class CheckPermission
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next,$permission_name)
{
//first check that name in your db
$permission = DB::table('Permission')->where('name',$permission_name)->first()
if($permission){
//here you have to get logged in user role
$role_id = Auth::user()->role;
## so now check permission
$check_permission = DB::table('Permission_role')->where('role_id',$role_id)->where('permission_id',$permission->id)->first();
if($check_permission){
return $next($request);
}
//if Permission not assigned for this user role show what you need
}
// if Permission name not in table then do what you need
## Ex1 : return 'Permission not in Database';
## Ex2 : return redirect()->back();

}
}

您的 路线文件
 Route::group(['middleware' => 'jwt.auth'], function() {
Route::post('classResult', 'ApiController@getClassResult')->middleware('checkPermission:view-class-result');
Route::post('studentResult', 'ApiController@studentResult')->middleware('checkPermission:view-student-result');
Route::post('getStudentExamResult', 'ApiController@downloadSchoolTemplate')->middleware('checkPermission:download-student-result');

}

关于php - Laravel : Setting dynamic routes based on access control list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48758552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com