Open manstie opened 3 weeks ago
I would expect that calling ->assignRole()
would clear any associated cached relations (It does when I use Tinker).
Checking for a permission will load the permissions relation on the model, but then we destroy and reload that when roles/permissions are changed for that model.
That said, Laravel lets you force a refresh: After you assign the role, call $user = $user->fresh();
Quick test in Tinker using some data similar to what's in the docs:
$user = User::first();
$role = Role::find(1); // 'Writer', with one permission: 'edit articles'
$before = $user->hasPermissionTo('edit articles');
$user->assignRole($role);
$afterRole =$user->hasRole(1);
$after = $user->hasPermissionTo('edit articles');
dd(compact('before', 'after'));
array:2 [
"before" => false
"after" => true
]
It may be worth adding that I am using the "teams" feature, and I get the same results in tinker, even with $user = $user->fresh()
:
This is isolated to your tests? If so, can you update the Issue Title to include that factor.
This package's test suite does extensive testing of adding and checking roles and permissions to ensure there are no caching issues, etc.
If you can create a simple fresh app (the Docs have a section on creating a new app and pushing it to a public github repo) that recreates this specific problem, we can determine whether this is a package bug or a bug in your application, and figure out the fix.
I have created an app replicating my environment and settings here: https://github.com/manstie/laravel-permissions-example
Something to note: I seed the users with roles, but when you query them via the user model they are not found, despite existing in the database. I suppose that's my issue or it's related. I wasn't setting the team id in my tinker session
Description
I was writing tests and came across this quirk.
Assuming you've given permission for a role to
do a thing
, this test still fails:But it works if you don't check permissions before assigning the role:
I assume this is some caching issue.
Steps To Reproduce
In the same function:
Example Application
https://github.com/manstie/laravel-permissions-example
Version of spatie/laravel-permission package:
6.9
Version of laravel/framework package:
11.9
PHP version:
8.2
Database engine and version:
No response
OS: Windows/Mac/Linux version:
No response