-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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; | ||||||
|
@@ -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, | ||||||
|
@@ -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'); | ||||||
|
@@ -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) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
array_unshift($this->cloudIdResolvers, $resolver); | ||||||
} | ||||||
|
||||||
/** | ||||||
* @param ICloudIdResolver $resolver | ||||||
*/ | ||||||
public function unregisterCloudIdResolver(ICloudIdResolver $resolver) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
if (($key = array_search($resolver, $this->cloudIdResolvers)) !== false) { | ||||||
array_splice($this->cloudIdResolvers, $key, 1); | ||||||
} | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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 { | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||
/** @var string */ | ||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
*/ | ||||||
public function registerCloudIdResolver(ICloudIdResolver $resolver); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
/** | ||||||
* @param ICloudIdResolver $resolver | ||||||
* | ||||||
* @since 26.0.0 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
*/ | ||||||
public function unregisterCloudIdResolver(ICloudIdResolver $resolver); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} |
There was a problem hiding this comment.
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.