我想讓編輯頁面的授權不顯示給除用戶之外的任何人,并且授權功能對我不起作用,它回傳 403 此操作未經授權。在這兩種情況下
class ProfilesController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index($user)
{
return view('profiles.index', [
'user' =>User::findOrFail($user)
]);
}
public function edit(User $user)
{
$this->authorize('update', $user->profile);
return view('profiles.edit', compact('user'));
}
public function update(User $user)
{
$this->authorize('update', $user->profile);
$data = request()->validate([
'title' => 'required',
'description' => 'required',
'url' => 'url',
'image' => '',
]);
auth()->user->profile->update($data);
return redirect("/profile/{$user->id}");
}
}
uj5u.com熱心網友回復:
您需要在 AuthServiceProvider 類中創建和注冊策略。更多資訊:https : //laravel.com/docs/master/authorization#registering-policies
假設您有一個包含“user_id”的 Profile 模型類,實作將或多或少像這樣。
<?php
namespace App\Policies;
use App\Models\Profile;
use App\Models\User;
class ProfilePolicy
{
/**
* Determine if the given profile can be updated by the user.
*
* @param \App\Models\User $user
* @param \App\Models\Profile $profile
* @return bool
*/
public function update(User $user, Profile $profile)
{
return $user->id === $profile->user_id;
}
}
當然這只是一個例子,因為政策可能有不同的實施方式
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/361744.html
上一篇:如何組合具有相同值的陣列
