Skip to content

Commit

Permalink
Using cache for user groups. Fixes #134
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnieezell committed Dec 12, 2019
1 parent 567df59 commit b110088
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ In your application, perform the following setup:

> php spark migrate -all
NOTE: This library uses your application's cache settings to reduce database lookups. If you want
to make use of this, simply make sure that your are using a cache engine other than `dummy` and
it is properly setup. The `GroupModel` and `PermissionModel` will handle caching and invalidation
in the background for you.

## Overview

When first installed, Myth:Auth is setup to provide all of the basic authentication services
Expand Down
7 changes: 4 additions & 3 deletions src/Authorization/FlatAuthorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function inGroup($groups, int $userId)

$userGroups = $this->groupModel->getGroupsForUser((int)$userId);

if (! $userGroups)
if (empty($userGroups))
{
return false;
}
Expand All @@ -92,8 +92,9 @@ public function inGroup($groups, int $userId)
}
else if (is_string($group))
{
$ids = array_column($userGroups, 'name');
if (in_array($group, $ids))
$names = array_column($userGroups, 'name');

if (in_array($group, $names))
{
return true;
}
Expand Down
23 changes: 18 additions & 5 deletions src/Authorization/GroupModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class GroupModel extends Model
*/
public function addUserToGroup(int $userId, int $groupId)
{
cache()->delete("{$userId}_groups");

$data = [
'user_id' => (int)$userId,
'group_id' => (int)$groupId
Expand All @@ -53,6 +55,8 @@ public function addUserToGroup(int $userId, int $groupId)
*/
public function removeUserFromGroup(int $userId, $groupId)
{
cache()->delete("{$userId}_groups");

return $this->db->table('auth_groups_users')
->where([
'user_id' => (int)$userId,
Expand All @@ -69,6 +73,8 @@ public function removeUserFromGroup(int $userId, $groupId)
*/
public function removeUserFromAllGroups(int $userId)
{
cache()->delete("{$userId}_groups");

return $this->db->table('auth_groups_users')
->where('user_id', (int)$userId)
->delete();
Expand All @@ -83,11 +89,18 @@ public function removeUserFromAllGroups(int $userId)
*/
public function getGroupsForUser(int $userId)
{
return $this->builder()
->select('auth_groups_users.*, auth_groups.name, auth_groups.description')
->join('auth_groups_users', 'auth_groups_users.group_id = auth_groups.id', 'left')
->where('user_id', $userId)
->get()->getResultArray();
if (! $found = cache("{$userId}_groups"))
{
$found = $this->builder()
->select('auth_groups_users.*, auth_groups.name, auth_groups.description')
->join('auth_groups_users', 'auth_groups_users.group_id = auth_groups.id', 'left')
->where('user_id', $userId)
->get()->getResultArray();

cache()->save("{$userId}_groups", $found, 300);
}

return $found;
}


Expand Down
12 changes: 6 additions & 6 deletions src/Authorization/PermissionModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,17 @@ public function getPermissionsForUser(int $userId): array
->get()
->getResultObject();

$found = array_merge($fromUser, $fromGroup);
$combined = array_merge($fromUser, $fromGroup);

$result = [];
foreach ($found as $row)
$found = [];
foreach ($combined as $row)
{
$result[$row->id] = strtolower($row->name);
$found[$row->id] = strtolower($row->name);
}

cache("{$userId}_permissions", $result, config('App'), 300);
cache()->save("{$userId}_permissions", $found, 300);
}

return $result;
return $found;
}
}
3 changes: 3 additions & 0 deletions tests/authorization/FlatAuthorizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public function setUp(): void

$this->auth = new FlatAuthorization($this->groups, $this->permissions);
$this->auth->setUserModel($this->users);

db_connect()->table('auth_groups_users')->truncate();
cache()->clean();
}

public function testInGroupSingleId()
Expand Down

0 comments on commit b110088

Please sign in to comment.