@@ -23,161 +23,161 @@ |
||
| 23 | 23 | * @group DB |
| 24 | 24 | */ |
| 25 | 25 | class CloudIdManagerTest extends TestCase { |
| 26 | - /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */ |
|
| 27 | - protected $contactsManager; |
|
| 28 | - /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */ |
|
| 29 | - private $urlGenerator; |
|
| 30 | - /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */ |
|
| 31 | - private $userManager; |
|
| 32 | - /** @var CloudIdManager */ |
|
| 33 | - private $cloudIdManager; |
|
| 34 | - /** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */ |
|
| 35 | - private $cacheFactory; |
|
| 36 | - |
|
| 37 | - |
|
| 38 | - protected function setUp(): void { |
|
| 39 | - parent::setUp(); |
|
| 40 | - |
|
| 41 | - $this->contactsManager = $this->createMock(IManager::class); |
|
| 42 | - $this->urlGenerator = $this->createMock(IURLGenerator::class); |
|
| 43 | - $this->userManager = $this->createMock(IUserManager::class); |
|
| 44 | - |
|
| 45 | - $this->cacheFactory = $this->createMock(ICacheFactory::class); |
|
| 46 | - $this->cacheFactory->method('createDistributed') |
|
| 47 | - ->willReturn(new ArrayCache('')); |
|
| 48 | - |
|
| 49 | - $this->cloudIdManager = new CloudIdManager( |
|
| 50 | - $this->contactsManager, |
|
| 51 | - $this->urlGenerator, |
|
| 52 | - $this->userManager, |
|
| 53 | - $this->cacheFactory, |
|
| 54 | - $this->createMock(IEventDispatcher::class) |
|
| 55 | - ); |
|
| 56 | - $this->overwriteService(ICloudIdManager::class, $this->cloudIdManager); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - public function dataGetDisplayNameFromContact(): array { |
|
| 60 | - return [ |
|
| 61 | - ['[email protected]', 'test', 'test'], |
|
| 62 | - ['[email protected]', null, null], |
|
| 63 | - ['[email protected]', 'test3@example', 'test3@example'], |
|
| 64 | - ['[email protected]', '[email protected]', null], |
|
| 65 | - ]; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @dataProvider dataGetDisplayNameFromContact |
|
| 70 | - */ |
|
| 71 | - public function testGetDisplayNameFromContact(string $cloudId, ?string $displayName, ?string $expected): void { |
|
| 72 | - $returnedContact = [ |
|
| 73 | - 'CLOUD' => [$cloudId], |
|
| 74 | - 'FN' => $expected, |
|
| 75 | - ]; |
|
| 76 | - if ($displayName === null) { |
|
| 77 | - unset($returnedContact['FN']); |
|
| 78 | - } |
|
| 79 | - $this->contactsManager->method('search') |
|
| 80 | - ->with($cloudId, ['CLOUD']) |
|
| 81 | - ->willReturn([$returnedContact]); |
|
| 82 | - |
|
| 83 | - $this->assertEquals($expected, $this->cloudIdManager->getDisplayNameFromContact($cloudId)); |
|
| 84 | - $this->assertEquals($expected, $this->cloudIdManager->getDisplayNameFromContact($cloudId)); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - public function cloudIdProvider(): array { |
|
| 88 | - return [ |
|
| 89 | - ['[email protected]', 'test', 'example.com', '[email protected]'], |
|
| 90 | - ['[email protected]/cloud', 'test', 'example.com/cloud', '[email protected]/cloud'], |
|
| 91 | - ['[email protected]/cloud/', 'test', 'example.com/cloud', '[email protected]/cloud'], |
|
| 92 | - ['[email protected]/cloud/index.php', 'test', 'example.com/cloud', '[email protected]/cloud'], |
|
| 93 | - ['[email protected]@example.com', '[email protected]', 'example.com', '[email protected]@example.com'], |
|
| 94 | - |
|
| 95 | - // Equal signs are not valid on Nextcloud side, but can be used by other federated OCM compatible servers |
|
| 96 | - ['[email protected]', 'test==', 'example.com', '[email protected]'], |
|
| 97 | - ['[email protected]', '==', 'example.com', '[email protected]'], |
|
| 98 | - ]; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * @dataProvider cloudIdProvider |
|
| 103 | - */ |
|
| 104 | - public function testResolveCloudId(string $cloudId, string $user, string $noProtocolRemote, string $cleanId): void { |
|
| 105 | - $displayName = 'Ample Ex'; |
|
| 106 | - |
|
| 107 | - $this->contactsManager->expects($this->any()) |
|
| 108 | - ->method('search') |
|
| 109 | - ->with($cleanId, ['CLOUD']) |
|
| 110 | - ->willReturn([ |
|
| 111 | - [ |
|
| 112 | - 'CLOUD' => [$cleanId], |
|
| 113 | - 'FN' => $displayName, |
|
| 114 | - ] |
|
| 115 | - ]); |
|
| 116 | - |
|
| 117 | - $cloudId = $this->cloudIdManager->resolveCloudId($cloudId); |
|
| 118 | - |
|
| 119 | - $this->assertEquals($user, $cloudId->getUser()); |
|
| 120 | - $this->assertEquals('https://' . $noProtocolRemote, $cloudId->getRemote()); |
|
| 121 | - $this->assertEquals($cleanId, $cloudId->getId()); |
|
| 122 | - $this->assertEquals($displayName . '@' . $noProtocolRemote, $cloudId->getDisplayId()); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - public function invalidCloudIdProvider(): array { |
|
| 126 | - return [ |
|
| 127 | - ['example.com'], |
|
| 128 | - ['test:[email protected]'], |
|
| 129 | - ['test/[email protected]'] |
|
| 130 | - ]; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * @dataProvider invalidCloudIdProvider |
|
| 135 | - */ |
|
| 136 | - public function testInvalidCloudId(string $cloudId): void { |
|
| 137 | - $this->expectException(\InvalidArgumentException::class); |
|
| 138 | - |
|
| 139 | - $this->contactsManager->expects($this->never()) |
|
| 140 | - ->method('search'); |
|
| 141 | - |
|
| 142 | - $this->cloudIdManager->resolveCloudId($cloudId); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - public function getCloudIdProvider(): array { |
|
| 146 | - return [ |
|
| 147 | - ['test', 'example.com', '[email protected]', null, 'https://example.com', 'https://example.com'], |
|
| 148 | - ['test', 'http://example.com', 'test@http://example.com', '[email protected]'], |
|
| 149 | - ['test', null, 'test@http://example.com', '[email protected]', 'http://example.com', 'http://example.com'], |
|
| 150 | - ['[email protected]', 'example.com', '[email protected]@example.com', null, 'https://example.com', 'https://example.com'], |
|
| 151 | - ['[email protected]', 'https://example.com', '[email protected]@example.com'], |
|
| 152 | - ['[email protected]', null, '[email protected]@example.com', null, 'https://example.com', 'https://example.com'], |
|
| 153 | - ['[email protected]', 'https://example.com/index.php/s/shareToken', '[email protected]@example.com', null, 'https://example.com', 'https://example.com'], |
|
| 154 | - ]; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * @dataProvider getCloudIdProvider |
|
| 159 | - */ |
|
| 160 | - public function testGetCloudId(string $user, ?string $remote, string $id, ?string $searchCloudId = null, ?string $localHost = 'https://example.com', ?string $expectedRemoteId = null): void { |
|
| 161 | - if ($remote !== null) { |
|
| 162 | - $this->contactsManager->expects($this->any()) |
|
| 163 | - ->method('search') |
|
| 164 | - ->with($searchCloudId ?? $id, ['CLOUD']) |
|
| 165 | - ->willReturn([ |
|
| 166 | - [ |
|
| 167 | - 'CLOUD' => [$searchCloudId ?? $id], |
|
| 168 | - 'FN' => 'Ample Ex', |
|
| 169 | - ] |
|
| 170 | - ]); |
|
| 171 | - } else { |
|
| 172 | - $this->urlGenerator->expects(self::once()) |
|
| 173 | - ->method('getAbsoluteUrl') |
|
| 174 | - ->willReturn($localHost); |
|
| 175 | - } |
|
| 176 | - $expectedRemoteId ??= $remote; |
|
| 177 | - |
|
| 178 | - $cloudId = $this->cloudIdManager->getCloudId($user, $remote); |
|
| 179 | - |
|
| 180 | - $this->assertEquals($id, $cloudId->getId(), 'Cloud ID'); |
|
| 181 | - $this->assertEquals($expectedRemoteId, $cloudId->getRemote(), 'Remote URL'); |
|
| 182 | - } |
|
| 26 | + /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */ |
|
| 27 | + protected $contactsManager; |
|
| 28 | + /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */ |
|
| 29 | + private $urlGenerator; |
|
| 30 | + /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */ |
|
| 31 | + private $userManager; |
|
| 32 | + /** @var CloudIdManager */ |
|
| 33 | + private $cloudIdManager; |
|
| 34 | + /** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */ |
|
| 35 | + private $cacheFactory; |
|
| 36 | + |
|
| 37 | + |
|
| 38 | + protected function setUp(): void { |
|
| 39 | + parent::setUp(); |
|
| 40 | + |
|
| 41 | + $this->contactsManager = $this->createMock(IManager::class); |
|
| 42 | + $this->urlGenerator = $this->createMock(IURLGenerator::class); |
|
| 43 | + $this->userManager = $this->createMock(IUserManager::class); |
|
| 44 | + |
|
| 45 | + $this->cacheFactory = $this->createMock(ICacheFactory::class); |
|
| 46 | + $this->cacheFactory->method('createDistributed') |
|
| 47 | + ->willReturn(new ArrayCache('')); |
|
| 48 | + |
|
| 49 | + $this->cloudIdManager = new CloudIdManager( |
|
| 50 | + $this->contactsManager, |
|
| 51 | + $this->urlGenerator, |
|
| 52 | + $this->userManager, |
|
| 53 | + $this->cacheFactory, |
|
| 54 | + $this->createMock(IEventDispatcher::class) |
|
| 55 | + ); |
|
| 56 | + $this->overwriteService(ICloudIdManager::class, $this->cloudIdManager); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + public function dataGetDisplayNameFromContact(): array { |
|
| 60 | + return [ |
|
| 61 | + ['[email protected]', 'test', 'test'], |
|
| 62 | + ['[email protected]', null, null], |
|
| 63 | + ['[email protected]', 'test3@example', 'test3@example'], |
|
| 64 | + ['[email protected]', '[email protected]', null], |
|
| 65 | + ]; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @dataProvider dataGetDisplayNameFromContact |
|
| 70 | + */ |
|
| 71 | + public function testGetDisplayNameFromContact(string $cloudId, ?string $displayName, ?string $expected): void { |
|
| 72 | + $returnedContact = [ |
|
| 73 | + 'CLOUD' => [$cloudId], |
|
| 74 | + 'FN' => $expected, |
|
| 75 | + ]; |
|
| 76 | + if ($displayName === null) { |
|
| 77 | + unset($returnedContact['FN']); |
|
| 78 | + } |
|
| 79 | + $this->contactsManager->method('search') |
|
| 80 | + ->with($cloudId, ['CLOUD']) |
|
| 81 | + ->willReturn([$returnedContact]); |
|
| 82 | + |
|
| 83 | + $this->assertEquals($expected, $this->cloudIdManager->getDisplayNameFromContact($cloudId)); |
|
| 84 | + $this->assertEquals($expected, $this->cloudIdManager->getDisplayNameFromContact($cloudId)); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + public function cloudIdProvider(): array { |
|
| 88 | + return [ |
|
| 89 | + ['[email protected]', 'test', 'example.com', '[email protected]'], |
|
| 90 | + ['[email protected]/cloud', 'test', 'example.com/cloud', '[email protected]/cloud'], |
|
| 91 | + ['[email protected]/cloud/', 'test', 'example.com/cloud', '[email protected]/cloud'], |
|
| 92 | + ['[email protected]/cloud/index.php', 'test', 'example.com/cloud', '[email protected]/cloud'], |
|
| 93 | + ['[email protected]@example.com', '[email protected]', 'example.com', '[email protected]@example.com'], |
|
| 94 | + |
|
| 95 | + // Equal signs are not valid on Nextcloud side, but can be used by other federated OCM compatible servers |
|
| 96 | + ['[email protected]', 'test==', 'example.com', '[email protected]'], |
|
| 97 | + ['[email protected]', '==', 'example.com', '[email protected]'], |
|
| 98 | + ]; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @dataProvider cloudIdProvider |
|
| 103 | + */ |
|
| 104 | + public function testResolveCloudId(string $cloudId, string $user, string $noProtocolRemote, string $cleanId): void { |
|
| 105 | + $displayName = 'Ample Ex'; |
|
| 106 | + |
|
| 107 | + $this->contactsManager->expects($this->any()) |
|
| 108 | + ->method('search') |
|
| 109 | + ->with($cleanId, ['CLOUD']) |
|
| 110 | + ->willReturn([ |
|
| 111 | + [ |
|
| 112 | + 'CLOUD' => [$cleanId], |
|
| 113 | + 'FN' => $displayName, |
|
| 114 | + ] |
|
| 115 | + ]); |
|
| 116 | + |
|
| 117 | + $cloudId = $this->cloudIdManager->resolveCloudId($cloudId); |
|
| 118 | + |
|
| 119 | + $this->assertEquals($user, $cloudId->getUser()); |
|
| 120 | + $this->assertEquals('https://' . $noProtocolRemote, $cloudId->getRemote()); |
|
| 121 | + $this->assertEquals($cleanId, $cloudId->getId()); |
|
| 122 | + $this->assertEquals($displayName . '@' . $noProtocolRemote, $cloudId->getDisplayId()); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + public function invalidCloudIdProvider(): array { |
|
| 126 | + return [ |
|
| 127 | + ['example.com'], |
|
| 128 | + ['test:[email protected]'], |
|
| 129 | + ['test/[email protected]'] |
|
| 130 | + ]; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * @dataProvider invalidCloudIdProvider |
|
| 135 | + */ |
|
| 136 | + public function testInvalidCloudId(string $cloudId): void { |
|
| 137 | + $this->expectException(\InvalidArgumentException::class); |
|
| 138 | + |
|
| 139 | + $this->contactsManager->expects($this->never()) |
|
| 140 | + ->method('search'); |
|
| 141 | + |
|
| 142 | + $this->cloudIdManager->resolveCloudId($cloudId); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + public function getCloudIdProvider(): array { |
|
| 146 | + return [ |
|
| 147 | + ['test', 'example.com', '[email protected]', null, 'https://example.com', 'https://example.com'], |
|
| 148 | + ['test', 'http://example.com', 'test@http://example.com', '[email protected]'], |
|
| 149 | + ['test', null, 'test@http://example.com', '[email protected]', 'http://example.com', 'http://example.com'], |
|
| 150 | + ['[email protected]', 'example.com', '[email protected]@example.com', null, 'https://example.com', 'https://example.com'], |
|
| 151 | + ['[email protected]', 'https://example.com', '[email protected]@example.com'], |
|
| 152 | + ['[email protected]', null, '[email protected]@example.com', null, 'https://example.com', 'https://example.com'], |
|
| 153 | + ['[email protected]', 'https://example.com/index.php/s/shareToken', '[email protected]@example.com', null, 'https://example.com', 'https://example.com'], |
|
| 154 | + ]; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * @dataProvider getCloudIdProvider |
|
| 159 | + */ |
|
| 160 | + public function testGetCloudId(string $user, ?string $remote, string $id, ?string $searchCloudId = null, ?string $localHost = 'https://example.com', ?string $expectedRemoteId = null): void { |
|
| 161 | + if ($remote !== null) { |
|
| 162 | + $this->contactsManager->expects($this->any()) |
|
| 163 | + ->method('search') |
|
| 164 | + ->with($searchCloudId ?? $id, ['CLOUD']) |
|
| 165 | + ->willReturn([ |
|
| 166 | + [ |
|
| 167 | + 'CLOUD' => [$searchCloudId ?? $id], |
|
| 168 | + 'FN' => 'Ample Ex', |
|
| 169 | + ] |
|
| 170 | + ]); |
|
| 171 | + } else { |
|
| 172 | + $this->urlGenerator->expects(self::once()) |
|
| 173 | + ->method('getAbsoluteUrl') |
|
| 174 | + ->willReturn($localHost); |
|
| 175 | + } |
|
| 176 | + $expectedRemoteId ??= $remote; |
|
| 177 | + |
|
| 178 | + $cloudId = $this->cloudIdManager->getCloudId($user, $remote); |
|
| 179 | + |
|
| 180 | + $this->assertEquals($id, $cloudId->getId(), 'Cloud ID'); |
|
| 181 | + $this->assertEquals($expectedRemoteId, $cloudId->getRemote(), 'Remote URL'); |
|
| 182 | + } |
|
| 183 | 183 | } |
@@ -21,236 +21,236 @@ |
||
| 21 | 21 | use OCP\User\Events\UserChangedEvent; |
| 22 | 22 | |
| 23 | 23 | class CloudIdManager implements ICloudIdManager { |
| 24 | - /** @var IManager */ |
|
| 25 | - private $contactsManager; |
|
| 26 | - /** @var IURLGenerator */ |
|
| 27 | - private $urlGenerator; |
|
| 28 | - /** @var IUserManager */ |
|
| 29 | - private $userManager; |
|
| 30 | - private ICache $memCache; |
|
| 31 | - private ICache $displayNameCache; |
|
| 32 | - /** @var array[] */ |
|
| 33 | - private array $cache = []; |
|
| 24 | + /** @var IManager */ |
|
| 25 | + private $contactsManager; |
|
| 26 | + /** @var IURLGenerator */ |
|
| 27 | + private $urlGenerator; |
|
| 28 | + /** @var IUserManager */ |
|
| 29 | + private $userManager; |
|
| 30 | + private ICache $memCache; |
|
| 31 | + private ICache $displayNameCache; |
|
| 32 | + /** @var array[] */ |
|
| 33 | + private array $cache = []; |
|
| 34 | 34 | |
| 35 | - public function __construct( |
|
| 36 | - IManager $contactsManager, |
|
| 37 | - IURLGenerator $urlGenerator, |
|
| 38 | - IUserManager $userManager, |
|
| 39 | - ICacheFactory $cacheFactory, |
|
| 40 | - IEventDispatcher $eventDispatcher, |
|
| 41 | - ) { |
|
| 42 | - $this->contactsManager = $contactsManager; |
|
| 43 | - $this->urlGenerator = $urlGenerator; |
|
| 44 | - $this->userManager = $userManager; |
|
| 45 | - $this->memCache = $cacheFactory->createDistributed('cloud_id_'); |
|
| 46 | - $this->displayNameCache = $cacheFactory->createDistributed('cloudid_name_'); |
|
| 47 | - $eventDispatcher->addListener(UserChangedEvent::class, [$this, 'handleUserEvent']); |
|
| 48 | - $eventDispatcher->addListener(CardUpdatedEvent::class, [$this, 'handleCardEvent']); |
|
| 49 | - } |
|
| 35 | + public function __construct( |
|
| 36 | + IManager $contactsManager, |
|
| 37 | + IURLGenerator $urlGenerator, |
|
| 38 | + IUserManager $userManager, |
|
| 39 | + ICacheFactory $cacheFactory, |
|
| 40 | + IEventDispatcher $eventDispatcher, |
|
| 41 | + ) { |
|
| 42 | + $this->contactsManager = $contactsManager; |
|
| 43 | + $this->urlGenerator = $urlGenerator; |
|
| 44 | + $this->userManager = $userManager; |
|
| 45 | + $this->memCache = $cacheFactory->createDistributed('cloud_id_'); |
|
| 46 | + $this->displayNameCache = $cacheFactory->createDistributed('cloudid_name_'); |
|
| 47 | + $eventDispatcher->addListener(UserChangedEvent::class, [$this, 'handleUserEvent']); |
|
| 48 | + $eventDispatcher->addListener(CardUpdatedEvent::class, [$this, 'handleCardEvent']); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - public function handleUserEvent(Event $event): void { |
|
| 52 | - if ($event instanceof UserChangedEvent && $event->getFeature() === 'displayName') { |
|
| 53 | - $userId = $event->getUser()->getUID(); |
|
| 54 | - $key = $userId . '@local'; |
|
| 55 | - unset($this->cache[$key]); |
|
| 56 | - $this->memCache->remove($key); |
|
| 57 | - } |
|
| 58 | - } |
|
| 51 | + public function handleUserEvent(Event $event): void { |
|
| 52 | + if ($event instanceof UserChangedEvent && $event->getFeature() === 'displayName') { |
|
| 53 | + $userId = $event->getUser()->getUID(); |
|
| 54 | + $key = $userId . '@local'; |
|
| 55 | + unset($this->cache[$key]); |
|
| 56 | + $this->memCache->remove($key); |
|
| 57 | + } |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - public function handleCardEvent(Event $event): void { |
|
| 61 | - if ($event instanceof CardUpdatedEvent) { |
|
| 62 | - $data = $event->getCardData()['carddata']; |
|
| 63 | - foreach (explode("\r\n", $data) as $line) { |
|
| 64 | - if (str_starts_with($line, 'CLOUD;')) { |
|
| 65 | - $parts = explode(':', $line, 2); |
|
| 66 | - if (isset($parts[1])) { |
|
| 67 | - $key = $parts[1]; |
|
| 68 | - unset($this->cache[$key]); |
|
| 69 | - $this->memCache->remove($key); |
|
| 70 | - } |
|
| 71 | - } |
|
| 72 | - } |
|
| 73 | - } |
|
| 74 | - } |
|
| 60 | + public function handleCardEvent(Event $event): void { |
|
| 61 | + if ($event instanceof CardUpdatedEvent) { |
|
| 62 | + $data = $event->getCardData()['carddata']; |
|
| 63 | + foreach (explode("\r\n", $data) as $line) { |
|
| 64 | + if (str_starts_with($line, 'CLOUD;')) { |
|
| 65 | + $parts = explode(':', $line, 2); |
|
| 66 | + if (isset($parts[1])) { |
|
| 67 | + $key = $parts[1]; |
|
| 68 | + unset($this->cache[$key]); |
|
| 69 | + $this->memCache->remove($key); |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * @param string $cloudId |
|
| 78 | - * @return ICloudId |
|
| 79 | - * @throws \InvalidArgumentException |
|
| 80 | - */ |
|
| 81 | - public function resolveCloudId(string $cloudId): ICloudId { |
|
| 82 | - // TODO magic here to get the url and user instead of just splitting on @ |
|
| 76 | + /** |
|
| 77 | + * @param string $cloudId |
|
| 78 | + * @return ICloudId |
|
| 79 | + * @throws \InvalidArgumentException |
|
| 80 | + */ |
|
| 81 | + public function resolveCloudId(string $cloudId): ICloudId { |
|
| 82 | + // TODO magic here to get the url and user instead of just splitting on @ |
|
| 83 | 83 | |
| 84 | - if (!$this->isValidCloudId($cloudId)) { |
|
| 85 | - throw new \InvalidArgumentException('Invalid cloud id'); |
|
| 86 | - } |
|
| 84 | + if (!$this->isValidCloudId($cloudId)) { |
|
| 85 | + throw new \InvalidArgumentException('Invalid cloud id'); |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - // Find the first character that is not allowed in user names |
|
| 89 | - $id = $this->stripShareLinkFragments($cloudId); |
|
| 90 | - $posSlash = strpos($id, '/'); |
|
| 91 | - $posColon = strpos($id, ':'); |
|
| 88 | + // Find the first character that is not allowed in user names |
|
| 89 | + $id = $this->stripShareLinkFragments($cloudId); |
|
| 90 | + $posSlash = strpos($id, '/'); |
|
| 91 | + $posColon = strpos($id, ':'); |
|
| 92 | 92 | |
| 93 | - if ($posSlash === false && $posColon === false) { |
|
| 94 | - $invalidPos = \strlen($id); |
|
| 95 | - } elseif ($posSlash === false) { |
|
| 96 | - $invalidPos = $posColon; |
|
| 97 | - } elseif ($posColon === false) { |
|
| 98 | - $invalidPos = $posSlash; |
|
| 99 | - } else { |
|
| 100 | - $invalidPos = min($posSlash, $posColon); |
|
| 101 | - } |
|
| 93 | + if ($posSlash === false && $posColon === false) { |
|
| 94 | + $invalidPos = \strlen($id); |
|
| 95 | + } elseif ($posSlash === false) { |
|
| 96 | + $invalidPos = $posColon; |
|
| 97 | + } elseif ($posColon === false) { |
|
| 98 | + $invalidPos = $posSlash; |
|
| 99 | + } else { |
|
| 100 | + $invalidPos = min($posSlash, $posColon); |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - $lastValidAtPos = strrpos($id, '@', $invalidPos - strlen($id)); |
|
| 103 | + $lastValidAtPos = strrpos($id, '@', $invalidPos - strlen($id)); |
|
| 104 | 104 | |
| 105 | - if ($lastValidAtPos !== false) { |
|
| 106 | - $user = substr($id, 0, $lastValidAtPos); |
|
| 107 | - $remote = substr($id, $lastValidAtPos + 1); |
|
| 105 | + if ($lastValidAtPos !== false) { |
|
| 106 | + $user = substr($id, 0, $lastValidAtPos); |
|
| 107 | + $remote = substr($id, $lastValidAtPos + 1); |
|
| 108 | 108 | |
| 109 | - // We accept slightly more chars when working with federationId than with a local userId. |
|
| 110 | - // We remove those eventual chars from the UserId before using |
|
| 111 | - // the IUserManager API to confirm its format. |
|
| 112 | - $this->userManager->validateUserId(str_replace('=', '-', $user)); |
|
| 109 | + // We accept slightly more chars when working with federationId than with a local userId. |
|
| 110 | + // We remove those eventual chars from the UserId before using |
|
| 111 | + // the IUserManager API to confirm its format. |
|
| 112 | + $this->userManager->validateUserId(str_replace('=', '-', $user)); |
|
| 113 | 113 | |
| 114 | - if (!empty($user) && !empty($remote)) { |
|
| 115 | - $remote = $this->ensureDefaultProtocol($remote); |
|
| 116 | - return new CloudId($id, $user, $remote, null); |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - throw new \InvalidArgumentException('Invalid cloud id'); |
|
| 120 | - } |
|
| 114 | + if (!empty($user) && !empty($remote)) { |
|
| 115 | + $remote = $this->ensureDefaultProtocol($remote); |
|
| 116 | + return new CloudId($id, $user, $remote, null); |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + throw new \InvalidArgumentException('Invalid cloud id'); |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - public function getDisplayNameFromContact(string $cloudId): ?string { |
|
| 123 | - $cachedName = $this->displayNameCache->get($cloudId); |
|
| 124 | - if ($cachedName !== null) { |
|
| 125 | - if ($cachedName === $cloudId) { |
|
| 126 | - return null; |
|
| 127 | - } |
|
| 128 | - return $cachedName; |
|
| 129 | - } |
|
| 122 | + public function getDisplayNameFromContact(string $cloudId): ?string { |
|
| 123 | + $cachedName = $this->displayNameCache->get($cloudId); |
|
| 124 | + if ($cachedName !== null) { |
|
| 125 | + if ($cachedName === $cloudId) { |
|
| 126 | + return null; |
|
| 127 | + } |
|
| 128 | + return $cachedName; |
|
| 129 | + } |
|
| 130 | 130 | |
| 131 | - $addressBookEntries = $this->contactsManager->search($cloudId, ['CLOUD'], [ |
|
| 132 | - 'limit' => 1, |
|
| 133 | - 'enumeration' => false, |
|
| 134 | - 'fullmatch' => false, |
|
| 135 | - 'strict_search' => true, |
|
| 136 | - ]); |
|
| 137 | - foreach ($addressBookEntries as $entry) { |
|
| 138 | - if (isset($entry['CLOUD'])) { |
|
| 139 | - foreach ($entry['CLOUD'] as $cloudID) { |
|
| 140 | - if ($cloudID === $cloudId) { |
|
| 141 | - // Warning, if user decides to make their full name local only, |
|
| 142 | - // no FN is found on federated servers |
|
| 143 | - if (isset($entry['FN'])) { |
|
| 144 | - $this->displayNameCache->set($cloudId, $entry['FN'], 15 * 60); |
|
| 145 | - return $entry['FN']; |
|
| 146 | - } else { |
|
| 147 | - $this->displayNameCache->set($cloudId, $cloudId, 15 * 60); |
|
| 148 | - return null; |
|
| 149 | - } |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - } |
|
| 153 | - } |
|
| 154 | - $this->displayNameCache->set($cloudId, $cloudId, 15 * 60); |
|
| 155 | - return null; |
|
| 156 | - } |
|
| 131 | + $addressBookEntries = $this->contactsManager->search($cloudId, ['CLOUD'], [ |
|
| 132 | + 'limit' => 1, |
|
| 133 | + 'enumeration' => false, |
|
| 134 | + 'fullmatch' => false, |
|
| 135 | + 'strict_search' => true, |
|
| 136 | + ]); |
|
| 137 | + foreach ($addressBookEntries as $entry) { |
|
| 138 | + if (isset($entry['CLOUD'])) { |
|
| 139 | + foreach ($entry['CLOUD'] as $cloudID) { |
|
| 140 | + if ($cloudID === $cloudId) { |
|
| 141 | + // Warning, if user decides to make their full name local only, |
|
| 142 | + // no FN is found on federated servers |
|
| 143 | + if (isset($entry['FN'])) { |
|
| 144 | + $this->displayNameCache->set($cloudId, $entry['FN'], 15 * 60); |
|
| 145 | + return $entry['FN']; |
|
| 146 | + } else { |
|
| 147 | + $this->displayNameCache->set($cloudId, $cloudId, 15 * 60); |
|
| 148 | + return null; |
|
| 149 | + } |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | + } |
|
| 154 | + $this->displayNameCache->set($cloudId, $cloudId, 15 * 60); |
|
| 155 | + return null; |
|
| 156 | + } |
|
| 157 | 157 | |
| 158 | - /** |
|
| 159 | - * @param string $user |
|
| 160 | - * @param string|null $remote |
|
| 161 | - * @return CloudId |
|
| 162 | - */ |
|
| 163 | - public function getCloudId(string $user, ?string $remote): ICloudId { |
|
| 164 | - $isLocal = $remote === null; |
|
| 165 | - if ($isLocal) { |
|
| 166 | - $remote = rtrim($this->urlGenerator->getAbsoluteURL('/'), '/'); |
|
| 167 | - } |
|
| 158 | + /** |
|
| 159 | + * @param string $user |
|
| 160 | + * @param string|null $remote |
|
| 161 | + * @return CloudId |
|
| 162 | + */ |
|
| 163 | + public function getCloudId(string $user, ?string $remote): ICloudId { |
|
| 164 | + $isLocal = $remote === null; |
|
| 165 | + if ($isLocal) { |
|
| 166 | + $remote = rtrim($this->urlGenerator->getAbsoluteURL('/'), '/'); |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | - // note that for remote id's we don't strip the protocol for the remote we use to construct the CloudId |
|
| 170 | - // this way if a user has an explicit non-https cloud id this will be preserved |
|
| 171 | - // we do still use the version without protocol for looking up the display name |
|
| 172 | - $remote = $this->stripShareLinkFragments($remote); |
|
| 173 | - $host = $this->removeProtocolFromUrl($remote); |
|
| 174 | - $remote = $this->ensureDefaultProtocol($remote); |
|
| 169 | + // note that for remote id's we don't strip the protocol for the remote we use to construct the CloudId |
|
| 170 | + // this way if a user has an explicit non-https cloud id this will be preserved |
|
| 171 | + // we do still use the version without protocol for looking up the display name |
|
| 172 | + $remote = $this->stripShareLinkFragments($remote); |
|
| 173 | + $host = $this->removeProtocolFromUrl($remote); |
|
| 174 | + $remote = $this->ensureDefaultProtocol($remote); |
|
| 175 | 175 | |
| 176 | - $key = $user . '@' . ($isLocal ? 'local' : $host); |
|
| 177 | - $cached = $this->cache[$key] ?? $this->memCache->get($key); |
|
| 178 | - if ($cached) { |
|
| 179 | - $this->cache[$key] = $cached; // put items from memcache into local cache |
|
| 180 | - return new CloudId($cached['id'], $cached['user'], $cached['remote'], $cached['displayName']); |
|
| 181 | - } |
|
| 176 | + $key = $user . '@' . ($isLocal ? 'local' : $host); |
|
| 177 | + $cached = $this->cache[$key] ?? $this->memCache->get($key); |
|
| 178 | + if ($cached) { |
|
| 179 | + $this->cache[$key] = $cached; // put items from memcache into local cache |
|
| 180 | + return new CloudId($cached['id'], $cached['user'], $cached['remote'], $cached['displayName']); |
|
| 181 | + } |
|
| 182 | 182 | |
| 183 | - if ($isLocal) { |
|
| 184 | - $localUser = $this->userManager->get($user); |
|
| 185 | - $displayName = $localUser ? $localUser->getDisplayName() : ''; |
|
| 186 | - } else { |
|
| 187 | - $displayName = null; |
|
| 188 | - } |
|
| 183 | + if ($isLocal) { |
|
| 184 | + $localUser = $this->userManager->get($user); |
|
| 185 | + $displayName = $localUser ? $localUser->getDisplayName() : ''; |
|
| 186 | + } else { |
|
| 187 | + $displayName = null; |
|
| 188 | + } |
|
| 189 | 189 | |
| 190 | - // For the visible cloudID we only strip away https |
|
| 191 | - $id = $user . '@' . $this->removeProtocolFromUrl($remote, true); |
|
| 190 | + // For the visible cloudID we only strip away https |
|
| 191 | + $id = $user . '@' . $this->removeProtocolFromUrl($remote, true); |
|
| 192 | 192 | |
| 193 | - $data = [ |
|
| 194 | - 'id' => $id, |
|
| 195 | - 'user' => $user, |
|
| 196 | - 'remote' => $remote, |
|
| 197 | - 'displayName' => $displayName, |
|
| 198 | - ]; |
|
| 199 | - $this->cache[$key] = $data; |
|
| 200 | - $this->memCache->set($key, $data, 15 * 60); |
|
| 201 | - return new CloudId($id, $user, $remote, $displayName); |
|
| 202 | - } |
|
| 193 | + $data = [ |
|
| 194 | + 'id' => $id, |
|
| 195 | + 'user' => $user, |
|
| 196 | + 'remote' => $remote, |
|
| 197 | + 'displayName' => $displayName, |
|
| 198 | + ]; |
|
| 199 | + $this->cache[$key] = $data; |
|
| 200 | + $this->memCache->set($key, $data, 15 * 60); |
|
| 201 | + return new CloudId($id, $user, $remote, $displayName); |
|
| 202 | + } |
|
| 203 | 203 | |
| 204 | - /** |
|
| 205 | - * @param string $url |
|
| 206 | - * @return string |
|
| 207 | - */ |
|
| 208 | - public function removeProtocolFromUrl(string $url, bool $httpsOnly = false): string { |
|
| 209 | - if (str_starts_with($url, 'https://')) { |
|
| 210 | - return substr($url, 8); |
|
| 211 | - } |
|
| 212 | - if (!$httpsOnly && str_starts_with($url, 'http://')) { |
|
| 213 | - return substr($url, 7); |
|
| 214 | - } |
|
| 204 | + /** |
|
| 205 | + * @param string $url |
|
| 206 | + * @return string |
|
| 207 | + */ |
|
| 208 | + public function removeProtocolFromUrl(string $url, bool $httpsOnly = false): string { |
|
| 209 | + if (str_starts_with($url, 'https://')) { |
|
| 210 | + return substr($url, 8); |
|
| 211 | + } |
|
| 212 | + if (!$httpsOnly && str_starts_with($url, 'http://')) { |
|
| 213 | + return substr($url, 7); |
|
| 214 | + } |
|
| 215 | 215 | |
| 216 | - return $url; |
|
| 217 | - } |
|
| 216 | + return $url; |
|
| 217 | + } |
|
| 218 | 218 | |
| 219 | - protected function ensureDefaultProtocol(string $remote): string { |
|
| 220 | - if (!str_contains($remote, '://')) { |
|
| 221 | - $remote = 'https://' . $remote; |
|
| 222 | - } |
|
| 219 | + protected function ensureDefaultProtocol(string $remote): string { |
|
| 220 | + if (!str_contains($remote, '://')) { |
|
| 221 | + $remote = 'https://' . $remote; |
|
| 222 | + } |
|
| 223 | 223 | |
| 224 | - return $remote; |
|
| 225 | - } |
|
| 224 | + return $remote; |
|
| 225 | + } |
|
| 226 | 226 | |
| 227 | - /** |
|
| 228 | - * Strips away a potential file names and trailing slashes: |
|
| 229 | - * - http://localhost |
|
| 230 | - * - http://localhost/ |
|
| 231 | - * - http://localhost/index.php |
|
| 232 | - * - http://localhost/index.php/s/{shareToken} |
|
| 233 | - * |
|
| 234 | - * all return: http://localhost |
|
| 235 | - * |
|
| 236 | - * @param string $remote |
|
| 237 | - * @return string |
|
| 238 | - */ |
|
| 239 | - protected function stripShareLinkFragments(string $remote): string { |
|
| 240 | - $remote = str_replace('\\', '/', $remote); |
|
| 241 | - if ($fileNamePosition = strpos($remote, '/index.php')) { |
|
| 242 | - $remote = substr($remote, 0, $fileNamePosition); |
|
| 243 | - } |
|
| 244 | - $remote = rtrim($remote, '/'); |
|
| 227 | + /** |
|
| 228 | + * Strips away a potential file names and trailing slashes: |
|
| 229 | + * - http://localhost |
|
| 230 | + * - http://localhost/ |
|
| 231 | + * - http://localhost/index.php |
|
| 232 | + * - http://localhost/index.php/s/{shareToken} |
|
| 233 | + * |
|
| 234 | + * all return: http://localhost |
|
| 235 | + * |
|
| 236 | + * @param string $remote |
|
| 237 | + * @return string |
|
| 238 | + */ |
|
| 239 | + protected function stripShareLinkFragments(string $remote): string { |
|
| 240 | + $remote = str_replace('\\', '/', $remote); |
|
| 241 | + if ($fileNamePosition = strpos($remote, '/index.php')) { |
|
| 242 | + $remote = substr($remote, 0, $fileNamePosition); |
|
| 243 | + } |
|
| 244 | + $remote = rtrim($remote, '/'); |
|
| 245 | 245 | |
| 246 | - return $remote; |
|
| 247 | - } |
|
| 246 | + return $remote; |
|
| 247 | + } |
|
| 248 | 248 | |
| 249 | - /** |
|
| 250 | - * @param string $cloudId |
|
| 251 | - * @return bool |
|
| 252 | - */ |
|
| 253 | - public function isValidCloudId(string $cloudId): bool { |
|
| 254 | - return str_contains($cloudId, '@'); |
|
| 255 | - } |
|
| 249 | + /** |
|
| 250 | + * @param string $cloudId |
|
| 251 | + * @return bool |
|
| 252 | + */ |
|
| 253 | + public function isValidCloudId(string $cloudId): bool { |
|
| 254 | + return str_contains($cloudId, '@'); |
|
| 255 | + } |
|
| 256 | 256 | } |