Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cloud id resolver interface #35732

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
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
32 changes: 32 additions & 0 deletions 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 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if there is a better way, but isValidCloudId will be called twice on each resolver, since it is then called by $this->isValidCloudId. Not ideal performance-wise long-term.

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function registerCloudIdResolver(ICloudIdResolver $resolver) {
public function registerCloudIdResolver(ICloudIdResolver $resolver): void {

array_unshift($this->cloudIdResolvers, $resolver);
}

/**
* @param ICloudIdResolver $resolver
*/
public function unregisterCloudIdResolver(ICloudIdResolver $resolver) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function unregisterCloudIdResolver(ICloudIdResolver $resolver) {
public function unregisterCloudIdResolver(ICloudIdResolver $resolver): void {

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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class CloudId implements ICloudId {
/**
* @since 27.0.0
*/
class CloudId implements ICloudId {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/** @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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @since 26.0.0
* @since 27.0.0

*/
public function registerCloudIdResolver(ICloudIdResolver $resolver);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function registerCloudIdResolver(ICloudIdResolver $resolver);
public function registerCloudIdResolver(ICloudIdResolver $resolver): void;


/**
* @param ICloudIdResolver $resolver
*
* @since 26.0.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @since 26.0.0
* @since 27.0.0

*/
public function unregisterCloudIdResolver(ICloudIdResolver $resolver);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function unregisterCloudIdResolver(ICloudIdResolver $resolver);
public function unregisterCloudIdResolver(ICloudIdResolver $resolver): void;

}
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;
Comment on lines +46 to +54
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the resolver returns true it indicates to the CloudIdManager that this cloud id should also be resolved by it. This is a cursory check and an exception can still be thrown during resolveCloudId

}
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