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



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

在管理部分,我有三个(或更多)角色,即"管理员"、"代理"和"作家"。

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

  • 仪表板(所有角色均可访问:管理员、代理、编写者)
  • 用户(可访问:管理员)
  • 订单(可访问:管理员、代理)
  • 页面(可访问:管理员、作家)
  • no_admin_here(可访问:代理、编写器)
目前,我

只与两个角色一起工作,但现在我陷入了困境。

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

路线.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 中间件(在内核中名为"check.php)

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 Middleware(在内核中名为"admin".php)

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 Middleware(在内核.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');
    });
});
因为"

代理"永远不会到达该部分,因为"管理员"中间件会阻止并注销他。而且,同样,我不能把其他角色都混在一起:

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

等。。

那么,有没有一种(简单的)方法可以轻松地将角色访问混合到部分?提前感谢您的帮助

我期望使用中间件参数更容易

最新更新