0

我是Laravel新手(非常新手),使用Laravel 5.2上的Cartalyst Sentinel来利用角色授权。Laravel和Sentinel:混合角色中间件授权

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

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

  • 仪表板(所有角色访问:管理,代理,作家)
  • 用户(访问:管理员)
  • 订单(访问:管理员,代理)
  • 页面(可访问:管理员,作家)
  • no_admin_here(访问:代理,作家)

目前我管理它只有两个角色,但现在我卡住了。

什么我迄今所做的(我只放了必要的代码):

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命名为 '检查')

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命名为 '管理员')

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命名为“代理人”)

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'); 
    }); 

}); 

因为“代理”永远达不到,因为“admin”的中间件将阻止和注销了他的部分。而且,同样的,我不能做所有的其他角色组合:

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

等。

那么,有没有一个(简单的)方式,我可以轻松地将角色访问板块?在此先感谢您的帮助

回答