问题描述
我正在尝试为两个不同的用户角色 Admin、Normal_User 设置访问操作的权限,如下所示.
I am trying to set permission to access an action to two different user roles Admin, Normal_User as shown below.
Route::group(['middleware' => ['role_check:Normal_User','role_check:Admin']], function() {
Route::get('/user/{user_id}', array('uses' => 'UserController@showUserDashboard', 'as' => 'showUserDashboard'));
});
该路由可由管理员或普通用户访问.但是在这个中间件配置中,用户需要同时是 Admin 和 Normal_User.如何在中间件参数传递中添加 OR 条件?或者有什么其他的方式给权限?
This route can be accessed by either Admin or Normal_user. But in this middleware configuration, user is required to be both Admin and Normal_User. How can I add OR condition in middleware parameter passing? Or is there any other method to give permission?
下面是我的中间件
public function handle($request, Closure $next, $role)
{
if ($role != Auth::user()->user_role->role ) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return response('Unauthorized.', 401);
}
}
return $next($request);
}
有人可以回复吗?
推荐答案
添加多个参数需要用逗号隔开:
To add multiple parameters, you need to seperate them with a comma:
Route::group(['middleware' => ['role_check:Normal_User,Admin']], function() {
Route::get('/user/{user_id}', array('uses' => 'UserController@showUserDashboard', 'as' => 'showUserDashboard'));
});
然后您可以像这样在中间件中访问它们:
Then you have access them to in your middleware like so:
public function handle($request, Closure $next, $role1, $role2) {..}
那里的逻辑由您来实现,没有自动说或"的方法.
The logic from there is up to you to implement, there is no automatic way to say "OR".
这篇关于如何在 Laravel 5.2 中使用 OR 条件将多个参数传递给中间件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!