]> source.dussan.org Git - nextcloud-server.git/commitdiff
Move federated_share_added into a typed event 21814/head
authorMorris Jobke <hey@morrisjobke.de>
Mon, 13 Jul 2020 13:43:42 +0000 (15:43 +0200)
committerMorris Jobke <hey@morrisjobke.de>
Thu, 23 Jul 2020 06:33:17 +0000 (08:33 +0200)
Signed-off-by: Morris Jobke <hey@morrisjobke.de>
21 files changed:
apps/dav/composer/composer/autoload_classmap.php
apps/dav/composer/composer/autoload_static.php
apps/dav/lib/CalDAV/InvitationResponse/InvitationResponseServer.php
apps/dav/lib/Events/SabrePluginAuthInitEvent.php [new file with mode: 0644]
apps/federatedfilesharing/composer/composer/autoload_classmap.php
apps/federatedfilesharing/composer/composer/autoload_static.php
apps/federatedfilesharing/lib/Events/FederatedShareAddedEvent.php [new file with mode: 0644]
apps/federatedfilesharing/lib/Notifications.php
apps/federatedfilesharing/lib/OCM/CloudFederationProviderFiles.php
apps/federatedfilesharing/tests/NotificationsTest.php
apps/federation/appinfo/app.php [deleted file]
apps/federation/composer/composer/autoload_classmap.php
apps/federation/composer/composer/autoload_static.php
apps/federation/lib/AppInfo/Application.php
apps/federation/lib/Listeners/FederatedShareAddedListener.php [new file with mode: 0644]
apps/federation/lib/Listeners/SabrePluginAuthInitListener.php [new file with mode: 0644]
apps/files_sharing/lib/AppInfo/Application.php
apps/files_sharing/lib/External/Manager.php
apps/files_sharing/lib/Hooks.php
apps/files_sharing/tests/External/ManagerTest.php
lib/private/Share20/ProviderFactory.php

index 71cf0c10876937c62291e06650adb5fe4cf159e7..b42750c25ea88d242b415fc9d89812937db967f6 100644 (file)
@@ -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',
index c0ed98a03ce60b484dd284a7e483fb84fc3b4f50..2d579289f0516e64c4f9e720e3ec29ecadd311d8 100644 (file)
@@ -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',
index 3f4031740cc680f6fae51ccca585382d293b5567..5040319bcbf3ec5f5d6b832a9400276044b153e1 100644 (file)
@@ -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 (file)
index 0000000..ca3c7b6
--- /dev/null
@@ -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;
+       }
+}
index 0806082369289bc19ce981fb36577aa48fc76369..8d1364c0d6a944c23a60840c409e1035b240a63a 100644 (file)
@@ -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',
index b0e17eb568f1d77f0a972bd1e845b5146dea6d8b..6e352d6f325f2f86f23b5571492fb2b64c072e1d 100644 (file)
@@ -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 (file)
index 0000000..f1269b9
--- /dev/null
@@ -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;
+       }
+}
index 7b2105ecb0f36bdeaaef5d7d269a7751351522cb..2dc3104e6a75deeebb730e43f09b069b5aa51298 100644 (file)
 
 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;
                        }
                }
index d427e324e1f0611f20ca03e64d10d11963fdba0b..228e481655ead853a78e0db06b84884c0c29be41 100644 (file)
@@ -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 {
index af48b5943800d642e5d031491c8afa9eee69e567..32ceedd45c0f377da637466277d8465b62f3d118 100644 (file)
@@ -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 (file)
index c48c2e2..0000000
+++ /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();
index 5c2bda17856aeabcc7e715459b3cfd7264991e90..922b99c0940b53d6609d5ff9e844bfae647b095d 100644 (file)
@@ -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',
index 1ee4e0f04a96459de16b055c888dd7fb8be88c1e..a79a03bd339585fdd0c5fa11516aea312e9f7dc4 100644 (file)
@@ -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',
index 09a6eed4abefd7675037f47ce67f008e93e8d02b..36a61298c90e58459572ba874bbd87ae70ccc878 100644 (file)
 
 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 (file)
index 0000000..21eb945
--- /dev/null
@@ -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 (file)
index 0000000..2e5b25c
--- /dev/null
@@ -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);
+               }
+       }
+}
index 1b1bd08d3fdaeff58fc256074e59a03510a9c422..6448d0fab4d1773cace072c1bd4a1967becc25ad 100644 (file)
@@ -94,7 +94,8 @@ class Application extends App {
                                $server->getCloudFederationFactory(),
                                $server->getGroupManager(),
                                $server->getUserManager(),
-                               $uid
+                               $uid,
+                               $server->query(IEventDispatcher::class)
                        );
                });
 
index 943b5abfe01310d8dbeb5c3a7a5709469a78ca0f..3b4c2bc8f54e7a54786da9735a7916e44844c4f7 100644 (file)
@@ -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;
                        }
                }
index a37a497f2bd9a20d7ccee6c2683afa7697fa196c..b3fe09bf9cfc9f9d8715a2c89da381cac490810c 100644 (file)
@@ -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']);
        }
index d6fab64dc67643399afe241328bb16d02793e2d6..a7ec79e9c2af0b86235f36ad1174a1b98d87c1c4 100644 (file)
@@ -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();
 
index 128033f6498cae8d27ff0bad71e3892ba61baeb4..0606fe9c8494ef5c9321f1766640eb5024067382 100644 (file)
@@ -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()