Laravel-permission: Cache Refresh on giving Direct Permissions

Created on 14 Oct 2017  路  4Comments  路  Source: spatie/laravel-permission

I've tried and followed the steps in installing this package.
One thing I've noticed is that, when adding a direct permission to a user, the changes don't reflect immediately.

I did this in laravel tinker.

  1. Create user
  2. Create role 'admin'
  3. Create permission 'edit articles'
  4. Add role to user
  5. Create permission 'delete articles'
  6. Add permission directly to user
  7. Call $user->getAllPermissions()
  8. Permission delete articles is nowhere to be found.

But when I restarted laravel tinker and fetched all permissions from the same user, it appeared.

Is there something I'm doing wrong or is there already a workaround for this?

Thank you!

Most helpful comment

I think you're just running into basic Eloquent model "state".

You've created a $user object.
Then you used this package to add permissions to "the user", but you didn't update the $user object itself.
When this package adds permissions to the user, it doesn't update that object in memory. You need to call fresh() on the $user object to get the updated associated properties.

$user->fresh()->getAllPermissions() will show the updated permissions.
If you want to actually update the $user object, use $user = $user->fresh();

All 4 comments

I think you're just running into basic Eloquent model "state".

You've created a $user object.
Then you used this package to add permissions to "the user", but you didn't update the $user object itself.
When this package adds permissions to the user, it doesn't update that object in memory. You need to call fresh() on the $user object to get the updated associated properties.

$user->fresh()->getAllPermissions() will show the updated permissions.
If you want to actually update the $user object, use $user = $user->fresh();

Ok, it worked. But why can't I use the can function with the direct permission I just gave to a user? Noting that I've already done $user = $user->fresh();.

I can see the direct permission when calling getAllPermissions() on the user, but when I use $user->can('direct permission'), it always return false.

Hi @wreighsantos

This is because can uses Laravel's gate. We only register all permission to the gate once during app boot (in PermissionRegistrar@registerPermissions).

If you really need the can method to work right after you assigned a permission to a user you could use app(PermissionRegistrar::class)->registerPermissions() to re-register all permissions on Laravel's gate immediately.

This will be fixed in v3 or when #505 gets merged.

Try
app(PermissionRegistrar::class)->forgetCachedPermissions()
work perfectly for me

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tripex picture tripex  路  3Comments

younus93 picture younus93  路  4Comments

feliperoan picture feliperoan  路  3Comments

ghost picture ghost  路  3Comments

ionesculiviucristian picture ionesculiviucristian  路  4Comments