From e092f91c44d075a7eb4bdda76b761cfb9c26b18a Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 17 May 2022 15:20:28 +0200 Subject: [PATCH] expose displayname cache trough a public interface Signed-off-by: Robin Appelman --- lib/private/Files/View.php | 5 +---- lib/private/Server.php | 5 +++++ lib/private/User/LazyUser.php | 6 ++---- lib/private/User/Manager.php | 11 +++++++++++ lib/public/IUserManager.php | 11 +++++++++++ 5 files changed, 30 insertions(+), 8 deletions(-) diff --git a/lib/private/Files/View.php b/lib/private/Files/View.php index 2b6732e2ba057..abedb58d5877a 100644 --- a/lib/private/Files/View.php +++ b/lib/private/Files/View.php @@ -103,8 +103,6 @@ class View { private LoggerInterface $logger; - private DisplayNameCache $displayNameCache; - /** * @param string $root * @throws \Exception If $root contains an invalid path @@ -121,7 +119,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); } @@ -1319,7 +1316,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); } /** diff --git a/lib/private/Server.php b/lib/private/Server.php index 6e6fa43048972..cc855eb030451 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -151,6 +151,7 @@ use OC\Tagging\TagMapper; use OC\Talk\Broker; use OC\Template\JSCombiner; +use OC\User\DisplayNameCache; use OCA\Theming\ImageManager; use OCA\Theming\ThemingDefaults; use OCA\Theming\Util; @@ -472,6 +473,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), diff --git a/lib/private/User/LazyUser.php b/lib/private/User/LazyUser.php index 8b98b112731e4..30e44d570617c 100644 --- a/lib/private/User/LazyUser.php +++ b/lib/private/User/LazyUser.php @@ -28,12 +28,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; } @@ -52,7 +50,7 @@ public function getUID() { } public function getDisplayName() { - return $this->displayNameCache->getDisplayName($this->uid); + return $this->userManager->getDisplayName($this->uid); } public function setDisplayName($displayName) { diff --git a/lib/private/User/Manager.php b/lib/private/User/Manager.php index c59cbaa7b2055..5a5edbdbd2778 100644 --- a/lib/private/User/Manager.php +++ b/lib/private/User/Manager.php @@ -93,6 +93,8 @@ class Manager extends PublicEmitter implements IUserManager { /** @var IEventDispatcher */ private $eventDispatcher; + private DisplayNameCache $displayNameCache; + public function __construct(IConfig $config, EventDispatcherInterface $oldDispatcher, ICacheFactory $cacheFactory, @@ -106,6 +108,7 @@ public function __construct(IConfig $config, unset($cachedUsers[$user->getUID()]); }); $this->eventDispatcher = $eventDispatcher; + $this->displayNameCache = new DisplayNameCache($cacheFactory, $this); } /** @@ -183,6 +186,10 @@ public function get($uid) { return null; } + public function getDisplayName(string $uid): string { + return $this->displayNameCache->getDisplayName($uid); + } + /** * get or construct the user object * @@ -740,4 +747,8 @@ private function verifyUid(string $uid): bool { return !file_exists(rtrim($dataDirectory, '/') . '/' . $uid); } + + public function getDisplayNameCache(): DisplayNameCache { + return $this->displayNameCache; + } } diff --git a/lib/public/IUserManager.php b/lib/public/IUserManager.php index e5c220af40c60..da7d477ad4317 100644 --- a/lib/public/IUserManager.php +++ b/lib/public/IUserManager.php @@ -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 *