且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Laravel阻止用户编辑/查看其他用户的资源

更新时间:2023-12-02 13:03:40

默认情况下不存在此类过滤器,但是您可以轻松创建一个过滤器(取决于数据库的设置方式).在app/filters.php中,您可以执行以下操作:

No such filter exists by default, however you can easily create one (depending on how your database is set up). Within app/filters.php, you may do something like this:

Route::filter('restrictPermission', function($route)
{
    $payment_id = $route->parameter('payment');

    if (!Auth::user()->payments()->find($payment_id)) return Redirect::to('/');
});

这会将当前登录用户的pay_id(在您的数据库中)与传递到路由中的{payment}参数进行比较.显然,根据数据库的设置方式(例如,如果payment_id在单独的表中),您需要更改条件.

This compares the currently logged in user's payment_id (in your database) to the {payment} argument passed into the route. Obviously, depending on how your database is set up (for instance if the payment_id is in a separate table) you need to change the conditional.

然后,将过滤器应用于您的路线:

Then, apply the filter to your route:

Route::get('/payment/edit/{payment}', array('before' => 'restrictPermission'));