门关闭总是被拒绝

时间:2016-12-19 18:00:52

标签: php laravel authorization laravel-5.3

我使用的是Laravel 5.3。我已经能够创建策略类,但是当我尝试注册一个门关闭时,它总是被拒绝。

以下是AuthServiceProvider

中的boot()方法
public function boot()
{
    $this->registerPolicies();

    Gate::define('view-admin-index', function ($user, $company) {
        return true;
    });
}

这是转储时的输出。

dd(Gate::has('view-admin-index')); => true
dd(Gate::allows('view-admin-index', $company)); => false

更新

如果这样做,我也会false而不是"here"

// In AuthServiceProvider
Gate::define('view-admin-index', function ($user, $company) {
    dd('here');
    return true;
});

// In controller, output is false
dd(Gate::allows('view-admin-index', $company));

更新2

// In controller, there is an authenticated user and output is false
dd(auth()->user()); // => User
dd(Gate::allows('view-admin-index', $company)); // => false

2 个答案:

答案 0 :(得分:0)

看起来你甚至没有打电话给关闭。如果您当前没有对用户进行身份验证,则会出现这种情况。见Source Here。如果您在dd(Auth::user())之前致电Gate::allows('view-admin-index', $company),是否收到任何输出?

如果您需要手动登录用户,您可以随时执行:

Auth::login(User::find($id));

答案 1 :(得分:0)

我有同样的问题,它看起来像是Laravel 5.3.28的一个大错误。 它只是不接受集合作为参数。 这是我的解决方法:

代码:

Gate::define('xx', function ($auth, $user) {
    return 1;
});

Route::get('example', function()
{
    $user = User::first();
    dd( Gate::allows('xx', $user) ); //false
});

我的解决方法:

Route::get('example', function()
{
    $user = (object)User::first()->toArray();
    dd( Gate::allows('xx', $user) ); //true
});
相关问题