Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions apps/files_sharing/tests/ExternalStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
namespace OCA\Files_Sharing\Tests;

use OC\Federation\CloudId;
use OCA\Files_Sharing\External\Manager as ExternalShareManager;
use OCA\Files_Sharing\External\Storage;
use OCP\Http\Client\IClient;
Expand Down Expand Up @@ -74,7 +73,7 @@ private function getTestStorage($uri) {

return new TestSharingExternalStorage(
[
'cloudId' => new CloudId('testOwner@' . $uri, 'testOwner', $uri),
'cloudId' => new \OCP\Federation\CloudId('testOwner@' . $uri, 'testOwner', $uri),
'remote' => $uri,
'owner' => 'testOwner',
'mountpoint' => 'remoteshare',
Expand Down
3 changes: 2 additions & 1 deletion lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@
'OCP\\Exceptions\\AppConfigIncorrectTypeException' => $baseDir . '/lib/public/Exceptions/AppConfigIncorrectTypeException.php',
'OCP\\Exceptions\\AppConfigTypeConflictException' => $baseDir . '/lib/public/Exceptions/AppConfigTypeConflictException.php',
'OCP\\Exceptions\\AppConfigUnknownKeyException' => $baseDir . '/lib/public/Exceptions/AppConfigUnknownKeyException.php',
'OCP\\Federation\\CloudId' => $baseDir . '/lib/public/Federation/CloudId.php',
'OCP\\Federation\\Events\\TrustedServerRemovedEvent' => $baseDir . '/lib/public/Federation/Events/TrustedServerRemovedEvent.php',
'OCP\\Federation\\Exceptions\\ActionNotSupportedException' => $baseDir . '/lib/public/Federation/Exceptions/ActionNotSupportedException.php',
'OCP\\Federation\\Exceptions\\AuthenticationFailedException' => $baseDir . '/lib/public/Federation/Exceptions/AuthenticationFailedException.php',
Expand All @@ -348,6 +349,7 @@
'OCP\\Federation\\ICloudFederationShare' => $baseDir . '/lib/public/Federation/ICloudFederationShare.php',
'OCP\\Federation\\ICloudId' => $baseDir . '/lib/public/Federation/ICloudId.php',
'OCP\\Federation\\ICloudIdManager' => $baseDir . '/lib/public/Federation/ICloudIdManager.php',
'OCP\\Federation\\ICloudIdResolver' => $baseDir . '/lib/public/Federation/ICloudIdResolver.php',
'OCP\\Files' => $baseDir . '/lib/public/Files.php',
'OCP\\FilesMetadata\\AMetadataEvent' => $baseDir . '/lib/public/FilesMetadata/AMetadataEvent.php',
'OCP\\FilesMetadata\\Event\\MetadataBackgroundEvent' => $baseDir . '/lib/public/FilesMetadata/Event/MetadataBackgroundEvent.php',
Expand Down Expand Up @@ -1555,7 +1557,6 @@
'OC\\Federation\\CloudFederationNotification' => $baseDir . '/lib/private/Federation/CloudFederationNotification.php',
'OC\\Federation\\CloudFederationProviderManager' => $baseDir . '/lib/private/Federation/CloudFederationProviderManager.php',
'OC\\Federation\\CloudFederationShare' => $baseDir . '/lib/private/Federation/CloudFederationShare.php',
'OC\\Federation\\CloudId' => $baseDir . '/lib/private/Federation/CloudId.php',
'OC\\Federation\\CloudIdManager' => $baseDir . '/lib/private/Federation/CloudIdManager.php',
'OC\\FilesMetadata\\FilesMetadataManager' => $baseDir . '/lib/private/FilesMetadata/FilesMetadataManager.php',
'OC\\FilesMetadata\\Job\\UpdateSingleMetadata' => $baseDir . '/lib/private/FilesMetadata/Job/UpdateSingleMetadata.php',
Expand Down
3 changes: 2 additions & 1 deletion lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Exceptions\\AppConfigIncorrectTypeException' => __DIR__ . '/../../..' . '/lib/public/Exceptions/AppConfigIncorrectTypeException.php',
'OCP\\Exceptions\\AppConfigTypeConflictException' => __DIR__ . '/../../..' . '/lib/public/Exceptions/AppConfigTypeConflictException.php',
'OCP\\Exceptions\\AppConfigUnknownKeyException' => __DIR__ . '/../../..' . '/lib/public/Exceptions/AppConfigUnknownKeyException.php',
'OCP\\Federation\\CloudId' => __DIR__ . '/../../..' . '/lib/public/Federation/CloudId.php',
'OCP\\Federation\\Events\\TrustedServerRemovedEvent' => __DIR__ . '/../../..' . '/lib/public/Federation/Events/TrustedServerRemovedEvent.php',
'OCP\\Federation\\Exceptions\\ActionNotSupportedException' => __DIR__ . '/../../..' . '/lib/public/Federation/Exceptions/ActionNotSupportedException.php',
'OCP\\Federation\\Exceptions\\AuthenticationFailedException' => __DIR__ . '/../../..' . '/lib/public/Federation/Exceptions/AuthenticationFailedException.php',
Expand All @@ -397,6 +398,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Federation\\ICloudFederationShare' => __DIR__ . '/../../..' . '/lib/public/Federation/ICloudFederationShare.php',
'OCP\\Federation\\ICloudId' => __DIR__ . '/../../..' . '/lib/public/Federation/ICloudId.php',
'OCP\\Federation\\ICloudIdManager' => __DIR__ . '/../../..' . '/lib/public/Federation/ICloudIdManager.php',
'OCP\\Federation\\ICloudIdResolver' => __DIR__ . '/../../..' . '/lib/public/Federation/ICloudIdResolver.php',
'OCP\\Files' => __DIR__ . '/../../..' . '/lib/public/Files.php',
'OCP\\FilesMetadata\\AMetadataEvent' => __DIR__ . '/../../..' . '/lib/public/FilesMetadata/AMetadataEvent.php',
'OCP\\FilesMetadata\\Event\\MetadataBackgroundEvent' => __DIR__ . '/../../..' . '/lib/public/FilesMetadata/Event/MetadataBackgroundEvent.php',
Expand Down Expand Up @@ -1604,7 +1606,6 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Federation\\CloudFederationNotification' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudFederationNotification.php',
'OC\\Federation\\CloudFederationProviderManager' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudFederationProviderManager.php',
'OC\\Federation\\CloudFederationShare' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudFederationShare.php',
'OC\\Federation\\CloudId' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudId.php',
'OC\\Federation\\CloudIdManager' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudIdManager.php',
'OC\\FilesMetadata\\FilesMetadataManager' => __DIR__ . '/../../..' . '/lib/private/FilesMetadata/FilesMetadataManager.php',
'OC\\FilesMetadata\\Job\\UpdateSingleMetadata' => __DIR__ . '/../../..' . '/lib/private/FilesMetadata/Job/UpdateSingleMetadata.php',
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 @@ -12,8 +12,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 @@ -31,6 +33,8 @@ class CloudIdManager implements ICloudIdManager {
private ICache $displayNameCache;
/** @var array[] */
private array $cache = [];
/** @var ICloudIdResolver[] */
private array $cloudIdResolvers = [];

public function __construct(
IManager $contactsManager,
Expand Down Expand Up @@ -80,6 +84,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 @@ -245,6 +255,28 @@ protected function stripShareLinkFragments(string $remote): string {
* @return bool
*/
public function isValidCloudId(string $cloudId): bool {
return str_contains($cloudId, '@');
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 @@ -6,11 +6,17 @@
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Federation;
namespace OCP\Federation;

use OCP\Federation\ICloudId;
use OCP\Federation\ICloudIdManager;
use OC\Federation\CloudIdManager;

/**
* Class CloudId
*
* @package OCP\Federation
*
* @since 12.0.0
*/
class CloudId implements ICloudId {
public function __construct(
protected string $id,
Expand All @@ -31,8 +37,7 @@ public function getId(): string {

public function getDisplayId(): string {
if ($this->displayName === null) {
/** @var CloudIdManager $cloudIdManager */
$cloudIdManager = \OCP\Server::get(ICloudIdManager::class);
$cloudIdManager = \OCP\Server::get(CloudIdManager::class);
$this->displayName = $cloudIdManager->getDisplayNameFromContact($this->getId());
}

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 @@ -55,4 +55,18 @@ public function isValidCloudId(string $cloudId): bool;
* @since 30.0.0 - Optional parameter $httpsOnly was added
*/
public function removeProtocolFromUrl(string $url, bool $httpsOnly = false): string;

/**
* @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 <[email protected]>
*
* @author Joas Schilling <[email protected]>
* @author Robin Appelman <[email protected]>
* @author Roeland Jago Douma <[email protected]>
* @author Sandro Mesterheide <[email protected]>
*
* @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 @@ -7,9 +7,9 @@
namespace Test\Collaboration\Collaborators;

use OC\Collaboration\Collaborators\LookupPlugin;
use OC\Federation\CloudId;
use OCP\Collaboration\Collaborators\ISearchResult;
use OCP\Collaboration\Collaborators\SearchResultType;
use OCP\Federation\CloudId;
use OCP\Federation\ICloudId;
use OCP\Federation\ICloudIdManager;
use OCP\Http\Client\IClient;
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 @@ -9,8 +9,8 @@

namespace Test\Federation;

use OC\Federation\CloudId;
use OC\Federation\CloudIdManager;
use OCP\Federation\CloudId;
use OCP\Federation\ICloudIdManager;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
Expand Down
Loading