From 7179002600ccde6d6757c068c9388430ed71a2f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Fri, 9 Jul 2021 08:00:03 +0200 Subject: [PATCH] Allow to get a local cloud id without going through the contacts manager MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- .../tests/AddressHandlerTest.php | 3 +- .../MountPublicLinkControllerTest.php | 3 +- .../tests/FederatedShareProviderTest.php | 3 +- .../tests/External/CacheTest.php | 4 +- .../tests/External/ManagerTest.php | 3 +- lib/private/Federation/CloudIdManager.php | 45 ++++++++++++++----- lib/private/Server.php | 2 +- lib/public/Federation/ICloudIdManager.php | 4 +- .../Collaborators/MailPluginTest.php | 4 +- .../Collaborators/RemotePluginTest.php | 3 +- tests/lib/Federation/CloudIdManagerTest.php | 36 ++++++++++----- 11 files changed, 79 insertions(+), 31 deletions(-) diff --git a/apps/federatedfilesharing/tests/AddressHandlerTest.php b/apps/federatedfilesharing/tests/AddressHandlerTest.php index f21d29ebd7e..13030e73cb0 100644 --- a/apps/federatedfilesharing/tests/AddressHandlerTest.php +++ b/apps/federatedfilesharing/tests/AddressHandlerTest.php @@ -32,6 +32,7 @@ use OCA\FederatedFileSharing\AddressHandler; use OCP\Contacts\IManager; use OCP\IL10N; use OCP\IURLGenerator; +use OCP\IUserManager; class AddressHandlerTest extends \Test\TestCase { /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */ @@ -59,7 +60,7 @@ class AddressHandlerTest extends \Test\TestCase { $this->contactsManager = $this->createMock(IManager::class); - $this->cloudIdManager = new CloudIdManager($this->contactsManager); + $this->cloudIdManager = new CloudIdManager($this->contactsManager, $this->urlGenerator, $this->createMock(IUserManager::class)); $this->addressHandler = new AddressHandler($this->urlGenerator, $this->il10n, $this->cloudIdManager); } diff --git a/apps/federatedfilesharing/tests/Controller/MountPublicLinkControllerTest.php b/apps/federatedfilesharing/tests/Controller/MountPublicLinkControllerTest.php index d3a87a4c416..ff979c23d2a 100644 --- a/apps/federatedfilesharing/tests/Controller/MountPublicLinkControllerTest.php +++ b/apps/federatedfilesharing/tests/Controller/MountPublicLinkControllerTest.php @@ -41,6 +41,7 @@ use OCP\Http\Client\IClientService; use OCP\IL10N; use OCP\IRequest; use OCP\ISession; +use OCP\IURLGenerator; use OCP\IUserManager; use OCP\IUserSession; use OCP\Share\IManager; @@ -106,7 +107,7 @@ class MountPublicLinkControllerTest extends \Test\TestCase { $this->userSession = $this->getMockBuilder(IUserSession::class)->disableOriginalConstructor()->getMock(); $this->clientService = $this->getMockBuilder('OCP\Http\Client\IClientService')->disableOriginalConstructor()->getMock(); $this->contactsManager = $this->createMock(IContactsManager::class); - $this->cloudIdManager = new CloudIdManager($this->contactsManager); + $this->cloudIdManager = new CloudIdManager($this->contactsManager, $this->createMock(IURLGenerator::class), $this->userManager); $this->controller = new MountPublicLinkController( 'federatedfilesharing', $this->request, diff --git a/apps/federatedfilesharing/tests/FederatedShareProviderTest.php b/apps/federatedfilesharing/tests/FederatedShareProviderTest.php index a3253323769..831b9b59b54 100644 --- a/apps/federatedfilesharing/tests/FederatedShareProviderTest.php +++ b/apps/federatedfilesharing/tests/FederatedShareProviderTest.php @@ -46,6 +46,7 @@ use OCP\IConfig; use OCP\IDBConnection; use OCP\IL10N; use OCP\ILogger; +use OCP\IURLGenerator; use OCP\IUserManager; use OCP\Share\IManager; use OCP\Share\IShare; @@ -115,7 +116,7 @@ class FederatedShareProviderTest extends \Test\TestCase { //$this->addressHandler = new AddressHandler(\OC::$server->getURLGenerator(), $this->l); $this->addressHandler = $this->getMockBuilder('OCA\FederatedFileSharing\AddressHandler')->disableOriginalConstructor()->getMock(); $this->contactsManager = $this->createMock(IContactsManager::class); - $this->cloudIdManager = new CloudIdManager($this->contactsManager); + $this->cloudIdManager = new CloudIdManager($this->contactsManager, $this->createMock(IURLGenerator::class), $this->userManager); $this->gsConfig = $this->createMock(\OCP\GlobalScale\IConfig::class); $this->userManager->expects($this->any())->method('userExists')->willReturn(true); diff --git a/apps/files_sharing/tests/External/CacheTest.php b/apps/files_sharing/tests/External/CacheTest.php index cbac5907d07..c538f7dd760 100644 --- a/apps/files_sharing/tests/External/CacheTest.php +++ b/apps/files_sharing/tests/External/CacheTest.php @@ -31,6 +31,8 @@ use OC\Federation\CloudIdManager; use OCA\Files_Sharing\Tests\TestCase; use OCP\Contacts\IManager; use OCP\Federation\ICloudIdManager; +use OCP\IURLGenerator; +use OCP\IUserManager; /** * Class Cache @@ -66,7 +68,7 @@ class CacheTest extends TestCase { $this->contactsManager = $this->createMock(IManager::class); - $this->cloudIdManager = new CloudIdManager($this->contactsManager); + $this->cloudIdManager = new CloudIdManager($this->contactsManager, $this->createMock(IURLGenerator::class), $this->createMock(IUserManager::class)); $this->remoteUser = $this->getUniqueID('remoteuser'); $this->storage = $this->getMockBuilder('\OCA\Files_Sharing\External\Storage') diff --git a/apps/files_sharing/tests/External/ManagerTest.php b/apps/files_sharing/tests/External/ManagerTest.php index 0098f67b2fb..facfcbd1ee6 100644 --- a/apps/files_sharing/tests/External/ManagerTest.php +++ b/apps/files_sharing/tests/External/ManagerTest.php @@ -42,6 +42,7 @@ use OCP\Federation\ICloudFederationProviderManager; use OCP\Http\Client\IClientService; use OCP\Http\Client\IResponse; use OCP\IGroupManager; +use OCP\IURLGenerator; use OCP\IUserManager; use OCP\Share\IShare; use Test\Traits\UserTrait; @@ -131,7 +132,7 @@ class ManagerTest extends TestCase { $this->testMountProvider = new MountProvider(\OC::$server->getDatabaseConnection(), function () { return $this->manager; - }, new CloudIdManager($this->contactsManager)); + }, new CloudIdManager($this->contactsManager, $this->createMock(IURLGenerator::class), $this->userManager)); } private function setupMounts() { 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,24 +111,39 @@ 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 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 diff --git a/tests/lib/Collaboration/Collaborators/MailPluginTest.php b/tests/lib/Collaboration/Collaborators/MailPluginTest.php index 3128231a108..ad18666e0ae 100644 --- a/tests/lib/Collaboration/Collaborators/MailPluginTest.php +++ b/tests/lib/Collaboration/Collaborators/MailPluginTest.php @@ -32,7 +32,9 @@ use OCP\Contacts\IManager; use OCP\Federation\ICloudIdManager; use OCP\IConfig; use OCP\IGroupManager; +use OCP\IURLGenerator; use OCP\IUser; +use OCP\IUserManager; use OCP\IUserSession; use OCP\Share\IShare; use Test\TestCase; @@ -70,7 +72,7 @@ class MailPluginTest extends TestCase { $this->groupManager = $this->createMock(IGroupManager::class); $this->knownUserService = $this->createMock(KnownUserService::class); $this->userSession = $this->createMock(IUserSession::class); - $this->cloudIdManager = new CloudIdManager($this->contactsManager); + $this->cloudIdManager = new CloudIdManager($this->contactsManager, $this->createMock(IURLGenerator::class), $this->createMock(IUserManager::class)); $this->searchResult = new SearchResult(); } diff --git a/tests/lib/Collaboration/Collaborators/RemotePluginTest.php b/tests/lib/Collaboration/Collaborators/RemotePluginTest.php index 981260a80dd..4072f3ecde1 100644 --- a/tests/lib/Collaboration/Collaborators/RemotePluginTest.php +++ b/tests/lib/Collaboration/Collaborators/RemotePluginTest.php @@ -30,6 +30,7 @@ use OCP\Collaboration\Collaborators\SearchResultType; use OCP\Contacts\IManager; use OCP\Federation\ICloudIdManager; use OCP\IConfig; +use OCP\IURLGenerator; use OCP\IUser; use OCP\IUserManager; use OCP\IUserSession; @@ -62,7 +63,7 @@ class RemotePluginTest extends TestCase { $this->userManager = $this->createMock(IUserManager::class); $this->config = $this->createMock(IConfig::class); $this->contactsManager = $this->createMock(IManager::class); - $this->cloudIdManager = new CloudIdManager($this->contactsManager); + $this->cloudIdManager = new CloudIdManager($this->contactsManager, $this->createMock(IURLGenerator::class), $this->createMock(IUserManager::class)); $this->searchResult = new SearchResult(); } diff --git a/tests/lib/Federation/CloudIdManagerTest.php b/tests/lib/Federation/CloudIdManagerTest.php index dd68abf0ecb..92f8a5fa8dd 100644 --- a/tests/lib/Federation/CloudIdManagerTest.php +++ b/tests/lib/Federation/CloudIdManagerTest.php @@ -23,20 +23,29 @@ namespace Test\Federation; use OC\Federation\CloudIdManager; use OCP\Contacts\IManager; +use OCP\IURLGenerator; +use OCP\IUserManager; use Test\TestCase; class CloudIdManagerTest extends TestCase { /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */ protected $contactsManager; + /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */ + private $urlGenerator; + /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */ + private $userManager; /** @var CloudIdManager */ private $cloudIdManager; + protected function setUp(): void { parent::setUp(); $this->contactsManager = $this->createMock(IManager::class); + $this->urlGenerator = $this->createMock(IURLGenerator::class); + $this->userManager = $this->createMock(IUserManager::class); - $this->cloudIdManager = new CloudIdManager($this->contactsManager); + $this->cloudIdManager = new CloudIdManager($this->contactsManager, $this->urlGenerator, $this->userManager); } public function cloudIdProvider() { @@ -104,6 +113,7 @@ class CloudIdManagerTest extends TestCase { return [ ['test', 'example.com', 'test@example.com'], ['test@example.com', 'example.com', 'test@example.com@example.com'], + ['test@example.com', null, 'test@example.com@example.com'], ]; } @@ -115,15 +125,21 @@ class CloudIdManagerTest extends TestCase { * @param string $id */ public function testGetCloudId($user, $remote, $id) { - $this->contactsManager->expects($this->any()) - ->method('search') - ->with($id, ['CLOUD']) - ->willReturn([ - [ - 'CLOUD' => [$id], - 'FN' => 'Ample Ex', - ] - ]); + if ($remote !== null) { + $this->contactsManager->expects($this->any()) + ->method('search') + ->with($id, ['CLOUD']) + ->willReturn([ + [ + 'CLOUD' => [$id], + 'FN' => 'Ample Ex', + ] + ]); + } else { + $this->urlGenerator->expects(self::once()) + ->method('getAbsoluteUrl') + ->willReturn('https://example.com'); + } $cloudId = $this->cloudIdManager->getCloudId($user, $remote);