Skip to content

Commit

Permalink
Allow to get a local cloud id without going through the contacts manager
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliusknorr committed Jul 14, 2021
1 parent f43c2b4 commit 7179002
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 31 deletions.
3 changes: 2 additions & 1 deletion apps/federatedfilesharing/tests/AddressHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use OCP\Contacts\IManager;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUserManager;

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

$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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use OCP\IL10N;
use OCP\IRequest;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Share\IManager;
Expand Down Expand Up @@ -106,7 +107,7 @@ protected function setUp(): void {
$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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
use OCP\IDBConnection;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\Share\IManager;
use OCP\Share\IShare;
Expand Down Expand Up @@ -115,7 +116,7 @@ protected function setUp(): void {
//$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);
Expand Down
4 changes: 3 additions & 1 deletion apps/files_sharing/tests/External/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
use OCA\Files_Sharing\Tests\TestCase;
use OCP\Contacts\IManager;
use OCP\Federation\ICloudIdManager;
use OCP\IURLGenerator;
use OCP\IUserManager;

/**
* Class Cache
Expand Down Expand Up @@ -66,7 +68,7 @@ protected function setUp(): void {

$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')
Expand Down
3 changes: 2 additions & 1 deletion apps/files_sharing/tests/External/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
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;
Expand Down Expand Up @@ -131,7 +132,7 @@ protected function setUp(): void {

$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() {
Expand Down
45 changes: 34 additions & 11 deletions lib/private/Federation/CloudIdManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,21 @@
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;
}

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

/**
* @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
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,7 @@ public function __construct($webRoot, \OC\Config $config) {
});

$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);
Expand Down
4 changes: 2 additions & 2 deletions lib/public/Federation/ICloudIdManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public function resolveCloudId(string $cloudId): ICloudId;
* 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
Expand Down
4 changes: 3 additions & 1 deletion tests/lib/Collaboration/Collaborators/MailPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
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;
Expand Down Expand Up @@ -70,7 +72,7 @@ protected function setUp(): void {
$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();
}
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/Collaboration/Collaborators/RemotePluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OCP\Contacts\IManager;
use OCP\Federation\ICloudIdManager;
use OCP\IConfig;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
Expand Down Expand Up @@ -62,7 +63,7 @@ protected function setUp(): void {
$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();
}

Expand Down
36 changes: 26 additions & 10 deletions tests/lib/Federation/CloudIdManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,29 @@

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() {
Expand Down Expand Up @@ -104,6 +113,7 @@ public function getCloudIdProvider() {
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'],
];
}

Expand All @@ -115,15 +125,21 @@ public function getCloudIdProvider() {
* @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);

Expand Down

0 comments on commit 7179002

Please sign in to comment.