spatie/laravel-permission

->hasPermissionTo() only returns true on one permission despite proper assignment of permissions

AnnoyingTechnology opened this issue · 5 comments

Describe the bug

I have created 2 permissions.

  • access_back_office
  • manage_users.

I have created 1 role (developer).
All permissions are assigned to that role using $role->givePermissionTo(Permission::all());

The permission table looks fine.

Modify | id | name | guard_name | created_at | updated_at | group_id
-- | -- | -- | -- | -- | -- | --
edit | 1 | access_back_office | web | 2023-07-20 12:17:48 | 2023-07-20 12:17:48 | 1
edit | 2 | manage_users | web | 2023-07-20 12:17:48 | 2023-07-20 12:17:48 | 2

The role_has_permission looks fine. Both permissions are associated to the role.

Modify | permission_id | role_id
-- | -- | --
edit | 1 | 1
edit | 2 | 1

But
auth()->user()->can('access_back_office') returns true (expected)
auth()->user()->hasPermissionTo('access_back_office') returns true (expected)
auth()->user()->can('manage_users') returns false (unexpected)
auth()->user()->hasPermissionTo('manage_users') throws There is no permission named "manage_users" for guard "web". (unexpected)

In spite of auth()->user()->getAllPermissions() returning both permissions.

me@glycyrrhiza:/var/www/domains/domain$ php artisan permission:show
Guard: web
+--------------------+-------------+
|                    | Developer |
+--------------------+-------------+
| access_back_office |  ✔          |
| manage_users       |  ✔          |
+--------------------+-------------+

Versions

  • spatie/laravel-permission package version: 5.10.2
  • illuminate/framework package 10.14.1
  • PHP version: 8.2.7
  • Database version: SQlite 3.34.1

Caches have been cleared (php artisan cache:clear), the database has been rebuilt (php artisan migrate:fresh --seed). User session has been closed and re-open.

I'm pretty sure this is something minor that I have overlooked, but I cannot figure it out.
Thanks for the help.

Caches have been cleared (php artisan cache:clear)

was spatie cache also cleared?

php artisan permission:cache-reset

Attempted, but it returns Unable to flush cache.

Good catch, it was/is the cache.

I'm using APC. Tried a while back switching to driver file without any change.
Although, restarting php-fpm right now does fix the issue.

Going to look for a known issue with laravel-permission+APCu.

Attempted, but it returns Unable to flush cache.
Going to look for a known issue with laravel-permission+APCu

Might be worth checking whether there's any known issues with APC+Laravel to trigger that message.
This package simply calls $cache->forget($key) (where $key is the cache key from the package config file).

This is extremely odd.

Laravel's documentation explicitly recommends APC, without any mentions of limitations.
The remainder of my applications that uses cache behaves properly.

I'm closing the issue as it doesn't seem spatie/laravel-permission specific.