aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private
diff options
context:
space:
mode:
authorArthur Schiwon <blizzz@arthur-schiwon.de>2020-11-16 17:56:44 +0100
committerArthur Schiwon <blizzz@arthur-schiwon.de>2020-11-24 22:52:31 +0100
commitc9814c716ac9eb4f29d6e975bed3d55e6f8002d3 (patch)
treecc70fe1de4f2df52774f4e63adec257f8461dc7a /lib/private
parent56cee9e405ed2c9ae9bcaa18b4d1b6f03ce9c684 (diff)
downloadnextcloud-server-c9814c716ac9eb4f29d6e975bed3d55e6f8002d3.tar.gz
nextcloud-server-c9814c716ac9eb4f29d6e975bed3d55e6f8002d3.zip
set the display name of federated sharees from addressbook
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
Diffstat (limited to 'lib/private')
-rw-r--r--lib/private/Federation/CloudId.php10
-rw-r--r--lib/private/Federation/CloudIdManager.php48
-rw-r--r--lib/private/Server.php2
3 files changed, 47 insertions, 13 deletions
diff --git a/lib/private/Federation/CloudId.php b/lib/private/Federation/CloudId.php
index 80e174589b0..c917a9d5816 100644
--- a/lib/private/Federation/CloudId.php
+++ b/lib/private/Federation/CloudId.php
@@ -36,6 +36,8 @@ class CloudId implements ICloudId {
private $user;
/** @var string */
private $remote;
+ /** @var string|null */
+ private $displayName;
/**
* CloudId constructor.
@@ -44,10 +46,11 @@ class CloudId implements ICloudId {
* @param string $user
* @param string $remote
*/
- public function __construct(string $id, string $user, string $remote) {
+ public function __construct(string $id, string $user, string $remote, ?string $displayName = null) {
$this->id = $id;
$this->user = $user;
$this->remote = $remote;
+ $this->displayName = $displayName;
}
/**
@@ -60,6 +63,11 @@ class CloudId implements ICloudId {
}
public function getDisplayId(): string {
+ if ($this->displayName) {
+ $atPos = strrpos($this->getId(), '@');
+ $atHost = substr($this->getId(), $atPos);
+ return $this->displayName . $atHost;
+ }
return str_replace('https://', '', str_replace('http://', '', $this->getId()));
}
diff --git a/lib/private/Federation/CloudIdManager.php b/lib/private/Federation/CloudIdManager.php
index a5ebc98c1fd..43bc0d86a38 100644
--- a/lib/private/Federation/CloudIdManager.php
+++ b/lib/private/Federation/CloudIdManager.php
@@ -29,10 +29,18 @@ declare(strict_types=1);
namespace OC\Federation;
+use OCP\Contacts\IManager;
use OCP\Federation\ICloudId;
use OCP\Federation\ICloudIdManager;
class CloudIdManager implements ICloudIdManager {
+ /** @var IManager */
+ private $contactsManager;
+
+ public function __construct(IManager $contactsManager) {
+ $this->contactsManager = $contactsManager;
+ }
+
/**
* @param string $cloudId
* @return ICloudId
@@ -60,23 +68,32 @@ class CloudIdManager implements ICloudIdManager {
$invalidPos = min($posSlash, $posColon);
}
- // Find the last @ before $invalidPos
- $pos = $lastAtPos = 0;
- while ($lastAtPos !== false && $lastAtPos <= $invalidPos) {
- $pos = $lastAtPos;
- $lastAtPos = strpos($id, '@', $pos + 1);
- }
+ $lastValidAtPos = strrpos($id, '@', $invalidPos - strlen($id));
- if ($pos !== false) {
- $user = substr($id, 0, $pos);
- $remote = substr($id, $pos + 1);
+ if ($lastValidAtPos !== false) {
+ $user = substr($id, 0, $lastValidAtPos);
+ $remote = substr($id, $lastValidAtPos + 1);
if (!empty($user) && !empty($remote)) {
- return new CloudId($id, $user, $remote);
+ return new CloudId($id, $user, $remote, $this->getDisplayNameFromContact($id));
}
}
throw new \InvalidArgumentException('Invalid cloud id');
}
+ protected function getDisplayNameFromContact(string $cloudId): ?string {
+ $addressBookEntries = $this->contactsManager->search($cloudId, ['CLOUD']);
+ foreach ($addressBookEntries as $entry) {
+ if (isset($entry['CLOUD'])) {
+ foreach ($entry['CLOUD'] as $cloudID) {
+ if ($cloudID === $cloudId) {
+ return $entry['FN'];
+ }
+ }
+ }
+ }
+ return null;
+ }
+
/**
* @param string $user
* @param string $remote
@@ -84,7 +101,16 @@ class CloudIdManager implements ICloudIdManager {
*/
public function getCloudId(string $user, string $remote): ICloudId {
// TODO check what the correct url is for remote (asking the remote)
- return new CloudId($user. '@' . $remote, $user, $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://'));
+ } else {
+ $host = $fixedRemote;
+ }
+ $id = $user . '@' . $remote;
+ return new CloudId($id, $user, $fixedRemote, $this->getDisplayNameFromContact($id));
}
/**
diff --git a/lib/private/Server.php b/lib/private/Server.php
index f446f831d5d..a0cb83b0a59 100644
--- a/lib/private/Server.php
+++ b/lib/private/Server.php
@@ -1242,7 +1242,7 @@ class Server extends ServerContainer implements IServerContainer {
});
$this->registerService(ICloudIdManager::class, function (Server $c) {
- return new CloudIdManager();
+ return new CloudIdManager($c->get(\OCP\Contacts\IManager::class));
});
$this->registerAlias(\OCP\GlobalScale\IConfig::class, \OC\GlobalScale\Config::class);