Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions lib/private/User/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,25 @@ public function clearBackends(): void {
* @param string $uid
* @return \OC\User\User|null Either the user or null if the specified user does not exist
*/
public function get($uid) {
public function get($uid, array $excludeBackends = []) {
if (is_null($uid) || $uid === '' || $uid === false) {
return null;
}
if (isset($this->cachedUsers[$uid])) { //check the cache first to prevent having to loop over the backends
return $this->cachedUsers[$uid];
}

if (strlen($uid) > IUser::MAX_USERID_LENGTH) {
return null;
}

// check the cache first to prevent having to loop over the backends
if ($excludeBackends === [] && isset($this->cachedUsers[$uid])) {
return $this->cachedUsers[$uid];
}

$cachedBackend = $this->cache->get(sha1($uid));
if (in_array($cachedBackend, $excludeBackends)) {
$cachedBackend = null;
}

if ($cachedBackend !== null && isset($this->backends[$cachedBackend])) {
// Cache has the info of the user backend already, so ask that one directly
$backend = $this->backends[$cachedBackend];
Expand All @@ -139,6 +145,10 @@ public function get($uid) {
continue;
}

if (in_array($i, $excludeBackends)) {
continue;
}

if ($backend->userExists($uid)) {
// Hash $uid to ensure that only valid characters are used for the cache key
$this->cache->set(sha1($uid), $i, 300);
Expand Down Expand Up @@ -182,12 +192,12 @@ public function getUserObject($uid, $backend, $cacheUser = true) {
* @param string $uid
* @return bool
*/
public function userExists($uid) {
public function userExists($uid, array $excludeBackends = []) {
if (strlen($uid) > IUser::MAX_USERID_LENGTH) {
return false;
}

$user = $this->get($uid);
$user = $this->get($uid, $excludeBackends);
return ($user !== null);
}

Expand Down
5 changes: 3 additions & 2 deletions lib/public/IUserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,14 @@ public function get($uid);
public function getDisplayName(string $uid): ?string;

/**
* check if a user exists
* Check if a user exists.
*
* @param string $uid
* @param list<string> $excludeBackends A list of IUserBackend::getBackendName() that need to be excluded from the search.
* @return bool
* @since 8.0.0
*/
public function userExists($uid);
public function userExists($uid, array $excludeBackends = []);

/**
* Check if the password is valid for the user
Expand Down
5 changes: 5 additions & 0 deletions tests/Core/Command/User/AddTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ public function testAddEmail(
$this->mailHelper->expects($isEmailValid && $shouldSendEmail ? static::once() : static::never())
->method('sendMail');

$this->consoleInput->method('getArgument')
->willReturnMap([
['uid', 'JohnDoe'],
]);

$this->consoleInput->method('getOption')
->willReturnMap([
['generate-password', 'true'],
Expand Down
Loading