如何使用'OR'中间件进行路由laravel 5

Gov*_*row 10 php laravel laravel-5 laravel-5.2

我有两种类型的用户,我已经创建了多个中间件.

某些路由需要允许这两种类型的用户.

我正在尝试以下代码:

Route::group(['namespace' => 'Common', 'middleware' => ['Auth1', 'Auth2']], function() {
    Route::get('viewdetail', array('as' => 'viewdetail', 'uses' => 'DashboardController@viewdetail'));
}); 
Run Code Online (Sandbox Code Playgroud)

但它没有工作:(

jed*_*ylo 13

中间件应该返回响应或将请求传递给管道.中间件彼此独立,不应该知道其他中间件运行.

您需要实现一个单独的中间件,它允许2个角色或单个中间件将允许的角色作为参数.

选项1:只创建一个中间件是Auth1和Auth2的组合版本,它检查2种用户类型.这是最简单的选择,虽然不是很灵活.

选项2:因为版本5.1中间件可以采用参数 - 请在此处查看更多详细信息:https://laravel.com/docs/5.1/middleware#middleware-parameters.您可以实现一个中间件,该中间件将检查用户角色列表,并在路径文件中定义允许的角色.以下代码应该可以解决问题:

// define allowed roles in your routes.php
Route::group(['namespace' => 'Common', 'middleware' => 'checkUserRoles:role1,role2', function() {
  //routes that should be allowed for users with role1 OR role2 go here
}); 

// PHP < 5.6
// create a parametrized middleware that takes allowed roles as parameters
public function handle($request, Closure $next) {

  // will contain ['role1', 'role2']
  $allowedRoles = array_slice(func_get_args(), 2);

  // do whatever role check logic you need
}

// PHP >= 5.6
// create a parametrized middleware that takes allowed roles as parameters
public function handle($request, Closure $next, ...$roles) {

  // $roles will contain ['role1', 'role2']

  // do whatever role check logic you need
}
Run Code Online (Sandbox Code Playgroud)


小智 5

本例 Laravel 5.2 如何将多个参数传递给带有 OR 条件的中间件

无需向您的 handle 方法添加多个参数并且每次向应用程序添加新角色时都必须更新它,您可以使其动态化。

中间件

 /**
 * Handle an incoming request.
 *
 * @param $request
 * @param Closure $next
 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
 */
public function handle($request, Closure $next) {

    $roles = array_slice(func_get_args(), 2); // [default, admin, manager]

    foreach ($roles as $role) {

        try {

            Role::whereName($role)->firstOrFail(); // make sure we got a "real" role

            if (Auth::user()->hasRole($role)) {
                return $next($request);
            }

        } catch (ModelNotFoundException $exception) {

            dd('Could not find role ' . $role);

        }
    }

    Flash::warning('Access Denied', 'You are not authorized to view that content.'); // custom flash class

    return redirect('/');
}
Run Code Online (Sandbox Code Playgroud)

路线

Route::group(['middleware' => ['role_check:default,admin,manager']], function() {
    Route::get('/user/{user_id}', array('uses' => 'UserController@showUserDashboard', 'as' => 'showUserDashboard'));
});
Run Code Online (Sandbox Code Playgroud)

这将检查经过身份验证的用户是否至少具有提供的一个角色,如果是,则将请求传递给下一个中间件堆栈。当然,hasRole()方法和角色本身需要您自己实现。

您可以使用 php 5.6

public function handle($request, Closure $next, ...$roles)
{
    foreach ($roles as $role) {

        try {
            if ($request->user()->can($role)) {
              return $next($request);
        }

        } catch (ModelNotFoundException $exception) {
          abort(403);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)