From ba104233b0dac2924761febf5ba16de68513be5a Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Fri, 2 Sep 2016 19:28:42 +0200 Subject: [PATCH] Move remote_shares OCS endpoint to AppFramework --- apps/files_sharing/appinfo/routes.php | 63 ++--- apps/files_sharing/lib/API/Remote.php | 230 ------------------ .../files_sharing/lib/AppInfo/Application.php | 1 + .../lib/Controller/RemoteController.php | 183 ++++++++++++++ 4 files changed, 217 insertions(+), 260 deletions(-) delete mode 100644 apps/files_sharing/lib/API/Remote.php create mode 100644 apps/files_sharing/lib/Controller/RemoteController.php diff --git a/apps/files_sharing/appinfo/routes.php b/apps/files_sharing/appinfo/routes.php index de6967c7d6..267e53829d 100644 --- a/apps/files_sharing/appinfo/routes.php +++ b/apps/files_sharing/appinfo/routes.php @@ -77,6 +77,39 @@ $application->registerRoutes($this, [ 'url' => '/api/v1/sharees', 'verb' => 'GET', ], + /* + * Remote Shares + */ + [ + 'name' => 'Remote#getShares', + 'url' => '/api/v1/remote_shares', + 'verb' => 'GET', + ], + [ + 'name' => 'Remote#getOpenShares', + 'url' => '/api/v1/remote_shares/pending', + 'verb' => 'GET', + ], + [ + 'name' => 'Remote#acceptShare', + 'url' => '/api/v1/remote_shares/pending/{id}', + 'verb' => 'POST', + ], + [ + 'name' => 'Remote#declineShare', + 'url' => '/api/v1/remote_shares/pending/{id}', + 'verb' => 'DELETE', + ], + [ + 'name' => 'Remote#getShare', + 'url' => '/api/v1/remote_shares/{id}', + 'verb' => 'GET', + ], + [ + 'name' => 'Remote#unshare', + 'url' => '/api/v1/remote_shares/{id}', + 'verb' => 'DELETE', + ], ], ]); @@ -96,33 +129,3 @@ $this->create('sharing_external_shareinfo', '/shareinfo') // OCS API //TODO: SET: mail notification, waiting for PR #4689 to be accepted - -API::register('get', - '/apps/files_sharing/api/v1/remote_shares', - array('\OCA\Files_Sharing\API\Remote', 'getShares'), - 'files_sharing'); - -API::register('get', - '/apps/files_sharing/api/v1/remote_shares/pending', - array('\OCA\Files_Sharing\API\Remote', 'getOpenShares'), - 'files_sharing'); - -API::register('post', - '/apps/files_sharing/api/v1/remote_shares/pending/{id}', - array('\OCA\Files_Sharing\API\Remote', 'acceptShare'), - 'files_sharing'); - -API::register('delete', - '/apps/files_sharing/api/v1/remote_shares/pending/{id}', - array('\OCA\Files_Sharing\API\Remote', 'declineShare'), - 'files_sharing'); - -API::register('get', - '/apps/files_sharing/api/v1/remote_shares/{id}', - array('\OCA\Files_Sharing\API\Remote', 'getShare'), - 'files_sharing'); - -API::register('delete', - '/apps/files_sharing/api/v1/remote_shares/{id}', - array('\OCA\Files_Sharing\API\Remote', 'unshare'), - 'files_sharing'); diff --git a/apps/files_sharing/lib/API/Remote.php b/apps/files_sharing/lib/API/Remote.php deleted file mode 100644 index a522f923a3..0000000000 --- a/apps/files_sharing/lib/API/Remote.php +++ /dev/null @@ -1,230 +0,0 @@ - - * @author Lukas Reschke - * @author Roeland Jago Douma - * - * @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 OCA\Files_Sharing\API; - -use OC\Files\Filesystem; -use OCA\FederatedFileSharing\DiscoveryManager; -use OCA\Files_Sharing\External\Manager; - -class Remote { - - /** - * Get list of pending remote shares - * - * @param array $params empty - * @return \OC_OCS_Result - */ - public static function getOpenShares($params) { - $discoveryManager = new DiscoveryManager( - \OC::$server->getMemCacheFactory(), - \OC::$server->getHTTPClientService() - ); - $externalManager = new Manager( - \OC::$server->getDatabaseConnection(), - Filesystem::getMountManager(), - Filesystem::getLoader(), - \OC::$server->getHTTPClientService(), - \OC::$server->getNotificationManager(), - $discoveryManager, - \OC_User::getUser() - ); - - return new \OC_OCS_Result($externalManager->getOpenShares()); - } - - /** - * Accept a remote share - * - * @param array $params contains the shareID 'id' which should be accepted - * @return \OC_OCS_Result - */ - public static function acceptShare($params) { - $discoveryManager = new DiscoveryManager( - \OC::$server->getMemCacheFactory(), - \OC::$server->getHTTPClientService() - ); - $externalManager = new Manager( - \OC::$server->getDatabaseConnection(), - Filesystem::getMountManager(), - Filesystem::getLoader(), - \OC::$server->getHTTPClientService(), - \OC::$server->getNotificationManager(), - $discoveryManager, - \OC_User::getUser() - ); - - if ($externalManager->acceptShare((int) $params['id'])) { - return new \OC_OCS_Result(); - } - - // Make sure the user has no notification for something that does not exist anymore. - $externalManager->processNotification((int) $params['id']); - - return new \OC_OCS_Result(null, 404, "wrong share ID, share doesn't exist."); - } - - /** - * Decline a remote share - * - * @param array $params contains the shareID 'id' which should be declined - * @return \OC_OCS_Result - */ - public static function declineShare($params) { - $discoveryManager = new DiscoveryManager( - \OC::$server->getMemCacheFactory(), - \OC::$server->getHTTPClientService() - ); - $externalManager = new Manager( - \OC::$server->getDatabaseConnection(), - Filesystem::getMountManager(), - Filesystem::getLoader(), - \OC::$server->getHTTPClientService(), - \OC::$server->getNotificationManager(), - $discoveryManager, - \OC_User::getUser() - ); - - if ($externalManager->declineShare((int) $params['id'])) { - return new \OC_OCS_Result(); - } - - // Make sure the user has no notification for something that does not exist anymore. - $externalManager->processNotification((int) $params['id']); - - return new \OC_OCS_Result(null, 404, "wrong share ID, share doesn't exist."); - } - - /** - * @param array $share Share with info from the share_external table - * @return array enriched share info with data from the filecache - */ - private static function extendShareInfo($share) { - $view = new \OC\Files\View('/' . \OC_User::getUser() . '/files/'); - $info = $view->getFileInfo($share['mountpoint']); - - $share['mimetype'] = $info->getMimetype(); - $share['mtime'] = $info->getMtime(); - $share['permissions'] = $info->getPermissions(); - $share['type'] = $info->getType(); - $share['file_id'] = $info->getId(); - - return $share; - } - - /** - * List accepted remote shares - * - * @param array $params - * @return \OC_OCS_Result - */ - public static function getShares($params) { - $discoveryManager = new DiscoveryManager( - \OC::$server->getMemCacheFactory(), - \OC::$server->getHTTPClientService() - ); - $externalManager = new Manager( - \OC::$server->getDatabaseConnection(), - Filesystem::getMountManager(), - Filesystem::getLoader(), - \OC::$server->getHTTPClientService(), - \OC::$server->getNotificationManager(), - $discoveryManager, - \OC_User::getUser() - ); - - $shares = $externalManager->getAcceptedShares(); - - $shares = array_map('self::extendShareInfo', $shares); - - return new \OC_OCS_Result($shares); - } - - /** - * Get info of a remote share - * - * @param array $params contains the shareID 'id' - * @return \OC_OCS_Result - */ - public static function getShare($params) { - $discoveryManager = new DiscoveryManager( - \OC::$server->getMemCacheFactory(), - \OC::$server->getHTTPClientService() - ); - $externalManager = new Manager( - \OC::$server->getDatabaseConnection(), - Filesystem::getMountManager(), - Filesystem::getLoader(), - \OC::$server->getHTTPClientService(), - \OC::$server->getNotificationManager(), - $discoveryManager, - \OC_User::getUser() - ); - - $shareInfo = $externalManager->getShare($params['id']); - - if ($shareInfo === false) { - return new \OC_OCS_Result(null, 404, 'share does not exist'); - } else { - $shareInfo = self::extendShareInfo($shareInfo); - return new \OC_OCS_Result($shareInfo); - } - } - - /** - * Unshare a remote share - * - * @param array $params contains the shareID 'id' which should be unshared - * @return \OC_OCS_Result - */ - public static function unshare($params) { - $discoveryManager = new DiscoveryManager( - \OC::$server->getMemCacheFactory(), - \OC::$server->getHTTPClientService() - ); - $externalManager = new Manager( - \OC::$server->getDatabaseConnection(), - Filesystem::getMountManager(), - Filesystem::getLoader(), - \OC::$server->getHTTPClientService(), - \OC::$server->getNotificationManager(), - $discoveryManager, - \OC_User::getUser() - ); - - $shareInfo = $externalManager->getShare($params['id']); - - if ($shareInfo === false) { - return new \OC_OCS_Result(null, 404, 'Share does not exist'); - } - - $mountPoint = '/' . \OC_User::getUser() . '/files' . $shareInfo['mountpoint']; - - if ($externalManager->removeShare($mountPoint) === true) { - return new \OC_OCS_Result(null); - } else { - return new \OC_OCS_Result(null, 403, 'Could not unshare'); - } - } -} diff --git a/apps/files_sharing/lib/AppInfo/Application.php b/apps/files_sharing/lib/AppInfo/Application.php index 0e70924da4..5558ffbdfc 100644 --- a/apps/files_sharing/lib/AppInfo/Application.php +++ b/apps/files_sharing/lib/AppInfo/Application.php @@ -111,6 +111,7 @@ class Application extends App { $uid ); }); + $container->registerAlias('OCA\Files_Sharing\External\Manager', 'ExternalManager'); /** * Middleware diff --git a/apps/files_sharing/lib/Controller/RemoteController.php b/apps/files_sharing/lib/Controller/RemoteController.php new file mode 100644 index 0000000000..7c7a608ff2 --- /dev/null +++ b/apps/files_sharing/lib/Controller/RemoteController.php @@ -0,0 +1,183 @@ + + * @author Lukas Reschke + * @author Roeland Jago Douma + * + * @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 OCA\Files_Sharing\Controller; + +use OCA\Files_Sharing\External\Manager; +use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\OCS\OCSForbiddenException; +use OCP\AppFramework\OCS\OCSNotFoundException; +use OCP\AppFramework\OCSController; +use OCP\IRequest; + +class RemoteController extends OCSController { + + /** @var Manager */ + private $externalManager; + + /** + * @NoAdminRequired + * + * Remote constructor. + * + * @param string $appName + * @param IRequest $request + * @param Manager $externalManager + */ + public function __construct($appName, + IRequest $request, + Manager $externalManager) { + parent::__construct($appName, $request); + + $this->externalManager = $externalManager; + } + + /** + * @NoAdminRequired + * + * Get list of pending remote shares + * + * @return DataResponse + */ + public function getOpenShares() { + return new DataResponse($this->externalManager->getOpenShares()); + } + + /** + * @NoAdminRequired + * + * Accept a remote share + * + * @param int $id + * @return DataResponse + * @throws OCSNotFoundException + */ + public function acceptShare($id) { + if ($this->externalManager->acceptShare($id)) { + return new DataResponse(); + } + + // Make sure the user has no notification for something that does not exist anymore. + $this->externalManager->processNotification($id); + + throw new OCSNotFoundException('wrong share ID, share doesn\'t exist.'); + } + + /** + * @NoAdminRequired + * + * Decline a remote share + * + * @param int $id + * @return DataResponse + * @throws OCSNotFoundException + */ + public function declineShare($id) { + if ($this->externalManager->declineShare($id)) { + return new DataResponse(); + } + + // Make sure the user has no notification for something that does not exist anymore. + $this->externalManager->processNotification($id); + + throw new OCSNotFoundException('wrong share ID, share doesn\'t exist.'); + } + + /** + * @param array $share Share with info from the share_external table + * @return array enriched share info with data from the filecache + */ + private static function extendShareInfo($share) { + $view = new \OC\Files\View('/' . \OC_User::getUser() . '/files/'); + $info = $view->getFileInfo($share['mountpoint']); + + $share['mimetype'] = $info->getMimetype(); + $share['mtime'] = $info->getMTime(); + $share['permissions'] = $info->getPermissions(); + $share['type'] = $info->getType(); + $share['file_id'] = $info->getId(); + + return $share; + } + + /** + * @NoAdminRequired + * + * List accepted remote shares + * + * @return DataResponse + */ + public function getShares() { + $shares = $this->externalManager->getAcceptedShares(); + $shares = array_map('self::extendShareInfo', $shares); + + return new DataResponse($shares); + } + + /** + * @NoAdminRequired + * + * Get info of a remote share + * + * @param int $id + * @return DataResponse + * @throws OCSNotFoundException + */ + public function getShare($id) { + $shareInfo = $this->externalManager->getShare($id); + + if ($shareInfo === false) { + throw new OCSNotFoundException('share does not exist'); + } else { + $shareInfo = self::extendShareInfo($shareInfo); + return new DataResponse($shareInfo); + } + } + + /** + * @NoAdminRequired + * + * Unshare a remote share + * + * @param int $id + * @return DataResponse + * @throws OCSNotFoundException + * @throws OCSForbiddenException + */ + public function unshare($id) { + $shareInfo = $this->externalManager->getShare($id); + + if ($shareInfo === false) { + throw new OCSNotFoundException('Share does not exist'); + } + + $mountPoint = '/' . \OC_User::getUser() . '/files' . $shareInfo['mountpoint']; + + if ($this->externalManager->removeShare($mountPoint) === true) { + return new DataResponse(); + } else { + throw new OCSForbiddenException('Could not unshare'); + } + } +}