Laravel 策略 - 如何将多个参数传递给函数
Laravel Policies - How to Pass Multiple Arguments to function
我正在尝试将用户角色授权给 delete/update post。我正在使用策略来这样做,但我只能将一个参数传递给策略函数。如果我传递的不仅仅是用户和另一个变量,则该变量不会传递到函数中。
模型:用户有很多角色,一个角色可以post多个post。因此出于授权目的,我必须将 post 的 character_id 与当前角色的 ID 进行比较...-
根据 docs,您可以将更多倍数传递给 Gate Facade:
Gate::define('delete-comment', function ($user, $post, $comment) {
//
});
但无论如何我都找不到政策。我要做的就是注入 Request 对象以获得授权所需的对象。基本上我什至不需要用户对象。
public function update(User $user, Post $post)
{
return $user->id === $post->user_id;
}
使用 Request 对象是可行的,但感觉非常 hacky。有没有更好的方法来实现这个?
编辑:
在 CharacterLocationController
中,我有一个方法 show
,我想在显示资源之前授权该操作。
public function show(Request $request, Character $character, Location $location)
{
$this->authorize([$location, $character]);
...
}
政策是这样注册的:'App\Location' => 'App\Policies\LocationPolicy'
在 AuthServiceProvider
我转储传递给策略函数的数组,它只输出 $location
.
public function show(User $user, $data) {
dd($data); // expecting location and character
return !$location->private || $location->authorized->contains($this->character);
}
我认为这里可能对哪些功能在做什么有些混淆。
当你使用
Gate::define('delete-comment', function ($user, $post, $comment) {
//
});
或在CommentPolicy
public function delete(User $user, Post $post, Comment $comment)
{
return $user->id === $post->user_id;
}
您所做的只是定义规则。在这一点上,我们不担心传递任何东西,只担心我们收到的对象能够或应该能够相互交互。这两者之间的唯一区别是在使用策略时,它只是一种将所有规则抽象为一个简单易读的简单方法 class。如果您的应用程序可能包含数百个表格和模型,那么如果您将这些规则散布在您的应用程序各处,它会很快变得混乱,因此政策将有助于使它们井井有条。
当你实际检查某人是否有权做某事时,你应该传递这些项目。例如,当您执行以下操作时,
if (Gate::allows('delete-comment', [$post, $comment])) {
//
}
或者如果在 CommentController
$this->authorize('delete', [$post, $comment]);
这就是控制将哪些参数传递给策略或 Gate::define
方法的内容。根据文档,已经为您添加了 $user
参数,因此在这种情况下,您只需要担心传递正确的 $post
和 $comment
被修改。
我正在尝试将用户角色授权给 delete/update post。我正在使用策略来这样做,但我只能将一个参数传递给策略函数。如果我传递的不仅仅是用户和另一个变量,则该变量不会传递到函数中。
模型:用户有很多角色,一个角色可以post多个post。因此出于授权目的,我必须将 post 的 character_id 与当前角色的 ID 进行比较...-
根据 docs,您可以将更多倍数传递给 Gate Facade:
Gate::define('delete-comment', function ($user, $post, $comment) {
//
});
但无论如何我都找不到政策。我要做的就是注入 Request 对象以获得授权所需的对象。基本上我什至不需要用户对象。
public function update(User $user, Post $post)
{
return $user->id === $post->user_id;
}
使用 Request 对象是可行的,但感觉非常 hacky。有没有更好的方法来实现这个?
编辑:
在 CharacterLocationController
中,我有一个方法 show
,我想在显示资源之前授权该操作。
public function show(Request $request, Character $character, Location $location)
{
$this->authorize([$location, $character]);
...
}
政策是这样注册的:'App\Location' => 'App\Policies\LocationPolicy'
在 AuthServiceProvider
我转储传递给策略函数的数组,它只输出 $location
.
public function show(User $user, $data) {
dd($data); // expecting location and character
return !$location->private || $location->authorized->contains($this->character);
}
我认为这里可能对哪些功能在做什么有些混淆。
当你使用
Gate::define('delete-comment', function ($user, $post, $comment) {
//
});
或在CommentPolicy
public function delete(User $user, Post $post, Comment $comment)
{
return $user->id === $post->user_id;
}
您所做的只是定义规则。在这一点上,我们不担心传递任何东西,只担心我们收到的对象能够或应该能够相互交互。这两者之间的唯一区别是在使用策略时,它只是一种将所有规则抽象为一个简单易读的简单方法 class。如果您的应用程序可能包含数百个表格和模型,那么如果您将这些规则散布在您的应用程序各处,它会很快变得混乱,因此政策将有助于使它们井井有条。
当你实际检查某人是否有权做某事时,你应该传递这些项目。例如,当您执行以下操作时,
if (Gate::allows('delete-comment', [$post, $comment])) {
//
}
或者如果在 CommentController
$this->authorize('delete', [$post, $comment]);
这就是控制将哪些参数传递给策略或 Gate::define
方法的内容。根据文档,已经为您添加了 $user
参数,因此在这种情况下,您只需要担心传递正确的 $post
和 $comment
被修改。