aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorblizzz <blizzz@arthur-schiwon.de>2021-07-14 23:18:51 +0200
committerGitHub <noreply@github.com>2021-07-14 23:18:51 +0200
commit5a22b0ac6729079f22342befd75fa9a5265469cf (patch)
treeb501755a14dc892c041eccd0ddbfc26fb7c092b3 /lib
parent81722d5655c96ab192918b0e088da59a20c51dff (diff)
parent7179002600ccde6d6757c068c9388430ed71a2f1 (diff)
downloadnextcloud-server-5a22b0ac6729079f22342befd75fa9a5265469cf.tar.gz
nextcloud-server-5a22b0ac6729079f22342befd75fa9a5265469cf.zip
Merge pull request #27884 from nextcloud/enh/cloud-id-local
Allow to get a local cloud id without going through the contacts manager
Diffstat (limited to 'lib')
-rw-r--r--lib/private/Federation/CloudIdManager.php45
-rw-r--r--lib/private/Server.php2
-rw-r--r--lib/private/User/User.php2
-rw-r--r--lib/public/Federation/ICloudIdManager.php4
4 files changed, 38 insertions, 15 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/private/User/User.php b/lib/private/User/User.php
index cae698300d9..b588ca1a0ee 100644
--- a/lib/private/User/User.php
+++ b/lib/private/User/User.php
@@ -466,7 +466,7 @@ class User implements IUser {
$uid = $this->getUID();
$server = $this->urlGenerator->getAbsoluteURL('/');
$server = rtrim($this->removeProtocolFromUrl($server), '/');
- return \OC::$server->getCloudIdManager()->getCloudId($uid, $server)->getId();
+ return $uid . '@' . $server;
}
/**
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