Skip to content

Commit b95de4c

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

4 files changed

Lines changed: 23 additions & 10 deletions

File tree

lib/private/User/Manager.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,21 @@ public function clearBackends() {
121121
* @param string $uid
122122
* @return \OC\User\User|null Either the user or null if the specified user does not exist
123123
*/
124-
public function get($uid) {
124+
public function get($uid, array $excludeBackends = []) {
125125
if (is_null($uid) || $uid === '' || $uid === false) {
126126
return null;
127127
}
128-
if (isset($this->cachedUsers[$uid])) { //check the cache first to prevent having to loop over the backends
128+
129+
// check the cache first to prevent having to loop over the backends
130+
if ($excludeBackends === [] && isset($this->cachedUsers[$uid])) {
129131
return $this->cachedUsers[$uid];
130132
}
131133

132134
$cachedBackend = $this->cache->get(sha1($uid));
135+
if (in_array($cachedBackend, $excludeBackends)) {
136+
$cachedBackend = null;
137+
}
138+
133139
if ($cachedBackend !== null && isset($this->backends[$cachedBackend])) {
134140
// Cache has the info of the user backend already, so ask that one directly
135141
$backend = $this->backends[$cachedBackend];
@@ -144,6 +150,10 @@ public function get($uid) {
144150
continue;
145151
}
146152

153+
if (in_array($i, $excludeBackends)) {
154+
continue;
155+
}
156+
147157
if ($backend->userExists($uid)) {
148158
// Hash $uid to ensure that only valid characters are used for the cache key
149159
$this->cache->set(sha1($uid), $i, 300);
@@ -187,8 +197,8 @@ public function getUserObject($uid, $backend, $cacheUser = true) {
187197
* @param string $uid
188198
* @return bool
189199
*/
190-
public function userExists($uid) {
191-
$user = $this->get($uid);
200+
public function userExists($uid, array $excludeBackends = []) {
201+
$user = $this->get($uid, $excludeBackends);
192202
return ($user !== null);
193203
}
194204

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 = []): bool;
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
@@ -111,6 +111,11 @@ public function testAddEmail(
111111
$this->mailHelper->expects($isEmailValid && $shouldSendEmail ? static::once() : static::never())
112112
->method('sendMail');
113113

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

tests/lib/Share20/ManagerTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -985,10 +985,7 @@ public function dataGeneralChecks() {
985985
public function testGeneralChecks($share, $exceptionMessage, $exception) {
986986
$thrown = null;
987987

988-
$this->userManager->method('userExists')->willReturnMap([
989-
['user0', true],
990-
['user1', true],
991-
]);
988+
$this->userManager->method('userExists')->willReturnCallBack(fn (string $userId) => $userId === 'user0' || $userId === 'user1');
992989

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

0 commit comments

Comments
 (0)