Skip to content

Commit 7179002

Browse files
committed
Allow to get a local cloud id without going through the contacts manager
Signed-off-by: Julius Härtl <[email protected]>
1 parent f43c2b4 commit 7179002

File tree

11 files changed

+79
-31
lines changed

11 files changed

+79
-31
lines changed

apps/federatedfilesharing/tests/AddressHandlerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use OCP\Contacts\IManager;
3333
use OCP\IL10N;
3434
use OCP\IURLGenerator;
35+
use OCP\IUserManager;
3536

3637
class AddressHandlerTest extends \Test\TestCase {
3738
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
@@ -59,7 +60,7 @@ protected function setUp(): void {
5960

6061
$this->contactsManager = $this->createMock(IManager::class);
6162

62-
$this->cloudIdManager = new CloudIdManager($this->contactsManager);
63+
$this->cloudIdManager = new CloudIdManager($this->contactsManager, $this->urlGenerator, $this->createMock(IUserManager::class));
6364

6465
$this->addressHandler = new AddressHandler($this->urlGenerator, $this->il10n, $this->cloudIdManager);
6566
}

apps/federatedfilesharing/tests/Controller/MountPublicLinkControllerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use OCP\IL10N;
4242
use OCP\IRequest;
4343
use OCP\ISession;
44+
use OCP\IURLGenerator;
4445
use OCP\IUserManager;
4546
use OCP\IUserSession;
4647
use OCP\Share\IManager;
@@ -106,7 +107,7 @@ protected function setUp(): void {
106107
$this->userSession = $this->getMockBuilder(IUserSession::class)->disableOriginalConstructor()->getMock();
107108
$this->clientService = $this->getMockBuilder('OCP\Http\Client\IClientService')->disableOriginalConstructor()->getMock();
108109
$this->contactsManager = $this->createMock(IContactsManager::class);
109-
$this->cloudIdManager = new CloudIdManager($this->contactsManager);
110+
$this->cloudIdManager = new CloudIdManager($this->contactsManager, $this->createMock(IURLGenerator::class), $this->userManager);
110111

111112
$this->controller = new MountPublicLinkController(
112113
'federatedfilesharing', $this->request,

apps/federatedfilesharing/tests/FederatedShareProviderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
use OCP\IDBConnection;
4747
use OCP\IL10N;
4848
use OCP\ILogger;
49+
use OCP\IURLGenerator;
4950
use OCP\IUserManager;
5051
use OCP\Share\IManager;
5152
use OCP\Share\IShare;
@@ -115,7 +116,7 @@ protected function setUp(): void {
115116
//$this->addressHandler = new AddressHandler(\OC::$server->getURLGenerator(), $this->l);
116117
$this->addressHandler = $this->getMockBuilder('OCA\FederatedFileSharing\AddressHandler')->disableOriginalConstructor()->getMock();
117118
$this->contactsManager = $this->createMock(IContactsManager::class);
118-
$this->cloudIdManager = new CloudIdManager($this->contactsManager);
119+
$this->cloudIdManager = new CloudIdManager($this->contactsManager, $this->createMock(IURLGenerator::class), $this->userManager);
119120
$this->gsConfig = $this->createMock(\OCP\GlobalScale\IConfig::class);
120121

121122
$this->userManager->expects($this->any())->method('userExists')->willReturn(true);

apps/files_sharing/tests/External/CacheTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
use OCA\Files_Sharing\Tests\TestCase;
3232
use OCP\Contacts\IManager;
3333
use OCP\Federation\ICloudIdManager;
34+
use OCP\IURLGenerator;
35+
use OCP\IUserManager;
3436

3537
/**
3638
* Class Cache
@@ -66,7 +68,7 @@ protected function setUp(): void {
6668

6769
$this->contactsManager = $this->createMock(IManager::class);
6870

69-
$this->cloudIdManager = new CloudIdManager($this->contactsManager);
71+
$this->cloudIdManager = new CloudIdManager($this->contactsManager, $this->createMock(IURLGenerator::class), $this->createMock(IUserManager::class));
7072
$this->remoteUser = $this->getUniqueID('remoteuser');
7173

7274
$this->storage = $this->getMockBuilder('\OCA\Files_Sharing\External\Storage')

apps/files_sharing/tests/External/ManagerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
use OCP\Http\Client\IClientService;
4343
use OCP\Http\Client\IResponse;
4444
use OCP\IGroupManager;
45+
use OCP\IURLGenerator;
4546
use OCP\IUserManager;
4647
use OCP\Share\IShare;
4748
use Test\Traits\UserTrait;
@@ -131,7 +132,7 @@ protected function setUp(): void {
131132

132133
$this->testMountProvider = new MountProvider(\OC::$server->getDatabaseConnection(), function () {
133134
return $this->manager;
134-
}, new CloudIdManager($this->contactsManager));
135+
}, new CloudIdManager($this->contactsManager, $this->createMock(IURLGenerator::class), $this->userManager));
135136
}
136137

137138
private function setupMounts() {

lib/private/Federation/CloudIdManager.php

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,21 @@
3333
use OCP\Contacts\IManager;
3434
use OCP\Federation\ICloudId;
3535
use OCP\Federation\ICloudIdManager;
36+
use OCP\IURLGenerator;
37+
use OCP\IUserManager;
3638

3739
class CloudIdManager implements ICloudIdManager {
3840
/** @var IManager */
3941
private $contactsManager;
42+
/** @var IURLGenerator */
43+
private $urlGenerator;
44+
/** @var IUserManager */
45+
private $userManager;
4046

41-
public function __construct(IManager $contactsManager) {
47+
public function __construct(IManager $contactsManager, IURLGenerator $urlGenerator, IUserManager $userManager) {
4248
$this->contactsManager = $contactsManager;
49+
$this->urlGenerator = $urlGenerator;
50+
$this->userManager = $userManager;
4351
}
4452

4553
/**
@@ -103,24 +111,39 @@ protected function getDisplayNameFromContact(string $cloudId): ?string {
103111

104112
/**
105113
* @param string $user
106-
* @param string $remote
114+
* @param string|null $remote
107115
* @return CloudId
108116
*/
109-
public function getCloudId(string $user, string $remote): ICloudId {
110-
// TODO check what the correct url is for remote (asking the remote)
111-
$fixedRemote = $this->fixRemoteURL($remote);
112-
if (strpos($fixedRemote, 'http://') === 0) {
113-
$host = substr($fixedRemote, strlen('http://'));
114-
} elseif (strpos($fixedRemote, 'https://') === 0) {
115-
$host = substr($fixedRemote, strlen('https://'));
117+
public function getCloudId(string $user, ?string $remote): ICloudId {
118+
if ($remote === null) {
119+
$remote = rtrim($this->removeProtocolFromUrl($this->urlGenerator->getAbsoluteURL('/')), '/');
120+
$fixedRemote = $this->fixRemoteURL($remote);
121+
$localUser = $this->userManager->get($user);
122+
$displayName = !is_null($localUser) ? $localUser->getDisplayName() : '';
116123
} else {
117-
$host = $fixedRemote;
124+
// TODO check what the correct url is for remote (asking the remote)
125+
$fixedRemote = $this->fixRemoteURL($remote);
126+
$host = $this->removeProtocolFromUrl($fixedRemote);
127+
$displayName = $this->getDisplayNameFromContact($user . '@' . $host);
118128
}
119129
$id = $user . '@' . $remote;
120-
$displayName = $this->getDisplayNameFromContact($user . '@' . $host);
121130
return new CloudId($id, $user, $fixedRemote, $displayName);
122131
}
123132

133+
/**
134+
* @param string $url
135+
* @return string
136+
*/
137+
private function removeProtocolFromUrl($url) {
138+
if (strpos($url, 'https://') === 0) {
139+
return substr($url, strlen('https://'));
140+
} elseif (strpos($url, 'http://') === 0) {
141+
return substr($url, strlen('http://'));
142+
}
143+
144+
return $url;
145+
}
146+
124147
/**
125148
* Strips away a potential file names and trailing slashes:
126149
* - http://localhost

lib/private/Server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1286,7 +1286,7 @@ public function __construct($webRoot, \OC\Config $config) {
12861286
});
12871287

12881288
$this->registerService(ICloudIdManager::class, function (ContainerInterface $c) {
1289-
return new CloudIdManager($c->get(\OCP\Contacts\IManager::class));
1289+
return new CloudIdManager($c->get(\OCP\Contacts\IManager::class), $c->get(IURLGenerator::class), $c->get(IUserManager::class));
12901290
});
12911291

12921292
$this->registerAlias(\OCP\GlobalScale\IConfig::class, \OC\GlobalScale\Config::class);

lib/public/Federation/ICloudIdManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ public function resolveCloudId(string $cloudId): ICloudId;
4646
* Get the cloud id for a remote user
4747
*
4848
* @param string $user
49-
* @param string $remote
49+
* @param string|null $remote (optional since 23.0.0 for local users)
5050
* @return ICloudId
5151
*
5252
* @since 12.0.0
5353
*/
54-
public function getCloudId(string $user, string $remote): ICloudId;
54+
public function getCloudId(string $user, ?string $remote): ICloudId;
5555

5656
/**
5757
* Check if the input is a correctly formatted cloud id

tests/lib/Collaboration/Collaborators/MailPluginTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
use OCP\Federation\ICloudIdManager;
3333
use OCP\IConfig;
3434
use OCP\IGroupManager;
35+
use OCP\IURLGenerator;
3536
use OCP\IUser;
37+
use OCP\IUserManager;
3638
use OCP\IUserSession;
3739
use OCP\Share\IShare;
3840
use Test\TestCase;
@@ -70,7 +72,7 @@ protected function setUp(): void {
7072
$this->groupManager = $this->createMock(IGroupManager::class);
7173
$this->knownUserService = $this->createMock(KnownUserService::class);
7274
$this->userSession = $this->createMock(IUserSession::class);
73-
$this->cloudIdManager = new CloudIdManager($this->contactsManager);
75+
$this->cloudIdManager = new CloudIdManager($this->contactsManager, $this->createMock(IURLGenerator::class), $this->createMock(IUserManager::class));
7476

7577
$this->searchResult = new SearchResult();
7678
}

tests/lib/Collaboration/Collaborators/RemotePluginTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use OCP\Contacts\IManager;
3131
use OCP\Federation\ICloudIdManager;
3232
use OCP\IConfig;
33+
use OCP\IURLGenerator;
3334
use OCP\IUser;
3435
use OCP\IUserManager;
3536
use OCP\IUserSession;
@@ -62,7 +63,7 @@ protected function setUp(): void {
6263
$this->userManager = $this->createMock(IUserManager::class);
6364
$this->config = $this->createMock(IConfig::class);
6465
$this->contactsManager = $this->createMock(IManager::class);
65-
$this->cloudIdManager = new CloudIdManager($this->contactsManager);
66+
$this->cloudIdManager = new CloudIdManager($this->contactsManager, $this->createMock(IURLGenerator::class), $this->createMock(IUserManager::class));
6667
$this->searchResult = new SearchResult();
6768
}
6869

0 commit comments

Comments
 (0)