【问题标题】:Laravel Policies - Before / After Update?Laravel 政策 - 更新之前/之后?
【发布时间】:2016-06-25 01:34:24
【问题描述】:

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

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

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

我的问题是,如果user_idPost 上发生更改,则授权方法将允许用户更改帖子,即使他们不拥有它,或者允许他们将用户更改为某人否则,意味着他们无法访问它。

这是否意味着我需要在更新其值之前和之后都调用$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?

【问题讨论】:

  • 您能解释一下如何在您的设计中更改user_id 吗?我的意思是可以通过表单提交来更改吗??
  • 我只是使用UserPost 作为示例,但是是的,想象一下会有一个<select /> 框来更改它所属的关联,新ID 是已提交。

标签: php laravel laravel-5 laravel-authorization


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-05
    • 1970-01-01
    • 2016-10-21
    • 1970-01-01
    • 2014-09-06
    • 1970-01-01
    • 2021-06-05
    • 2021-02-13
    相关资源
    最近更新 更多