aboutsummaryrefslogtreecommitdiffstats
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
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
-rw-r--r--apps/federatedfilesharing/tests/AddressHandlerTest.php3
-rw-r--r--apps/federatedfilesharing/tests/Controller/MountPublicLinkControllerTest.php3
-rw-r--r--apps/federatedfilesharing/tests/FederatedShareProviderTest.php3
-rw-r--r--apps/files_sharing/tests/External/CacheTest.php4
-rw-r--r--apps/files_sharing/tests/External/ManagerTest.php3
-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
-rw-r--r--tests/lib/Collaboration/Collaborators/MailPluginTest.php4
-rw-r--r--tests/lib/Collaboration/Collaborators/RemotePluginTest.php3
-rw-r--r--tests/lib/Federation/CloudIdManagerTest.php36
12 files changed, 80 insertions, 32 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,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
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);