Skip to content

Commit 7d875e9

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

3 files changed

Lines changed: 25 additions & 9 deletions

File tree

lib/private/User/Manager.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,25 @@ public function clearBackends(): void {
110110
* @param string $uid
111111
* @return \OC\User\User|null Either the user or null if the specified user does not exist
112112
*/
113-
public function get($uid) {
113+
public function get($uid, array $excludeBackends = []): ?\OCP\IUser {
114114
if (is_null($uid) || $uid === '' || $uid === false) {
115115
return null;
116116
}
117-
if (isset($this->cachedUsers[$uid])) { //check the cache first to prevent having to loop over the backends
118-
return $this->cachedUsers[$uid];
119-
}
120117

121118
if (strlen($uid) > IUser::MAX_USERID_LENGTH) {
122119
return null;
123120
}
124121

122+
// check the cache first to prevent having to loop over the backends
123+
if ($excludeBackends === [] && isset($this->cachedUsers[$uid])) {
124+
return $this->cachedUsers[$uid];
125+
}
126+
125127
$cachedBackend = $this->cache->get(sha1($uid));
128+
if (in_array($cachedBackend, $excludeBackends)) {
129+
$cachedBackend = null;
130+
}
131+
126132
if ($cachedBackend !== null && isset($this->backends[$cachedBackend])) {
127133
// Cache has the info of the user backend already, so ask that one directly
128134
$backend = $this->backends[$cachedBackend];
@@ -137,6 +143,10 @@ public function get($uid) {
137143
continue;
138144
}
139145

146+
if (in_array($i, $excludeBackends)) {
147+
continue;
148+
}
149+
140150
if ($backend->userExists($uid)) {
141151
// Hash $uid to ensure that only valid characters are used for the cache key
142152
$this->cache->set(sha1($uid), $i, 300);
@@ -180,12 +190,12 @@ public function getUserObject($uid, $backend, $cacheUser = true) {
180190
* @param string $uid
181191
* @return bool
182192
*/
183-
public function userExists($uid) {
193+
public function userExists($uid, array $excludeBackends = []): bool {
184194
if (strlen($uid) > IUser::MAX_USERID_LENGTH) {
185195
return false;
186196
}
187197

188-
$user = $this->get($uid);
198+
$user = $this->get($uid, $excludeBackends);
189199
return ($user !== null);
190200
}
191201

lib/public/IUserManager.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function clearBackends();
6666
* @return \OCP\IUser|null Either the user or null if the specified user does not exist
6767
* @since 8.0.0
6868
*/
69-
public function get($uid);
69+
public function get($uid): ?\OCP\IUser;
7070

7171
/**
7272
* Get the display name of a user
@@ -78,13 +78,14 @@ public function get($uid);
7878
public function getDisplayName(string $uid): ?string;
7979

8080
/**
81-
* check if a user exists
81+
* Check if a user exists.
8282
*
8383
* @param string $uid
84+
* @param list<string> $excludeBackends A list of IUserBackend::getBackendName() that need to be excluded from the search.
8485
* @return bool
8586
* @since 8.0.0
8687
*/
87-
public function userExists($uid);
88+
public function userExists($uid, array $excludeBackends = []): bool;
8889

8990
/**
9091
* 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
@@ -109,6 +109,11 @@ public function testAddEmail(
109109
$this->mailHelper->expects($isEmailValid && $shouldSendEmail ? static::once() : static::never())
110110
->method('sendMail');
111111

112+
$this->consoleInput->method('getArgument')
113+
->willReturnMap([
114+
['uid', 'JohnDoe'],
115+
]);
116+
112117
$this->consoleInput->method('getOption')
113118
->willReturnMap([
114119
['generate-password', 'true'],

0 commit comments

Comments
 (0)