From 238fe188aafdd3f9201602cab43339d1d955ce6d Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Wed, 18 Jul 2018 18:03:04 +0300 Subject: [PATCH] Cleanup ocs routing --- apps/federatedfilesharing/appinfo/routes.php | 14 +++ .../lib/AppInfo/Application.php | 72 ++++++++++++++-- .../RequestHandlerController.php} | 85 +++++++++++-------- .../AppFramework/Routing/RouteConfig.php | 5 +- lib/private/Route/Router.php | 2 +- ocs/routes.php | 74 ---------------- 6 files changed, 136 insertions(+), 116 deletions(-) create mode 100644 apps/federatedfilesharing/appinfo/routes.php rename apps/federatedfilesharing/lib/{RequestHandler.php => Controller/RequestHandlerController.php} (93%) diff --git a/apps/federatedfilesharing/appinfo/routes.php b/apps/federatedfilesharing/appinfo/routes.php new file mode 100644 index 000000000000..ae7f98f187d1 --- /dev/null +++ b/apps/federatedfilesharing/appinfo/routes.php @@ -0,0 +1,14 @@ + [ + ['root' => '/cloud', 'name' => 'RequestHandler#createShare', 'url' => '/shares', 'verb' => 'POST'], + ['root' => '/cloud', 'name' => 'RequestHandler#reShare', 'url' => '/shares/{id}/reshare', 'verb' => 'POST'], + ['root' => '/cloud', 'name' => 'RequestHandler#updatePermissions', 'url' => '/shares/{id}/permissions', 'verb' => 'POST'], + ['root' => '/cloud', 'name' => 'RequestHandler#acceptShare', 'url' => '', 'verb' => 'POST'], + ['root' => '/cloud', 'name' => 'RequestHandler#acceptShare', 'url' => '/shares/{id}/accept', 'verb' => 'POST'], + ['root' => '/cloud', 'name' => 'RequestHandler#declineShare', 'url' => '/shares/{id}/decline', 'verb' => 'POST'], + ['root' => '/cloud', 'name' => 'RequestHandler#unshare', 'url' => '/shares/{id}/unshare', 'verb' => 'POST'], + ['root' => '/cloud', 'name' => 'RequestHandler#revoke', 'url' => '/shares/{id}/revoke', 'verb' => 'POST'], + ] +]; diff --git a/apps/federatedfilesharing/lib/AppInfo/Application.php b/apps/federatedfilesharing/lib/AppInfo/Application.php index b93a4b40d248..809a42fb65c7 100644 --- a/apps/federatedfilesharing/lib/AppInfo/Application.php +++ b/apps/federatedfilesharing/lib/AppInfo/Application.php @@ -21,8 +21,13 @@ namespace OCA\FederatedFileSharing\AppInfo; +use OCA\FederatedFileSharing\AddressHandler; +use OCA\FederatedFileSharing\DiscoveryManager; use OCA\FederatedFileSharing\FederatedShareProvider; use OCA\FederatedFileSharing\FedShareManager; +use OCA\FederatedFileSharing\Notifications; +use OCA\FederatedFileSharing\Controller\RequestHandlerController; +use OCA\FederatedFileSharing\TokenHandler; use OCP\AppFramework\App; class Application extends App { @@ -33,6 +38,61 @@ class Application extends App { /** @var FedShareManager */ protected $federatedShareManager; + public function __construct() { + parent::__construct('federatedfilesharing'); + $container = $this->getContainer(); + $server = $container->getServer(); + + $container->registerService( + 'AddressHandler', + function ($c) use ($server) { + return new AddressHandler( + $server->getURLGenerator(), + $server->getL10N('federatedfilesharing') + ); + } + ); + + $container->registerService( + 'DiscoveryManager', + function ($c) use ($server) { + return new DiscoveryManager( + $server->getMemCacheFactory(), + $server->getHTTPClientService() + ); + } + ); + + $container->registerService( + 'Notifications', + function ($c) use ($server) { + return new Notifications( + $c->query('AddressHandler'), + $server->getHTTPClientService(), + $c->query('DiscoveryManager'), + $server->getJobList(), + $server->getConfig() + ); + } + ); + + $container->registerService( + 'RequestHandlerController', + function ($c) use ($server) { + return new RequestHandlerController( + $c->query('AppName'), + $c->query('Request'), + $this->getFederatedShareProvider(), + $server->getDatabaseConnection(), + $c->query('Notifications'), + $c->query('AddressHandler'), + $this->getFederatedShareManager(), + $server->getEventDispatcher() + ); + } + ); + } + /** * get instance of federated share provider * @@ -60,7 +120,7 @@ public function getFederatedShareManager() { /** * initialize federated share manager */ - protected function initFederatedSharemanager() { + protected function initFederatedShareManager() { $this->federatedShareManager = new FedShareManager( $this->getFederatedShareProvider(), \OC::$server->getUserManager(), @@ -73,26 +133,26 @@ protected function initFederatedSharemanager() { * initialize federated share provider */ protected function initFederatedShareProvider() { - $addressHandler = new \OCA\FederatedFileSharing\AddressHandler( + $addressHandler = new AddressHandler( \OC::$server->getURLGenerator(), \OC::$server->getL10N('federatedfilesharing') ); - $discoveryManager = new \OCA\FederatedFileSharing\DiscoveryManager( + $discoveryManager = new DiscoveryManager( \OC::$server->getMemCacheFactory(), \OC::$server->getHTTPClientService() ); - $notifications = new \OCA\FederatedFileSharing\Notifications( + $notifications = new Notifications( $addressHandler, \OC::$server->getHTTPClientService(), $discoveryManager, \OC::$server->getJobList(), \OC::$server->getConfig() ); - $tokenHandler = new \OCA\FederatedFileSharing\TokenHandler( + $tokenHandler = new TokenHandler( \OC::$server->getSecureRandom() ); - $this->federatedShareProvider = new \OCA\FederatedFileSharing\FederatedShareProvider( + $this->federatedShareProvider = new FederatedShareProvider( \OC::$server->getDatabaseConnection(), $addressHandler, $notifications, diff --git a/apps/federatedfilesharing/lib/RequestHandler.php b/apps/federatedfilesharing/lib/Controller/RequestHandlerController.php similarity index 93% rename from apps/federatedfilesharing/lib/RequestHandler.php rename to apps/federatedfilesharing/lib/Controller/RequestHandlerController.php index 0094c760ac2a..24a4953b9fbc 100644 --- a/apps/federatedfilesharing/lib/RequestHandler.php +++ b/apps/federatedfilesharing/lib/Controller/RequestHandlerController.php @@ -24,11 +24,16 @@ * */ -namespace OCA\FederatedFileSharing; +namespace OCA\FederatedFileSharing\Controller; use OC\OCS\Result; +use OCA\FederatedFileSharing\AddressHandler; +use OCA\FederatedFileSharing\FederatedShareProvider; +use OCA\FederatedFileSharing\FedShareManager; +use OCA\FederatedFileSharing\Notifications; use OCA\Files_Sharing\Activity; use OCP\AppFramework\Http; +use OCP\AppFramework\OCSController; use OCP\Constants; use OCP\IDBConnection; use OCP\IRequest; @@ -38,13 +43,13 @@ use Symfony\Component\EventDispatcher\GenericEvent; /** - * Class RequestHandler + * Class RequestHandlerController * - * handles OCS Request to the federated share API + * Handles OCS Request to the federated share API * * @package OCA\FederatedFileSharing\API */ -class RequestHandler { +class RequestHandlerController extends OCSController { /** @var FederatedShareProvider */ private $federatedShareProvider; @@ -52,12 +57,6 @@ class RequestHandler { /** @var IDBConnection */ private $connection; - /** @var Share\IManager */ - private $shareManager; - - /** @var IRequest */ - private $request; - /** @var Notifications */ private $notifications; @@ -76,28 +75,28 @@ class RequestHandler { /** * Server2Server constructor. * + * @param string $appName + * @param IRequest $request * @param FederatedShareProvider $federatedShareProvider * @param IDBConnection $connection - * @param Share\IManager $shareManager - * @param IRequest $request * @param Notifications $notifications * @param AddressHandler $addressHandler * @param FedShareManager $fedShareManager * @param EventDispatcherInterface $eventDispatcher */ - public function __construct(FederatedShareProvider $federatedShareProvider, - IDBConnection $connection, - Share\IManager $shareManager, + public function __construct($appName, IRequest $request, + FederatedShareProvider $federatedShareProvider, + IDBConnection $connection, Notifications $notifications, AddressHandler $addressHandler, FedShareManager $fedShareManager, EventDispatcherInterface $eventDispatcher ) { + parent::__construct($appName, $request); + $this->federatedShareProvider = $federatedShareProvider; $this->connection = $connection; - $this->shareManager = $shareManager; - $this->request = $request; $this->notifications = $notifications; $this->addressHandler = $addressHandler; $this->fedShareManager = $fedShareManager; @@ -105,12 +104,14 @@ public function __construct(FederatedShareProvider $federatedShareProvider, } /** + * @NoCSRFRequired + * @PublicPage + * * create a new share * - * @param array $params * @return Result */ - public function createShare($params) { + public function createShare() { if (!$this->isS2SEnabled(true)) { return new Result(null, 503, 'Server does not support federated cloud sharing'); } @@ -221,14 +222,16 @@ public function createShare($params) { } /** + * @NoCSRFRequired + * @PublicPage + * * create re-share on behalf of another user * - * @param $params + * @param int $id * * @return Result */ - public function reShare($params) { - $id = isset($params['id']) ? (int)$params['id'] : null; + public function reShare($id) { $token = $this->request->getParam('token', null); $shareWith = $this->request->getParam('shareWith', null); $permission = (int)$this->request->getParam('permission', null); @@ -272,17 +275,20 @@ public function reShare($params) { } /** + * @NoCSRFRequired + * @PublicPage + * * accept server-to-server share * - * @param array $params + * @param int $id + * * @return Result */ - public function acceptShare($params) { + public function acceptShare($id) { if (!$this->isS2SEnabled()) { return new Result(null, 503, 'Server does not support federated cloud sharing'); } - $id = $params['id']; $token = isset($_POST['token']) ? $_POST['token'] : null; try { @@ -302,17 +308,20 @@ public function acceptShare($params) { } /** + * @NoCSRFRequired + * @PublicPage + * * decline server-to-server share * - * @param array $params + * @param int $id + * * @return Result */ - public function declineShare($params) { + public function declineShare($id) { if (!$this->isS2SEnabled()) { return new Result(null, 503, 'Server does not support federated cloud sharing'); } - $id = (int)$params['id']; $token = isset($_POST['token']) ? $_POST['token'] : null; try { @@ -333,17 +342,20 @@ public function declineShare($params) { } /** + * @NoCSRFRequired + * @PublicPage + * * remove server-to-server share if it was unshared by the owner * - * @param array $params + * @param int $id + * * @return Result */ - public function unshare($params) { + public function unshare($id) { if (!$this->isS2SEnabled()) { return new Result(null, 503, 'Server does not support federated cloud sharing'); } - $id = $params['id']; $token = isset($_POST['token']) ? $_POST['token'] : null; $query = \OCP\DB::prepare('SELECT * FROM `*PREFIX*share_external` WHERE `remote_id` = ? AND `share_token` = ?'); @@ -388,14 +400,16 @@ private function cleanupRemote($remote) { } /** + * @NoCSRFRequired + * @PublicPage + * * federated share was revoked, either by the owner or the re-sharer * - * @param $params + * @param int $id * * @return Result */ - public function revoke($params) { - $id = (int)$params['id']; + public function revoke($id) { $token = $this->request->getParam('token'); $share = $this->federatedShareProvider->getShareById($id); @@ -470,6 +484,9 @@ protected function verifyShare(Share\IShare $share, $token) { } /** + * @NoCSRFRequired + * @PublicPage + * * update share information to keep federated re-shares in sync * * @param array $params diff --git a/lib/private/AppFramework/Routing/RouteConfig.php b/lib/private/AppFramework/Routing/RouteConfig.php index 1aceb5e5fb41..b0e6a2c58e4c 100644 --- a/lib/private/AppFramework/Routing/RouteConfig.php +++ b/lib/private/AppFramework/Routing/RouteConfig.php @@ -91,7 +91,10 @@ private function processOCS(array $routes) { $postFix = $ocsRoute['postfix']; } - $url = $ocsRoute['url']; + $root = (isset($ocsRoute['root'])) + ? $ocsRoute['root'] + : '/apps/' . $this->appName; + $url = $root . $ocsRoute['url']; $verb = isset($ocsRoute['verb']) ? \strtoupper($ocsRoute['verb']) : 'GET'; $split = \explode('#', $name, 2); diff --git a/lib/private/Route/Router.php b/lib/private/Route/Router.php index 58a8d0997d09..f52c605e7780 100644 --- a/lib/private/Route/Router.php +++ b/lib/private/Route/Router.php @@ -155,7 +155,7 @@ public function loadRoutes($app = null) { // Also add the OCS collection $collection = $this->getCollection($app.'.ocs'); - $collection->addPrefix('/ocsapp/apps/' . $app); + $collection->addPrefix('/ocsapp/'); $this->root->addCollection($collection); } } diff --git a/ocs/routes.php b/ocs/routes.php index cea33386c542..cdd2e0309dd5 100644 --- a/ocs/routes.php +++ b/ocs/routes.php @@ -80,77 +80,3 @@ 'core', API::USER_AUTH ); - -// Server-to-Server Sharing -if (\OC::$server->getAppManager()->isEnabledForUser('files_sharing')) { - $federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application('federatedfilesharing'); - $addressHandler = new \OCA\FederatedFileSharing\AddressHandler( - \OC::$server->getURLGenerator(), - \OC::$server->getL10N('federatedfilesharing') - ); - $notification = new \OCA\FederatedFileSharing\Notifications( - $addressHandler, - \OC::$server->getHTTPClientService(), - new \OCA\FederatedFileSharing\DiscoveryManager(\OC::$server->getMemCacheFactory(), \OC::$server->getHTTPClientService()), - \OC::$server->getJobList(), - \OC::$server->getConfig() - ); - $s2s = new OCA\FederatedFileSharing\RequestHandler( - $federatedSharingApp->getFederatedShareProvider(), - \OC::$server->getDatabaseConnection(), - \OC::$server->getShareManager(), - \OC::$server->getRequest(), - $notification, - $addressHandler, - $federatedSharingApp->getFederatedShareManager(), - \OC::$server->getEventDispatcher() - ); - API::register('post', - '/cloud/shares', - [$s2s, 'createShare'], - 'files_sharing', - API::GUEST_AUTH - ); - - API::register('post', - '/cloud/shares/{id}/reshare', - [$s2s, 'reShare'], - 'files_sharing', - API::GUEST_AUTH - ); - - API::register('post', - '/cloud/shares/{id}/permissions', - [$s2s, 'updatePermissions'], - 'files_sharing', - API::GUEST_AUTH - ); - - API::register('post', - '/cloud/shares/{id}/accept', - [$s2s, 'acceptShare'], - 'files_sharing', - API::GUEST_AUTH - ); - - API::register('post', - '/cloud/shares/{id}/decline', - [$s2s, 'declineShare'], - 'files_sharing', - API::GUEST_AUTH - ); - - API::register('post', - '/cloud/shares/{id}/unshare', - [$s2s, 'unshare'], - 'files_sharing', - API::GUEST_AUTH - ); - - API::register('post', - '/cloud/shares/{id}/revoke', - [$s2s, 'revoke'], - 'files_sharing', - API::GUEST_AUTH - ); -}