From 4ab3c16403e69811cf2353ba2bfeafcbcf259c42 Mon Sep 17 00:00:00 2001 From: Louis Chemineau Date: Wed, 18 Jan 2023 16:53:22 +0100 Subject: Pluggable share provider Signed-off-by: Carl Schwan Signed-off-by: Louis Chemineau --- .../AppFramework/Bootstrap/RegistrationContext.php | 22 ++++++++ lib/private/Server.php | 2 + lib/private/Share20/PublicShareTemplateFactory.php | 63 ++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 lib/private/Share20/PublicShareTemplateFactory.php (limited to 'lib/private') diff --git a/lib/private/AppFramework/Bootstrap/RegistrationContext.php b/lib/private/AppFramework/Bootstrap/RegistrationContext.php index 236aa106d02..a78a895d029 100644 --- a/lib/private/AppFramework/Bootstrap/RegistrationContext.php +++ b/lib/private/AppFramework/Bootstrap/RegistrationContext.php @@ -52,6 +52,7 @@ use OCP\Http\WellKnown\IHandler; use OCP\Notification\INotifier; use OCP\Profile\ILinkAction; use OCP\Search\IProvider; +use OCP\Share\IPublicShareTemplateProvider; use OCP\Support\CrashReport\IReporter; use OCP\UserMigration\IMigrator as IUserMigrator; use Psr\Log\LoggerInterface; @@ -127,6 +128,9 @@ class RegistrationContext { /** @var ParameterRegistration[] */ private $sensitiveMethods = []; + /** @var ServiceRegistration[] */ + private $publicShareTemplateProviders = []; + /** @var LoggerInterface */ private $logger; @@ -326,6 +330,13 @@ class RegistrationContext { $methods ); } + + public function registerPublicShareTemplateProvider(string $class): void { + $this->context->registerPublicShareTemplateProvider( + $this->appId, + $class + ); + } }; } @@ -461,6 +472,10 @@ class RegistrationContext { $this->sensitiveMethods[] = new ParameterRegistration($appId, $class, $methods); } + public function registerPublicShareTemplateProvider(string $appId, string $class): void { + $this->publicShareTemplateProviders[] = new ServiceRegistration($appId, $class); + } + /** * @param App[] $apps */ @@ -738,4 +753,11 @@ class RegistrationContext { public function getSensitiveMethods(): array { return $this->sensitiveMethods; } + + /** + * @return ServiceRegistration[] + */ + public function getPublicShareTemplateProviders(): array { + return $this->publicShareTemplateProviders; + } } diff --git a/lib/private/Server.php b/lib/private/Server.php index d8857b4efc6..bd33cdf58bd 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -1451,6 +1451,8 @@ class Server extends ServerContainer implements IServerContainer { $this->registerAlias(IBinaryFinder::class, BinaryFinder::class); + $this->registerAlias(\OCP\Share\IPublicShareTemplateFactory::class, \OC\Share20\PublicShareTemplateFactory::class); + $this->connectDispatcher(); } diff --git a/lib/private/Share20/PublicShareTemplateFactory.php b/lib/private/Share20/PublicShareTemplateFactory.php new file mode 100644 index 00000000000..222f327496a --- /dev/null +++ b/lib/private/Share20/PublicShareTemplateFactory.php @@ -0,0 +1,63 @@ + + * + * @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 . + * + */ + +namespace OC\Share20; + +use Exception; +use OC\AppFramework\Bootstrap\Coordinator; +use OCA\Files_Sharing\DefaultPublicShareTemplateProvider; +use OCP\Server; +use OCP\Share\IShare; +use OCP\Share\IPublicShareTemplateFactory; +use OCP\Share\IPublicShareTemplateProvider; + +class PublicShareTemplateFactory implements IPublicShareTemplateFactory { + public function __construct( + private Coordinator $coordinator, + private DefaultPublicShareTemplateProvider $defaultProvider, + ) { + } + + public function getProvider(IShare $share): IPublicShareTemplateProvider { + $context = $this->coordinator->getRegistrationContext(); + if ($context === null) { + throw new Exception("Can't retrieve public share template providers as context is not defined"); + } + + $providers = array_map( + fn ($registration) => Server::get($registration->getService()), + $context->getPublicShareTemplateProviders() + ); + + $filteredProviders = array_filter( + $providers, + fn (IPublicShareTemplateProvider $provider) => $provider->shouldRespond($share) + ); + + if (count($filteredProviders) === 0) { + return $this->defaultProvider; + } else { + return array_shift($filteredProviders); + } + } +} -- cgit v1.2.3