Skip to content

Commit 2ceadbb

Browse files
committed
fix: Remove user memoization
Backport of #2674
1 parent ce36c15 commit 2ceadbb

2 files changed

Lines changed: 17 additions & 44 deletions

File tree

lib/CurrentUser.php

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@
1717

1818
class CurrentUser {
1919

20-
/** @var string|null */
21-
protected $identifier = null;
22-
/** @var string|false|null */
23-
protected $cloudId = false;
24-
/** @var string|false|null */
25-
protected $sessionUser = false;
26-
2720
public function __construct(
2821
protected readonly IUserSession $userSession,
2922
protected readonly IRequest $request,
@@ -40,63 +33,46 @@ public function getUser(): ?IUser {
4033
* Get an identifier for the user, session or token
4134
*/
4235
public function getUserIdentifier(): string {
43-
if ($this->identifier !== null) {
44-
return $this->identifier;
45-
}
46-
4736
$uid = $this->getUID();
4837
if ($uid !== null) {
49-
$this->identifier = $uid;
50-
return $this->identifier;
38+
return $uid;
5139
}
5240

5341
$cloudId = $this->getCloudIDFromToken();
5442
if ($cloudId !== null) {
55-
$this->identifier = $cloudId;
56-
return $this->identifier;
43+
return $cloudId;
5744
}
5845

5946
$nickname = htmlspecialchars($this->request->getHeader('X-NC-Nickname'));
6047
if ($nickname !== '') {
61-
$this->identifier = $nickname . ' (' . $this->l10nFactory->get('comments')->t('remote user') . ')';
62-
return $this->identifier;
48+
return $nickname . ' (' . $this->l10nFactory->get('comments')->t('remote user') . ')';
6349
}
6450

6551
// Nothing worked, fallback to empty string
66-
$this->identifier = '';
67-
return $this->identifier;
52+
return '';
6853
}
6954

7055
/**
7156
* Get the current user id from the session
7257
*/
7358
public function getUID(): ?string {
74-
if ($this->sessionUser === false) {
75-
$user = $this->userSession->getUser();
76-
if ($user instanceof IUser) {
77-
$this->sessionUser = (string)$user->getUID();
78-
} else {
79-
$this->sessionUser = null;
80-
}
59+
$user = $this->userSession->getUser();
60+
if ($user instanceof IUser) {
61+
return $user->getUID();
8162
}
82-
83-
return $this->sessionUser;
63+
return null;
8464
}
8565

8666
/**
8767
* Get the current user cloud id from the session
8868
*/
8969
public function getCloudId(): ?string {
90-
if ($this->cloudId === false) {
91-
$user = $this->userSession->getUser();
92-
if ($user instanceof IUser) {
93-
$this->cloudId = (string)$user->getCloudId();
94-
} else {
95-
$this->cloudId = $this->getCloudIDFromToken();
96-
}
70+
$user = $this->userSession->getUser();
71+
if ($user instanceof IUser) {
72+
return $user->getCloudId();
73+
} else {
74+
return $this->getCloudIDFromToken();
9775
}
98-
99-
return $this->cloudId;
10076
}
10177

10278
/**

tests/CurrentUserTest.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,19 @@ protected function getInstance(array $methods = []): CurrentUser|MockObject {
8484

8585
public static function dataGetUserIdentifier(): array {
8686
return [
87-
[null, null, null, ''],
88-
[null, 'uid', '-1', 'uid'],
89-
[null, null, 'token', 'token'],
90-
['cached', -1, -1, 'cached'],
87+
[null, null, ''],
88+
['uid', '-1', 'uid'],
89+
[null, 'token', 'token'],
9190
];
9291
}
9392

9493
#[DataProvider('dataGetUserIdentifier')]
95-
public function testGetUserIdentifier(?string $cachedIdentifier, string|int|null $uidResult, ?string $tokenResult, string $expected): void {
94+
public function testGetUserIdentifier(string|int|null $uidResult, ?string $tokenResult, string $expected): void {
9695
$instance = $this->getInstance([
9796
'getUID',
9897
'getCloudIDFromToken',
9998
]);
10099

101-
self::invokePrivate($instance, 'identifier', [$cachedIdentifier]);
102-
103100
$instance->expects($uidResult !== -1 ? $this->once() : $this->never())
104101
->method('getUID')
105102
->willReturn($uidResult);

0 commit comments

Comments
 (0)