spatie / laravel-permission

Associate users with roles and permissions
https://spatie.be/docs/laravel-permission
MIT License
12.05k stars 1.76k forks source link

not all but some users returning no roles or permissions, and noticing 9223372036854775807 issue on model_id in debug #2700

Open matthewstick opened 1 month ago

matthewstick commented 1 month ago

Description

Was running Laravel 5, spatie/laravel-permission 2.28 - no issues Migrated to Laravel 10, Spatie 6.9.0 - issues with some users.

I have many users where it works fine after the update.

But I have one user where it worked for years, except now it does not. Their user is returning an empty array for the roles, and also no permissions.

I tried adjusting the settings and giving it others roles and max permissions, no dice.

I tried making a brand new user, with the exact same permissions, and the brand new users works great.

Cache has been cleared many times.

I've spent days debugging Spatie vendor packages. The only thing I'm noticing in the bad query, the model_id gets blown out of the water and becomes 9223372036854775807. Wheras normally it returns something more sane, like the second example.

My gut says cache, but I've disabled the cache in any way I can think of.

BAD QUERY select roles.*, model_has_roles.model_id as pivot_model_id, model_has_roles.role_id as pivot_role_id, model_has_roles.model_type as pivot_model_type from roles inner join model_has_roles on roles.id = model_has_roles.role_id where model_has_roles.model_id in (9223372036854775807) and model_has_roles.model_type = 'App\User';

GOOD QUERY select roles.*, model_has_roles.model_id as pivot_model_id, model_has_roles.role_id as pivot_role_id, model_has_roles.model_type as pivot_model_type from roles inner join model_has_roles on roles.id = model_has_roles.role_id where model_has_roles.model_id in (9) and model_has_roles.model_type = 'App\User';

IDeas

Steps To Reproduce

1. 2. 3. ...

Example Application

No response

Version of spatie/laravel-permission package:

6.9

Version of laravel/framework package:

10

PHP version:

8.1

Database engine and version:

No response

OS: Windows/Mac/Linux version:

No response

drbyte commented 1 month ago

Curiously, 9223372036854775807 is the maximum value for a long number in various languages.

What is the db field type for the id in the users table? Have you converted to/from uuid with this project?

Is there any attribute or accessor that might translate a smaller integer value to some very large value by virtue of some type-conversion taking place? (including converting to a string or to an exponent value, whether intended or not)

matthewstick commented 1 month ago

Curiously, 9223372036854775807 is the maximum value for a long number in various languages.

What is the db field type for the id in the users table? Have you converted to/from uuid with this project?

Hi. They'd all been using uuid since the beginning about 7 or 8 years ago. They are all char(36).

There's about 500 active users. I ran a report, and 31 of them had "no roles" show up. They were working on Spatie 2/Laravel 5. They stopped working Spatie 6/Laravel 10. It also stopped working Spatie 5, but I upgraded to Spatie 6 and it also doesnt work there.

I agree the issue seems to be the model_id is blowing out the value as a number, hnce the 9223372036854775807. The only issue is that these 500 or so active users all use the same tables.

For example, a user has 99e62127-c1d9-49c9-9566-22c678240447 as their id. No roles. So what I do is find and replace that value with some random new uuid aross the db, such as 2b6a148f-6d46-4d60-a6bd-d07aac131292. Then suddenly the user is reporting roles and is working again.

So my "fix" is to replace the uuid's on these 31 users that returned no roles. But I'd love to get to the bottom of it. There's nothing anywhere in the code, because once I find and replace the ID, the user suddenly works. This makes me thing it's cache.

I tried a million versions of cache clear:

php artisan cache:clear php artisan config:cache php artisan cache:forget spatie.permission.cache php artisan cache:forget laravelspatie.permission.cache php artisan config:clear php artisan cache:forget spatie.permission.cache php artisan permission:cache-reset

All work, except the last one. It says... php artisan permission:cache-reset Unable to flush cache.

I've disabled cache entirely. I've tried on 4 different server builds (local vagrant, dev, staging, prod). All servers have the same issue. So feels like... not a cache?

It's completely crazy. Maybe the problem will go away forever when I cycle the UUIDs. However, maybe it will come back. Would love some insight in case it's a bigger issue. I've gone deep into the vendor files trying to debug this.. and I just see nothing, other than it totally works once I swap out UUIDs.

Thanks in advance.