欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

两个或中间件或 一个通过验证 另一个就不要验证

我试图创建自定义授权laravel 5.2和认证。

所以在我的路线文件,首先检查是否被认证的用户或不使用认证中间件

Route::group(['middleware' => ['auth']], function () {

});

在有三个用户角色

1.visitor

2.manager

3.admin

与经理和管理员我有两中间件。

managermiddleware(经理)和aminmiddleware(管理员)

对于一个特定的功能,我想可以访问这两admin经理

为了这个目的,我使用这个代码

Route::group(['middleware' => ['auth']], function () {
    Route::group(['middleware' => ['admin','manager']], function () {
        Route::get('test','TestController@index')
    });
});

当然它不工作。二中间件检查admin经理不能因为一个用户只属于一个特定的角色。

所以我想用进入['middleware' => ['admin','manager']]

有没有办法在Laravel办吗?

分享编辑
 

为什么不叫管理中间件:

Route::group(['middleware' => ['auth']], function () {
  Route::group(['middleware' => ['admin']], function () {
    Route::get('test','TestController@index')
  });
});

如果用户如果注册为一个管理进程的下一个请求,如果没有的话,打电话给经理中间件:

class AdminMiddleware
{
    public function handle($request, Closure $next)
    {
        if (isAdmin())
            return $next($request);
        //If not admin call to ManagerMiddleware
        return app(ManagerMiddleware::class)->handle($request, function ($request) use ($next) {
            if (isManager())
                return $next($request);
            //if neither admin nor manager reject request
            return redirect()->home();
        });
    }
}

或检查它周围的其他方式,首先经理然后行政这样你仍然可以使用独立的管理的权限。我不知道,求解方便,我会那样做为止。

分享编辑
 

来自 https://stackoverflow.com/questions/35548315/laravel-route-group-using-or-condition-for-middleware

Laravel Route group using or condition for middleware

 

I am trying to create custom authorization and authentication in Laravel 5.2.

So in my routes file, first i check if user is authenticated or not, usingauthmiddleware.

Route::group(['middleware' => ['auth']], function () {

});

I have 3 users role.

1.visitor

2.manager

3.admin

and for manager and admin i have two middleware.

ManagerMiddleware(manager) andAminMiddleware(admin)

For a specific function i want to give access to bothadminandmanager.

For this purpose , i used this code

Route::group(['middleware' => ['auth']], function () {
    Route::group(['middleware' => ['admin','manager']], function () {
        Route::get('test','TestController@index')
    });
});

Of course it does not work. Second middleware check bothadminandmanagerand it fails because an user only belongs to a specific role.

So i want to useorin['middleware' => ['admin','manager']]

Is there any way to do it in Laravel?

shareedit
 

Why not just call the admin middleware:

Route::group(['middleware' => ['auth']], function () {
  Route::group(['middleware' => ['admin']], function () {
    Route::get('test','TestController@index')
  });
});

If the user if registered as an Admin process to next request, if not, call themanager middleware :

class AdminMiddleware
{
    public function handle($request, Closure $next)
    {
        if (isAdmin())
            return $next($request);
        //If not admin call to ManagerMiddleware
        return app(ManagerMiddleware::class)->handle($request, function ($request) use ($next) {
            if (isManager())
                return $next($request);
            //if neither admin nor manager reject request
            return redirect()->home();
        });
    }
}

Or checking it the other way around, first manager then admin so that you can still use the admin priviledge independently. I don't know if that solution is convenient, that's what I would do so far.

shareedit
 

来自 https://stackoverflow.com/questions/35548315/laravel-route-group-using-or-condition-for-middleware


普通分类: