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 13, 2020
1 parent 2c87ce6 commit 8ccff5e
Show file tree
Hide file tree
Showing 11 changed files with 276 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +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\EventDispatcher\IEventDispatcher;
use OCP\SabrePluginEvent;
use Sabre\DAV\Auth\Plugin;
use Sabre\VObject\ITip\Message;
Expand All @@ -46,7 +48,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 +70,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;
}
}
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.

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 {
}
}
60 changes: 60 additions & 0 deletions apps/federation/lib/Listeners/FederatedShareAddedEventListener.php
Original file line number Diff line number Diff line change
@@ -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 FederatedShareAddedEventListener 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);
}
}
}
Loading

0 comments on commit 8ccff5e

Please sign in to comment.