hasAccess(-6ren">
gpt4 book ai didi

php - 在 Laravel 5/6/7/8/9 中使用表单请求验证时出现错误 "This action is unauthorized."

转载 作者:可可西里 更新时间:2023-11-01 13:08:29 25 4
gpt4 key购买 nike

我做了一些像这样的门:

Gate::define('update-post', function  ($user, Post $post) {
return $user->hasAccess(['update-post']) or $user->id == $post->user_id;
});

我检查了我的数据库,它具有更新后访问权限,并且用户 ID 与帖子中的相同。但我得到了:

This action is unauthorized.

错误。那我在这里做错了吗?谢谢。

最佳答案

我前段时间在开始使用Form Request的时候遇到过类似的问题数据验证类。我注意到以下几点:

如果您使用 Form Requests验证数据,然后首先,检查您是否正确设置了允许它通过的授权规则。这是由 authorize() 方法处理的,该方法必须返回一个 boolean默认设置为 false :

namespace App\Http\Requests\Users;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;

class UpdateUserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
/**
* By default it returns false, change it to
* something like this if u are checking authentication
*/
return Auth::check(); // <-------

/**
* You could also use something more granular, like
* a policy rule or an admin validation like this:
* return auth()->user()->isAdmin();
*
* Or just return true if you handle the authorisation
* anywhere else:
* return true;
*/
}

public function rules()
{
// your validations...
}

}

关于php - 在 Laravel 5/6/7/8/9 中使用表单请求验证时出现错误 "This action is unauthorized.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47128903/

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