【问题标题】:Laravel 4 conditional route filterLaravel 4 条件路由过滤器
【发布时间】:2015-08-30 15:34:27
【问题描述】:

我有一组路由,我希望仅当用户位于某个部门或他们尝试访问的路由中的 id 与他们的登录 id 匹配时才允许用户访问。

我有:

Route::group(array('before' => 'auth.department:6|auth.me'), function () {

    Route::get('users/{id}/outofoffice', ['as' => 'users.outofoffice.form', 'uses' => 'RackspaceController@outOfOfficeForm']);
    Route::post('users/{id}/outofoffice', ['as' => 'users.outofoffice.save', 'uses' => 'RackspaceController@outOfOfficeSave']);

    Route::get('users', ['as' => 'users.list', 'uses' => 'UserController@index']);
    Route::get('users/{id}/edit', ['as' => 'users.edit', 'uses' => 'UserController@edit']);
    Route::post('users/{id}', ['as' => 'users.update', 'uses' => 'UserController@update']);

});

但它不起作用,以前的 '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 谢谢,我制作了另一个过滤器,两者都可以。

标签: php laravel laravel-4 laravel-routing


【解决方案1】:

不是解决方案,但也许这会对某人有所帮助。

同样的事情,这里提到了解决方法How to apply multiple filters on Laravel 4 route group?

我现在也测试了这个,因为我遇到了同样的问题。所以,|符号仅表示 AND,它的工作原理是这个原理,我将它与 Sentry 插件一起使用。

Route::post('/insert', array('as' => 'insertKom', 'uses' => 'KommunikationController@insertKom', 'before' => 'hasAccess:admin|hasAccess:contact.insert'));

例如我的 2 个权限是:

hasAccess:admin: 1
hasAccess:contact.insert: 1

此方案通过,用户可以访问路由。

将权限更改为:

hasAccess:admin: 0
hasAccess:contact.insert: 1

不过,这个解决方案还是以某种方式通过了。用户访问了该路线。不太清楚为什么。

将权限更改为:

hasAccess:admin: 1
hasAccess:contact.insert: 0

而这个没有通过。用户无权访问该路由。有趣的事情,就像它只检查最后一个权限。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-17
    • 1970-01-01
    • 2014-12-19
    相关资源
    最近更新 更多