【问题标题】:Laravel policy issue, 403 not found for show pageLaravel 政策问题,显示页面找不到 403
【发布时间】:2021-08-26 09:26:34
【问题描述】:

我在使用 Laravel 政策时遇到了一些问题,无法弄清楚为什么这不起作用? 我有一个策略附加到具有所有常规方法(索引、创建、显示等)的模型 索引页面工作正常,但我在进入查看页面时一直找不到 403 页面? 正如您在政策中看到的那样,无论检查是否成功,我都返回了 true 并且它仍然返回 403

运输控制器

/**
 * Display the specified resource.
 *
 * @param int $id
 * @param ShippingModel $shippingModel
 * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\View\View
 * @throws \Illuminate\Auth\Access\AuthorizationException
 */
public function show($id, ShippingModel $shippingModel)
{
    $this->authorize('view', ShippingModel::class);

    return view('pages.warehouse.shipping.show');

}

ShippingModelPolicy

    /**
 * Determine whether the user can view the model.
 *
 * @param  \App\Models\User  $user
 * @param  \App\Models\ShippingModel  $shippingModel
 * @return \Illuminate\Auth\Access\Response|bool
 */
public function view(User $user, ShippingModel $shippingModel)
{
    if ($user->isSuperAdmin() || $user->hasPermissionTo(205, 'web')) {
        return true;
    }
    return true;
}

我已经在方法中添加了授权,即使我也定义了 authorizeController 但它仍然不起作用

    public function __construct()
{
    $this->authorizeResource(ShippingModel::class, 'shippingModel');
}

【问题讨论】:

    标签: php laravel-8 laravel-permission


    【解决方案1】:

    您的view 策略方法依赖于模型,但控制器上的检查不考虑模型。删除第二个参数,只留下User $user

    【讨论】:

      猜你喜欢
      • 2021-06-05
      • 2021-12-24
      • 2021-12-07
      • 2020-07-28
      • 2020-03-30
      • 2019-01-09
      • 1970-01-01
      • 2019-09-02
      • 2021-04-05
      相关资源
      最近更新 更多