使用Gate之前未调用的策略()

问题描述 投票:0回答:1

我创建了一个名为ProjectPolicy的策略,其中包含一个函数:

public function update(User $user, Project $project)
{
    return $project->owner_id == $user->id;
}

我在我的AuthServiceProvider注册了我的政策:

protected $policies = [
    'App\Project' => 'App\Policies\ProjectPolicy'
]

同样在AuthServiceProvider,我有这个:

public function boot(Gate $gate)
{
    $this->registerPolicies();

    $gate->before(function ($user) {
        return $user->isAdmin();
    });
}

如果用户是管理员,则应该不应用该策略。但是,当我这样做时,它完全取消了非管理员用户的访问权限。为什么?

php laravel
1个回答
0
投票

要修复它,boot方法应更新为:

public function boot(Gate $gate)
{
    $this->registerPolicies();

    $gate->before(function ($user) {
        return $user->isAdmin() ? true : null;
    });
}

来自https://laravel.com/docs/5.8/authorization#intercepting-gate-checks

如果before回调返回非null结果,则结果将被视为检查结果。

© www.soinside.com 2019 - 2024. All rights reserved.