Skip to content

Commit 4d5a97b

Browse files
authored
Merge pull request #2674 from nextcloud/bugfix/fix-user-memoization
Fix: Remove user memoization
2 parents 55af841 + 1673f42 commit 4d5a97b

2 files changed

Lines changed: 13 additions & 40 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 IUserSession $userSession,
2922
protected IRequest $request,
@@ -41,65 +34,48 @@ public function getUser(): ?IUser {
4134
* @return string
4235
*/
4336
public function getUserIdentifier() {
44-
if ($this->identifier !== null) {
45-
return $this->identifier;
46-
}
47-
4837
$uid = $this->getUID();
4938
if ($uid !== null) {
50-
$this->identifier = $uid;
51-
return $this->identifier;
39+
return $uid;
5240
}
5341

5442
$cloudId = $this->getCloudIDFromToken();
5543
if ($cloudId !== null) {
56-
$this->identifier = $cloudId;
57-
return $this->identifier;
44+
return $cloudId;
5845
}
5946

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

6652
// Nothing worked, fallback to empty string
67-
$this->identifier = '';
68-
return $this->identifier;
53+
return '';
6954
}
7055

7156
/**
7257
* Get the current user id from the session
7358
* @return string|null
7459
*/
7560
public function getUID() {
76-
if ($this->sessionUser === false) {
77-
$user = $this->userSession->getUser();
78-
if ($user instanceof IUser) {
79-
$this->sessionUser = $user->getUID();
80-
} else {
81-
$this->sessionUser = null;
82-
}
61+
$user = $this->userSession->getUser();
62+
if ($user instanceof IUser) {
63+
return (string)$user->getUID();
8364
}
84-
85-
return $this->sessionUser;
65+
return null;
8666
}
8767

8868
/**
8969
* Get the current user cloud id from the session
9070
* @return string|null
9171
*/
9272
public function getCloudId() {
93-
if ($this->cloudId === false) {
94-
$user = $this->userSession->getUser();
95-
if ($user instanceof IUser) {
96-
$this->cloudId = (string)$user->getCloudId();
97-
} else {
98-
$this->cloudId = $this->getCloudIDFromToken();
99-
}
73+
$user = $this->userSession->getUser();
74+
if ($user instanceof IUser) {
75+
return (string)$user->getCloudId();
76+
} else {
77+
return $this->getCloudIDFromToken();
10078
}
101-
102-
return $this->cloudId;
10379
}
10480

10581
/**

tests/CurrentUserTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ public static function dataGetUserIdentifier(): array {
8585
[null, null, null, ''],
8686
[null, 'uid', -1, 'uid'],
8787
[null, null, 'token', 'token'],
88-
['cached', -1, -1, 'cached'],
8988
];
9089
}
9190

@@ -96,8 +95,6 @@ public function testGetUserIdentifier(?string $cachedIdentifier, string|int|null
9695
'getCloudIDFromToken',
9796
]);
9897

99-
self::invokePrivate($instance, 'identifier', [$cachedIdentifier]);
100-
10198
$instance->expects($uidResult !== -1 ? $this->once() : $this->never())
10299
->method('getUID')
103100
->willReturn($uidResult);

0 commit comments

Comments
 (0)