0

I am using Spatie Permissions for my Laravel app. I have assigned permissions to a role, and the role to a user.

When I use $user->getAllPermissions() or $user->getPermissionsViaRoles() it displays them all in JSON format.

However, when I am ensuring they have a certain permission before viewing a route using Spatie middleware (permission/can) it says unauthorized.

Route::get('/admin/users', \App\Http\Livewire\Admin\User\UserList::class)->middleware('permission:view users')->name('admin.users');

I was wondering how I can make it so it knows the permission is assigned through the users role?

It says best practice is to do it how I've done, rather than assigning permissions direct to users.

I've tried using AI, googling, etc and found no answers.

Gayan Mettananda
  • 1,498
  • 14
  • 21
  • Did you add the RoleMiddleware, PermissionMiddleware and RoleOrPermissionMiddleware to your app/Http/Kernel.php? You can check here how to do it in different versions of Laravel https://spatie.be/docs/laravel-permission/v5/basic-usage/middleware#content-package-middleware. – Dumitru Aug 06 '23 at 07:58
  • @Dumitru yep I've done that – Ethan Patchell Aug 06 '23 at 09:39

1 Answers1

0

are you using the \Illuminate\Auth\Middleware\Authorize::class in your kernal.php? I believe that to be required for using the can middleware.

amac
  • 921
  • 1
  • 6
  • 17