summaryrefslogtreecommitdiffstats
path: root/apps/federation/lib
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2020-07-13 15:43:42 +0200
committerMorris Jobke <hey@morrisjobke.de>2020-07-23 08:33:17 +0200
commit0763a173321488acaadceb1eb0ecf1ec8691bf21 (patch)
treee3a722bae6695c6ce2e0feaf06298effcf92d773 /apps/federation/lib
parent99d0ba5f7ea45bb636cfcfc2de144e25af88b916 (diff)
downloadnextcloud-server-0763a173321488acaadceb1eb0ecf1ec8691bf21.tar.gz
nextcloud-server-0763a173321488acaadceb1eb0ecf1ec8691bf21.zip
Move federated_share_added into a typed event
Signed-off-by: Morris Jobke <hey@morrisjobke.de>
Diffstat (limited to 'apps/federation/lib')
-rw-r--r--apps/federation/lib/AppInfo/Application.php54
-rw-r--r--apps/federation/lib/Listeners/FederatedShareAddedListener.php60
-rw-r--r--apps/federation/lib/Listeners/SabrePluginAuthInitListener.php57
3 files changed, 131 insertions, 40 deletions
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);
+ }
+ }
+}