Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose display name cache trough a public interface #32461

Merged
merged 2 commits into from
Aug 16, 2022
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
7 changes: 1 addition & 6 deletions lib/private/Files/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@
use Icewind\Streams\CallbackWrapper;
use OC\Files\Mount\MoveableMount;
use OC\Files\Storage\Storage;
use OC\User\DisplayNameCache;
use OC\User\LazyUser;
use OC\User\User;
use OCA\Files_Sharing\SharedMount;
use OCP\Constants;
use OCP\Files\Cache\ICacheEntry;
Expand Down Expand Up @@ -103,8 +101,6 @@ class View {

private LoggerInterface $logger;

private DisplayNameCache $displayNameCache;
icewind1991 marked this conversation as resolved.
Show resolved Hide resolved

/**
* @param string $root
* @throws \Exception If $root contains an invalid path
Expand All @@ -121,7 +117,6 @@ public function __construct($root = '') {
$this->lockingProvider = \OC::$server->getLockingProvider();
$this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider);
$this->userManager = \OC::$server->getUserManager();
$this->displayNameCache = \OC::$server->get(DisplayNameCache::class);
$this->logger = \OC::$server->get(LoggerInterface::class);
}

Expand Down Expand Up @@ -1319,7 +1314,7 @@ public function hasUpdated($path, $time) {
* @return IUser
*/
private function getUserObjectForOwner(string $ownerId) {
return new LazyUser($ownerId, $this->displayNameCache, $this->userManager);
return new LazyUser($ownerId, $this->userManager);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
use OC\Tagging\TagMapper;
use OC\Talk\Broker;
use OC\Template\JSCombiner;
use OC\User\DisplayNameCache;
use OC\User\Listeners\UserChangedListener;
use OC\User\Listeners\UserDeletedListener;
use OCA\Theming\ImageManager;
Expand Down Expand Up @@ -474,6 +475,10 @@ public function __construct($webRoot, \OC\Config $config) {
$this->registerDeprecatedAlias('UserManager', \OC\User\Manager::class);
$this->registerAlias(\OCP\IUserManager::class, \OC\User\Manager::class);

$this->registerService(DisplayNameCache::class, function (ContainerInterface $c) {
return $c->get(\OC\User\Manager::class)->getDisplayNameCache();
});

$this->registerService(\OCP\IGroupManager::class, function (ContainerInterface $c) {
$groupManager = new \OC\Group\Manager(
$this->get(IUserManager::class),
Expand Down
6 changes: 2 additions & 4 deletions lib/private/User/LazyUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@

class LazyUser implements IUser {
private ?IUser $user = null;
private DisplayNameCache $displayNameCache;
private string $uid;
private IUserManager $userManager;

public function __construct(string $uid, DisplayNameCache $displayNameCache, IUserManager $userManager) {
$this->displayNameCache = $displayNameCache;
public function __construct(string $uid, IUserManager $userManager) {
$this->uid = $uid;
$this->userManager = $userManager;
}
Expand All @@ -53,7 +51,7 @@ public function getUID() {
}

public function getDisplayName() {
return $this->displayNameCache->getDisplayName($this->uid);
return $this->userManager->getDisplayName($this->uid);
}

public function setDisplayName($displayName) {
Expand Down
11 changes: 11 additions & 0 deletions lib/private/User/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ class Manager extends PublicEmitter implements IUserManager {
/** @var IEventDispatcher */
private $eventDispatcher;

private DisplayNameCache $displayNameCache;

public function __construct(IConfig $config,
EventDispatcherInterface $oldDispatcher,
ICacheFactory $cacheFactory,
Expand All @@ -108,6 +110,7 @@ public function __construct(IConfig $config,
unset($cachedUsers[$user->getUID()]);
});
$this->eventDispatcher = $eventDispatcher;
$this->displayNameCache = new DisplayNameCache($cacheFactory, $this);
}

/**
Expand Down Expand Up @@ -185,6 +188,10 @@ public function get($uid) {
return null;
}

public function getDisplayName(string $uid): string {
return $this->displayNameCache->getDisplayName($uid);
}

/**
* get or construct the user object
*
Expand Down Expand Up @@ -742,4 +749,8 @@ private function verifyUid(string $uid): bool {

return !file_exists(rtrim($dataDirectory, '/') . '/' . $uid);
}

public function getDisplayNameCache(): DisplayNameCache {
return $this->displayNameCache;
}
}
11 changes: 11 additions & 0 deletions lib/public/IUserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ public function clearBackends() ;
*/
public function get($uid);

/**
* Get the display name of a user
*
* Note that this will return the uid if the user is not found instead of throwing an exception
*
* @param string $uid
* @return string
* @since 25.0.0
*/
public function getDisplayName(string $uid): string;

/**
* check if a user exists
*
Expand Down
1 change: 0 additions & 1 deletion tests/lib/User/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ protected function setUp(): void {
$this->cache = $this->createMock(ICache::class);

$this->cacheFactory->method('createDistributed')
->with('user_backend_map')
->willReturn($this->cache);
}

Expand Down