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

Removed redundant group permissions db lookup #530

Merged
merged 4 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 1 addition & 15 deletions src/Authorization/PermissionModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,7 @@ class PermissionModel extends Model
*/
public function doesUserHavePermission(int $userId, int $permissionId): bool
{
// Check user permissions and take advantage of caching
$userPerms = $this->getPermissionsForUser($userId);

if (count($userPerms) && array_key_exists($permissionId, $userPerms)) {
return true;
}

// Check group permissions
$count = $this->db->table('auth_groups_permissions')
->join('auth_groups_users', 'auth_groups_users.group_id = auth_groups_permissions.group_id', 'inner')
->where('auth_groups_permissions.permission_id', $permissionId)
->where('auth_groups_users.user_id', $userId)
->countAllResults();

return $count > 0;
return array_key_exists($permissionId, $this->getPermissionsForUser($userId));
}

/**
Expand Down
29 changes: 29 additions & 0 deletions tests/authorization/FlatAuthorizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,35 @@ public function testRemovePermissionFromUser()
]);
}

public function testGetPermissionsForUser()
{
$user = $this->createUser();
$group = $this->createGroup();
$permission = $this->createPermission();
$permission2 = $this->createPermission();

$this->assertIsArray($this->permissions->getPermissionsForUser($user->id));
$this->assertSame([], $this->permissions->getPermissionsForUser($user->id));

$this->auth->addPermissionToUser($permission->id, $user->id); //user's permissions
$this->auth->addPermissionToGroup($permission2->id, $group->id); // groups permission
$this->auth->addUserToGroup($user->id, $group->id);

$expected = [
$permission->id => $permission->name,
$permission2->id => $permission2->name,
];

$actual = $this->permissions->getPermissionsForUser($user->id);

$this->assertEqualsCanonicalizing($expected, $actual);

$this->assertArrayHasKey($permission->id, $actual);
$this->assertArrayHasKey($permission2->id, $actual);
$this->assertArrayNotHasKey(9999, $actual);
$this->assertCount(2, $actual);
}

public function testDoesUserHavePermission()
{
$user = $this->createUser();
Expand Down