Laravel 仅检索属于经过身份验证的用户的模型记录
Laravel only retrieve Model Records if they belong to authenticated user
我正在使用 Laravel 5.4 并且有一个名为 Order
的模型。
为了测试我创建了两个用户和两个订单,每个用户有一个订单。
我刚刚看到我能够检索不是我当前用户的人的订单。我正在使用 Auth::user()->orders
检索用户自己的订单列表。但为了显示特定订单的详细信息,我这样做:
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$order = CustomerOrder::findOrFail($id)->with('products')->get();
return view('order.show')
->with('order', $order);
}
我在这里错过了什么?是否有中间件或其他东西告诉应用程序只允许访问与经过身份验证的用户关联的订单?
编辑:所以我尝试使用策略 OrderPolicy
(CRUD) 来做到这一点。
政策的view()
功能:
/**
* Determine whether the user can view the customerOrder.
*
* @param \App\User $user
* @param \App\CustomerOrder $customerOrder
* @return mixed
*/
public function view(User $user, CustomerOrder $customerOrder)
{
return $user->id === $customerOrder->user_id;
}
我已经在 AuthServiceProvider.php
中注册了它:
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
Adress::class => AdressPolicy::class, //Doesn't work either
Order::class => OrderPolicy::class
];
我仍然可以查看其他用户的订单。
如果您想获取属于已验证用户的订单,请执行以下操作:
CustomerOrder::where('user_id', auth()->user()->id)->with('products')->find($id);
你有几个选择。我选择的最佳选择是使用策略。可在此处找到相关文档:
https://laravel.com/docs/5.4/authorization
或者可以做类似的事情:
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$user = request()->user();
$order = $user->orders()->with('products')->find($id)->get();
return view('order.show', compact('order'));
}
在您的用户模型上使用订单关系函数。
更新回复
根据您提供的策略和资源路由,您应该能够做到:
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show(CustomerOrder $order)
{
$this->authorize('view', $order);
return view('order.show', compact('order'));
}
另一种方法是使用定义的关系并告诉它只检索 ID 为 $id
的关系。像这样:
$customerOrder = auth()->user()->orders()->with('products')->find($id);
记住,
首先你制定政策。
其次你注册它。
第三,你在你的普通控制器中使用类似 $this->authorize('view', $order);
的东西。
你错过了第三步,你可以在这里找到文档:
[https://laravel.com/docs/5.8/authorization#authorizing-actions-using-policies][1]
我正在使用 Laravel 5.4 并且有一个名为 Order
的模型。
为了测试我创建了两个用户和两个订单,每个用户有一个订单。
我刚刚看到我能够检索不是我当前用户的人的订单。我正在使用 Auth::user()->orders
检索用户自己的订单列表。但为了显示特定订单的详细信息,我这样做:
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$order = CustomerOrder::findOrFail($id)->with('products')->get();
return view('order.show')
->with('order', $order);
}
我在这里错过了什么?是否有中间件或其他东西告诉应用程序只允许访问与经过身份验证的用户关联的订单?
编辑:所以我尝试使用策略 OrderPolicy
(CRUD) 来做到这一点。
政策的view()
功能:
/**
* Determine whether the user can view the customerOrder.
*
* @param \App\User $user
* @param \App\CustomerOrder $customerOrder
* @return mixed
*/
public function view(User $user, CustomerOrder $customerOrder)
{
return $user->id === $customerOrder->user_id;
}
我已经在 AuthServiceProvider.php
中注册了它:
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
Adress::class => AdressPolicy::class, //Doesn't work either
Order::class => OrderPolicy::class
];
我仍然可以查看其他用户的订单。
如果您想获取属于已验证用户的订单,请执行以下操作:
CustomerOrder::where('user_id', auth()->user()->id)->with('products')->find($id);
你有几个选择。我选择的最佳选择是使用策略。可在此处找到相关文档:
https://laravel.com/docs/5.4/authorization
或者可以做类似的事情:
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$user = request()->user();
$order = $user->orders()->with('products')->find($id)->get();
return view('order.show', compact('order'));
}
在您的用户模型上使用订单关系函数。
更新回复
根据您提供的策略和资源路由,您应该能够做到:
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show(CustomerOrder $order)
{
$this->authorize('view', $order);
return view('order.show', compact('order'));
}
另一种方法是使用定义的关系并告诉它只检索 ID 为 $id
的关系。像这样:
$customerOrder = auth()->user()->orders()->with('products')->find($id);
记住,
首先你制定政策。
其次你注册它。
第三,你在你的普通控制器中使用类似 $this->authorize('view', $order);
的东西。
你错过了第三步,你可以在这里找到文档:
[https://laravel.com/docs/5.8/authorization#authorizing-actions-using-policies][1]