Skip to content

Commit caed435

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

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
@@ -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 = []): \OC\User\User|null {
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);
@@ -182,12 +192,12 @@ public function getUserObject($uid, $backend, $cacheUser = true) {
182192
* @param string $uid
183193
* @return bool
184194
*/
185-
public function userExists($uid) {
195+
public function userExists($uid, array $excludeBackends = []): bool {
186196
if (strlen($uid) > IUser::MAX_USERID_LENGTH) {
187197
return false;
188198
}
189199

190-
$user = $this->get($uid);
200+
$user = $this->get($uid, $excludeBackends);
191201
return ($user !== null);
192202
}
193203

lib/public/IUserManager.php

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

7474
/**
7575
* Get the display name of a user
@@ -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 = []): bool;
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)