From 8004aa77212eed6dd1142c713275a4e0c5d0c40e Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Tue, 16 Aug 2022 14:10:05 +0200 Subject: [PATCH] Make DisplayNameCache return null if user doesn't exists Signed-off-by: Carl Schwan --- apps/files_sharing/lib/Cache.php | 2 +- lib/private/User/DisplayNameCache.php | 4 ++-- lib/private/User/LazyUser.php | 2 +- lib/private/User/Manager.php | 2 +- lib/public/IUserManager.php | 6 ++---- 5 files changed, 7 insertions(+), 9 deletions(-) diff --git a/apps/files_sharing/lib/Cache.php b/apps/files_sharing/lib/Cache.php index 9f11431008f35..707b64b79d737 100644 --- a/apps/files_sharing/lib/Cache.php +++ b/apps/files_sharing/lib/Cache.php @@ -170,7 +170,7 @@ protected function formatCacheEntry($entry, $path = null) { private function getOwnerDisplayName() { if (!$this->ownerDisplayName) { $uid = $this->storage->getOwner(''); - $this->ownerDisplayName = $this->displayNameCache->getDisplayName($uid); + $this->ownerDisplayName = $this->displayNameCache->getDisplayName($uid) ?? $uid; } return $this->ownerDisplayName; } diff --git a/lib/private/User/DisplayNameCache.php b/lib/private/User/DisplayNameCache.php index 22a79863e4978..5d1cc8940d760 100644 --- a/lib/private/User/DisplayNameCache.php +++ b/lib/private/User/DisplayNameCache.php @@ -47,7 +47,7 @@ public function __construct(ICacheFactory $cacheFactory, IUserManager $userManag $this->userManager = $userManager; } - public function getDisplayName(string $userId) { + public function getDisplayName(string $userId): ?string { if (isset($this->cache[$userId])) { return $this->cache[$userId]; } @@ -61,7 +61,7 @@ public function getDisplayName(string $userId) { if ($user) { $displayName = $user->getDisplayName(); } else { - $displayName = $userId; + $displayName = null; } $this->cache[$userId] = $displayName; $this->memCache->set($userId, $displayName, 60 * 10); // 10 minutes diff --git a/lib/private/User/LazyUser.php b/lib/private/User/LazyUser.php index 118dd3d0699bb..096578b8f375d 100644 --- a/lib/private/User/LazyUser.php +++ b/lib/private/User/LazyUser.php @@ -51,7 +51,7 @@ public function getUID() { } public function getDisplayName() { - return $this->userManager->getDisplayName($this->uid); + return $this->userManager->getDisplayName($this->uid) ?? $this->uid; } public function setDisplayName($displayName) { diff --git a/lib/private/User/Manager.php b/lib/private/User/Manager.php index 55ac663f3ecae..be5151313c4c8 100644 --- a/lib/private/User/Manager.php +++ b/lib/private/User/Manager.php @@ -188,7 +188,7 @@ public function get($uid) { return null; } - public function getDisplayName(string $uid): string { + public function getDisplayName(string $uid): ?string { return $this->displayNameCache->getDisplayName($uid); } diff --git a/lib/public/IUserManager.php b/lib/public/IUserManager.php index c107b2c5025aa..af0d5f08809a8 100644 --- a/lib/public/IUserManager.php +++ b/lib/public/IUserManager.php @@ -87,13 +87,11 @@ 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 + * @return string|null * @since 25.0.0 */ - public function getDisplayName(string $uid): string; + public function getDisplayName(string $uid): ?string; /** * check if a user exists