Skip to content

Commit

Permalink
Move federated_share_added into a typed event
Browse files Browse the repository at this point in the history
Signed-off-by: Morris Jobke <hey@morrisjobke.de>
  • Loading branch information
MorrisJobke committed Jul 14, 2020
1 parent 2c87ce6 commit 089e72b
Show file tree
Hide file tree
Showing 17 changed files with 284 additions and 121 deletions.
1 change: 1 addition & 0 deletions apps/dav/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
'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',
Expand Down
1 change: 1 addition & 0 deletions apps/dav/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
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;

Expand All @@ -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));
Expand All @@ -67,8 +69,8 @@ public function getCurrentPrincipal() {
});

// 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());
Expand Down
53 changes: 53 additions & 0 deletions apps/dav/lib/Events/SabrePluginAuthInitEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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;

/**
* @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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'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\\Migration\\Version1010Date20200630191755' => $baseDir . '/../lib/Migration/Version1010Date20200630191755.php',
'OCA\\FederatedFileSharing\\Notifications' => $baseDir . '/../lib/Notifications.php',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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\\Migration\\Version1010Date20200630191755' => __DIR__ . '/..' . '/../lib/Migration/Version1010Date20200630191755.php',
'OCA\\FederatedFileSharing\\Notifications' => __DIR__ . '/..' . '/../lib/Notifications.php',
Expand Down
6 changes: 4 additions & 2 deletions apps/federatedfilesharing/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ function () use ($container) {
$server->query(\OCP\OCS\IDiscoveryService::class),
\OC::$server->getJobList(),
\OC::$server->getCloudFederationProviderManager(),
\OC::$server->getCloudFederationFactory()
\OC::$server->getCloudFederationFactory(),
$server->query(IEventDispatcher::class)
);
return new RequestHandlerController(
$c->query('AppName'),
Expand Down Expand Up @@ -148,7 +149,8 @@ protected function initFederatedShareProvider() {
\OC::$server->query(\OCP\OCS\IDiscoveryService::class),
\OC::$server->getJobList(),
\OC::$server->getCloudFederationProviderManager(),
\OC::$server->getCloudFederationFactory()
\OC::$server->getCloudFederationFactory(),
\OC::$server->query(IEventDispatcher::class)
);
$tokenHandler = new \OCA\FederatedFileSharing\TokenHandler(
\OC::$server->getSecureRandom()
Expand Down
52 changes: 52 additions & 0 deletions apps/federatedfilesharing/lib/Events/FederatedShareAddedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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;

/**
* @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;
}
}
20 changes: 10 additions & 10 deletions apps/federatedfilesharing/lib/Notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -53,28 +55,25 @@ 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;
$this->discoveryService = $discoveryService;
$this->jobList = $jobList;
$this->federationProviderManager = $federationProviderManager;
$this->cloudFederationFactory = $cloudFederationFactory;
$this->eventDispatcher = $eventDispatcher;
}

/**
Expand Down Expand Up @@ -119,7 +118,8 @@ public function sendRemoteShare($token, $shareWith, $name, $remote_id, $owner, $
$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;
}
}
Expand Down
28 changes: 0 additions & 28 deletions apps/federation/appinfo/app.php

This file was deleted.

2 changes: 2 additions & 0 deletions apps/federation/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
'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\\FederatedShareAddedEventListener' => $baseDir . '/../lib/Listeners/FederatedShareAddedEventListener.php',
'OCA\\Federation\\Listener\\SabrePluginAuthInitEventListener' => $baseDir . '/../lib/Listeners/SabrePluginAuthInitEventListener.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',
Expand Down
2 changes: 2 additions & 0 deletions apps/federation/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -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\\FederatedShareAddedEventListener' => __DIR__ . '/..' . '/../lib/Listeners/FederatedShareAddedEventListener.php',
'OCA\\Federation\\Listener\\SabrePluginAuthInitEventListener' => __DIR__ . '/..' . '/../lib/Listeners/SabrePluginAuthInitEventListener.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',
Expand Down
54 changes: 14 additions & 40 deletions apps/federation/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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\FederatedShareAddedEventListener;
use OCA\Federation\Listener\SabrePluginAuthInitEventListener;
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, FederatedShareAddedEventListener::class);
$context->registerEventListener(SabrePluginAuthInitEvent::class, SabrePluginAuthInitEventListener::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 {
}
}
Loading

0 comments on commit 089e72b

Please sign in to comment.