0

我对在 Laravel 中使用 Policy 类的最佳方式有点困惑。

假设我有 aUser和 a Post,我在更新帖子时有一个策略方法来检查 a 是否User拥有该帖子。

我应该在从数据库加载Post对象后立即将其传递给方法吗?authorize或者一旦我更新了可填充值?

我的问题是,如果在user_id上进行更改Post,则 authorize 方法将允许用户更改帖子,即使他们不拥有它,或者它将允许他们将用户更改为其他人,这意味着他们无法访问它。

这是否意味着我需要$this->authorize('update', $post)在更新其值之前和之后都调用?

$post = Post::findOrFail($id); 

$this->authorize('update', $post); // Should I call it here

$post->fill($request->input());

$this->authorize('update', $post); // Or here? Or, both places?

或者,我是否应该使用请求验证来确保用户无法输入他们无法访问的实体的 ID?

4

1 回答 1

1

您需要在更新值之前调用 $this->authorize('update', $post)

$post = Post::findOrFail($id); 

$this->authorize('update', $post); // throw a HttpException if the action is not authorized

$post->fill($request->input());

来源:https ://laravel.com/docs/5.2/authorization#controller-authorization

于 2016-05-31T15:21:21.057 回答