Skip to content

Commit f4e5f6c

Browse files
authored
Merge pull request #216 from screencom/1.0
Add tags to the cache to easily purge ACL cache
2 parents 82f872a + befb29b commit f4e5f6c

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

src/Kodeine/Acl/Models/Eloquent/Role.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function users()
4848
*/
4949
public function getPermissions()
5050
{
51-
return \Cache::remember(
51+
return \Cache::tags(config('acl.cacheTags', []))->remember(
5252
'acl.getPermissionsInheritedById_'.$this->id,
5353
config('acl.cacheMinutes'),
5454
function () {

src/Kodeine/Acl/Traits/HasPermission.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public function permissions()
3434
public function getPermissions()
3535
{
3636
// user permissions overridden from role.
37-
$permissions = \Cache::remember(
38-
'acl.getPermissionsById_'.$this->id,
37+
$permissions = \Cache::tags(config('acl.cacheTags', []))->remember(
38+
'acl.getPermissionsById_' . $this->id,
3939
config('acl.cacheMinutes'),
4040
function () {
4141
return $this->getPermissionsInherited();
@@ -48,10 +48,14 @@ function () {
4848
// true values.
4949
foreach ($this->roles as $role) {
5050
foreach ($role->getPermissions() as $slug => $array) {
51-
if ( array_key_exists($slug, $permissions) ) {
51+
if (array_key_exists($slug, $permissions)) {
5252
foreach ($array as $clearance => $value) {
53-
if( !array_key_exists( $clearance, $permissions[$slug] ) ) {
53+
if (! array_key_exists($clearance, $permissions[$slug])) {
5454
! $value ?: $permissions[$slug][$clearance] = true;
55+
} else {
56+
if ($permissions[$slug][$clearance] != $value) {
57+
$permissions[$slug][$clearance] = $value;
58+
}
5559
}
5660
}
5761
} else {
@@ -74,7 +78,7 @@ public function can($permission, $operator = null)
7478
{
7579
// user permissions including
7680
// all of user role permissions
77-
$merge = \Cache::remember(
81+
$merge = \Cache::tags(config('acl.cacheTags', []))->remember(
7882
'acl.getMergeById_'.$this->id,
7983
config('acl.cacheMinutes'),
8084
function () {
@@ -193,4 +197,4 @@ protected function parsePermissionId($permission)
193197

194198
return (int) $permission;
195199
}
196-
}
200+
}

src/Kodeine/Acl/Traits/HasRole.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function roles()
2626
{
2727
$model = config('acl.role', 'Kodeine\Acl\Models\Eloquent\Role');
2828

29-
return $this->belongsToMany($model)->withTimestamps();
29+
return $this->belongsToMany($model)->withTimestamps()->orderBy('roles.created_at');
3030
}
3131

3232
/**
@@ -36,7 +36,7 @@ public function roles()
3636
*/
3737
public function getRoles()
3838
{
39-
$this_roles = \Cache::remember(
39+
$this_roles = \Cache::tags(config('acl.cacheTags', []))->remember(
4040
'acl.getRolesById_'.$this->id,
4141
config('acl.cacheMinutes'),
4242
function () {

src/config/acl.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,11 @@
2727
*/
2828

2929
'cacheMinutes' => 1,
30+
31+
/**
32+
* Cache tags
33+
*
34+
* All cached entries are tagged to easily purge them on demand
35+
*/
36+
'cacheTags' => ['acl']
3037
];

0 commit comments

Comments
 (0)