php 错误“此操作未经授权。” 在 Laravel 5.5+、6.X 和 7.X 中使用表单请求验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47128903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:32:09  来源:igfitidea点击:

Errors "This action is unauthorized." using Form Request validations in Laravel 5.5+, 6.X & 7.X

phplaravelvalidationlaravel-5authorization

提问by Ying

I make some gate like this:

我做了一些这样的门:

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

I checked my database and it has update-post access and the user id is same as in the post. but I got:

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

This action is unauthorized.

此操作未经授权。

errors. so am I do some mistake here? thanks.

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

回答by Kenny Horna

I run with a similar problem some time ago when starting to use Form Requestclasses for data validation(using php artisan make:request UpdateUserRequestfor example).

我前段时间开始使用表单请求类进行数据验证时遇到了类似的问题(php artisan make:request UpdateUserRequest例如使用)。

If you are using a Form Requestto validate the data, then first of all, check that you set properly the authorization rule that will allow it to pass. This is handled by the authorize()method that must return a boolean, that by default is set tofalse:

如果您使用表单请求来验证数据,那么首先,请检查您是否正确设置了允许其通过的授权规则。这是由authorize()必须返回 a的方法处理的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 authorization
         * anywhere else:
         * return true;
         */ 
    }

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

}

回答by Rajesh Chaudhary

Make sure you return trueon "authorize" method

确保在“授权”方法上返回true

public function authorize()
{
    return true;
}

回答by user3257040

<?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;
    }
}

回答by Luca C.

In my case, I was not doing the right check in Gate::define(...)

就我而言,我没有正确办理登机手续 Gate::define(...)

So maybe double check your logic in that function

所以也许在那个函数中仔细检查你的逻辑

回答by peyman ezati

This problem occurred to me when I did not return truein php artisan make:request SellRequestin functionpublic function authorize()

这个问题发生在我身上时,我没return truephp artisan make:request SellRequest函数public function authorize()

<?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'
        ];
    }
}