diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2017-07-25 23:25:23 +0300 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2017-08-10 14:49:09 +0300 |
commit | c9d2e31d527190660c51a75741747178f029091c (patch) | |
tree | 71aad956d8da297a96861bd9ad685f6250bb7d95 /apps/files_sharing | |
parent | 8a539ec0f6dc1650902a9340197d62688164d462 (diff) |
Remove old code + add Middleware
* Add proper middleware for shareinfo
* Remove old shareinfo routes
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'apps/files_sharing')
-rw-r--r-- | apps/files_sharing/ajax/shareinfo.php | 111 | ||||
-rw-r--r-- | apps/files_sharing/appinfo/routes.php | 9 | ||||
-rw-r--r-- | apps/files_sharing/lib/AppInfo/Application.php | 8 | ||||
-rw-r--r-- | apps/files_sharing/lib/Controller/ShareInfoController.php | 9 | ||||
-rw-r--r-- | apps/files_sharing/lib/Middleware/ShareInfoMiddleware.php | 84 |
5 files changed, 96 insertions, 125 deletions
diff --git a/apps/files_sharing/ajax/shareinfo.php b/apps/files_sharing/ajax/shareinfo.php deleted file mode 100644 index a32b0a07328..00000000000 --- a/apps/files_sharing/ajax/shareinfo.php +++ /dev/null @@ -1,111 +0,0 @@ -<?php -/** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Bjoern Schiessle <bjoern@schiessle.org> - * @author Björn Schießle <bjoern@schiessle.org> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Stefan Weil <sw@weilnetz.de> - * @author Vincent Petry <pvince81@owncloud.com> - * - * @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 <http://www.gnu.org/licenses/> - * - */ - -OCP\JSON::checkAppEnabled('files_sharing'); - -if (!isset($_GET['t'])) { - \OC_Response::setStatus(400); //400 Bad Request - exit; -} - -$federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application(); -$federatedShareProvider = $federatedSharingApp->getFederatedShareProvider(); - -if ($federatedShareProvider->isOutgoingServer2serverShareEnabled() === false) { - \OC_Response::setStatus(404); // 404 not found - exit; -} - -$token = $_GET['t']; - -$password = null; -if (isset($_POST['password'])) { - $password = $_POST['password']; -} - -$relativePath = null; -if (isset($_GET['dir'])) { - $relativePath = $_GET['dir']; -} - -$data = \OCA\Files_Sharing\Helper::setupFromToken($token, $relativePath, $password); - -/** @var \OCP\Share\IShare $share */ -$share = $data['share']; -// Load the files -$path = $data['realPath']; - -$isWritable = $share->getPermissions() & (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_CREATE); -if (!$isWritable) { - // FIXME: should not add storage wrappers outside of preSetup, need to find a better way - $previousLog = \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false); - \OC\Files\Filesystem::addStorageWrapper('readonly', function ($mountPoint, $storage) { - return new \OC\Files\Storage\Wrapper\PermissionsMask(array('storage' => $storage, 'mask' => \OCP\Constants::PERMISSION_READ + \OCP\Constants::PERMISSION_SHARE)); - }); - \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper($previousLog); -} - -$rootInfo = \OC\Files\Filesystem::getFileInfo($path); -$rootView = new \OC\Files\View(''); - -if($rootInfo === false || !($share->getPermissions() & \OCP\Constants::PERMISSION_READ)) { - OCP\JSON::error(array('data' => 'Share is not readable.')); - exit(); -} - -/** - * @param \OCP\Files\FileInfo $dir - * @param \OC\Files\View $view - * @return array - */ -function getChildInfo($dir, $view, $sharePermissions) { - $children = $view->getDirectoryContent($dir->getPath()); - $result = array(); - foreach ($children as $child) { - $formatted = \OCA\Files\Helper::formatFileInfo($child); - if ($child->getType() === 'dir') { - $formatted['children'] = getChildInfo($child, $view, $sharePermissions); - } - $formatted['mtime'] = $formatted['mtime'] / 1000; - $formatted['permissions'] = $sharePermissions & (int)$formatted['permissions']; - $result[] = $formatted; - } - return $result; -} - -$result = \OCA\Files\Helper::formatFileInfo($rootInfo); -$result['mtime'] = $result['mtime'] / 1000; -$result['permissions'] = (int)$result['permissions'] & $share->getPermissions(); - - -if ($rootInfo->getType() === 'dir') { - $result['children'] = getChildInfo($rootInfo, $rootView, $share->getPermissions()); -} - -OCP\JSON::success(array('data' => $result)); diff --git a/apps/files_sharing/appinfo/routes.php b/apps/files_sharing/appinfo/routes.php index 104c4167e84..310b1c46eb6 100644 --- a/apps/files_sharing/appinfo/routes.php +++ b/apps/files_sharing/appinfo/routes.php @@ -26,8 +26,7 @@ * */ -$application = new \OCA\Files_Sharing\AppInfo\Application(); -$application->registerRoutes($this, [ +return [ 'resources' => [ 'ExternalShares' => ['url' => '/api/externalShares'], ], @@ -126,8 +125,4 @@ $application->registerRoutes($this, [ 'verb' => 'DELETE', ], ], -]); - -/** @var $this \OCP\Route\IRouter */ -$this->create('sharing_external_shareinfo', '/shareinfo3') - ->actionInclude('files_sharing/ajax/shareinfo.php'); +]; diff --git a/apps/files_sharing/lib/AppInfo/Application.php b/apps/files_sharing/lib/AppInfo/Application.php index db2175c3445..3e95c738df8 100644 --- a/apps/files_sharing/lib/AppInfo/Application.php +++ b/apps/files_sharing/lib/AppInfo/Application.php @@ -28,6 +28,7 @@ namespace OCA\Files_Sharing\AppInfo; use OCA\Files_Sharing\Middleware\OCSShareAPIMiddleware; +use OCA\Files_Sharing\Middleware\ShareInfoMiddleware; use OCA\Files_Sharing\MountProvider; use OCP\AppFramework\App; use OC\AppFramework\Utility\SimpleContainer; @@ -124,9 +125,16 @@ class Application extends App { ); }); + $container->registerService(ShareInfoMiddleware::class, function () use ($server) { + return new ShareInfoMiddleware( + $server->getShareManager() + ); + }); + // Execute middlewares $container->registerMiddleWare('SharingCheckMiddleware'); $container->registerMiddleWare('OCSShareAPIMiddleware'); + $container->registerMiddleWare(ShareInfoMiddleware::class); $container->registerService('MountProvider', function (IContainer $c) { /** @var \OCP\IServerContainer $server */ diff --git a/apps/files_sharing/lib/Controller/ShareInfoController.php b/apps/files_sharing/lib/Controller/ShareInfoController.php index d926721f06c..696b064ac0f 100644 --- a/apps/files_sharing/lib/Controller/ShareInfoController.php +++ b/apps/files_sharing/lib/Controller/ShareInfoController.php @@ -41,9 +41,9 @@ class ShareInfoController extends ApiController { * @param null $password * @param null $dir * @return JSONResponse + * @throws ShareNotFound */ public function info($t, $password = null, $dir = null) { - $this->logger->error('HERE!'); try { $share = $this->shareManager->getShareByToken($t); } catch (ShareNotFound $e) { @@ -79,12 +79,7 @@ class ShareInfoController extends ApiController { } } - $result = [ - 'data' => $this->parseNode($node), - 'status' => 'success' - ]; - - return new JSONResponse($result); + return new JSONResponse($this->parseNode($node)); } private function parseNode(Node $node) { diff --git a/apps/files_sharing/lib/Middleware/ShareInfoMiddleware.php b/apps/files_sharing/lib/Middleware/ShareInfoMiddleware.php new file mode 100644 index 00000000000..a069ecacc2a --- /dev/null +++ b/apps/files_sharing/lib/Middleware/ShareInfoMiddleware.php @@ -0,0 +1,84 @@ +<?php + +namespace OCA\Files_Sharing\Middleware; + +use OCA\FederatedFileSharing\FederatedShareProvider; +use OCA\Files_Sharing\Controller\ShareInfoController; +use OCA\Files_Sharing\Exceptions\S2SException; +use OCP\AppFramework\Controller; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\JSONResponse; +use OCP\AppFramework\Http\Response; +use OCP\AppFramework\Middleware; +use OCP\Share\IManager; + +class ShareInfoMiddleware extends Middleware { + /** @var IManager */ + private $shareManager; + + public function __construct(IManager $shareManager) { + $this->shareManager = $shareManager; + } + + /** + * @param Controller $controller + * @param string $methodName + * @throws S2SException + */ + public function beforeController(Controller $controller, $methodName) { + if (!($controller instanceof ShareInfoController)) { + return; + } + + if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) { + throw new S2SException(); + } + } + + /** + * @param Controller $controller + * @param string $methodName + * @param \Exception $exception + * @throws \Exception + * @return Response + */ + public function afterException(Controller $controller, $methodName, \Exception $exception) { + if (!($controller instanceof ShareInfoController)) { + throw $exception; + } + + if ($exception instanceof S2SException) { + return new JSONResponse([], Http::STATUS_NOT_FOUND); + } + } + + /** + * @param Controller $controller + * @param string $methodName + * @param Response $response + * @return Response + */ + public function afterController(Controller $controller, $methodName, Response $response) { + if (!($controller instanceof ShareInfoController)) { + return $response; + } + + if (!($response instanceof JSONResponse)) { + return $response; + } + + $data = $response->getData(); + $status = 'error'; + + if ($response->getStatus() === Http::STATUS_OK) { + $status = 'success'; + } + + $response->setData([ + 'data' => $data, + 'status' => $status, + ]); + + return $response; + } +} |