Allow to get a local cloud id without going through the contacts manager

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2021-07-09 08:00:03 +02:00
Родитель f43c2b45d8
Коммит 7179002600
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4C614C6ED2CDE6DF
11 изменённых файлов: 79 добавлений и 31 удалений

Просмотреть файл

@ -32,6 +32,7 @@ use OCA\FederatedFileSharing\AddressHandler;
use OCP\Contacts\IManager; use OCP\Contacts\IManager;
use OCP\IL10N; use OCP\IL10N;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use OCP\IUserManager;
class AddressHandlerTest extends \Test\TestCase { class AddressHandlerTest extends \Test\TestCase {
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */ /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
@ -59,7 +60,7 @@ class AddressHandlerTest extends \Test\TestCase {
$this->contactsManager = $this->createMock(IManager::class); $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); $this->addressHandler = new AddressHandler($this->urlGenerator, $this->il10n, $this->cloudIdManager);
} }

Просмотреть файл

@ -41,6 +41,7 @@ use OCP\Http\Client\IClientService;
use OCP\IL10N; use OCP\IL10N;
use OCP\IRequest; use OCP\IRequest;
use OCP\ISession; use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IUserManager; use OCP\IUserManager;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\Share\IManager; use OCP\Share\IManager;
@ -106,7 +107,7 @@ class MountPublicLinkControllerTest extends \Test\TestCase {
$this->userSession = $this->getMockBuilder(IUserSession::class)->disableOriginalConstructor()->getMock(); $this->userSession = $this->getMockBuilder(IUserSession::class)->disableOriginalConstructor()->getMock();
$this->clientService = $this->getMockBuilder('OCP\Http\Client\IClientService')->disableOriginalConstructor()->getMock(); $this->clientService = $this->getMockBuilder('OCP\Http\Client\IClientService')->disableOriginalConstructor()->getMock();
$this->contactsManager = $this->createMock(IContactsManager::class); $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( $this->controller = new MountPublicLinkController(
'federatedfilesharing', $this->request, 'federatedfilesharing', $this->request,

Просмотреть файл

@ -46,6 +46,7 @@ use OCP\IConfig;
use OCP\IDBConnection; use OCP\IDBConnection;
use OCP\IL10N; use OCP\IL10N;
use OCP\ILogger; use OCP\ILogger;
use OCP\IURLGenerator;
use OCP\IUserManager; use OCP\IUserManager;
use OCP\Share\IManager; use OCP\Share\IManager;
use OCP\Share\IShare; use OCP\Share\IShare;
@ -115,7 +116,7 @@ class FederatedShareProviderTest extends \Test\TestCase {
//$this->addressHandler = new AddressHandler(\OC::$server->getURLGenerator(), $this->l); //$this->addressHandler = new AddressHandler(\OC::$server->getURLGenerator(), $this->l);
$this->addressHandler = $this->getMockBuilder('OCA\FederatedFileSharing\AddressHandler')->disableOriginalConstructor()->getMock(); $this->addressHandler = $this->getMockBuilder('OCA\FederatedFileSharing\AddressHandler')->disableOriginalConstructor()->getMock();
$this->contactsManager = $this->createMock(IContactsManager::class); $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->gsConfig = $this->createMock(\OCP\GlobalScale\IConfig::class);
$this->userManager->expects($this->any())->method('userExists')->willReturn(true); $this->userManager->expects($this->any())->method('userExists')->willReturn(true);

Просмотреть файл

@ -31,6 +31,8 @@ use OC\Federation\CloudIdManager;
use OCA\Files_Sharing\Tests\TestCase; use OCA\Files_Sharing\Tests\TestCase;
use OCP\Contacts\IManager; use OCP\Contacts\IManager;
use OCP\Federation\ICloudIdManager; use OCP\Federation\ICloudIdManager;
use OCP\IURLGenerator;
use OCP\IUserManager;
/** /**
* Class Cache * Class Cache
@ -66,7 +68,7 @@ class CacheTest extends TestCase {
$this->contactsManager = $this->createMock(IManager::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->remoteUser = $this->getUniqueID('remoteuser'); $this->remoteUser = $this->getUniqueID('remoteuser');
$this->storage = $this->getMockBuilder('\OCA\Files_Sharing\External\Storage') $this->storage = $this->getMockBuilder('\OCA\Files_Sharing\External\Storage')

Просмотреть файл

@ -42,6 +42,7 @@ use OCP\Federation\ICloudFederationProviderManager;
use OCP\Http\Client\IClientService; use OCP\Http\Client\IClientService;
use OCP\Http\Client\IResponse; use OCP\Http\Client\IResponse;
use OCP\IGroupManager; use OCP\IGroupManager;
use OCP\IURLGenerator;
use OCP\IUserManager; use OCP\IUserManager;
use OCP\Share\IShare; use OCP\Share\IShare;
use Test\Traits\UserTrait; use Test\Traits\UserTrait;
@ -131,7 +132,7 @@ class ManagerTest extends TestCase {
$this->testMountProvider = new MountProvider(\OC::$server->getDatabaseConnection(), function () { $this->testMountProvider = new MountProvider(\OC::$server->getDatabaseConnection(), function () {
return $this->manager; return $this->manager;
}, new CloudIdManager($this->contactsManager)); }, new CloudIdManager($this->contactsManager, $this->createMock(IURLGenerator::class), $this->userManager));
} }
private function setupMounts() { private function setupMounts() {

Просмотреть файл

@ -33,13 +33,21 @@ namespace OC\Federation;
use OCP\Contacts\IManager; use OCP\Contacts\IManager;
use OCP\Federation\ICloudId; use OCP\Federation\ICloudId;
use OCP\Federation\ICloudIdManager; use OCP\Federation\ICloudIdManager;
use OCP\IURLGenerator;
use OCP\IUserManager;
class CloudIdManager implements ICloudIdManager { class CloudIdManager implements ICloudIdManager {
/** @var IManager */ /** @var IManager */
private $contactsManager; 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->contactsManager = $contactsManager;
$this->urlGenerator = $urlGenerator;
$this->userManager = $userManager;
} }
/** /**
@ -103,24 +111,39 @@ class CloudIdManager implements ICloudIdManager {
/** /**
* @param string $user * @param string $user
* @param string $remote * @param string|null $remote
* @return CloudId * @return CloudId
*/ */
public function getCloudId(string $user, string $remote): ICloudId { public function getCloudId(string $user, ?string $remote): ICloudId {
// TODO check what the correct url is for remote (asking the remote) if ($remote === null) {
$fixedRemote = $this->fixRemoteURL($remote); $remote = rtrim($this->removeProtocolFromUrl($this->urlGenerator->getAbsoluteURL('/')), '/');
if (strpos($fixedRemote, 'http://') === 0) { $fixedRemote = $this->fixRemoteURL($remote);
$host = substr($fixedRemote, strlen('http://')); $localUser = $this->userManager->get($user);
} elseif (strpos($fixedRemote, 'https://') === 0) { $displayName = !is_null($localUser) ? $localUser->getDisplayName() : '';
$host = substr($fixedRemote, strlen('https://'));
} else { } 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; $id = $user . '@' . $remote;
$displayName = $this->getDisplayNameFromContact($user . '@' . $host);
return new CloudId($id, $user, $fixedRemote, $displayName); 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: * Strips away a potential file names and trailing slashes:
* - http://localhost * - http://localhost

Просмотреть файл

@ -1286,7 +1286,7 @@ class Server extends ServerContainer implements IServerContainer {
}); });
$this->registerService(ICloudIdManager::class, function (ContainerInterface $c) { $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); $this->registerAlias(\OCP\GlobalScale\IConfig::class, \OC\GlobalScale\Config::class);

Просмотреть файл

@ -46,12 +46,12 @@ interface ICloudIdManager {
* Get the cloud id for a remote user * Get the cloud id for a remote user
* *
* @param string $user * @param string $user
* @param string $remote * @param string|null $remote (optional since 23.0.0 for local users)
* @return ICloudId * @return ICloudId
* *
* @since 12.0.0 * @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 * Check if the input is a correctly formatted cloud id

Просмотреть файл

@ -32,7 +32,9 @@ use OCP\Contacts\IManager;
use OCP\Federation\ICloudIdManager; use OCP\Federation\ICloudIdManager;
use OCP\IConfig; use OCP\IConfig;
use OCP\IGroupManager; use OCP\IGroupManager;
use OCP\IURLGenerator;
use OCP\IUser; use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\Share\IShare; use OCP\Share\IShare;
use Test\TestCase; use Test\TestCase;
@ -70,7 +72,7 @@ class MailPluginTest extends TestCase {
$this->groupManager = $this->createMock(IGroupManager::class); $this->groupManager = $this->createMock(IGroupManager::class);
$this->knownUserService = $this->createMock(KnownUserService::class); $this->knownUserService = $this->createMock(KnownUserService::class);
$this->userSession = $this->createMock(IUserSession::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(); $this->searchResult = new SearchResult();
} }

Просмотреть файл

@ -30,6 +30,7 @@ use OCP\Collaboration\Collaborators\SearchResultType;
use OCP\Contacts\IManager; use OCP\Contacts\IManager;
use OCP\Federation\ICloudIdManager; use OCP\Federation\ICloudIdManager;
use OCP\IConfig; use OCP\IConfig;
use OCP\IURLGenerator;
use OCP\IUser; use OCP\IUser;
use OCP\IUserManager; use OCP\IUserManager;
use OCP\IUserSession; use OCP\IUserSession;
@ -62,7 +63,7 @@ class RemotePluginTest extends TestCase {
$this->userManager = $this->createMock(IUserManager::class); $this->userManager = $this->createMock(IUserManager::class);
$this->config = $this->createMock(IConfig::class); $this->config = $this->createMock(IConfig::class);
$this->contactsManager = $this->createMock(IManager::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(); $this->searchResult = new SearchResult();
} }

Просмотреть файл

@ -23,20 +23,29 @@ namespace Test\Federation;
use OC\Federation\CloudIdManager; use OC\Federation\CloudIdManager;
use OCP\Contacts\IManager; use OCP\Contacts\IManager;
use OCP\IURLGenerator;
use OCP\IUserManager;
use Test\TestCase; use Test\TestCase;
class CloudIdManagerTest extends TestCase { class CloudIdManagerTest extends TestCase {
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */ /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
protected $contactsManager; protected $contactsManager;
/** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
private $urlGenerator;
/** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
private $userManager;
/** @var CloudIdManager */ /** @var CloudIdManager */
private $cloudIdManager; private $cloudIdManager;
protected function setUp(): void { protected function setUp(): void {
parent::setUp(); parent::setUp();
$this->contactsManager = $this->createMock(IManager::class); $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() { public function cloudIdProvider() {
@ -104,6 +113,7 @@ class CloudIdManagerTest extends TestCase {
return [ return [
['test', 'example.com', 'test@example.com'], ['test', 'example.com', 'test@example.com'],
['test@example.com', 'example.com', 'test@example.com@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 * @param string $id
*/ */
public function testGetCloudId($user, $remote, $id) { public function testGetCloudId($user, $remote, $id) {
$this->contactsManager->expects($this->any()) if ($remote !== null) {
->method('search') $this->contactsManager->expects($this->any())
->with($id, ['CLOUD']) ->method('search')
->willReturn([ ->with($id, ['CLOUD'])
[ ->willReturn([
'CLOUD' => [$id], [
'FN' => 'Ample Ex', 'CLOUD' => [$id],
] 'FN' => 'Ample Ex',
]); ]
]);
} else {
$this->urlGenerator->expects(self::once())
->method('getAbsoluteUrl')
->willReturn('https://example.com');
}
$cloudId = $this->cloudIdManager->getCloudId($user, $remote); $cloudId = $this->cloudIdManager->getCloudId($user, $remote);