diff --git a/src/Kodeine/Acl/Traits/HasPermission.php b/src/Kodeine/Acl/Traits/HasPermission.php index c50664f..f1307e0 100644 --- a/src/Kodeine/Acl/Traits/HasPermission.php +++ b/src/Kodeine/Acl/Traits/HasPermission.php @@ -33,32 +33,41 @@ public function permissions() */ public function getPermissions() { - // user permissions overridden from role. + $permissions = \Cache::remember( 'acl.getPermissionsById_'.$this->id, config('acl.cacheMinutes'), function () { - return $this->getPermissionsInherited(); - } - ); + $permissions = []; + // permissions based on role. + // more permissive permission wins + // if user has multiple roles we keep + // true values. + foreach ($this->roles as $role) { + foreach ($role->getPermissions() as $slug => $array) { + if ( array_key_exists($slug, $permissions) ) { + foreach ($array as $clearance => $value) { + ! $value ?: $permissions[$slug][$clearance] = true; + } + } else { + $permissions = array_merge($permissions, [$slug => $array]); + } + } + } - // permissions based on role. - // more permissive permission wins - // if user has multiple roles we keep - // true values. - foreach ($this->roles as $role) { - foreach ($role->getPermissions() as $slug => $array) { - if ( array_key_exists($slug, $permissions) ) { - foreach ($array as $clearance => $value) { - if( !array_key_exists( $clearance, $permissions[$slug] ) ) { - ! $value ?: $permissions[$slug][$clearance] = true; + // user permissions that override roles ones. + foreach ($this->getPermissionsInherited() as $slug => $array) { + if ( array_key_exists($slug, $permissions) ) { + foreach ($array as $clearance => $value) { + $permissions[$slug][$clearance] = $value; } + } else { + $permissions = array_merge($permissions, [$slug => $array]); } - } else { - $permissions = array_merge($permissions, [$slug => $array]); } + return $permissions; } - } + ); return $permissions; } @@ -98,6 +107,8 @@ function () { */ public function assignPermission($permission) { + $this->deletePermissionCache(); + return $this->mapArray($permission, function ($permission) { $permissionId = $this->parsePermissionId($permission); @@ -120,6 +131,8 @@ public function assignPermission($permission) */ public function revokePermission($permission) { + $this->deletePermissionCache(); + return $this->mapArray($permission, function ($permission) { $permissionId = $this->parsePermissionId($permission); @@ -143,7 +156,7 @@ public function syncPermissions($permissions) return $sync; }); - + $this->deletePermissionCache(); return $this->permissions()->sync($sync); } @@ -154,6 +167,7 @@ public function syncPermissions($permissions) */ public function revokeAllPermissions() { + $this->deletePermissionCache(); return $this->permissions()->detach(); } @@ -165,6 +179,17 @@ public function revokeAllPermissions() */ + /** + * Delete cache for this traits. + * + * @return null + */ + protected function deletePermissionCache() + { + \Cache::forget('acl.getPermissionsById_'.$this->id); + \Cache::forget('acl.getMergeById_'.$this->id); + } + /** * Parses permission id from object or array. * diff --git a/src/Kodeine/Acl/Traits/HasRole.php b/src/Kodeine/Acl/Traits/HasRole.php index 17dfbe1..3ada514 100644 --- a/src/Kodeine/Acl/Traits/HasRole.php +++ b/src/Kodeine/Acl/Traits/HasRole.php @@ -117,11 +117,14 @@ public function hasRole($slug, $operator = null) */ public function assignRole($role) { + $this->deleteRoleCache(); + return $this->mapArray($role, function ($role) { $roleId = $this->parseRoleId($role); if ( ! $this->roles->keyBy('id')->has($roleId) ) { + $this->roles()->attach($roleId); return $role; @@ -139,6 +142,8 @@ public function assignRole($role) */ public function revokeRole($role) { + $this->deleteRoleCache(); + return $this->mapArray($role, function ($role) { $roleId = $this->parseRoleId($role); @@ -163,6 +168,8 @@ public function syncRoles($roles) return $sync; }); + $this->deleteRoleCache(); + return $this->roles()->sync($sync); } @@ -173,6 +180,8 @@ public function syncRoles($roles) */ public function revokeAllRoles() { + $this->deleteRoleCache(); + return $this->roles()->detach(); } @@ -183,6 +192,16 @@ public function revokeAllRoles() | */ + /** + * Delete cache for this traits. + * + * @return null + */ + protected function deleteRoleCache() + { + \Cache::forget('acl.getRolesById_'.$this->id); + } + /** * @param $slug * @param $roles