Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 47 additions & 9 deletions lib/UserBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class UserBackend extends ABackend implements
IGetRealUIDBackend,
IPasswordHashBackend {

/** @var CappedMemoryCache<array{'uid': string, 'displayname': ?string, 'email': ?string}|false> */
private CappedMemoryCache $cache;

private bool $allowListing = true;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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(
Expand All @@ -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;
}
Expand Down
Loading