Yin*_*ing 8 php validation authorization laravel laravel-5
我做这样的门:
Gate::define('update-post', function ($user, Post $post) {
return $user->hasAccess(['update-post']) or $user->id == $post->user_id;
});
Run Code Online (Sandbox Code Playgroud)
我检查了我的数据库,它有更新帖子访问权限,用户ID与帖子相同.但我得到了:
此操作未经授权.
错误.所以我在这里犯了一些错误吗?谢谢.
Ken*_*rna 15
我开始使用表单请求类进行数据验证时使用类似的问题(php artisan make:request UpdateUserRequest例如使用).
如果您使用表单请求来验证数据,那么首先,检查您是否正确设置了正确的规则以允许它通过.这是在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();
*/
}
public function rules()
{
// your validations...
}
}
Run Code Online (Sandbox Code Playgroud)
Jam*_*has 11
对于 Laravel 8(也适用于 Laravel 9),转到文件夹 app->http->requests 选择类文件(在我的例子中是 StoreStudentRequest.php)并在函数授权中将返回值设置为 true;
public function authorize()
{
return true;
}
Run Code Online (Sandbox Code Playgroud)
确保您在“授权”方法上返回true
public function authorize()
{
return true;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
当我没有return true使用php 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'
];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15641 次 |
| 最近记录: |