From fb27dffca8c4f82eaaf2bc9bebc5689ea8778288 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 1 Sep 2026 17:59:51 +0200 Subject: [PATCH] fix: populate cache when creating user Signed-off-by: Robin Appelman --- lib/UserBackend.php | 56 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/lib/UserBackend.php b/lib/UserBackend.php index d3114b4b..46a5d777 100644 --- a/lib/UserBackend.php +++ b/lib/UserBackend.php @@ -38,6 +38,7 @@ class UserBackend extends ABackend implements IGetRealUIDBackend, IPasswordHashBackend { + /** @var CappedMemoryCache */ private CappedMemoryCache $cache; private bool $allowListing = true; @@ -73,8 +74,11 @@ public function createUser(string $uid, string $password): bool { $result = $qb->executeStatement(); - // Clear cache - unset($this->cache[$uid]); + $this->cache[$uid] = [ + 'uid' => $uid, + 'displayname' => null, + 'email' => null, + ]; return (bool)$result; } @@ -98,7 +102,11 @@ public function deleteUser($uid): bool { $result = $query->executeStatement(); if (isset($this->cache[$uid])) { - unset($this->cache[$uid]); + $cached = $this->cache[$uid]; + if ($cached['email']) { + $this->cache[$cached['email']] = false; + } + $this->cache[$uid] = false; } return (bool)$result; @@ -109,7 +117,18 @@ public function setInitialEmail(string $uid, string $email): bool { $query->update('guests_users') ->set('email', $query->createNamedParameter($email)) ->where($query->expr()->eq('uid_lower', $query->createNamedParameter(mb_strtolower($uid)))); - return (bool)$query->executeStatement(); + $result = (bool)$query->executeStatement(); + if ($result && isset($this->cache[$uid])) { + $cached = $this->cache[$uid]; + if ($cached['email'] !== null) { + $this->cache[$cached['email']] = false; + } + + $cached['email'] = $email; + $this->cache[$uid] = $cached; + $this->cache[$email] = $cached; + } + return $result; } /** @@ -179,7 +198,14 @@ public function setDisplayName(string $uid, string $displayName): bool { ->where($query->expr()->eq('uid_lower', $query->createNamedParameter(mb_strtolower($uid)))); $query->executeStatement(); - $this->cache[$uid]['displayname'] = $displayName; + $cached = $this->cache[$uid]; + if ($cached) { + $cached['displayname'] = $displayName; + $this->cache[$uid] = $cached; + if ($cached['email']) { + $this->cache[$cached['email']] = $cached; + } + } return true; } @@ -338,7 +364,7 @@ private function loadUser($uid): bool { if (!isset($this->cache[$uid])) { $qb = $this->dbConn->getQueryBuilder(); - $qb->select('uid', 'displayname') + $qb->select('uid', 'displayname', 'email') ->from('guests_users') ->where( $qb->expr()->eq( @@ -358,9 +384,21 @@ private function loadUser($uid): bool { // "uid" is primary key, so there can only be a single result if ($row !== false) { - $this->cache[$uid] = []; - $this->cache[$uid]['uid'] = (string)$row['uid']; - $this->cache[$uid]['displayname'] = (string)$row['displayname']; + $email = $row['email']; + $realUid = (string)$row['uid']; + + $this->cache[$realUid] = [ + 'uid' => $realUid, + 'email' => $email, + 'displayname' => $row['displayname'], + ]; + if ($email) { + $this->cache[$email] = [ + 'uid' => $realUid, + 'email' => $email, + 'displayname' => $row['displayname'], + ]; + } } else { return false; }