From 0763a173321488acaadceb1eb0ecf1ec8691bf21 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Mon, 13 Jul 2020 15:43:42 +0200 Subject: [PATCH] Move federated_share_added into a typed event Signed-off-by: Morris Jobke --- .../composer/composer/autoload_classmap.php | 1 + .../dav/composer/composer/autoload_static.php | 1 + .../InvitationResponseServer.php | 10 ++-- .../lib/Events/SabrePluginAuthInitEvent.php | 56 +++++++++++++++++ .../composer/composer/autoload_classmap.php | 1 + .../composer/composer/autoload_static.php | 1 + .../lib/Events/FederatedShareAddedEvent.php | 54 +++++++++++++++++ .../lib/Notifications.php | 20 +++---- .../lib/OCM/CloudFederationProviderFiles.php | 4 +- .../tests/NotificationsTest.php | 11 +++- apps/federation/appinfo/app.php | 28 --------- .../composer/composer/autoload_classmap.php | 2 + .../composer/composer/autoload_static.php | 2 + apps/federation/lib/AppInfo/Application.php | 54 +++++------------ .../Listeners/FederatedShareAddedListener.php | 60 +++++++++++++++++++ .../Listeners/SabrePluginAuthInitListener.php | 57 ++++++++++++++++++ .../files_sharing/lib/AppInfo/Application.php | 3 +- apps/files_sharing/lib/External/Manager.php | 53 ++++++---------- apps/files_sharing/lib/Hooks.php | 5 +- .../tests/External/ManagerTest.php | 7 ++- lib/private/Share20/ProviderFactory.php | 4 +- 21 files changed, 309 insertions(+), 125 deletions(-) create mode 100644 apps/dav/lib/Events/SabrePluginAuthInitEvent.php create mode 100644 apps/federatedfilesharing/lib/Events/FederatedShareAddedEvent.php delete mode 100644 apps/federation/appinfo/app.php create mode 100644 apps/federation/lib/Listeners/FederatedShareAddedListener.php create mode 100644 apps/federation/lib/Listeners/SabrePluginAuthInitListener.php diff --git a/apps/dav/composer/composer/autoload_classmap.php b/apps/dav/composer/composer/autoload_classmap.php index 71cf0c1087..b42750c25e 100644 --- a/apps/dav/composer/composer/autoload_classmap.php +++ b/apps/dav/composer/composer/autoload_classmap.php @@ -170,6 +170,7 @@ return array( 'OCA\\DAV\\Direct\\DirectHome' => $baseDir . '/../lib/Direct/DirectHome.php', 'OCA\\DAV\\Direct\\Server' => $baseDir . '/../lib/Direct/Server.php', 'OCA\\DAV\\Direct\\ServerFactory' => $baseDir . '/../lib/Direct/ServerFactory.php', + 'OCA\\DAV\\Events\\SabrePluginAuthInitEvent' => $baseDir . '/../lib/Events/SabrePluginAuthInitEvent.php', 'OCA\\DAV\\Exception\\UnsupportedLimitOnInitialSyncException' => $baseDir . '/../lib/Exception/UnsupportedLimitOnInitialSyncException.php', 'OCA\\DAV\\Files\\BrowserErrorPagePlugin' => $baseDir . '/../lib/Files/BrowserErrorPagePlugin.php', 'OCA\\DAV\\Files\\FileSearchBackend' => $baseDir . '/../lib/Files/FileSearchBackend.php', diff --git a/apps/dav/composer/composer/autoload_static.php b/apps/dav/composer/composer/autoload_static.php index c0ed98a03c..2d579289f0 100644 --- a/apps/dav/composer/composer/autoload_static.php +++ b/apps/dav/composer/composer/autoload_static.php @@ -185,6 +185,7 @@ class ComposerStaticInitDAV 'OCA\\DAV\\Direct\\DirectHome' => __DIR__ . '/..' . '/../lib/Direct/DirectHome.php', 'OCA\\DAV\\Direct\\Server' => __DIR__ . '/..' . '/../lib/Direct/Server.php', 'OCA\\DAV\\Direct\\ServerFactory' => __DIR__ . '/..' . '/../lib/Direct/ServerFactory.php', + 'OCA\\DAV\\Events\\SabrePluginAuthInitEvent' => __DIR__ . '/..' . '/../lib/Events/SabrePluginAuthInitEvent.php', 'OCA\\DAV\\Exception\\UnsupportedLimitOnInitialSyncException' => __DIR__ . '/..' . '/../lib/Exception/UnsupportedLimitOnInitialSyncException.php', 'OCA\\DAV\\Files\\BrowserErrorPagePlugin' => __DIR__ . '/..' . '/../lib/Files/BrowserErrorPagePlugin.php', 'OCA\\DAV\\Files\\FileSearchBackend' => __DIR__ . '/..' . '/../lib/Files/FileSearchBackend.php', diff --git a/apps/dav/lib/CalDAV/InvitationResponse/InvitationResponseServer.php b/apps/dav/lib/CalDAV/InvitationResponse/InvitationResponseServer.php index 3f4031740c..5040319bcb 100644 --- a/apps/dav/lib/CalDAV/InvitationResponse/InvitationResponseServer.php +++ b/apps/dav/lib/CalDAV/InvitationResponse/InvitationResponseServer.php @@ -30,8 +30,9 @@ use OCA\DAV\Connector\Sabre\AnonymousOptionsPlugin; use OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin; use OCA\DAV\Connector\Sabre\CachingTree; use OCA\DAV\Connector\Sabre\DavAclPlugin; +use OCA\DAV\Events\SabrePluginAuthInitEvent; use OCA\DAV\RootCollection; -use OCP\SabrePluginEvent; +use OCP\EventDispatcher\IEventDispatcher; use Sabre\DAV\Auth\Plugin; use Sabre\VObject\ITip\Message; @@ -46,7 +47,8 @@ class InvitationResponseServer { public function __construct() { $baseUri = \OC::$WEBROOT . '/remote.php/dav/'; $logger = \OC::$server->getLogger(); - $dispatcher = \OC::$server->getEventDispatcher(); + /** @var IEventDispatcher $dispatcher */ + $dispatcher = \OC::$server->query(IEventDispatcher::class); $root = new RootCollection(); $this->server = new \OCA\DAV\Connector\Sabre\Server(new CachingTree($root)); @@ -67,8 +69,8 @@ class InvitationResponseServer { }); // allow setup of additional auth backends - $event = new SabrePluginEvent($this->server); - $dispatcher->dispatch('OCA\DAV\Connector\Sabre::authInit', $event); + $event = new SabrePluginAuthInitEvent($this->server); + $dispatcher->dispatchTyped($event); $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $logger)); $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin()); diff --git a/apps/dav/lib/Events/SabrePluginAuthInitEvent.php b/apps/dav/lib/Events/SabrePluginAuthInitEvent.php new file mode 100644 index 0000000000..ca3c7b6552 --- /dev/null +++ b/apps/dav/lib/Events/SabrePluginAuthInitEvent.php @@ -0,0 +1,56 @@ + + * + * @author Morris Jobke + * + * @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 OCA\DAV\Events; + +use OCP\EventDispatcher\Event; +use Sabre\DAV\Server; + +/** + * This event is triggered during the setup of the SabreDAV server to allow the + * registration of additional authentication backends. + * + * @since 20.0.0 + */ +class SabrePluginAuthInitEvent extends Event { + + /** @var Server */ + private $server; + + /** + * @since 20.0.0 + */ + public function __construct(Server $server) { + $this->server = $server; + } + + /** + * @since 20.0.0 + */ + public function getServer(): Server { + return $this->server; + } +} diff --git a/apps/federatedfilesharing/composer/composer/autoload_classmap.php b/apps/federatedfilesharing/composer/composer/autoload_classmap.php index 0806082369..8d1364c0d6 100644 --- a/apps/federatedfilesharing/composer/composer/autoload_classmap.php +++ b/apps/federatedfilesharing/composer/composer/autoload_classmap.php @@ -11,6 +11,7 @@ return array( 'OCA\\FederatedFileSharing\\BackgroundJob\\RetryJob' => $baseDir . '/../lib/BackgroundJob/RetryJob.php', 'OCA\\FederatedFileSharing\\Controller\\MountPublicLinkController' => $baseDir . '/../lib/Controller/MountPublicLinkController.php', 'OCA\\FederatedFileSharing\\Controller\\RequestHandlerController' => $baseDir . '/../lib/Controller/RequestHandlerController.php', + 'OCA\\FederatedFileSharing\\Events\\FederatedShareAddedEvent' => $baseDir . '/../lib/Events/FederatedShareAddedEvent.php', 'OCA\\FederatedFileSharing\\FederatedShareProvider' => $baseDir . '/../lib/FederatedShareProvider.php', 'OCA\\FederatedFileSharing\\Listeners\\LoadAdditionalScriptsListener' => $baseDir . '/../lib/Listeners/LoadAdditionalScriptsListener.php', 'OCA\\FederatedFileSharing\\Migration\\Version1010Date20200630191755' => $baseDir . '/../lib/Migration/Version1010Date20200630191755.php', diff --git a/apps/federatedfilesharing/composer/composer/autoload_static.php b/apps/federatedfilesharing/composer/composer/autoload_static.php index b0e17eb568..6e352d6f32 100644 --- a/apps/federatedfilesharing/composer/composer/autoload_static.php +++ b/apps/federatedfilesharing/composer/composer/autoload_static.php @@ -26,6 +26,7 @@ class ComposerStaticInitFederatedFileSharing 'OCA\\FederatedFileSharing\\BackgroundJob\\RetryJob' => __DIR__ . '/..' . '/../lib/BackgroundJob/RetryJob.php', 'OCA\\FederatedFileSharing\\Controller\\MountPublicLinkController' => __DIR__ . '/..' . '/../lib/Controller/MountPublicLinkController.php', 'OCA\\FederatedFileSharing\\Controller\\RequestHandlerController' => __DIR__ . '/..' . '/../lib/Controller/RequestHandlerController.php', + 'OCA\\FederatedFileSharing\\Events\\FederatedShareAddedEvent' => __DIR__ . '/..' . '/../lib/Events/FederatedShareAddedEvent.php', 'OCA\\FederatedFileSharing\\FederatedShareProvider' => __DIR__ . '/..' . '/../lib/FederatedShareProvider.php', 'OCA\\FederatedFileSharing\\Listeners\\LoadAdditionalScriptsListener' => __DIR__ . '/..' . '/../lib/Listeners/LoadAdditionalScriptsListener.php', 'OCA\\FederatedFileSharing\\Migration\\Version1010Date20200630191755' => __DIR__ . '/..' . '/../lib/Migration/Version1010Date20200630191755.php', diff --git a/apps/federatedfilesharing/lib/Events/FederatedShareAddedEvent.php b/apps/federatedfilesharing/lib/Events/FederatedShareAddedEvent.php new file mode 100644 index 0000000000..f1269b9f93 --- /dev/null +++ b/apps/federatedfilesharing/lib/Events/FederatedShareAddedEvent.php @@ -0,0 +1,54 @@ + + * + * @author Morris Jobke + * + * @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 OCA\FederatedFileSharing\Events; + +use OCP\EventDispatcher\Event; + +/** + * This event is triggered when a federated share is successfully added + * + * @since 20.0.0 + */ +class FederatedShareAddedEvent extends Event { + + /** @var string */ + private $remote; + + /** + * @since 20.0.0 + */ + public function __construct(string $remote) { + $this->remote = $remote; + } + + /** + * @since 20.0.0 + */ + public function getRemote(): string { + return $this->remote; + } +} diff --git a/apps/federatedfilesharing/lib/Notifications.php b/apps/federatedfilesharing/lib/Notifications.php index 7b2105ecb0..2dc3104e6a 100644 --- a/apps/federatedfilesharing/lib/Notifications.php +++ b/apps/federatedfilesharing/lib/Notifications.php @@ -25,8 +25,10 @@ namespace OCA\FederatedFileSharing; +use OCA\FederatedFileSharing\Events\FederatedShareAddedEvent; use OCP\AppFramework\Http; use OCP\BackgroundJob\IJobList; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Federation\ICloudFederationFactory; use OCP\Federation\ICloudFederationProviderManager; use OCP\Http\Client\IClientService; @@ -53,21 +55,17 @@ class Notifications { /** @var ICloudFederationFactory */ private $cloudFederationFactory; - /** - * @param AddressHandler $addressHandler - * @param IClientService $httpClientService - * @param IDiscoveryService $discoveryService - * @param IJobList $jobList - * @param ICloudFederationProviderManager $federationProviderManager - * @param ICloudFederationFactory $cloudFederationFactory - */ + /** @var IEventDispatcher */ + private $eventDispatcher; + public function __construct( AddressHandler $addressHandler, IClientService $httpClientService, IDiscoveryService $discoveryService, IJobList $jobList, ICloudFederationProviderManager $federationProviderManager, - ICloudFederationFactory $cloudFederationFactory + ICloudFederationFactory $cloudFederationFactory, + IEventDispatcher $eventDispatcher ) { $this->addressHandler = $addressHandler; $this->httpClientService = $httpClientService; @@ -75,6 +73,7 @@ class Notifications { $this->jobList = $jobList; $this->federationProviderManager = $federationProviderManager; $this->cloudFederationFactory = $cloudFederationFactory; + $this->eventDispatcher = $eventDispatcher; } /** @@ -119,7 +118,8 @@ class Notifications { $ocsSuccess = $ocsStatus && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200); if ($result['success'] && (!$ocsStatus ||$ocsSuccess)) { - \OC_Hook::emit('OCP\Share', 'federated_share_added', ['server' => $remote]); + $event = new FederatedShareAddedEvent($remote); + $this->eventDispatcher->dispatchTyped($event); return true; } } diff --git a/apps/federatedfilesharing/lib/OCM/CloudFederationProviderFiles.php b/apps/federatedfilesharing/lib/OCM/CloudFederationProviderFiles.php index d427e324e1..228e481655 100644 --- a/apps/federatedfilesharing/lib/OCM/CloudFederationProviderFiles.php +++ b/apps/federatedfilesharing/lib/OCM/CloudFederationProviderFiles.php @@ -34,6 +34,7 @@ use OCA\Files_Sharing\Activity\Providers\RemoteShares; use OCP\Activity\IManager as IActivityManager; use OCP\App\IAppManager; use OCP\Constants; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Federation\Exceptions\ActionNotSupportedException; use OCP\Federation\Exceptions\AuthenticationFailedException; use OCP\Federation\Exceptions\BadRequestException; @@ -240,7 +241,8 @@ class CloudFederationProviderFiles implements ICloudFederationProvider { \OC::$server->getCloudFederationFactory(), \OC::$server->getGroupManager(), \OC::$server->getUserManager(), - $shareWith + $shareWith, + \OC::$server->query(IEventDispatcher::class) ); try { diff --git a/apps/federatedfilesharing/tests/NotificationsTest.php b/apps/federatedfilesharing/tests/NotificationsTest.php index af48b59438..32ceedd45c 100644 --- a/apps/federatedfilesharing/tests/NotificationsTest.php +++ b/apps/federatedfilesharing/tests/NotificationsTest.php @@ -27,6 +27,7 @@ namespace OCA\FederatedFileSharing\Tests; use OCA\FederatedFileSharing\AddressHandler; use OCA\FederatedFileSharing\Notifications; use OCP\BackgroundJob\IJobList; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Federation\ICloudFederationFactory; use OCP\Federation\ICloudFederationProviderManager; use OCP\Http\Client\IClientService; @@ -52,6 +53,9 @@ class NotificationsTest extends \Test\TestCase { /** @var ICloudFederationFactory|\PHPUnit_Framework_MockObject_MockObject */ private $cloudFederationFactory; + /** @var IEventDispatcher|\PHPUnit_Framework_MockObject_MockObject */ + private $eventDispatcher; + protected function setUp(): void { parent::setUp(); @@ -62,6 +66,7 @@ class NotificationsTest extends \Test\TestCase { ->disableOriginalConstructor()->getMock(); $this->cloudFederationProviderManager = $this->createMock(ICloudFederationProviderManager::class); $this->cloudFederationFactory = $this->createMock(ICloudFederationFactory::class); + $this->eventDispatcher = $this->createMock(IEventDispatcher::class); } /** @@ -78,7 +83,8 @@ class NotificationsTest extends \Test\TestCase { $this->discoveryService, $this->jobList, $this->cloudFederationProviderManager, - $this->cloudFederationFactory + $this->cloudFederationFactory, + $this->eventDispatcher ); } else { $instance = $this->getMockBuilder('OCA\FederatedFileSharing\Notifications') @@ -89,7 +95,8 @@ class NotificationsTest extends \Test\TestCase { $this->discoveryService, $this->jobList, $this->cloudFederationProviderManager, - $this->cloudFederationFactory + $this->cloudFederationFactory, + $this->eventDispatcher ] )->setMethods($mockedMethods)->getMock(); } diff --git a/apps/federation/appinfo/app.php b/apps/federation/appinfo/app.php deleted file mode 100644 index c48c2e208a..0000000000 --- a/apps/federation/appinfo/app.php +++ /dev/null @@ -1,28 +0,0 @@ - - * @author Christoph Wurst - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Federation\AppInfo; - -/** @var Application $app */ -$app = \OC::$server->query(Application::class); -$app->registerHooks(); diff --git a/apps/federation/composer/composer/autoload_classmap.php b/apps/federation/composer/composer/autoload_classmap.php index 5c2bda1785..922b99c094 100644 --- a/apps/federation/composer/composer/autoload_classmap.php +++ b/apps/federation/composer/composer/autoload_classmap.php @@ -15,6 +15,8 @@ return array( 'OCA\\Federation\\DAV\\FedAuth' => $baseDir . '/../lib/DAV/FedAuth.php', 'OCA\\Federation\\DbHandler' => $baseDir . '/../lib/DbHandler.php', 'OCA\\Federation\\Hooks' => $baseDir . '/../lib/Hooks.php', + 'OCA\\Federation\\Listener\\FederatedShareAddedListener' => $baseDir . '/../lib/Listeners/FederatedShareAddedListener.php', + 'OCA\\Federation\\Listener\\SabrePluginAuthInitListener' => $baseDir . '/../lib/Listeners/SabrePluginAuthInitListener.php', 'OCA\\Federation\\Middleware\\AddServerMiddleware' => $baseDir . '/../lib/Middleware/AddServerMiddleware.php', 'OCA\\Federation\\Migration\\Version1010Date20200630191302' => $baseDir . '/../lib/Migration/Version1010Date20200630191302.php', 'OCA\\Federation\\Settings\\Admin' => $baseDir . '/../lib/Settings/Admin.php', diff --git a/apps/federation/composer/composer/autoload_static.php b/apps/federation/composer/composer/autoload_static.php index 1ee4e0f04a..a79a03bd33 100644 --- a/apps/federation/composer/composer/autoload_static.php +++ b/apps/federation/composer/composer/autoload_static.php @@ -30,6 +30,8 @@ class ComposerStaticInitFederation 'OCA\\Federation\\DAV\\FedAuth' => __DIR__ . '/..' . '/../lib/DAV/FedAuth.php', 'OCA\\Federation\\DbHandler' => __DIR__ . '/..' . '/../lib/DbHandler.php', 'OCA\\Federation\\Hooks' => __DIR__ . '/..' . '/../lib/Hooks.php', + 'OCA\\Federation\\Listener\\FederatedShareAddedListener' => __DIR__ . '/..' . '/../lib/Listeners/FederatedShareAddedListener.php', + 'OCA\\Federation\\Listener\\SabrePluginAuthInitListener' => __DIR__ . '/..' . '/../lib/Listeners/SabrePluginAuthInitListener.php', 'OCA\\Federation\\Middleware\\AddServerMiddleware' => __DIR__ . '/..' . '/../lib/Middleware/AddServerMiddleware.php', 'OCA\\Federation\\Migration\\Version1010Date20200630191302' => __DIR__ . '/..' . '/../lib/Migration/Version1010Date20200630191302.php', 'OCA\\Federation\\Settings\\Admin' => __DIR__ . '/..' . '/../lib/Settings/Admin.php', diff --git a/apps/federation/lib/AppInfo/Application.php b/apps/federation/lib/AppInfo/Application.php index 09a6eed4ab..36a61298c9 100644 --- a/apps/federation/lib/AppInfo/Application.php +++ b/apps/federation/lib/AppInfo/Application.php @@ -27,58 +27,32 @@ namespace OCA\Federation\AppInfo; -use OCA\Federation\DAV\FedAuth; -use OCA\Federation\Hooks; +use OCA\DAV\Events\SabrePluginAuthInitEvent; +use OCA\FederatedFileSharing\Events\FederatedShareAddedEvent; +use OCA\Federation\Listener\FederatedShareAddedListener; +use OCA\Federation\Listener\SabrePluginAuthInitListener; use OCA\Federation\Middleware\AddServerMiddleware; use OCP\AppFramework\App; -use OCP\SabrePluginEvent; -use OCP\Share; -use OCP\Util; -use Sabre\DAV\Auth\Plugin; -use Sabre\DAV\Server; +use OCP\AppFramework\Bootstrap\IBootContext; +use OCP\AppFramework\Bootstrap\IBootstrap; +use OCP\AppFramework\Bootstrap\IRegistrationContext; -class Application extends App { +class Application extends App implements IBootstrap { /** * @param array $urlParams */ public function __construct($urlParams = []) { parent::__construct('federation', $urlParams); - $this->registerMiddleware(); } - private function registerMiddleware() { - $container = $this->getContainer(); - $container->registerAlias('AddServerMiddleware', AddServerMiddleware::class); - $container->registerMiddleWare('AddServerMiddleware'); + public function register(IRegistrationContext $context): void { + $context->registerMiddleware(AddServerMiddleware::class); + + $context->registerEventListener(FederatedShareAddedEvent::class, FederatedShareAddedListener::class); + $context->registerEventListener(SabrePluginAuthInitEvent::class, SabrePluginAuthInitListener::class); } - /** - * listen to federated_share_added hooks to auto-add new servers to the - * list of trusted servers. - */ - public function registerHooks() { - $container = $this->getContainer(); - $hooksManager = $container->query(Hooks::class); - - Util::connectHook( - Share::class, - 'federated_share_added', - $hooksManager, - 'addServerHook' - ); - - $dispatcher = $container->getServer()->getEventDispatcher(); - $dispatcher->addListener('OCA\DAV\Connector\Sabre::authInit', function ($event) use ($container) { - if ($event instanceof SabrePluginEvent) { - $server = $event->getServer(); - if ($server instanceof Server) { - $authPlugin = $server->getPlugin('auth'); - if ($authPlugin instanceof Plugin) { - $authPlugin->addBackend($container->query(FedAuth::class)); - } - } - } - }); + public function boot(IBootContext $context): void { } } diff --git a/apps/federation/lib/Listeners/FederatedShareAddedListener.php b/apps/federation/lib/Listeners/FederatedShareAddedListener.php new file mode 100644 index 0000000000..21eb945077 --- /dev/null +++ b/apps/federation/lib/Listeners/FederatedShareAddedListener.php @@ -0,0 +1,60 @@ + + * + * @author Morris Jobke + * + * @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 OCA\Federation\Listener; + +use OCA\FederatedFileSharing\Events\FederatedShareAddedEvent; +use OCA\Federation\TrustedServers; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; + +/** + * Automatically add new servers to the list of trusted servers. + * + * @since 20.0.0 + */ +class FederatedShareAddedListener implements IEventListener { + /** @var TrustedServers */ + private $trustedServers; + + public function __construct(TrustedServers $trustedServers) { + $this->trustedServers = $trustedServers; + } + + public function handle(Event $event): void { + if (!($event instanceof FederatedShareAddedEvent)) { + return; + } + + $server = $event->getRemote(); + if ( + $this->trustedServers->getAutoAddServers() === true && + $this->trustedServers->isTrustedServer($server) === false + ) { + $this->trustedServers->addServer($server); + } + } +} diff --git a/apps/federation/lib/Listeners/SabrePluginAuthInitListener.php b/apps/federation/lib/Listeners/SabrePluginAuthInitListener.php new file mode 100644 index 0000000000..2e5b25c929 --- /dev/null +++ b/apps/federation/lib/Listeners/SabrePluginAuthInitListener.php @@ -0,0 +1,57 @@ + + * + * @author Morris Jobke + * + * @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 OCA\Federation\Listener; + +use OCA\DAV\Events\SabrePluginAuthInitEvent; +use OCA\Federation\DAV\FedAuth; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use Sabre\DAV\Auth\Plugin; + +/** + * @since 20.0.0 + */ +class SabrePluginAuthInitListener implements IEventListener { + /** @var FedAuth */ + private $fedAuth; + + public function __construct(FedAuth $fedAuth) { + $this->fedAuth = $fedAuth; + } + + public function handle(Event $event): void { + if (!($event instanceof SabrePluginAuthInitEvent)) { + return; + } + + $server = $event->getServer(); + $authPlugin = $server->getPlugin('auth'); + if ($authPlugin instanceof Plugin) { + $authPlugin->addBackend($this->fedAuth); + } + } +} diff --git a/apps/files_sharing/lib/AppInfo/Application.php b/apps/files_sharing/lib/AppInfo/Application.php index 1b1bd08d3f..6448d0fab4 100644 --- a/apps/files_sharing/lib/AppInfo/Application.php +++ b/apps/files_sharing/lib/AppInfo/Application.php @@ -94,7 +94,8 @@ class Application extends App { $server->getCloudFederationFactory(), $server->getGroupManager(), $server->getUserManager(), - $uid + $uid, + $server->query(IEventDispatcher::class) ); }); diff --git a/apps/files_sharing/lib/External/Manager.php b/apps/files_sharing/lib/External/Manager.php index 943b5abfe0..3b4c2bc8f5 100644 --- a/apps/files_sharing/lib/External/Manager.php +++ b/apps/files_sharing/lib/External/Manager.php @@ -33,7 +33,9 @@ namespace OCA\Files_Sharing\External; use OC\Files\Filesystem; +use OCA\FederatedFileSharing\Events\FederatedShareAddedEvent; use OCA\Files_Sharing\Helper; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Federation\ICloudFederationFactory; use OCP\Federation\ICloudFederationProviderManager; use OCP\Files; @@ -50,39 +52,25 @@ use OCP\Share\IShare; class Manager { public const STORAGE = '\OCA\Files_Sharing\External\Storage'; - /** - * @var string - */ + /** @var string */ private $uid; - /** - * @var IDBConnection - */ + /** @var IDBConnection */ private $connection; - /** - * @var \OC\Files\Mount\Manager - */ + /** @var \OC\Files\Mount\Manager */ private $mountManager; - /** - * @var IStorageFactory - */ + /** @var IStorageFactory */ private $storageLoader; - /** - * @var IClientService - */ + /** @var IClientService */ private $clientService; - /** - * @var IManager - */ + /** @var IManager */ private $notificationManager; - /** - * @var IDiscoveryService - */ + /** @var IDiscoveryService */ private $discoveryService; /** @var ICloudFederationProviderManager */ @@ -97,19 +85,9 @@ class Manager { /** @var IUserManager */ private $userManager; - /** - * @param IDBConnection $connection - * @param \OC\Files\Mount\Manager $mountManager - * @param IStorageFactory $storageLoader - * @param IClientService $clientService - * @param IManager $notificationManager - * @param IDiscoveryService $discoveryService - * @param ICloudFederationProviderManager $cloudFederationProviderManager - * @param ICloudFederationFactory $cloudFederationFactory - * @param IGroupManager $groupManager - * @param IUserManager $userManager - * @param string $uid - */ + /** @var IEventDispatcher */ + private $eventDispatcher; + public function __construct(IDBConnection $connection, \OC\Files\Mount\Manager $mountManager, IStorageFactory $storageLoader, @@ -120,7 +98,8 @@ class Manager { ICloudFederationFactory $cloudFederationFactory, IGroupManager $groupManager, IUserManager $userManager, - $uid) { + string $uid, + IEventDispatcher $eventDispatcher) { $this->connection = $connection; $this->mountManager = $mountManager; $this->storageLoader = $storageLoader; @@ -132,6 +111,7 @@ class Manager { $this->cloudFederationFactory = $cloudFederationFactory; $this->groupManager = $groupManager; $this->userManager = $userManager; + $this->eventDispatcher = $eventDispatcher; } /** @@ -300,7 +280,8 @@ class Manager { } if ($userShareAccepted === true) { $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'accept'); - \OC_Hook::emit(Share::class, 'federated_share_added', ['server' => $share['remote']]); + $event = new FederatedShareAddedEvent($share['remote']); + $this->eventDispatcher->dispatchTyped($event); $result = true; } } diff --git a/apps/files_sharing/lib/Hooks.php b/apps/files_sharing/lib/Hooks.php index a37a497f2b..b3fe09bf9c 100644 --- a/apps/files_sharing/lib/Hooks.php +++ b/apps/files_sharing/lib/Hooks.php @@ -28,6 +28,7 @@ namespace OCA\Files_Sharing; use OC\Files\Filesystem; +use OCP\EventDispatcher\IEventDispatcher; class Hooks { public static function deleteUser($params) { @@ -42,7 +43,9 @@ class Hooks { \OC::$server->getCloudFederationFactory(), \OC::$server->getGroupManager(), \OC::$server->getUserManager(), - $params['uid']); + $params['uid'], + \OC::$server->query(IEventDispatcher::class) + ); $manager->removeUserShares($params['uid']); } diff --git a/apps/files_sharing/tests/External/ManagerTest.php b/apps/files_sharing/tests/External/ManagerTest.php index d6fab64dc6..a7ec79e9c2 100644 --- a/apps/files_sharing/tests/External/ManagerTest.php +++ b/apps/files_sharing/tests/External/ManagerTest.php @@ -33,6 +33,7 @@ use OC\Files\Storage\StorageFactory; use OCA\Files_Sharing\External\Manager; use OCA\Files_Sharing\External\MountProvider; use OCA\Files_Sharing\Tests\TestCase; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Federation\ICloudFederationFactory; use OCP\Federation\ICloudFederationProviderManager; use OCP\Http\Client\IClientService; @@ -80,6 +81,8 @@ class ManagerTest extends TestCase { */ private $user; private $testMountProvider; + /** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */ + private $eventDispatcher; protected function setUp(): void { parent::setUp(); @@ -94,6 +97,7 @@ class ManagerTest extends TestCase { $this->cloudFederationFactory = $this->createMock(ICloudFederationFactory::class); $this->groupManager = $this->createMock(IGroupManager::class); $this->userManager = $this->createMock(IUserManager::class); + $this->eventDispatcher = $this->createMock(IEventDispatcher::class); $this->manager = $this->getMockBuilder(Manager::class) ->setConstructorArgs( @@ -108,7 +112,8 @@ class ManagerTest extends TestCase { $this->cloudFederationFactory, $this->groupManager, $this->userManager, - $this->uid + $this->uid, + $this->eventDispatcher, ] )->setMethods(['tryOCMEndPoint'])->getMock(); diff --git a/lib/private/Share20/ProviderFactory.php b/lib/private/Share20/ProviderFactory.php index 128033f649..0606fe9c84 100644 --- a/lib/private/Share20/ProviderFactory.php +++ b/lib/private/Share20/ProviderFactory.php @@ -39,6 +39,7 @@ use OCA\FederatedFileSharing\TokenHandler; use OCA\ShareByMail\Settings\SettingsManager; use OCA\ShareByMail\ShareByMailProvider; use OCP\Defaults; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IServerContainer; use OCP\Share\IProviderFactory; use OCP\Share\IShare; @@ -127,7 +128,8 @@ class ProviderFactory implements IProviderFactory { $this->serverContainer->query(\OCP\OCS\IDiscoveryService::class), $this->serverContainer->getJobList(), \OC::$server->getCloudFederationProviderManager(), - \OC::$server->getCloudFederationFactory() + \OC::$server->getCloudFederationFactory(), + $this->serverContainer->query(IEventDispatcher::class) ); $tokenHandler = new TokenHandler( $this->serverContainer->getSecureRandom()