-1

I am defining Gates within service provider. Like this:

        $permissions = \DB::table('permissions')->whereNotNull('route_name')->get();
        foreach ( $permissions as $permission) {
            Gate::define($permission->route_name, function($user) use ($permission){
                return $user->hasPermission($permission->route_name);
            });
        };

I am able to access the current user within the callback of the Gate but I cant acces the user outside the callback. Instead that above I want define the gates directly like:

        $permissions = \Auth::user()->permissions();
        foreach ( $permissions as $permission) {
            Gate::define($permission->route_name, function($user) {
                return true;
            });
        };

Is that possible ?

PS: I can't access auth()->user() like in some similar questions because I am using stateless authentication without session.

Skeletor
  • 3,201
  • 4
  • 30
  • 51

1 Answers1

0

The Gate::define is a callback method. The gates are defined in boot stage. They don't run at the time they are defined in boot stage, but only made ready to run in runtime. In the boot stage most of services (including auth) are not loaded yet.

So it is not possible to access the (logged) user at this time.

Skeletor
  • 3,201
  • 4
  • 30
  • 51