Php Model::find($id)在Laravel中是如何安全的?
我有一个应用程序,根据登录者创建条目。如果我使用Php Model::find($id)在Laravel中是如何安全的?,php,laravel,Php,Laravel,我有一个应用程序,根据登录者创建条目。如果我使用find($id)方法,它将返回json响应。功能如下: public function edit($id) { $chore = Chore::find($id); return response()->json($chore); } public function authorize() { return $this->route('chore')->u
find($id)
方法,它将返回json
响应。功能如下:
public function edit($id)
{
$chore = Chore::find($id);
return response()->json($chore);
}
public function authorize() {
return $this->route('chore')->user_id === $this->user()->id
}
现在,如果我在哪里编辑id值,我可能可以访问其他用户的数据,而这些数据根本不安全。
因此,我添加了一个额外的列user\u id
,用于检查谁已登录:
public function edit($id)
{
$chore = Chore::find($id)
->where('user_id', Auth::id());
return response()->json($chore);
}
但是拉威尔当然不能让它变得简单,所以它不起作用。添加->get()
将返回一个数组而不是json响应。
首先,在任何使用身份验证的应用程序中,find($id)
是如何安全的?其次,我如何在find($id)
子句下添加另一个条件?我需要在JSON
中返回数据,否则我需要重写我的所有前端,这在此时并不理想
我还尝试:
public function edit($id)
{
$chore = Chore::where('id', $id)
->where('user_id', Auth::id());
return response()->json($chore);
}
但是运气不好您只需要在
查找
方法之前调用where
方法
$chore = Chore::where('user_id', Auth::id())
->find($id);
或者,如果您已经在用户模型上正确设置了关系
// In User.php
public function chores()
{
return $this->hasMany(Chore::class, 'user_id', 'id');
}
那你也可以
$chore = Auth::user()->chores()->find($id);
虽然这似乎是额外的工作,但它更方便、更易于维护。您只需在
查找方法之前调用where
方法
$chore = Chore::where('user_id', Auth::id())
->find($id);
或者,如果您已经在用户模型上正确设置了关系
// In User.php
public function chores()
{
return $this->hasMany(Chore::class, 'user_id', 'id');
}
那你也可以
$chore = Auth::user()->chores()->find($id);
虽然这看起来像是额外的工作,但它更方便、更易于维护。如果Chore与您的用户模型存在一对一的关系,那么您可以在User.php
模型中创建关系
public function chore() {
return $this->hasOne(Chore::class);
}
然后在控制器中,只需调用auth()->user()->chore
雄辩的find()
方法只能通过主键查找一条记录。如果你使用额外的验证,它是完全安全的。您可以使用路由模型绑定来简化代码
web.php
Route::get('/chore/{chore}/', 'ChoreController@edit');
然后在你的控制器里
public function edit(Chore $chore)
{
if (! $chore->user_id === auth()->id()) {
// throw error or redirect, or whetever
}
return response()->json($chore);
}
为了进一步简化控制器,您可以创建一个表单请求,并将其作为常规请求注入控制器的方法中。()
然后可以将验证移到表单请求中。它应该是这样的:
public function edit($id)
{
$chore = Chore::find($id);
return response()->json($chore);
}
public function authorize() {
return $this->route('chore')->user_id === $this->user()->id
}
如果Chore与您的用户模型是一对一的关系,那么您可以在User.php
模型中创建一个关系
public function chore() {
return $this->hasOne(Chore::class);
}
然后在控制器中,只需调用auth()->user()->chore
雄辩的find()
方法只能通过主键查找一条记录。如果你使用额外的验证,它是完全安全的。您可以使用路由模型绑定来简化代码
web.php
Route::get('/chore/{chore}/', 'ChoreController@edit');
然后在你的控制器里
public function edit(Chore $chore)
{
if (! $chore->user_id === auth()->id()) {
// throw error or redirect, or whetever
}
return response()->json($chore);
}
为了进一步简化控制器,您可以创建一个表单请求,并将其作为常规请求注入控制器的方法中。()
然后可以将验证移到表单请求中。它应该是这样的:
public function edit($id)
{
$chore = Chore::find($id);
return response()->json($chore);
}
public function authorize() {
return $this->route('chore')->user_id === $this->user()->id
}
是的,这很有效!我没有在用户上设置关系,但我可能会这样做。我不打算删除用户,但是,嗯,也许……是的,这很有效!我没有在用户上设置关系,但我可能会这样做。我不打算删除用户,但嗯,也许。。。