Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Group Permissions and User cache #533

Open
MGatner opened this issue Jul 12, 2022 · 4 comments
Open

Bug: Group Permissions and User cache #533

MGatner opened this issue Jul 12, 2022 · 4 comments

Comments

@MGatner
Copy link
Collaborator

MGatner commented Jul 12, 2022

User permissions cache is not updated when Group permissions are altered, creating a potential lag time between adding/removing a group permission and the update to the user.

@rivaldysaputraagus
Copy link

I also encountered a problem like this, how to fix it?
Any references to problems like this ?

@manageruz
Copy link
Collaborator

I also encountered a problem like this, how to fix it?
Any references to problems like this ?

There are three methods in GroupModel.php file which alter group permissions. They are addPermissionToGroup(), removePermissionFromGroup() and removePermissionFromAllGroups().
So after group permissions altered you should retrieve users which belong to that group and delete there user permisssions cache files.

@manageruz
Copy link
Collaborator

Next time when there will be a call to get user's permissions, new data will be retrieved from database and saved to cache.

@GagaPoloJr
Copy link

I faced this issue while using has_permission() from Helper Functions which refers to this function getPermissionsForUser() which contains cache()->save("{$userId}_permissions", $found, 300);

Then I checked the model between permissionModel & groupModel and see the difference.
You can see function addPermissionToGroup in groupModel doesn't delete cache {$userId}_permissions.

It means that when the has_permission() code runs every time, it caches the current_user permissions {$userId}_permissions.

PermissionModel.php

{
...
},

public function addPermissionToUser(int $permissionId, int $userId)
   {
       cache()->delete("{$userId}_permissions"); //these line
       return $this->db->table('auth_users_permissions')->insert([
           'user_id'       => $userId,
           'permission_id' => $permissionId,
       ]);
   }

GroupModel.php

{
...
},

 public function addPermissionToGroup(int $permissionId, int $groupId)
    {
        
        $data = [
            'permission_id' => $permissionId,
            'group_id'      => $groupId,
        ];

        return $this->db->table('auth_groups_permissions')->insert($data);
    }

alternative way can add this cache()->delete("{$userId}_permissions") on function addPermissionToGroup() to update the cache. Then can follow the code flow for fix errors related to the function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants