Skip to content

Commit 2fe2427

Browse files
committed
Add cloud id resolver interface
Signed-off-by: Sandro Mesterheide <sandro.mesterheide@extern.publicplan.de>
1 parent a5443ac commit 2fe2427

7 files changed

Lines changed: 106 additions & 7 deletions

File tree

apps/files_sharing/tests/ExternalStorageTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
*/
2828
namespace OCA\Files_Sharing\Tests;
2929

30-
use OC\Federation\CloudId;
30+
use OCP\Federation\CloudId;
3131
use OCP\Http\Client\IClient;
3232
use OCP\Http\Client\IClientService;
3333
use OCP\Http\Client\IResponse;

lib/private/Federation/CloudIdManager.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@
3434
use OCP\Contacts\IManager;
3535
use OCP\EventDispatcher\Event;
3636
use OCP\EventDispatcher\IEventDispatcher;
37+
use OCP\Federation\CloudId;
3738
use OCP\Federation\ICloudId;
3839
use OCP\Federation\ICloudIdManager;
40+
use OCP\Federation\ICloudIdResolver;
3941
use OCP\ICache;
4042
use OCP\ICacheFactory;
4143
use OCP\IURLGenerator;
@@ -52,6 +54,8 @@ class CloudIdManager implements ICloudIdManager {
5254
private ICache $memCache;
5355
/** @var array[] */
5456
private array $cache = [];
57+
/** @var ICloudIdResolver[] */
58+
private array $cloudIdResolvers = [];
5559

5660
public function __construct(
5761
IManager $contactsManager,
@@ -65,7 +69,7 @@ public function __construct(
6569
$this->userManager = $userManager;
6670
$this->memCache = $cacheFactory->createDistributed('cloud_id_');
6771
$eventDispatcher->addListener(UserChangedEvent::class, [$this, 'handleUserEvent']);
68-
$eventDispatcher->addListener(CardUpdatedEvent::class, [$this, 'handleCardEvent']);
72+
$eventDispatcher->addListener(CardUpdatedEvent::class, [$this, 'handleCardEvent']);
6973
}
7074

7175
public function handleUserEvent(Event $event): void {
@@ -100,6 +104,12 @@ public function handleCardEvent(Event $event): void {
100104
*/
101105
public function resolveCloudId(string $cloudId): ICloudId {
102106
// TODO magic here to get the url and user instead of just splitting on @
107+
108+
foreach ($this->cloudIdResolvers as $resolver) {
109+
if ($resolver->isValidCloudId($cloudId)) {
110+
return $resolver->resolveCloudId($cloudId);
111+
}
112+
}
103113

104114
if (!$this->isValidCloudId($cloudId)) {
105115
throw new \InvalidArgumentException('Invalid cloud id');
@@ -246,6 +256,28 @@ protected function fixRemoteURL(string $remote): string {
246256
* @return bool
247257
*/
248258
public function isValidCloudId(string $cloudId): bool {
259+
foreach ($this->cloudIdResolvers as $resolver) {
260+
if ($resolver->isValidCloudId($cloudId)) {
261+
return true;
262+
}
263+
}
264+
249265
return strpos($cloudId, '@') !== false;
250266
}
267+
268+
/**
269+
* @param ICloudIdResolver $resolver
270+
*/
271+
public function registerCloudIdResolver(ICloudIdResolver $resolver) {
272+
array_unshift($this->cloudIdResolvers, $resolver);
273+
}
274+
275+
/**
276+
* @param ICloudIdResolver $resolver
277+
*/
278+
public function unregisterCloudIdResolver(ICloudIdResolver $resolver) {
279+
if (($key = array_search($resolver, $this->cloudIdResolvers)) !== false) {
280+
array_splice($this->cloudIdResolvers, $key, 1);
281+
}
282+
}
251283
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2626
*
2727
*/
28-
namespace OC\Federation;
29-
30-
use OCP\Federation\ICloudId;
28+
namespace OCP\Federation;
3129

3230
class CloudId implements ICloudId {
3331
/** @var string */

lib/public/Federation/ICloudIdManager.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,18 @@ public function getCloudId(string $user, ?string $remote): ICloudId;
6262
* @since 12.0.0
6363
*/
6464
public function isValidCloudId(string $cloudId): bool;
65+
66+
/**
67+
* @param ICloudIdResolver $resolver
68+
*
69+
* @since 26.0.0
70+
*/
71+
public function registerCloudIdResolver(ICloudIdResolver $resolver);
72+
73+
/**
74+
* @param ICloudIdResolver $resolver
75+
*
76+
* @since 26.0.0
77+
*/
78+
public function unregisterCloudIdResolver(ICloudIdResolver $resolver);
6579
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2017, Robin Appelman <robin@icewind.nl>
7+
*
8+
* @author Joas Schilling <coding@schilljs.com>
9+
* @author Robin Appelman <robin@icewind.nl>
10+
* @author Roeland Jago Douma <roeland@famdouma.nl>
11+
* @author Sandro Mesterheide <sandro.mesterheide@extern.publicplan.de>
12+
*
13+
* @license GNU AGPL version 3 or any later version
14+
*
15+
* This program is free software: you can redistribute it and/or modify
16+
* it under the terms of the GNU Affero General Public License as
17+
* published by the Free Software Foundation, either version 3 of the
18+
* License, or (at your option) any later version.
19+
*
20+
* This program is distributed in the hope that it will be useful,
21+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
22+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23+
* GNU Affero General Public License for more details.
24+
*
25+
* You should have received a copy of the GNU Affero General Public License
26+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
27+
*
28+
*/
29+
namespace OCP\Federation;
30+
31+
/**
32+
* Interface for resolving federated cloud ids
33+
*
34+
* @since 26.0.0
35+
*/
36+
interface ICloudIdResolver{
37+
/**
38+
* @param string $cloudId
39+
* @return ICloudId
40+
* @throws \InvalidArgumentException
41+
*
42+
* @since 26.0.0
43+
*/
44+
public function resolveCloudId(string $cloudId): ICloudId;
45+
46+
/**
47+
* Check if the input is a correctly formatted cloud id
48+
*
49+
* @param string $cloudId
50+
* @return bool
51+
*
52+
* @since 26.0.0
53+
*/
54+
public function isValidCloudId(string $cloudId): bool;
55+
}

tests/lib/Collaboration/Collaborators/LookupPluginTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
namespace Test\Collaboration\Collaborators;
2525

2626
use OC\Collaboration\Collaborators\LookupPlugin;
27-
use OC\Federation\CloudId;
27+
use OCP\Federation\CloudId;
2828
use OCP\Collaboration\Collaborators\ISearchResult;
2929
use OCP\Collaboration\Collaborators\SearchResultType;
3030
use OCP\Federation\ICloudId;

tests/lib/Federation/CloudIdTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
namespace Test\Federation;
2323

24-
use OC\Federation\CloudId;
24+
use OCP\Federation\CloudId;
2525
use Test\TestCase;
2626

2727
class CloudIdTest extends TestCase {

0 commit comments

Comments
 (0)