Skip to content

Commit

Permalink
Add cloud id resolver interface
Browse files Browse the repository at this point in the history
Signed-off-by: Sandro Mesterheide <sandro.mesterheide@extern.publicplan.de>
  • Loading branch information
smesterheide committed Feb 20, 2023
1 parent a5443ac commit 2fe2427
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 7 deletions.
2 changes: 1 addition & 1 deletion apps/files_sharing/tests/ExternalStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/
namespace OCA\Files_Sharing\Tests;

use OC\Federation\CloudId;
use OCP\Federation\CloudId;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use OCP\Http\Client\IResponse;
Expand Down
34 changes: 33 additions & 1 deletion lib/private/Federation/CloudIdManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
use OCP\Contacts\IManager;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Federation\CloudId;
use OCP\Federation\ICloudId;
use OCP\Federation\ICloudIdManager;
use OCP\Federation\ICloudIdResolver;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IURLGenerator;
Expand All @@ -52,6 +54,8 @@ class CloudIdManager implements ICloudIdManager {
private ICache $memCache;
/** @var array[] */
private array $cache = [];
/** @var ICloudIdResolver[] */
private array $cloudIdResolvers = [];

public function __construct(
IManager $contactsManager,
Expand All @@ -65,7 +69,7 @@ public function __construct(
$this->userManager = $userManager;
$this->memCache = $cacheFactory->createDistributed('cloud_id_');
$eventDispatcher->addListener(UserChangedEvent::class, [$this, 'handleUserEvent']);
$eventDispatcher->addListener(CardUpdatedEvent::class, [$this, 'handleCardEvent']);
$eventDispatcher->addListener(CardUpdatedEvent::class, [$this, 'handleCardEvent']);
}

public function handleUserEvent(Event $event): void {
Expand Down Expand Up @@ -100,6 +104,12 @@ public function handleCardEvent(Event $event): void {
*/
public function resolveCloudId(string $cloudId): ICloudId {
// TODO magic here to get the url and user instead of just splitting on @

foreach ($this->cloudIdResolvers as $resolver) {
if ($resolver->isValidCloudId($cloudId)) {
return $resolver->resolveCloudId($cloudId);
}
}

if (!$this->isValidCloudId($cloudId)) {
throw new \InvalidArgumentException('Invalid cloud id');
Expand Down Expand Up @@ -246,6 +256,28 @@ protected function fixRemoteURL(string $remote): string {
* @return bool
*/
public function isValidCloudId(string $cloudId): bool {
foreach ($this->cloudIdResolvers as $resolver) {
if ($resolver->isValidCloudId($cloudId)) {
return true;
}
}

return strpos($cloudId, '@') !== false;
}

/**
* @param ICloudIdResolver $resolver
*/
public function registerCloudIdResolver(ICloudIdResolver $resolver) {
array_unshift($this->cloudIdResolvers, $resolver);
}

/**
* @param ICloudIdResolver $resolver
*/
public function unregisterCloudIdResolver(ICloudIdResolver $resolver) {
if (($key = array_search($resolver, $this->cloudIdResolvers)) !== false) {
array_splice($this->cloudIdResolvers, $key, 1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OC\Federation;

use OCP\Federation\ICloudId;
namespace OCP\Federation;

class CloudId implements ICloudId {
/** @var string */
Expand Down
14 changes: 14 additions & 0 deletions lib/public/Federation/ICloudIdManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,18 @@ public function getCloudId(string $user, ?string $remote): ICloudId;
* @since 12.0.0
*/
public function isValidCloudId(string $cloudId): bool;

/**
* @param ICloudIdResolver $resolver
*
* @since 26.0.0
*/
public function registerCloudIdResolver(ICloudIdResolver $resolver);

/**
* @param ICloudIdResolver $resolver
*
* @since 26.0.0
*/
public function unregisterCloudIdResolver(ICloudIdResolver $resolver);
}
55 changes: 55 additions & 0 deletions lib/public/Federation/ICloudIdResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2017, Robin Appelman <robin@icewind.nl>
*
* @author Joas Schilling <coding@schilljs.com>
* @author Robin Appelman <robin@icewind.nl>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Sandro Mesterheide <sandro.mesterheide@extern.publicplan.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCP\Federation;

/**
* Interface for resolving federated cloud ids
*
* @since 26.0.0
*/
interface ICloudIdResolver{
/**
* @param string $cloudId
* @return ICloudId
* @throws \InvalidArgumentException
*
* @since 26.0.0
*/
public function resolveCloudId(string $cloudId): ICloudId;

/**
* Check if the input is a correctly formatted cloud id
*
* @param string $cloudId
* @return bool
*
* @since 26.0.0
*/
public function isValidCloudId(string $cloudId): bool;
}
2 changes: 1 addition & 1 deletion tests/lib/Collaboration/Collaborators/LookupPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
namespace Test\Collaboration\Collaborators;

use OC\Collaboration\Collaborators\LookupPlugin;
use OC\Federation\CloudId;
use OCP\Federation\CloudId;
use OCP\Collaboration\Collaborators\ISearchResult;
use OCP\Collaboration\Collaborators\SearchResultType;
use OCP\Federation\ICloudId;
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/Federation/CloudIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace Test\Federation;

use OC\Federation\CloudId;
use OCP\Federation\CloudId;
use Test\TestCase;

class CloudIdTest extends TestCase {
Expand Down

0 comments on commit 2fe2427

Please sign in to comment.