如何在 Laravel 中阻止相同类型的用户访问其他人的数据?

How to block same type of users accessing others data in Laravel?

我的项目有两种类型的用户,管理员和客户。我需要通过 URL 传递 id 来阻止客户访问其他客户的详细信息,如下所述。在这个客户中有两类,高级客户和来宾客户。这里我也需要以上功能。我正在使用过滤器将不同的仪表板路由到管理员和客户。我的 filter.php 文件如下。

Route::filter('auth', function()
{
    if (Auth::guest())
    {
        if (Request::ajax())
        {
            return Response::make('Unauthorized', 401);
        }
        else
        {
            return Redirect::route('home');
        }
    }
});

Route::filter('admin', function()
{
    if (Auth::user()->isAdmin != 0) return Redirect::route('getLogout');
});
Route::filter('client', function()
{
    if (Auth::user()->isAdmin != 1) return Redirect::route('getLogout');
});

Thsi 工作正常。

但是如果客户试图通过 URL 访问其他客户的详细信息,他会得到它。例如,在我的项目中有一个 URL 可以访问客户资料。这是通过 url 传递客户端 ID 来完成的,就像这样 "localhost/public/profile/{id}"。 route.php 文件的一部分如下。

Route::group(array('before' => 'auth'), function()
{
     Route::group(array('before' => 'auth|client'), function() 
     {
        Route::get('profile/{id}', array('uses' => 'ClientController@viewProfile', 'as' =>'viewProfile'));
     });
});

如何通过传递 ID、使用过滤器或其他方法阻止客户访问其他客户的个人资料?我尝试在控制器中指定,但看起来不太好。

在我的客户中,也有两种类型的客户。高级和来宾客户。这些类型在名为 Clients 的模型中为 table 命名的客户端指定。在 clients table 中有一个名为 type 的字段。我需要阻止来宾客户端访问某些高级客户端可以访问的 URL。

有人能帮忙吗??

基本思路是创建一个 custom filter 来获取当前用户的 ID,将其与请求中的用户 ID 进行比较,然后采取所需的操作,这可能是重定向到其他地方。将它包裹在您希望它影响的任何路线上。

过滤器看起来像这样:

Route::filter('profile_access', function($route, $request, $value)
{
    $requestedId = $route->getParameter('id');
    $userId = MyUserService::getCurrentUser()->getId();

    // compare and redirect...
});