diff options
author | Arthur Schiwon <blizzz@arthur-schiwon.de> | 2020-11-16 17:56:44 +0100 |
---|---|---|
committer | Arthur Schiwon <blizzz@arthur-schiwon.de> | 2020-11-24 11:30:11 +0100 |
commit | 16a78f535a3b607864e0d151de13b0f161520f5c (patch) | |
tree | a9024ddbfa76b51e123177628e17ccb840821880 /lib/private/Federation/CloudIdManager.php | |
parent | 6d3d6fb81a9b52700f77463fb3fa9084b03132f5 (diff) | |
download | nextcloud-server-16a78f535a3b607864e0d151de13b0f161520f5c.tar.gz nextcloud-server-16a78f535a3b607864e0d151de13b0f161520f5c.zip |
set the display name of federated sharees from addressbook
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
Diffstat (limited to 'lib/private/Federation/CloudIdManager.php')
-rw-r--r-- | lib/private/Federation/CloudIdManager.php | 48 |
1 files changed, 37 insertions, 11 deletions
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)); } /** |