Skip to content

Commit

Permalink
fix: invalidate cached user groups
Browse files Browse the repository at this point in the history
Signed-off-by: Hugo Renard <hugo.renard@protonmail.com>
  • Loading branch information
hrenard committed Dec 12, 2024
1 parent f6d1d18 commit 0aefd68
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions lib/private/Group/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ class Manager extends PublicEmitter implements IGroupManager {
/** @var array<string, list<string>> */
private $cachedUserGroups = [];

/** @var bool */
private $cachedUserGroupsDirty = false;

/** @var \OC\SubAdmin */
private $subAdmin = null;

Expand All @@ -103,24 +106,39 @@ public function __construct(\OC\User\Manager $userManager,

$cachedGroups = &$this->cachedGroups;
$cachedUserGroups = &$this->cachedUserGroups;
$cachedUserGroupsDirty = &$this->cachedUserGroupsDirty;
$this->listen('\OC\Group', 'preAddUser', function ($group) use (&$cachedUserGroupsDirty) {
/**
* @var \OC\Group\Group $group
*/
$cachedUserGroupsDirty = true;
});
$this->listen('\OC\Group', 'preRemoveUser', function ($group) use (&$cachedUserGroupsDirty) {
/**
* @var \OC\Group\Group $group
*/
$cachedUserGroupsDirty = true;
});
$this->listen('\OC\Group', 'postDelete', function ($group) use (&$cachedGroups, &$cachedUserGroups) {
/**
* @var \OC\Group\Group $group
*/
unset($cachedGroups[$group->getGID()]);
$cachedUserGroups = [];
});
$this->listen('\OC\Group', 'postAddUser', function ($group) use (&$cachedUserGroups) {
$this->listen('\OC\Group', 'postAddUser', function ($group) use (&$cachedUserGroups, &$cachedUserGroupsDirty) {
/**
* @var \OC\Group\Group $group
*/
$cachedUserGroups = [];
$cachedUserGroupsDirty = false;
});
$this->listen('\OC\Group', 'postRemoveUser', function ($group) use (&$cachedUserGroups) {
$this->listen('\OC\Group', 'postRemoveUser', function ($group) use (&$cachedUserGroups, &$cachedUserGroupsDirty) {
/**
* @var \OC\Group\Group $group
*/
$cachedUserGroups = [];
$cachedUserGroupsDirty = false;
});
}

Expand Down Expand Up @@ -403,7 +421,7 @@ public function getUserGroupIds(IUser $user): array {
* @return string[]
*/
private function getUserIdGroupIds(string $uid): array {
if (!isset($this->cachedUserGroups[$uid])) {
if (!isset($this->cachedUserGroups[$uid]) || $this->cachedUserGroupsDirty) {
$groups = [];
foreach ($this->backends as $backend) {
if ($groupIds = $backend->getUserGroups($uid)) {
Expand Down

0 comments on commit 0aefd68

Please sign in to comment.