gpt4 book ai didi

php - Laravel 策略(函数 App\Policy 的参数太少)

转载 作者:搜寻专家 更新时间:2023-10-31 21:47:59 27 4
gpt4 key购买 nike

我正在尝试为用户设置我的策略。但是我不断收到以下错误:

Too few arguments to function App\Policies\UserPolicy::update(), 1 passed in /vendor/laravel/framework/src/Illuminate/Auth/Access/Gate.php on line 481 and exactly 2 expected (View: /resources/views/users/index.blade.php)

ErrorException /app/Policies/UserPolicy.php 20

which is on the UserPolicy@update function

当我以 super_admin 身份登录时,它工作正常,但每当我以不同角色的用户身份登录时,它就会抛出此错误。

下面是我目前的实现:

用户策略

class UserPolicy
{
use HandlesAuthorization;

public function update(User $user, User $userEdit) {
if ($user->id == $userEdit->id) {
return true;
}
return $user->can('update_user');
}

public function before($user, $ability) {
if ($user->hasRole('super_admin')) {
return true;
}
}
}

用户 Controller

class UsersController extends Controller {

public function __construct() {
$this->middleware('auth');
}

public function edit(User $user) {
$this->authorize('update', $user);
return view('users.edit', [
'user' => User::with('roles', 'level')->find($user->id),
'surveys' => \App\Survey::all(),
]);
}

public function update(UserRequest $request, User $user) {
$this->authorize('update', $user);
$request->save();
session()->flash('success', 'User successfully updated');

// means user is editing his own profile
if (auth()->id() == $user->id) {
return redirect('/dashboard');
} else {
return redirect('/users');
}
}
}

用户请求

class UserRequest extends FormRequest {

public function authorize() {
return true;
}

public function rules() {
switch ($this->method()) {
case 'POST':
return [
'name' => 'required|string',
'email' => 'required|string|email|max:255|unique:users',
'role' => 'required|exists:roles,id',
'level' => 'required|string',
];
break;

case 'PATCH':
return [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users,email,'.$this->user->id,
'role' => 'sometimes|exists:roles,id',
'level' => 'sometimes|string',
'password' => 'nullable|sometimes|string|min:6|confirmed'
];
break;

default:
break;
}
}

public function save() {
switch (request()->method()) {
case 'POST':
$this->createUser();
break;

case 'PATCH':
$this->updateUser();
break;

default:
break;
}
}

protected function createUser() {
// random generate password
$password = str_random(8);

$user = User::create([
'name' => request('name'),
'email' => request('email'),
'level_id' => request('level'),
'password' => Hash::make($password),
]);
$user->assignRoleById(request('role'));

Mail::to($user)->send(new WelcomeMail($user, $password));
}

protected function updateUser() {
$user = User::findOrFail($this->user->id);
$user->name = request('name');
$user->email = request('email');

if (request('password') != '') {
$user->password = Hash::make(request('password'));
}

if (request('level') != '') {
$user->level_id = request('level');
}

$user->update();

if (request('role') != '') {
$user->roles()->sync([request('role')]);
}
}
}

AuthServiceProvider

class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
\App\User::class => \App\Policies\UserPolicy::class,
];

/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
foreach ($this->getPermissions() as $permission) {
Gate::define($permission->name, function($user) use ($permission) {
return $user->hasRole($permission->roles);
});
}
}

protected function getPermissions() {
return Permission::with('roles')->get();
}
}

最佳答案

在我调用的 View 文件中

@can('update', App\User::class)
<!-- html code --!>
@endcan

代替

@can('update', $user)
<!-- html code --!>
@endcan

我没有将用户实例传递给导致错误的函数。

关于php - Laravel 策略(函数 App\Policy 的参数太少),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52566692/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com