Skip to content

Commit 808245e

Browse files
CarlSchwanbackportbot[bot]
authored andcommitted
feat: Allow to check user existence outside specific user backends
feat: Allow to check user existence outside specific user backends Signed-off-by: Carl Schwan <carl@carlschwan.eu> [skip ci]
1 parent 0131bd9 commit 808245e

5 files changed

Lines changed: 26 additions & 13 deletions

File tree

lib/private/Share20/Manager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,12 @@ protected function verifyPassword(?string $password): void {
142142
protected function generalCreateChecks(IShare $share, bool $isUpdate = false): void {
143143
if ($share->getShareType() === IShare::TYPE_USER) {
144144
// We expect a valid user as sharedWith for user shares
145-
if (!$this->userManager->userExists($share->getSharedWith())) {
145+
if ($shareWith === null || !$this->userManager->userExists($shareWith)) {
146146
throw new \InvalidArgumentException($this->l->t('Share recipient is not a valid user'));
147147
}
148148
} elseif ($share->getShareType() === IShare::TYPE_GROUP) {
149149
// We expect a valid group as sharedWith for group shares
150-
if (!$this->groupManager->groupExists($share->getSharedWith())) {
150+
if ($shareWith === null || !$this->groupManager->groupExists($shareWith)) {
151151
throw new \InvalidArgumentException($this->l->t('Share recipient is not a valid group'));
152152
}
153153
} elseif ($share->getShareType() === IShare::TYPE_LINK) {

lib/private/User/Manager.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,21 @@ public function get($uid) {
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);
@@ -187,7 +197,7 @@ public function userExists($uid) {
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(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
@@ -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'],

tests/lib/Share20/ManagerTest.php

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

11631163
$thrown = null;
11641164

1165-
$this->userManager->method('userExists')->willReturnMap([
1166-
['user0', true],
1167-
['user1', true],
1168-
]);
1165+
$this->userManager->method('userExists')->willReturnCallBack(fn (string $userId) => $userId === 'user0' || $userId === 'user1');
11691166

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

0 commit comments

Comments
 (0)