diff options
author | Julius Härtl <jus@bitgrid.net> | 2021-07-09 08:00:03 +0200 |
---|---|---|
committer | Julius Härtl <jus@bitgrid.net> | 2021-07-14 16:08:05 +0200 |
commit | 7179002600ccde6d6757c068c9388430ed71a2f1 (patch) | |
tree | 5b0fd29678ce392db2764923d5f6bb393435d596 /lib | |
parent | f43c2b45d8177e8c924b8f97f80c92164a3d5412 (diff) | |
download | nextcloud-server-7179002600ccde6d6757c068c9388430ed71a2f1.tar.gz nextcloud-server-7179002600ccde6d6757c068c9388430ed71a2f1.zip |
Allow to get a local cloud id without going through the contacts manager
Signed-off-by: Julius Härtl <jus@bitgrid.net>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/Federation/CloudIdManager.php | 45 | ||||
-rw-r--r-- | lib/private/Server.php | 2 | ||||
-rw-r--r-- | lib/public/Federation/ICloudIdManager.php | 4 |
3 files changed, 37 insertions, 14 deletions
diff --git a/lib/private/Federation/CloudIdManager.php b/lib/private/Federation/CloudIdManager.php index 694f48eb1cc..24437456fd0 100644 --- a/lib/private/Federation/CloudIdManager.php +++ b/lib/private/Federation/CloudIdManager.php @@ -33,13 +33,21 @@ namespace OC\Federation; use OCP\Contacts\IManager; use OCP\Federation\ICloudId; use OCP\Federation\ICloudIdManager; +use OCP\IURLGenerator; +use OCP\IUserManager; class CloudIdManager implements ICloudIdManager { /** @var IManager */ private $contactsManager; + /** @var IURLGenerator */ + private $urlGenerator; + /** @var IUserManager */ + private $userManager; - public function __construct(IManager $contactsManager) { + public function __construct(IManager $contactsManager, IURLGenerator $urlGenerator, IUserManager $userManager) { $this->contactsManager = $contactsManager; + $this->urlGenerator = $urlGenerator; + $this->userManager = $userManager; } /** @@ -103,25 +111,40 @@ class CloudIdManager implements ICloudIdManager { /** * @param string $user - * @param string $remote + * @param string|null $remote * @return CloudId */ - public function getCloudId(string $user, string $remote): ICloudId { - // TODO check what the correct url is for remote (asking the remote) - $fixedRemote = $this->fixRemoteURL($remote); - if (strpos($fixedRemote, 'http://') === 0) { - $host = substr($fixedRemote, strlen('http://')); - } elseif (strpos($fixedRemote, 'https://') === 0) { - $host = substr($fixedRemote, strlen('https://')); + public function getCloudId(string $user, ?string $remote): ICloudId { + if ($remote === null) { + $remote = rtrim($this->removeProtocolFromUrl($this->urlGenerator->getAbsoluteURL('/')), '/'); + $fixedRemote = $this->fixRemoteURL($remote); + $localUser = $this->userManager->get($user); + $displayName = !is_null($localUser) ? $localUser->getDisplayName() : ''; } else { - $host = $fixedRemote; + // TODO check what the correct url is for remote (asking the remote) + $fixedRemote = $this->fixRemoteURL($remote); + $host = $this->removeProtocolFromUrl($fixedRemote); + $displayName = $this->getDisplayNameFromContact($user . '@' . $host); } $id = $user . '@' . $remote; - $displayName = $this->getDisplayNameFromContact($user . '@' . $host); return new CloudId($id, $user, $fixedRemote, $displayName); } /** + * @param string $url + * @return string + */ + private function removeProtocolFromUrl($url) { + if (strpos($url, 'https://') === 0) { + return substr($url, strlen('https://')); + } elseif (strpos($url, 'http://') === 0) { + return substr($url, strlen('http://')); + } + + return $url; + } + + /** * Strips away a potential file names and trailing slashes: * - http://localhost * - http://localhost/ diff --git a/lib/private/Server.php b/lib/private/Server.php index 03d6a4146ed..0320eda2b91 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -1286,7 +1286,7 @@ class Server extends ServerContainer implements IServerContainer { }); $this->registerService(ICloudIdManager::class, function (ContainerInterface $c) { - return new CloudIdManager($c->get(\OCP\Contacts\IManager::class)); + return new CloudIdManager($c->get(\OCP\Contacts\IManager::class), $c->get(IURLGenerator::class), $c->get(IUserManager::class)); }); $this->registerAlias(\OCP\GlobalScale\IConfig::class, \OC\GlobalScale\Config::class); diff --git a/lib/public/Federation/ICloudIdManager.php b/lib/public/Federation/ICloudIdManager.php index df7bd127baf..1612c03ba4a 100644 --- a/lib/public/Federation/ICloudIdManager.php +++ b/lib/public/Federation/ICloudIdManager.php @@ -46,12 +46,12 @@ interface ICloudIdManager { * Get the cloud id for a remote user * * @param string $user - * @param string $remote + * @param string|null $remote (optional since 23.0.0 for local users) * @return ICloudId * * @since 12.0.0 */ - public function getCloudId(string $user, string $remote): ICloudId; + public function getCloudId(string $user, ?string $remote): ICloudId; /** * Check if the input is a correctly formatted cloud id |