diff options
author | Morris Jobke <hey@morrisjobke.de> | 2020-07-13 16:43:42 +0300 |
---|---|---|
committer | Morris Jobke <hey@morrisjobke.de> | 2020-07-23 09:33:17 +0300 |
commit | 0763a173321488acaadceb1eb0ecf1ec8691bf21 (patch) | |
tree | e3a722bae6695c6ce2e0feaf06298effcf92d773 /apps | |
parent | 99d0ba5f7ea45bb636cfcfc2de144e25af88b916 (diff) |
Move federated_share_added into a typed event
Signed-off-by: Morris Jobke <hey@morrisjobke.de>
Diffstat (limited to 'apps')
20 files changed, 306 insertions, 124 deletions
diff --git a/apps/dav/composer/composer/autoload_classmap.php b/apps/dav/composer/composer/autoload_classmap.php index 71cf0c10876..b42750c25ea 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 c0ed98a03ce..2d579289f05 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 3f4031740cc..5040319bcbf 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 00000000000..ca3c7b65527 --- /dev/null +++ b/apps/dav/lib/Events/SabrePluginAuthInitEvent.php @@ -0,0 +1,56 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright Copyright (c) 2020, Morris Jobke <hey@morrisjobke.de> + * + * @author Morris Jobke <hey@morrisjobke.de> + * + * @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 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 08060823692..8d1364c0d6a 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 b0e17eb568f..6e352d6f325 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 00000000000..f1269b9f938 --- /dev/null +++ b/apps/federatedfilesharing/lib/Events/FederatedShareAddedEvent.php @@ -0,0 +1,54 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright Copyright (c) 2020, Morris Jobke <hey@morrisjobke.de> + * + * @author Morris Jobke <hey@morrisjobke.de> + * + * @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 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 7b2105ecb0f..2dc3104e6a7 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 d427e324e1f..228e481655e 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 af48b594380..32ceedd45c0 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 c48c2e208a8..00000000000 --- a/apps/federation/appinfo/app.php +++ /dev/null @@ -1,28 +0,0 @@ -<?php -/** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Björn Schießle <bjoern@schiessle.org> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @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 <http://www.gnu.org/licenses/> - * - */ - -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 5c2bda17856..922b99c0940 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 1ee4e0f04a9..a79a03bd339 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 09a6eed4abe..36a61298c90 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); - /** - * 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' - ); + $context->registerEventListener(FederatedShareAddedEvent::class, FederatedShareAddedListener::class); + $context->registerEventListener(SabrePluginAuthInitEvent::class, SabrePluginAuthInitListener::class); + } - $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 00000000000..21eb9450772 --- /dev/null +++ b/apps/federation/lib/Listeners/FederatedShareAddedListener.php @@ -0,0 +1,60 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright Copyright (c) 2020, Morris Jobke <hey@morrisjobke.de> + * + * @author Morris Jobke <hey@morrisjobke.de> + * + * @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 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 00000000000..2e5b25c929b --- /dev/null +++ b/apps/federation/lib/Listeners/SabrePluginAuthInitListener.php @@ -0,0 +1,57 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright Copyright (c) 2020, Morris Jobke <hey@morrisjobke.de> + * + * @author Morris Jobke <hey@morrisjobke.de> + * + * @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 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 1b1bd08d3fd..6448d0fab4d 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 943b5abfe01..3b4c2bc8f54 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 a37a497f2bd..b3fe09bf9cf 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 d6fab64dc67..a7ec79e9c2a 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(); |