【问题标题】:Laravel 5.5 Multiple user type nested route groupLaravel 5.5 多用户类型嵌套路由组
【发布时间】:2018-02-12 00:36:01
【问题描述】:

我有两种用户类型:操作和维护。 Operations 用户类型的所有路由都可以通过 Maintenance 用户类型访问,但并非 Maintenance 拥有的所有路由都无法通过 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,
];

中间件/操作.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 中间件组内

【问题讨论】:

  • 向我们展示您的app/Http/Kernel.php 代码以及您的operations 中间件代码
  • @NikolaGavric 代码已被编辑。

标签: php laravel laravel-5.5


【解决方案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('/');
}

【讨论】:

    【解决方案2】:

    我认为您在 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 添加其他参数,然后将其转发,如下所示:

    中间件/操作.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
        }
    }
    

    【讨论】:

    • 我不认为我的查询有问题,因为如果我将操作组与维护分开,它可以顺利运行。只有当我将组放入另一个组时它才会停止工作。
    • 嗯,那么您可以尝试将这些中间件实际用作group 中间件而不是独立的中间件,检查编辑
    • 相同的输出?它是否会引发任何错误,您能否解释一下行为,究竟发生了什么?
    • 无法访问操作中间件组上的路由。但我可以访问维护中间件上的所有路由
    • 无法访问的真正含义是什么?服务器给你 401、403 或者你得到一个错误,或者你得到一个空白页面,或者它把你重定向到某个地方?
    猜你喜欢
    • 2016-06-20
    • 2018-08-05
    • 2020-09-04
    • 2018-06-23
    • 2019-04-02
    • 1970-01-01
    • 2018-02-26
    • 2018-03-01
    • 1970-01-01
    相关资源
    最近更新 更多