fix: resolve permission/role cache not reflecting issues
- UserController: use Spatie syncRoles([$role]) instead of direct Eloquent relations relation sync() which bypassed Spatie model events and cache observers - RoleController: explicitly call forgetCachedPermissions() on store and update to force cache refresh - Clear application cache
This commit is contained in:
@@ -190,10 +190,13 @@ class UserController extends BaseController
|
||||
$role = Role::where('id', $request->roles)
|
||||
->where('created_by', $created_by)->first();
|
||||
|
||||
$user->roles()->sync([$role->id]);
|
||||
$user->syncRoles([$role]);
|
||||
$user->type = $role->name;
|
||||
$user->save();
|
||||
|
||||
// Clear Spatie permission cache
|
||||
app(\Spatie\Permission\PermissionRegistrar::class)->forgetCachedPermissions();
|
||||
|
||||
// Trigger email notification
|
||||
event(new \App\Events\UserCreated($user, $request->password));
|
||||
|
||||
@@ -230,11 +233,14 @@ class UserController extends BaseController
|
||||
$role = Role::where('id', $request->roles)
|
||||
->where('created_by', $created_by)->first();
|
||||
|
||||
$user->roles()->sync([$role->id]);
|
||||
$user->syncRoles([$role]);
|
||||
$user->type = $role->name;
|
||||
}
|
||||
|
||||
$user->save();
|
||||
|
||||
// Clear Spatie permission cache
|
||||
app(\Spatie\Permission\PermissionRegistrar::class)->forgetCachedPermissions();
|
||||
return redirect()->route('users.index')->with('success', __('User updated with roles'));
|
||||
}
|
||||
return redirect()->back()->with('error', __('Unable to update User. Please try again!'));
|
||||
|
||||
Reference in New Issue
Block a user