Skip to content

Commit 01a43d9

Browse files
CarlSchwanbackportbot[bot]
authored andcommitted
feat: Allow to check user existence outside specific user backends
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
1 parent b579a10 commit 01a43d9

5 files changed

Lines changed: 31 additions & 25 deletions

File tree

lib/private/Share20/Manager.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,15 @@ protected function verifyPassword(?string $password): void {
157157
* @suppress PhanUndeclaredClassMethod
158158
*/
159159
protected function generalChecks(IShare $share): void {
160+
$shareWith = $share->getSharedWith();
160161
if ($share->getShareType() === IShare::TYPE_USER) {
161162
// We expect a valid user as sharedWith for user shares
162-
if (!$this->userManager->userExists($share->getSharedWith())) {
163+
if ($shareWith === null || !$this->userManager->userExists($shareWith)) {
163164
throw new \InvalidArgumentException($this->l->t('Share recipient is not a valid user'));
164165
}
165166
} elseif ($share->getShareType() === IShare::TYPE_GROUP) {
166167
// We expect a valid group as sharedWith for group shares
167-
if (!$this->groupManager->groupExists($share->getSharedWith())) {
168+
if ($shareWith === null || !$this->groupManager->groupExists($shareWith)) {
168169
throw new \InvalidArgumentException($this->l->t('Share recipient is not a valid group'));
169170
}
170171
} elseif ($share->getShareType() === IShare::TYPE_LINK) {

lib/private/User/Manager.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -118,25 +118,29 @@ public function clearBackends(): void {
118118
}
119119

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

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

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

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

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

208-
$user = $this->get($uid);
210+
$user = $this->get($uid, $excludeBackends);
209211
return ($user !== null);
210212
}
211213

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(string $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
@@ -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'],

tests/lib/Share20/ManagerTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,10 +1195,7 @@ public function testGeneralChecks(array $shareParams, ?string $exceptionMessage,
11951195

11961196
$thrown = null;
11971197

1198-
$this->userManager->method('userExists')->willReturnMap([
1199-
['user0', true],
1200-
['user1', true],
1201-
]);
1198+
$this->userManager->method('userExists')->willReturnCallBack(fn (string $userId) => $userId === 'user0' || $userId === 'user1');
12021199

12031200
$user0 = $this->createMock(IUser::class);
12041201
$user0

0 commit comments

Comments
 (0)