Laravel 和 Sentinel:混合角色中间件进行授权

Laravel and Sentinel: mix roles middleware for authorization

我是 Laravel 新手(非常新手),在 Laravel 5.2 上使用 Cartalyst Sentinel 来利用角色授权。

在管理部分,我有三个(或更多)角色,即 "admin"、"agent" 和 "writer"。

我还有一些部分应该具有混合角色访问权限,即像这样:

目前我设法让它只使用两个角色,但现在我卡住了。

到目前为止我做了什么(我只放了必要的代码):

routes.php

// only authenticated users can access these pages
Route::group(['prefix' => 'admin', 'as' => 'admin.', 'middleware' => ['check']], function(){

    // these pages are accessible to all roles
    Route::get('dashboard', ['as' => 'dashboard', function(){
        return view('admin/dashboard');
    }]);

    // only admin can access this section
    Route::group(['middleware' => 'admin'], function(){

        Route::get('users', function(){
            return view('admin/users');
        });

    });

});

SentinelCheck 中间件(在 Kernel.php 中命名为 'check')

if (!Sentinel::check()) { // user is not authenticated
    return redirect()->route('admin.login')->with('error', 'You must be logged to view the page');
}
if (Sentinel::inRole('customer')) { // user is authenticated but he is a customer
    return redirect()->route('admin.login')->with('error', 'You are a customer and cannot access to backend section');
}

SentinelAdmin 中间件(在 Kernel.php 中命名为 'admin')

if (!Sentinel::inRole('admin')) { // user is authenticated but he is not an admin
    return redirect()->route('admin.login')->with('error', 'You are not admin and cannot view requested section');
}

SentinelAgent 中间件(在 Kernel.php 中命名为 'agent')

if (!Sentinel::inRole('agent')) { // user is authenticated but he is not an agent
    return redirect()->route('admin.login')->with('error', 'You are not agent and cannot view requested section');
}

正如我所说,到目前为止还不错,但是当我尝试混合角色时事情就搞砸了;即我不能写这样的路线:

// only admin and agent can access this section
Route::group(['middleware' => ['admin', 'agent']], function(){

    Route::get('orders', function(){
        return view('admin/orders');
    });

});

因为 "agent" 永远不会到达该部分,因为 "admin" 中间件将阻止并注销他。而且,同样,我不能兼任所有其他角色:

['middleware' => ['admin', 'writer']]
['middleware' => ['agent', 'writer']]
['middleware' => ['admin', 'writer', 'whatever_else_role']]

等等

那么,有没有一种(简单的)方法可以让我轻松地混合角色对部分的访问?预先感谢您的帮助

使用 middleware parameters

比我预期的要容易