Skip to content

Commit e092f91

Browse files
committed
expose displayname cache trough a public interface
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent 83f831c commit e092f91

5 files changed

Lines changed: 30 additions & 8 deletions

File tree

lib/private/Files/View.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ class View {
103103

104104
private LoggerInterface $logger;
105105

106-
private DisplayNameCache $displayNameCache;
107-
108106
/**
109107
* @param string $root
110108
* @throws \Exception If $root contains an invalid path
@@ -121,7 +119,6 @@ public function __construct($root = '') {
121119
$this->lockingProvider = \OC::$server->getLockingProvider();
122120
$this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider);
123121
$this->userManager = \OC::$server->getUserManager();
124-
$this->displayNameCache = \OC::$server->get(DisplayNameCache::class);
125122
$this->logger = \OC::$server->get(LoggerInterface::class);
126123
}
127124

@@ -1319,7 +1316,7 @@ public function hasUpdated($path, $time) {
13191316
* @return IUser
13201317
*/
13211318
private function getUserObjectForOwner(string $ownerId) {
1322-
return new LazyUser($ownerId, $this->displayNameCache, $this->userManager);
1319+
return new LazyUser($ownerId, $this->userManager);
13231320
}
13241321

13251322
/**

lib/private/Server.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
use OC\Tagging\TagMapper;
152152
use OC\Talk\Broker;
153153
use OC\Template\JSCombiner;
154+
use OC\User\DisplayNameCache;
154155
use OCA\Theming\ImageManager;
155156
use OCA\Theming\ThemingDefaults;
156157
use OCA\Theming\Util;
@@ -472,6 +473,10 @@ public function __construct($webRoot, \OC\Config $config) {
472473
$this->registerDeprecatedAlias('UserManager', \OC\User\Manager::class);
473474
$this->registerAlias(\OCP\IUserManager::class, \OC\User\Manager::class);
474475

476+
$this->registerService(DisplayNameCache::class, function (ContainerInterface $c) {
477+
return $c->get(\OC\User\Manager::class)->getDisplayNameCache();
478+
});
479+
475480
$this->registerService(\OCP\IGroupManager::class, function (ContainerInterface $c) {
476481
$groupManager = new \OC\Group\Manager(
477482
$this->get(IUserManager::class),

lib/private/User/LazyUser.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,10 @@
2828

2929
class LazyUser implements IUser {
3030
private ?IUser $user = null;
31-
private DisplayNameCache $displayNameCache;
3231
private string $uid;
3332
private IUserManager $userManager;
3433

35-
public function __construct(string $uid, DisplayNameCache $displayNameCache, IUserManager $userManager) {
36-
$this->displayNameCache = $displayNameCache;
34+
public function __construct(string $uid, IUserManager $userManager) {
3735
$this->uid = $uid;
3836
$this->userManager = $userManager;
3937
}
@@ -52,7 +50,7 @@ public function getUID() {
5250
}
5351

5452
public function getDisplayName() {
55-
return $this->displayNameCache->getDisplayName($this->uid);
53+
return $this->userManager->getDisplayName($this->uid);
5654
}
5755

5856
public function setDisplayName($displayName) {

lib/private/User/Manager.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ class Manager extends PublicEmitter implements IUserManager {
9393
/** @var IEventDispatcher */
9494
private $eventDispatcher;
9595

96+
private DisplayNameCache $displayNameCache;
97+
9698
public function __construct(IConfig $config,
9799
EventDispatcherInterface $oldDispatcher,
98100
ICacheFactory $cacheFactory,
@@ -106,6 +108,7 @@ public function __construct(IConfig $config,
106108
unset($cachedUsers[$user->getUID()]);
107109
});
108110
$this->eventDispatcher = $eventDispatcher;
111+
$this->displayNameCache = new DisplayNameCache($cacheFactory, $this);
109112
}
110113

111114
/**
@@ -183,6 +186,10 @@ public function get($uid) {
183186
return null;
184187
}
185188

189+
public function getDisplayName(string $uid): string {
190+
return $this->displayNameCache->getDisplayName($uid);
191+
}
192+
186193
/**
187194
* get or construct the user object
188195
*
@@ -740,4 +747,8 @@ private function verifyUid(string $uid): bool {
740747

741748
return !file_exists(rtrim($dataDirectory, '/') . '/' . $uid);
742749
}
750+
751+
public function getDisplayNameCache(): DisplayNameCache {
752+
return $this->displayNameCache;
753+
}
743754
}

lib/public/IUserManager.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ public function clearBackends() ;
8484
*/
8585
public function get($uid);
8686

87+
/**
88+
* Get the display name of a user
89+
*
90+
* Note that this will return the uid if the user is not found instead of throwing an exception
91+
*
92+
* @param string $uid
93+
* @return string
94+
* @since 25.0.0
95+
*/
96+
public function getDisplayName(string $uid): string;
97+
8798
/**
8899
* check if a user exists
89100
*

0 commit comments

Comments
 (0)