Skip to content

Commit 4b16af9

Browse files
committed
feat: Allow to check user existence outside specific user backends
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
1 parent ca4ce5a commit 4b16af9

4 files changed

Lines changed: 25 additions & 12 deletions

File tree

lib/private/User/Manager.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,25 @@ public function clearBackends() {
126126
* @param string $uid
127127
* @return \OC\User\User|null Either the user or null if the specified user does not exist
128128
*/
129-
public function get($uid) {
129+
public function get($uid, array $excludeBackends = []) {
130130
if (is_null($uid) || $uid === '' || $uid === false) {
131131
return null;
132132
}
133-
if (isset($this->cachedUsers[$uid])) { //check the cache first to prevent having to loop over the backends
134-
return $this->cachedUsers[$uid];
135-
}
136133

137134
if (strlen($uid) > self::MAX_USERID_LENGTH) {
138135
return null;
139136
}
140137

138+
// check the cache first to prevent having to loop over the backends
139+
if ($excludeBackends === [] && isset($this->cachedUsers[$uid])) {
140+
return $this->cachedUsers[$uid];
141+
}
142+
141143
$cachedBackend = $this->cache->get(sha1($uid));
144+
if (in_array($cachedBackend, $excludeBackends)) {
145+
$cachedBackend = null;
146+
}
147+
142148
if ($cachedBackend !== null && isset($this->backends[$cachedBackend])) {
143149
// Cache has the info of the user backend already, so ask that one directly
144150
$backend = $this->backends[$cachedBackend];
@@ -153,6 +159,10 @@ public function get($uid) {
153159
continue;
154160
}
155161

162+
if (in_array($i, $excludeBackends)) {
163+
continue;
164+
}
165+
156166
if ($backend->userExists($uid)) {
157167
// Hash $uid to ensure that only valid characters are used for the cache key
158168
$this->cache->set(sha1($uid), $i, 300);
@@ -196,12 +206,12 @@ public function getUserObject($uid, $backend, $cacheUser = true) {
196206
* @param string $uid
197207
* @return bool
198208
*/
199-
public function userExists($uid) {
209+
public function userExists($uid, array $excludeBackends = []) {
200210
if (strlen($uid) > self::MAX_USERID_LENGTH) {
201211
return false;
202212
}
203213

204-
$user = $this->get($uid);
214+
$user = $this->get($uid, $excludeBackends);
205215
return ($user !== null);
206216
}
207217

lib/public/IUserManager.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,14 @@ public function get($uid);
7777
public function getDisplayName(string $uid): ?string;
7878

7979
/**
80-
* check if a user exists
80+
* Check if a user exists.
8181
*
8282
* @param string $uid
83+
* @param list<string> $excludeBackends A list of IUserBackend::getBackendName() that need to be excluded from the search.
8384
* @return bool
8485
* @since 8.0.0
8586
*/
86-
public function userExists($uid);
87+
public function userExists($uid, array $excludeBackends = []);
8788

8889
/**
8990
* Check if the password is valid for the user

tests/Core/Command/User/AddTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ public function testAddEmail(
110110
$this->mailHelper->expects($isEmailValid && $shouldSendEmail ? static::once() : static::never())
111111
->method('sendMail');
112112

113+
$this->consoleInput->method('getArgument')
114+
->willReturnMap([
115+
['uid', 'JohnDoe'],
116+
]);
117+
113118
$this->consoleInput->method('getOption')
114119
->will(static::returnValueMap([
115120
['generate-password', 'true'],

tests/lib/Share20/ManagerTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -993,10 +993,7 @@ public function dataGeneralChecks() {
993993
public function testGeneralChecks($share, $exceptionMessage, $exception): void {
994994
$thrown = null;
995995

996-
$this->userManager->method('userExists')->willReturnMap([
997-
['user0', true],
998-
['user1', true],
999-
]);
996+
$this->userManager->method('userExists')->willReturnCallBack(fn (string $userId) => $userId === 'user0' || $userId === 'user1');
1000997

1001998
$this->groupManager->method('groupExists')->willReturnMap([
1002999
['group0', true],

0 commit comments

Comments
 (0)