Laravel 5.5多用户类型嵌套路由组

时间:2018-02-12 00:36:01

标签: php laravel laravel-5.5

我有两种用户类型:操作和维护。 维护用户类型可以访问Operations用户类型的所有路由,但操作无法访问维护所有的所有路由。

这是我现有的代码。

Route::group(['middleware'=>'maintenance'], function(){
    //routes here

    Route::group(['middleware'=>'operations'], function(){
         //routes here

    });
});

kernel.php

 protected $routeMiddleware = [
    'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'revalidate' => \App\Http\Middleware\RevalidateBackHistory::class,
    'dev' => \App\Http\Middleware\isDev::class,
    'operations' => \App\Http\Middleware\operations::class,
    'maintenance' => \App\Http\Middleware\maintenance::class,
];

中间件/ operations.php

public function handle($request, Closure $next)
{
    $a = UserAccess::where(['employee_id'=>\Auth::user()->id, 'user_type_id'=>1])->first();
    if($a){
        return $next($request);
    }

    return redirect('/');
}

但它不起作用。维护可以访问其所有路由,但无法访问操作,操作根本无法访问任何路径。

注意:两个组都在auth中间件组

2 个答案:

答案 0 :(得分:1)

为此找到了解决方法。我创建了另一个中间件,它通过将两个参数传递给该中间件来满足操作和维护,特别是1 =操作和2 =维护,并检查了中间件访问。这是代码:

Route::group(['middleware'=>'access:2,2'], function(){
    //Routes that can be accessed by Maintenance user type
});

Route::group(['middleware'=>['access:1,2']], function(){
    //Routes that can be accesed by Operations and Maintenance user type.
});

这是中间件:

public function handle($request, Closure $next, $ops, $main)
{
    $a = UserAccess::where('employee_id',\Auth::user()->id)->whereIn('user_type_id', [$ops, $main])->first();
    if($a){
        return $next($request);
    }

    return redirect('/');
}

修改

优化代码以消除参数冗余。

Web.php:

Route::group(['middleware'=>'access:2'], function(){
    //Routes that can be accessed by Maintenance user type
});

Route::group(['middleware'=>['access:1,2']], function(){
    //Routes that can be accesed by Operations and Maintenance user type.
});

access.php

public function handle($request, Closure $next, $ops, $main = 0)
{
    $a = UserAccess::where('employee_id',\Auth::user()->id)->whereIn('user_type_id', [$ops, $main])->first();
    if($a){
        return $next($request);
    }

    return redirect('/');
}

答案 1 :(得分:0)

我认为您错过了where查询中的其他数组,请尝试更改operations中间件中的代码,如下所示:

$a = UserAccess::where([
         ['employee_id', \Auth::user()->id],
         ['user_type_id', 1]
     ])->first();

修改

然后尝试将middlewares移动到$middlewareGroups web中间件之下,如下所示:

'web' => [
    ...
],
'operations' => \App\Http\Middleware\operations::class,
'maintenance' => \App\Http\Middleware\maintenance::class,

编辑2:

$a变量的代码更改为此

$a = UserAccess::where([
         ['employee_id', Auth::user()->id], //Hence there is no \ before Auth
         ['user_type_id', 1]
     ])->first();

然后在该文件的顶部包含以下use

use Illuminate\Support\Facades\Auth;

编辑3:

您可以向request添加其他参数,然后向前发送,如下所示:

<强>中间件/ operations.php

function handle($request, Closure $next) {
    ...
    $request->attributes->add(['passedOperations' => true]);
    return next($next);
}

<强>中间件/ maintenance.php

function handle($request, Closure $next) {
    ...
    if($request->get('passedOperations')) {
        return next($next);
    } else {
        //didn't pass operations
    }
}