From 095418493ef909d25808d278ee099fb34070c7fb Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Mon, 23 Nov 2020 23:51:19 +0100 Subject: [PATCH 1/2] Use proper methods for display name retrieval Signed-off-by: Morris Jobke --- apps/files_sharing/lib/Cache.php | 9 ++++++++- lib/private/TemplateLayout.php | 14 ++++++++++++-- lib/private/legacy/OC_User.php | 26 -------------------------- 3 files changed, 20 insertions(+), 29 deletions(-) diff --git a/apps/files_sharing/lib/Cache.php b/apps/files_sharing/lib/Cache.php index 25e92d23962fa..ab8adda725ea1 100644 --- a/apps/files_sharing/lib/Cache.php +++ b/apps/files_sharing/lib/Cache.php @@ -38,6 +38,7 @@ use OCP\Files\Search\ISearchComparison; use OCP\Files\Search\ISearchOperator; use OCP\Files\StorageNotAvailableException; +use OCP\IUserManager; /** * Metadata cache for shared files @@ -174,7 +175,13 @@ protected function formatCacheEntry($entry, $path = null) { private function getOwnerDisplayName() { if (!$this->ownerDisplayName) { - $this->ownerDisplayName = \OC_User::getDisplayName($this->storage->getOwner('')); + $uid = $this->storage->getOwner(''); + $user = \OC::$server->get(IUserManager::class)->get($uid); + if ($user) { + $this->ownerDisplayName = $user->getDisplayName(); + } else { + $this->ownerDisplayName = $uid; + } } return $this->ownerDisplayName; } diff --git a/lib/private/TemplateLayout.php b/lib/private/TemplateLayout.php index 82bd0fd22e703..16d4423838bcf 100644 --- a/lib/private/TemplateLayout.php +++ b/lib/private/TemplateLayout.php @@ -52,6 +52,7 @@ use OCP\IConfig; use OCP\IInitialStateService; use OCP\INavigationManager; +use OCP\IUserSession; use OCP\Support\Subscription\IRegistry; use OCP\Util; @@ -121,7 +122,12 @@ public function __construct($renderAs, $appId = '') { break; } } - $userDisplayName = \OC_User::getDisplayName(); + + $userDisplayName = false; + $user = \OC::$server->get(IUserSession::class)->getUser(); + if ($user) { + $userDisplayName = $user->getDisplayName(); + } $this->assign('user_displayname', $userDisplayName); $this->assign('user_uid', \OC_User::getUser()); @@ -152,7 +158,11 @@ public function __construct($renderAs, $appId = '') { \OC_Util::addStyle('guest'); $this->assign('bodyid', 'body-login'); - $userDisplayName = \OC_User::getDisplayName(); + $userDisplayName = false; + $user = \OC::$server->get(IUserSession::class)->getUser(); + if ($user) { + $userDisplayName = $user->getDisplayName(); + } $this->assign('user_displayname', $userDisplayName); $this->assign('user_uid', \OC_User::getUser()); } elseif ($renderAs === TemplateResponse::RENDER_AS_PUBLIC) { diff --git a/lib/private/legacy/OC_User.php b/lib/private/legacy/OC_User.php index dd97fb63e335c..f955c5c6938aa 100644 --- a/lib/private/legacy/OC_User.php +++ b/lib/private/legacy/OC_User.php @@ -328,32 +328,6 @@ public static function getUser() { } } - /** - * get the display name of the user currently logged in. - * - * @param string $uid - * @return string|bool uid or false - * @deprecated 8.1.0 fetch \OCP\IUser (has getDisplayName()) by using method - * get() of \OCP\IUserManager - \OC::$server->getUserManager() - */ - public static function getDisplayName($uid = null) { - if ($uid) { - $user = \OC::$server->getUserManager()->get($uid); - if ($user) { - return $user->getDisplayName(); - } else { - return $uid; - } - } else { - $user = \OC::$server->getUserSession()->getUser(); - if ($user) { - return $user->getDisplayName(); - } else { - return false; - } - } - } - /** * Set password * From 2690481cbad009b74292fdfbd028fc8b5b01a394 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Sun, 29 Nov 2020 21:45:06 +0100 Subject: [PATCH 2/2] Use DI for files_sharing Cache Signed-off-by: Morris Jobke --- apps/files_sharing/lib/Cache.php | 17 +++++++---------- apps/files_sharing/lib/SharedStorage.php | 7 ++++++- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/apps/files_sharing/lib/Cache.php b/apps/files_sharing/lib/Cache.php index ab8adda725ea1..8729426221b93 100644 --- a/apps/files_sharing/lib/Cache.php +++ b/apps/files_sharing/lib/Cache.php @@ -46,15 +46,12 @@ * don't use this class directly if you need to get metadata, use \OC\Files\Filesystem::getFileInfo instead */ class Cache extends CacheJail { - /** - * @var \OCA\Files_Sharing\SharedStorage - */ + /** @var \OCA\Files_Sharing\SharedStorage */ private $storage; - - /** - * @var ICacheEntry - */ + /** @var ICacheEntry */ private $sourceRootInfo; + /** @var IUserManager */ + private $userManager; private $rootUnchanged = true; @@ -64,11 +61,11 @@ class Cache extends CacheJail { /** * @param \OCA\Files_Sharing\SharedStorage $storage - * @param ICacheEntry $sourceRootInfo */ - public function __construct($storage, ICacheEntry $sourceRootInfo) { + public function __construct($storage, ICacheEntry $sourceRootInfo, IUserManager $userManager) { $this->storage = $storage; $this->sourceRootInfo = $sourceRootInfo; + $this->userManager = $userManager; $this->numericId = $sourceRootInfo->getStorageId(); parent::__construct( @@ -176,7 +173,7 @@ protected function formatCacheEntry($entry, $path = null) { private function getOwnerDisplayName() { if (!$this->ownerDisplayName) { $uid = $this->storage->getOwner(''); - $user = \OC::$server->get(IUserManager::class)->get($uid); + $user = $this->userManager->get($uid); if ($user) { $this->ownerDisplayName = $user->getDisplayName(); } else { diff --git a/apps/files_sharing/lib/SharedStorage.php b/apps/files_sharing/lib/SharedStorage.php index 662c5ad365152..f4a525ce871d3 100644 --- a/apps/files_sharing/lib/SharedStorage.php +++ b/apps/files_sharing/lib/SharedStorage.php @@ -43,6 +43,7 @@ use OCP\Files\NotFoundException; use OCP\Files\Storage\IDisableEncryptionStorage; use OCP\Files\Storage\IStorage; +use OCP\IUserManager; use OCP\Lock\ILockingProvider; use OCP\Share\IShare; @@ -385,7 +386,11 @@ public function getCache($path = '', $storage = null) { return new FailedCache(); } - $this->cache = new \OCA\Files_Sharing\Cache($storage, $sourceRoot, $this->superShare); + $this->cache = new \OCA\Files_Sharing\Cache( + $storage, + $sourceRoot, + \OC::$server->get(IUserManager::class) + ); return $this->cache; }