1

我正在尝试ACL使用 Laravel Policys 和 Gate 来实现。下面是我的代码。用户策略:

class UserPolicy
{
    use HandlesAuthorization;

    public function isRoleAllowed(User $user, $role)
    {
        return in_array( $role , $user->getRoles());
    }
}

身份验证服务提供者:

class AuthServiceProvider extends ServiceProvider
{
    protected $policies = [
        'App\Model' => 'App\Policies\ModelPolicy',
    ];
    public function boot()
    {
        $this->registerPolicies();


        // User Policy
        foreach ( get_class_methods(new \App\Policies\UserPolicy) as $method ) {
            Gate::define($method, "App\Policies\UserPolicy@{$method}");
        }

    }
}

我如何尝试在我的控制器中访问 isRoleAllowed,

Gate::allows('isRoleAllowed', 'APPROVE_INVOICE')

它总是返回 true。它没有执行 isRoleAllowed 函数。我不知道我哪里错了。

4

0 回答 0