Remove old code + add Middleware

* Add proper middleware for shareinfo
* Remove old shareinfo routes

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma 2017-07-25 22:25:23 +02:00
parent 8a539ec0f6
commit c9d2e31d52
No known key found for this signature in database
GPG Key ID: F941078878347C0C
5 changed files with 96 additions and 125 deletions

View File

@ -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));

View File

@ -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');
];

View File

@ -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 */

View File

@ -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) {

View File

@ -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;
}
}