【发布时间】:2019-10-11 01:52:39
【问题描述】:
我有以下政策:
<?php
namespace App\Policies;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
class EventPolicy
{
use HandlesAuthorization;
/**
* Determine whether the user can view the whole model.
*
* @param User $user
* @return boolean
*/
public function list(?User $user) {
return true;
}
}
AuthServiceProvider 包含正确的绑定:
protected $policies = [
Event::class => EventPolicy::class,
];
在我的控制器中:
public function index() {
$this->authorize('list');
return $this->repository->paginate();
}
这会返回 403 Forbidden 回复:
This action is unauthorized.
Laravel 文档声明您可以让用户声明 User 可选或设置默认的 null 值。两种方法都试过了。
我正在运行v5.8
有什么想法吗?
【问题讨论】:
-
你用的是什么 laravel 版本?似乎在 5.7+ 中添加了访客用户授权
-
我使用的是 5.8 版