3
我有一組路由,我想讓用戶只有在某個部門或他們試圖訪問的路由中的ID與他們登錄的ID相匹配時才能訪問。Laravel 4條件路由過濾器
我:
Route::group(array('before' => 'auth.department:6|auth.me'), function() {
Route::get('users/{id}/outofoffice', ['as' => 'users.outofoffice.form', 'uses' => '[email protected]']);
Route::post('users/{id}/outofoffice', ['as' => 'users.outofoffice.save', 'uses' => '[email protected]']);
Route::get('users', ['as' => 'users.list', 'uses' => '[email protected]']);
Route::get('users/{id}/edit', ['as' => 'users.edit', 'uses' => '[email protected]']);
Route::post('users/{id}', ['as' => 'users.update', 'uses' => '[email protected]']);
});
但它不工作,以前 'auth.department:6' 按預期工作,但是當我將其更改爲 'auth.department:6 | auth.me',用戶仍然被拒絕訪問。該過濾器被定義爲:
Route::filter('auth.department', function($route, $request)
{
if(Auth::level() > 5) return null;
$departmentIds = array_slice(func_get_args(), 2);
if(!in_array(Auth::dept(), $departmentIds)) {
if (Request::ajax())
{
return Response::make('Unauthorized', 401);
}
else
{
return Response::make('Unauthorized', 401);
}
}
});
Route::filter('auth.me', function(\Illuminate\Routing\Route $route, $request){
if($route->getParameter('id') == Auth::id()) {
return null;
} else {
return BaseController::failed(['authorization' => ['Unauthorized']], 401);
}
});
我這樣做:
Route::filter('auth.dept-6-or-me', function(\Illuminate\Routing\Route $route, $request){
if(Auth::level() > 5) return null;
$departmentIds = array_slice(func_get_args(), 2);
if($route->getParameter('id') == Auth::id()) {
return null;
}
elseif(!in_array(Auth::dept(), $departmentIds)) {
if (Request::ajax())
{
return Response::make('Unauthorized', 401);
}
else
{
return Response::make('Unauthorized', 401);
}
} else {
if (Request::ajax())
{
return Response::make('Unauthorized', 401);
}
else
{
return Response::make('Unauthorized', 401);
}
}
});
我很確定filter1 | filter2中的'|'是指「和」,而不是「或」。除非有人構建了另一種解決方案,否則您可能必須創建第三個過濾器,並且只需調用其他兩個過濾器。 –
我很確定喬爾有權利 –
@JoelHinz謝謝,我做了另一個過濾器,做這兩件事。 – imperium2335