mirror of
https://github.com/nextcloud/server.git
synced 2026-02-20 00:12:30 -05:00
feat: add api to get a user object without verifying they exist
Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
parent
408f9e8256
commit
5924a2e3ec
3 changed files with 44 additions and 0 deletions
|
|
@ -833,4 +833,8 @@ class Manager extends PublicEmitter implements IUserManager {
|
|||
}
|
||||
} while (count($userIds) === $batchSize && $limit !== 0);
|
||||
}
|
||||
|
||||
public function getExistingUser(string $userId, ?string $displayName = null): IUser {
|
||||
return new LazyUser($userId, $this, $displayName);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -62,6 +62,9 @@ interface IUserManager {
|
|||
/**
|
||||
* get a user by user id
|
||||
*
|
||||
* If you're already 100% sure that the user exists,
|
||||
* consider IUserManager::getExistingUser which has less overhead.
|
||||
*
|
||||
* @param string $uid
|
||||
* @return \OCP\IUser|null Either the user or null if the specified user does not exist
|
||||
* @since 8.0.0
|
||||
|
|
@ -246,4 +249,19 @@ interface IUserManager {
|
|||
* @since 32.0.0
|
||||
*/
|
||||
public function getSeenUsers(int $offset = 0, ?int $limit = null): \Iterator;
|
||||
|
||||
/**
|
||||
* Get a user by user id without validating that the user exists.
|
||||
*
|
||||
* This should only be used if you're certain that the provided user id exists in the system.
|
||||
* Using this to get a user object for a non-existing user will lead to unexpected behavior down the line.
|
||||
*
|
||||
* If you're not 100% sure that the user exists, use IUserManager::get instead.
|
||||
*
|
||||
* @param string $userId
|
||||
* @param ?string $displayName If the display name is known in advance you can provide it so it doesn't have to be fetched again
|
||||
* @return IUser
|
||||
* @since 33.0.0
|
||||
*/
|
||||
public function getExistingUser(string $userId, ?string $displayName = null): IUser;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -740,4 +740,26 @@ class ManagerTest extends TestCase {
|
|||
$this->assertEquals('uid1', $users[0]->getUID());
|
||||
$this->assertEquals('uid2', $users[1]->getUID());
|
||||
}
|
||||
|
||||
public function testGetExistingUser() {
|
||||
$backend = $this->createMock(\Test\Util\User\Dummy::class);
|
||||
$backend->method('userExists')
|
||||
->with('foobar')
|
||||
->willReturn(true);
|
||||
$backend->method('getDisplayName')
|
||||
->willReturn('Foo Bar');
|
||||
$backend->method('implementsActions')
|
||||
->willReturnCallback(fn (int $action) => $action === Backend::GET_DISPLAYNAME);
|
||||
|
||||
$manager = new Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
|
||||
$manager->registerBackend($backend);
|
||||
|
||||
$user = $manager->getExistingUser('foobar');
|
||||
$this->assertEquals('foobar', $user->getUID());
|
||||
$this->assertEquals('Foo Bar', $user->getDisplayName());
|
||||
|
||||
$user = $manager->getExistingUser('nobody', 'None');
|
||||
$this->assertEquals('nobody', $user->getUID());
|
||||
$this->assertEquals('None', $user->getDisplayName());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue