Skip to content

Commit 08787ed

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

2 files changed

Lines changed: 23 additions & 20 deletions

File tree

lib/private/User/Manager.php

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -119,25 +119,29 @@ public function clearBackends(): void {
119119
}
120120

121121
/**
122-
* get a user by user id
123-
*
124-
* @param string $uid
125-
* @return User|null Either the user or null if the specified user does not exist
122+
* {@inheritDoc}
123+
* @param list<string> $excludeBackends A list of IUserBackend::getBackendName() that need to be excluded from the search.
126124
*/
127125
#[\Override]
128-
public function get($uid) {
129-
if (is_null($uid) || $uid === '' || $uid === false) {
126+
public function get(string $uid, array $excludeBackends = []): \OCP\IUser|null {
127+
if ($uid === '') {
130128
return null;
131129
}
132-
if (isset($this->cachedUsers[$uid])) { //check the cache first to prevent having to loop over the backends
133-
return $this->cachedUsers[$uid];
134-
}
135130

136131
if (strlen($uid) > IUser::MAX_USERID_LENGTH) {
137132
return null;
138133
}
139134

135+
// check the cache first to prevent having to loop over the backends
136+
if ($excludeBackends === [] && isset($this->cachedUsers[$uid])) {
137+
return $this->cachedUsers[$uid];
138+
}
139+
140140
$cachedBackend = $this->cache->get(sha1($uid));
141+
if (in_array($cachedBackend, $excludeBackends)) {
142+
$cachedBackend = null;
143+
}
144+
141145
if ($cachedBackend !== null && isset($this->backends[$cachedBackend])) {
142146
// Cache has the info of the user backend already, so ask that one directly
143147
$backend = $this->backends[$cachedBackend];
@@ -152,6 +156,10 @@ public function get($uid) {
152156
continue;
153157
}
154158

159+
if (in_array($i, $excludeBackends)) {
160+
continue;
161+
}
162+
155163
if ($backend->userExists($uid)) {
156164
// Hash $uid to ensure that only valid characters are used for the cache key
157165
$this->cache->set(sha1($uid), $i, 300);
@@ -194,19 +202,13 @@ public function getUserObject($uid, $backend, $cacheUser = true) {
194202
return $user;
195203
}
196204

197-
/**
198-
* check if a user exists
199-
*
200-
* @param string $uid
201-
* @return bool
202-
*/
203205
#[\Override]
204-
public function userExists($uid) {
206+
public function userExists(string $uid, array $excludeBackends = []): bool {
205207
if (strlen($uid) > IUser::MAX_USERID_LENGTH) {
206208
return false;
207209
}
208210

209-
$user = $this->get($uid);
211+
$user = $this->get($uid, $excludeBackends);
210212
return ($user !== null);
211213
}
212214

lib/public/IUserManager.php

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

7575
/**
7676
* Get the display name of a user
@@ -82,13 +82,14 @@ public function get($uid);
8282
public function getDisplayName(string $uid): ?string;
8383

8484
/**
85-
* check if a user exists
85+
* Check if a user exists.
8686
*
8787
* @param string $uid
88+
* @param list<string> $excludeBackends A list of IUserBackend::getBackendName() that need to be excluded from the search.
8889
* @return bool
8990
* @since 8.0.0
9091
*/
91-
public function userExists($uid);
92+
public function userExists(string $uid, array $excludeBackends = []): bool;
9293

9394
/**
9495
* Check if the password is valid for the user

0 commit comments

Comments
 (0)