Skip to content

Commit 6eb8e8d

Browse files
authored
Merge pull request nextcloud#57519 from nextcloud/get-existing-api
feat: add api to get a user object without verifying they exist
2 parents a4cf714 + 5924a2e commit 6eb8e8d

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

lib/private/User/Manager.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,4 +833,8 @@ public function getSeenUsers(int $offset = 0, ?int $limit = null): \Iterator {
833833
}
834834
} while (count($userIds) === $batchSize && $limit !== 0);
835835
}
836+
837+
public function getExistingUser(string $userId, ?string $displayName = null): IUser {
838+
return new LazyUser($userId, $this, $displayName);
839+
}
836840
}

lib/public/IUserManager.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public function clearBackends();
6262
/**
6363
* get a user by user id
6464
*
65+
* If you're already 100% sure that the user exists,
66+
* consider IUserManager::getExistingUser which has less overhead.
67+
*
6568
* @param string $uid
6669
* @return \OCP\IUser|null Either the user or null if the specified user does not exist
6770
* @since 8.0.0
@@ -248,4 +251,19 @@ public function getLastLoggedInUsers(?int $limit = null, int $offset = 0, string
248251
* @since 32.0.0
249252
*/
250253
public function getSeenUsers(int $offset = 0, ?int $limit = null): \Iterator;
254+
255+
/**
256+
* Get a user by user id without validating that the user exists.
257+
*
258+
* This should only be used if you're certain that the provided user id exists in the system.
259+
* Using this to get a user object for a non-existing user will lead to unexpected behavior down the line.
260+
*
261+
* If you're not 100% sure that the user exists, use IUserManager::get instead.
262+
*
263+
* @param string $userId
264+
* @param ?string $displayName If the display name is known in advance you can provide it so it doesn't have to be fetched again
265+
* @return IUser
266+
* @since 33.0.0
267+
*/
268+
public function getExistingUser(string $userId, ?string $displayName = null): IUser;
251269
}

tests/lib/User/ManagerTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,4 +740,26 @@ public function testGetByEmail(): void {
740740
$this->assertEquals('uid1', $users[0]->getUID());
741741
$this->assertEquals('uid2', $users[1]->getUID());
742742
}
743+
744+
public function testGetExistingUser() {
745+
$backend = $this->createMock(\Test\Util\User\Dummy::class);
746+
$backend->method('userExists')
747+
->with('foobar')
748+
->willReturn(true);
749+
$backend->method('getDisplayName')
750+
->willReturn('Foo Bar');
751+
$backend->method('implementsActions')
752+
->willReturnCallback(fn (int $action) => $action === Backend::GET_DISPLAYNAME);
753+
754+
$manager = new Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
755+
$manager->registerBackend($backend);
756+
757+
$user = $manager->getExistingUser('foobar');
758+
$this->assertEquals('foobar', $user->getUID());
759+
$this->assertEquals('Foo Bar', $user->getDisplayName());
760+
761+
$user = $manager->getExistingUser('nobody', 'None');
762+
$this->assertEquals('nobody', $user->getUID());
763+
$this->assertEquals('None', $user->getDisplayName());
764+
}
743765
}

0 commit comments

Comments
 (0)