我的授权功能在 laravel-5.8 中总是失败?

发布于 2025-01-17 01:59:45 字数 1157 浏览 5 评论 0原文

我在创建函数中创建了一个策略,我正在检查该用户是否能够创建记录,并且我正在 AthServiceProvider.php 中注册模型和策略,之后我使用 在控制器内部进行检查>$this->authorize('create') 即使用户有效,它也总是失败,您能帮我解决这个问题

错误:- 此操作未经授权

restaurentContoller.php

class RestaurentsController extends Controller
{
    protected $repository;

    public function __construct(RestaurentRepository $repository){
        $this->repository = $repository;
    }

    public function postRestaurent(RestaurentRequest $request){
        $data = $request->all();
        $data['admin_id'] = $this->getAccountId($request);
        $this->authorize('create');
        $rest = $this->repository->create($data);
        return response()->json(fractal($rest,new RestuarentTransformer));        
    }
}

RestaurentPolicy.php

   public function create(User $user)
    {
        return ($user->admin_id=1) ? true : false;
    }

api.php

Route::post('/postRest',[RestaurentsController::class,'postRestaurent'])->middleware(['CheckAdmin']);

I created one policy inside create function i am checking weather this user can able to create records and i am registering the Model and policy in the AthServiceProvider.php after that i am checking inside the controller by using $this->authorize('create') it's failing always even the user is valid,can you please help me how to resolve this issue

Error:- This Action is unathorized

restaurentContoller.php

class RestaurentsController extends Controller
{
    protected $repository;

    public function __construct(RestaurentRepository $repository){
        $this->repository = $repository;
    }

    public function postRestaurent(RestaurentRequest $request){
        $data = $request->all();
        $data['admin_id'] = $this->getAccountId($request);
        $this->authorize('create');
        $rest = $this->repository->create($data);
        return response()->json(fractal($rest,new RestuarentTransformer));        
    }
}

RestaurentPolicy.php

   public function create(User $user)
    {
        return ($user->admin_id=1) ? true : false;
    }

api.php

Route::post('/postRest',[RestaurentsController::class,'postRestaurent'])->middleware(['CheckAdmin']);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_畞蕅 2025-01-24 01:59:45

如果您使用请求类,则必须将授权方法返回 false 更改为 true

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

If you use Request Classes you have to change authorize method return false to true

class RestaurentStoreRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return false;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文