From 5eddde64860a77b2355aaf31044ab6692ed36265 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 17 Sep 2015 13:47:58 +0200 Subject: [PATCH 1/2] Add a Sabre plugin that emits an event for apps --- apps/files/appinfo/remote.php | 3 +- apps/files_sharing/publicwebdav.php | 3 +- .../connector/sabre/listenerplugin.php | 59 +++++++++++++++++++ lib/private/connector/sabre/serverfactory.php | 6 +- .../sabre/requesttest/requesttest.php | 3 +- 5 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 lib/private/connector/sabre/listenerplugin.php diff --git a/apps/files/appinfo/remote.php b/apps/files/appinfo/remote.php index 36479ae13d..4d0d8b3e2e 100644 --- a/apps/files/appinfo/remote.php +++ b/apps/files/appinfo/remote.php @@ -39,7 +39,8 @@ $serverFactory = new \OC\Connector\Sabre\ServerFactory( \OC::$server->getDatabaseConnection(), \OC::$server->getUserSession(), \OC::$server->getMountManager(), - \OC::$server->getTagManager() + \OC::$server->getTagManager(), + \OC::$server->getEventDispatcher() ); // Backends diff --git a/apps/files_sharing/publicwebdav.php b/apps/files_sharing/publicwebdav.php index eec158dd4b..773a15c888 100644 --- a/apps/files_sharing/publicwebdav.php +++ b/apps/files_sharing/publicwebdav.php @@ -39,7 +39,8 @@ $serverFactory = new \OC\Connector\Sabre\ServerFactory( \OC::$server->getDatabaseConnection(), \OC::$server->getUserSession(), \OC::$server->getMountManager(), - \OC::$server->getTagManager() + \OC::$server->getTagManager(), + \OC::$server->getEventDispatcher() ); $requestUri = \OC::$server->getRequest()->getRequestUri(); diff --git a/lib/private/connector/sabre/listenerplugin.php b/lib/private/connector/sabre/listenerplugin.php new file mode 100644 index 0000000000..86cb8ebe56 --- /dev/null +++ b/lib/private/connector/sabre/listenerplugin.php @@ -0,0 +1,59 @@ + + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @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 + * + */ + +namespace OC\Connector\Sabre; + +use Sabre\DAV\Server; +use Sabre\DAV\ServerPlugin; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; + +class ListenerPlugin extends ServerPlugin { + /** @var EventDispatcherInterface */ + protected $dispatcher; + + /** + * @param EventDispatcherInterface $dispatcher + */ + public function __construct(EventDispatcherInterface $dispatcher) { + $this->dispatcher = $dispatcher; + } + + /** + * This initialize the plugin + * + * @param Server $server + */ + public function initialize(Server $server) { + $server->on('beforeMethod', array($this, 'emitListener'), 15); + } + + /** + * This method is called before any HTTP method and returns http status code 503 + * in case the system is in maintenance mode. + * + * @return bool + */ + public function emitListener() { + $this->dispatcher->dispatch('OC\Connector\Sabre::beforeMethod'); + + return true; + } +} diff --git a/lib/private/connector/sabre/serverfactory.php b/lib/private/connector/sabre/serverfactory.php index 042cc051d3..54470b0b8a 100644 --- a/lib/private/connector/sabre/serverfactory.php +++ b/lib/private/connector/sabre/serverfactory.php @@ -28,6 +28,7 @@ use OCP\ILogger; use OCP\ITagManager; use OCP\IUserSession; use Sabre\DAV\Auth\Backend\BackendInterface; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; class ServerFactory { public function __construct( @@ -36,7 +37,8 @@ class ServerFactory { IDBConnection $databaseConnection, IUserSession $userSession, IMountManager $mountManager, - ITagManager $tagManager + ITagManager $tagManager, + EventDispatcherInterface $dispatcher ) { $this->config = $config; $this->logger = $logger; @@ -44,6 +46,7 @@ class ServerFactory { $this->userSession = $userSession; $this->mountManager = $mountManager; $this->tagManager = $tagManager; + $this->dispatcher = $dispatcher; } /** @@ -71,6 +74,7 @@ class ServerFactory { $server->addPlugin(new \OC\Connector\Sabre\FilesPlugin($objectTree)); $server->addPlugin(new \OC\Connector\Sabre\ExceptionLoggerPlugin('webdav', $this->logger)); $server->addPlugin(new \OC\Connector\Sabre\LockPlugin($objectTree)); + $server->addPlugin(new \OC\Connector\Sabre\ListenerPlugin($this->dispatcher)); // wait with registering these until auth is handled and the filesystem is setup $server->on('beforeMethod', function () use ($server, $objectTree, $viewCallBack) { diff --git a/tests/lib/connector/sabre/requesttest/requesttest.php b/tests/lib/connector/sabre/requesttest/requesttest.php index 7f33dcf817..7afe264503 100644 --- a/tests/lib/connector/sabre/requesttest/requesttest.php +++ b/tests/lib/connector/sabre/requesttest/requesttest.php @@ -45,7 +45,8 @@ abstract class RequestTest extends TestCase { \OC::$server->getDatabaseConnection(), \OC::$server->getUserSession(), \OC::$server->getMountManager(), - \OC::$server->getTagManager() + \OC::$server->getTagManager(), + \OC::$server->getEventDispatcher() ); } From 44e6c4f39843b1c00ef04bded80bdf2a887f7382 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 17 Sep 2015 15:11:05 +0200 Subject: [PATCH 2/2] Do not "use" Server --- lib/private/connector/sabre/listenerplugin.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/private/connector/sabre/listenerplugin.php b/lib/private/connector/sabre/listenerplugin.php index 86cb8ebe56..d0d40f4dc8 100644 --- a/lib/private/connector/sabre/listenerplugin.php +++ b/lib/private/connector/sabre/listenerplugin.php @@ -21,7 +21,6 @@ namespace OC\Connector\Sabre; -use Sabre\DAV\Server; use Sabre\DAV\ServerPlugin; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -39,9 +38,9 @@ class ListenerPlugin extends ServerPlugin { /** * This initialize the plugin * - * @param Server $server + * @param \Sabre\DAV\Server $server */ - public function initialize(Server $server) { + public function initialize(\Sabre\DAV\Server $server) { $server->on('beforeMethod', array($this, 'emitListener'), 15); }