在Laravel 5/6/7/8/9/10中使用表单请求验证时出现错误“此操作未经授权,”

utugiqy6  于 2023-01-18  发布在  其他
关注(0)|答案(9)|浏览(174)

我做了这样的门:

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

我检查了我的数据库,它有更新后访问和用户ID是相同的帖子。但我得到:
此操作未经授权。
错误,我是不是犯了什么错误,谢谢.

ao218c7q

ao218c7q1#

不久前,我在开始使用Form Request类进行数据验证时遇到了类似的问题。
如果您使用表单请求来验证数据,那么首先,检查您是否正确设置了允许它通过的授权规则。这由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...
    }

}
kr98yfug

kr98yfug2#

确保在“authorize”方法上返回**true

public function authorize()
{
    return true;
}
1dkrff03

1dkrff033#

对于Laravel 8,进入文件夹app-〉http-〉requests,选择类文件(在我的例子中是StoreStudentRequest.php),并在函数authorize中设置返回值为true;

public function authorize()
{
    return true;
}
kx7yvsdv

kx7yvsdv4#

当我在函数public function authorize()中的php artisan make:request SellRequest中没有return true时,我遇到了这个问题

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class SellRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
        public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'city'=>'required',
            'address'=>'required',
            'type'=>'required',
            'land'=>'required',
            'area'=>'required'
        ];
    }
}
g52tjvyc

g52tjvyc5#

<?php 
namespace App\Modules\UserManagement\Request;

use Illuminate\Foundation\Http\FormRequest;
use Response;

class UserRequest extends FormRequest
{
     /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    public function rules()
    {

        $rules = [
            'full_name' => 'required',
            'email' => 'required|email',
            'password' => 'required',
            're_enter_password' => 'required'
        ];

        return $rules;
    }
}
1szpjjfi

1szpjjfi6#

在我的例子中,我没有在Gate::define(...)中执行正确的检查
所以也许要仔细检查一下这个函数的逻辑

tsm1rwdh

tsm1rwdh7#

需要授权函数返回true

public function authorize()
{
    return TRUE;
}

然后添加auth facade或使用Auth;

o2rvlv0m

o2rvlv0m8#

如果您已经配置了authorize(),但仍然遇到同样的问题,您可以检查您的route/api.php您可能会遇到为2 Controller声明相同路径的错误。

Route::resource('users', UserController::class)/*authorized true*/;
Route::resource('users', User2Controller::class)/*unauthorized true*/;
trnvg8h3

trnvg8h39#

在你的控制器中添加这一行

**use Illuminate\Http\Request;**

代替

use App\Http\Requests\StoreGameRequest;

并将函数中的参数更改为

public function store(**Request $request**)
    {
        $request->validate([

相关问题