Skip to content

Commit b6863b6

Browse files
Merge pull request #62916 from nextcloud/backport/62870/stable33
[stable33] feat: Allow to check user existence outside specific user backends
2 parents 19efd25 + caadd3d commit b6863b6

3 files changed

Lines changed: 24 additions & 8 deletions

File tree

‎lib/private/User/Manager.php‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,25 @@ public function clearBackends(): void {
112112
* @param string $uid
113113
* @return \OC\User\User|null Either the user or null if the specified user does not exist
114114
*/
115-
public function get($uid) {
115+
public function get($uid, array $excludeBackends = []) {
116116
if (is_null($uid) || $uid === '' || $uid === false) {
117117
return null;
118118
}
119-
if (isset($this->cachedUsers[$uid])) { //check the cache first to prevent having to loop over the backends
120-
return $this->cachedUsers[$uid];
121-
}
122119

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

124+
// check the cache first to prevent having to loop over the backends
125+
if ($excludeBackends === [] && isset($this->cachedUsers[$uid])) {
126+
return $this->cachedUsers[$uid];
127+
}
128+
127129
$cachedBackend = $this->cache->get(sha1($uid));
130+
if (in_array($cachedBackend, $excludeBackends)) {
131+
$cachedBackend = null;
132+
}
133+
128134
if ($cachedBackend !== null && isset($this->backends[$cachedBackend])) {
129135
// Cache has the info of the user backend already, so ask that one directly
130136
$backend = $this->backends[$cachedBackend];
@@ -139,6 +145,10 @@ public function get($uid) {
139145
continue;
140146
}
141147

148+
if (in_array($i, $excludeBackends)) {
149+
continue;
150+
}
151+
142152
if ($backend->userExists($uid)) {
143153
// Hash $uid to ensure that only valid characters are used for the cache key
144154
$this->cache->set(sha1($uid), $i, 300);
@@ -186,12 +196,12 @@ public function getUserObject($uid, $backend, $cacheUser = true) {
186196
* @param string $uid
187197
* @return bool
188198
*/
189-
public function userExists($uid) {
199+
public function userExists($uid, array $excludeBackends = []) {
190200
if (strlen($uid) > IUser::MAX_USERID_LENGTH) {
191201
return false;
192202
}
193203

194-
$user = $this->get($uid);
204+
$user = $this->get($uid, $excludeBackends);
195205
return ($user !== null);
196206
}
197207

‎lib/public/IUserManager.php‎

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

8383
/**
84-
* check if a user exists
84+
* Check if a user exists.
8585
*
8686
* @param string $uid
87+
* @param list<string> $excludeBackends A list of IUserBackend::getBackendName() that need to be excluded from the search.
8788
* @return bool
8889
* @since 8.0.0
8990
*/
90-
public function userExists($uid);
91+
public function userExists($uid, array $excludeBackends = []);
9192

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

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

0 commit comments

Comments
 (0)