Skip to content
Merged
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
4 changes: 4 additions & 0 deletions lib/private/User/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -833,4 +833,8 @@ public function getSeenUsers(int $offset = 0, ?int $limit = null): \Iterator {
}
} while (count($userIds) === $batchSize && $limit !== 0);
}

public function getExistingUser(string $userId, ?string $displayName = null): IUser {
return new LazyUser($userId, $this, $displayName);
}
}
18 changes: 18 additions & 0 deletions lib/public/IUserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public function clearBackends();
/**
* 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
Expand Down Expand Up @@ -246,4 +249,19 @@ public function getLastLoggedInUsers(?int $limit = null, int $offset = 0, string
* @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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would call it getLazyUser.
getExistingUser confuses me, to me it sounds like it will check that the user exists because I want to make sure it exists, not that it will skip the check because I already did it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intentionally avoided "lazy" in the naming because that's an implementation details, from an API consumer POV the main difference is whether or not the existence is verified.

Open for other options though

}
22 changes: 22 additions & 0 deletions tests/lib/User/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -740,4 +740,26 @@ public function testGetByEmail(): void {
$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