Skip to content

Commit 0ab3781

Browse files
committed
Fix LDAP LoginListener by adding new group relationships to caches before firing the event
Signed-off-by: Côme Chilliet <[email protected]>
1 parent f46c3d5 commit 0ab3781

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

apps/user_ldap/lib/Group_LDAP.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@
5656
class Group_LDAP extends ABackend implements GroupInterface, IGroupLDAP, IGetDisplayNameBackend, IDeleteGroupBackend {
5757
protected bool $enabled = false;
5858

59-
/** @var CappedMemoryCache<string[]> $cachedGroupMembers array of users with gid as key */
59+
/** @var CappedMemoryCache<string[]> $cachedGroupMembers array of user DN with gid as key */
6060
protected CappedMemoryCache $cachedGroupMembers;
61-
/** @var CappedMemoryCache<array[]> $cachedGroupsByMember array of groups with uid as key */
61+
/** @var CappedMemoryCache<array[]> $cachedGroupsByMember array of groups with user DN as key */
6262
protected CappedMemoryCache $cachedGroupsByMember;
6363
/** @var CappedMemoryCache<string[]> $cachedNestedGroups array of groups with gid (DN) as key */
6464
protected CappedMemoryCache $cachedNestedGroups;
@@ -1357,4 +1357,35 @@ public function getDisplayName(string $gid): string {
13571357
public function dn2GroupName(string $dn): string|false {
13581358
return $this->access->dn2groupname($dn);
13591359
}
1360+
1361+
public function addRelationshipToCaches(string $uid, ?string $dnUser, string $gid): void {
1362+
$dnGroup = $this->access->groupname2dn($gid);
1363+
$dnUser ??= $this->access->username2dn($uid);
1364+
if ($dnUser === false || $dnGroup === false) {
1365+
return;
1366+
}
1367+
if (isset($this->cachedGroupMembers[$gid])) {
1368+
$this->cachedGroupMembers[$gid] = array_merge($this->cachedGroupMembers[$gid], [$dnUser]);
1369+
}
1370+
unset($this->cachedGroupsByMember[$dnUser]);
1371+
unset($this->cachedNestedGroups[$gid]);
1372+
$cacheKey = 'inGroup' . $uid . ':' . $gid;
1373+
$this->access->connection->writeToCache($cacheKey, true);
1374+
$cacheKeyMembers = 'inGroup-members:' . $gid;
1375+
if (!is_null($data = $this->access->connection->getFromCache($cacheKeyMembers))) {
1376+
$this->access->connection->writeToCache($cacheKeyMembers, array_merge($data, [$dnUser]));
1377+
}
1378+
$cacheKey = '_groupMembers' . $dnGroup;
1379+
if (!is_null($data = $this->access->connection->getFromCache($cacheKey))) {
1380+
$this->access->connection->writeToCache($cacheKey, array_merge($data, [$dnUser]));
1381+
}
1382+
$cacheKey = 'getUserGroups' . $uid;
1383+
if (!is_null($data = $this->access->connection->getFromCache($cacheKey))) {
1384+
$this->access->connection->writeToCache($cacheKey, array_merge($data, [$gid]));
1385+
}
1386+
// These cache keys cannot be easily updated:
1387+
// $cacheKey = 'usersInGroup-' . $gid . '-' . $search . '-' . $limit . '-' . $offset;
1388+
// $cacheKey = 'usersInGroup-' . $gid . '-' . $search;
1389+
// $cacheKey = 'countUsersInGroup-' . $gid . '-' . $search;
1390+
}
13601391
}

apps/user_ldap/lib/Group_Proxy.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,4 +384,8 @@ public function getBackendName(): string {
384384
public function searchInGroup(string $gid, string $search = '', int $limit = -1, int $offset = 0): array {
385385
return $this->handleRequest($gid, 'searchInGroup', [$gid, $search, $limit, $offset]);
386386
}
387+
388+
public function addRelationshipToCaches(string $uid, ?string $dnUser, string $gid): void {
389+
$this->handleRequest($gid, 'addRelationshipToCaches', [$uid, $dnUser, $gid]);
390+
}
387391
}

apps/user_ldap/lib/LoginListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ private function updateGroups(IUser $userObject): void {
9393
continue;
9494
}
9595
$this->groupMembershipMapper->insert(GroupMembership::fromParams(['groupid' => $groupId,'userid' => $userId]));
96-
// TODO: empty cache to avoid crash
96+
$this->groupBackend->addRelationshipToCaches($userId, null, $groupId);
9797
$this->dispatcher->dispatchTyped(new UserAddedEvent($groupObject, $userObject));
9898
$this->logger->info(
9999
__CLASS__ . ' – {user} added to {group}',

0 commit comments

Comments
 (0)