Skip to content

Commit

Permalink
Merge pull request #24318 from nextcloud/techdebt/noid/remove-oc_user…
Browse files Browse the repository at this point in the history
…-getDisplayName

Use proper methods for display name retrieval
  • Loading branch information
ChristophWurst authored Jun 17, 2021
2 parents 18ff261 + 2690481 commit b73f40e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 39 deletions.
24 changes: 14 additions & 10 deletions apps/files_sharing/lib/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,20 @@
use OCP\Files\Search\ISearchComparison;
use OCP\Files\Search\ISearchOperator;
use OCP\Files\StorageNotAvailableException;
use OCP\IUserManager;

/**
* Metadata cache for shared files
*
* 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;

Expand All @@ -63,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(
Expand Down Expand Up @@ -174,7 +172,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 = $this->userManager->get($uid);
if ($user) {
$this->ownerDisplayName = $user->getDisplayName();
} else {
$this->ownerDisplayName = $uid;
}
}
return $this->ownerDisplayName;
}
Expand Down
7 changes: 6 additions & 1 deletion apps/files_sharing/lib/SharedStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down
14 changes: 12 additions & 2 deletions lib/private/TemplateLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
use OCP\IConfig;
use OCP\IInitialStateService;
use OCP\INavigationManager;
use OCP\IUserSession;
use OCP\Support\Subscription\IRegistry;
use OCP\Util;

Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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) {
Expand Down
26 changes: 0 additions & 26 deletions lib/private/legacy/OC_User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down

0 comments on commit b73f40e

Please sign in to comment.