diff options
author | Lukas Reschke <lukas@statuscode.ch> | 2016-08-08 14:59:59 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-08-08 14:59:59 +0200 |
commit | 70eef2a82e1f7a08e8783af16927c21e59d71ccb (patch) | |
tree | 83a6d711904048622e4f211bbde39fe3f7220402 /apps/files_sharing/lib | |
parent | e4436e46cb38675fc14b608af094e4e3b5fa7c19 (diff) | |
parent | dd9f195afd242c3d6023e8b6afecb73a61a99d60 (diff) | |
download | nextcloud-server-70eef2a82e1f7a08e8783af16927c21e59d71ccb.tar.gz nextcloud-server-70eef2a82e1f7a08e8783af16927c21e59d71ccb.zip |
Merge pull request #445 from nextcloud/ocs_share_to_appframework
OCS Share API to appframework
Diffstat (limited to 'apps/files_sharing/lib')
-rw-r--r-- | apps/files_sharing/lib/API/OCSShareWrapper.php | 64 | ||||
-rw-r--r-- | apps/files_sharing/lib/API/Share20OCS.php | 227 | ||||
-rw-r--r-- | apps/files_sharing/lib/AppInfo/Application.php | 24 | ||||
-rw-r--r-- | apps/files_sharing/lib/Middleware/OCSShareAPIMiddleware.php | 52 |
4 files changed, 185 insertions, 182 deletions
diff --git a/apps/files_sharing/lib/API/OCSShareWrapper.php b/apps/files_sharing/lib/API/OCSShareWrapper.php deleted file mode 100644 index fc1115647ed..00000000000 --- a/apps/files_sharing/lib/API/OCSShareWrapper.php +++ /dev/null @@ -1,64 +0,0 @@ -<?php -/** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @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/> - * - */ -namespace OCA\Files_Sharing\API; - -class OCSShareWrapper { - - /** - * @return Share20OCS - */ - private function getShare20OCS() { - return new Share20OCS( - \OC::$server->getShareManager(), - \OC::$server->getGroupManager(), - \OC::$server->getUserManager(), - \OC::$server->getRequest(), - \OC::$server->getRootFolder(), - \OC::$server->getURLGenerator(), - \OC::$server->getUserSession()->getUser(), - \OC::$server->getL10N('files_sharing') - ); - } - - public function getAllShares() { - return $this->getShare20OCS()->getShares(); - } - - public function createShare() { - return $this->getShare20OCS()->createShare(); - } - - public function getShare($params) { - $id = $params['id']; - return $this->getShare20OCS()->getShare($id); - } - - public function updateShare($params) { - $id = $params['id']; - return $this->getShare20OCS()->updateShare($id); - } - - public function deleteShare($params) { - $id = $params['id']; - return $this->getShare20OCS()->deleteShare($id); - } -} diff --git a/apps/files_sharing/lib/API/Share20OCS.php b/apps/files_sharing/lib/API/Share20OCS.php index 593e9d877c7..0cce05c3b17 100644 --- a/apps/files_sharing/lib/API/Share20OCS.php +++ b/apps/files_sharing/lib/API/Share20OCS.php @@ -23,6 +23,12 @@ */ namespace OCA\Files_Sharing\API; +use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\OCS\OCSBadRequestException; +use OCP\AppFramework\OCS\OCSException; +use OCP\AppFramework\OCS\OCSForbiddenException; +use OCP\AppFramework\OCS\OCSNotFoundException; +use OCP\AppFramework\OCSController; use OCP\Files\NotFoundException; use OCP\IGroupManager; use OCP\IL10N; @@ -32,7 +38,6 @@ use OCP\IURLGenerator; use OCP\IUser; use OCP\Files\IRootFolder; use OCP\Lock\LockedException; -use OCP\Share; use OCP\Share\IManager; use OCP\Share\Exceptions\ShareNotFound; use OCP\Share\Exceptions\GenericShareException; @@ -43,7 +48,7 @@ use OCP\Lock\ILockingProvider; * * @package OCA\Files_Sharing\API */ -class Share20OCS { +class Share20OCS extends OCSController { /** @var IManager */ private $shareManager; @@ -52,7 +57,7 @@ class Share20OCS { /** @var IUserManager */ private $userManager; /** @var IRequest */ - private $request; + protected $request; /** @var IRootFolder */ private $rootFolder; /** @var IURLGenerator */ @@ -61,28 +66,35 @@ class Share20OCS { private $currentUser; /** @var IL10N */ private $l; + /** @var \OCP\Files\Node */ + private $lockedNode; /** * Share20OCS constructor. * + * @param string $appName + * @param IRequest $request * @param IManager $shareManager * @param IGroupManager $groupManager * @param IUserManager $userManager - * @param IRequest $request * @param IRootFolder $rootFolder * @param IURLGenerator $urlGenerator * @param IUser $currentUser + * @param IL10N $l10n */ public function __construct( + $appName, + IRequest $request, IManager $shareManager, IGroupManager $groupManager, IUserManager $userManager, - IRequest $request, IRootFolder $rootFolder, IURLGenerator $urlGenerator, IUser $currentUser, IL10N $l10n ) { + parent::__construct($appName, $request); + $this->shareManager = $shareManager; $this->userManager = $userManager; $this->groupManager = $groupManager; @@ -133,7 +145,7 @@ class Share20OCS { } else { $result['item_type'] = 'file'; } - $result['mimetype'] = $node->getMimeType(); + $result['mimetype'] = $node->getMimetype(); $result['storage_id'] = $node->getStorage()->getId(); $result['storage'] = $node->getStorage()->getCache()->getNumericStorageId(); $result['item_source'] = $node->getId(); @@ -175,96 +187,93 @@ class Share20OCS { /** * Get a specific share by id * + * @NoAdminRequired + * * @param string $id - * @return \OC_OCS_Result + * @return DataResponse + * @throws OCSNotFoundException */ public function getShare($id) { - if (!$this->shareManager->shareApiEnabled()) { - return new \OC_OCS_Result(null, 404, $this->l->t('Share API is disabled')); - } - try { $share = $this->getShareById($id); } catch (ShareNotFound $e) { - return new \OC_OCS_Result(null, 404, $this->l->t('Wrong share ID, share doesn\'t exist')); + throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist')); } if ($this->canAccessShare($share)) { try { $share = $this->formatShare($share); - return new \OC_OCS_Result([$share]); + return new DataResponse(['data' => [$share]]); } catch (NotFoundException $e) { //Fall trough } } - return new \OC_OCS_Result(null, 404, $this->l->t('Wrong share ID, share doesn\'t exist')); + throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist')); } /** * Delete a share * + * @NoAdminRequired + * * @param string $id - * @return \OC_OCS_Result + * @return DataResponse + * @throws OCSNotFoundException */ public function deleteShare($id) { - if (!$this->shareManager->shareApiEnabled()) { - return new \OC_OCS_Result(null, 404, $this->l->t('Share API is disabled')); - } - try { $share = $this->getShareById($id); } catch (ShareNotFound $e) { - return new \OC_OCS_Result(null, 404, $this->l->t('Wrong share ID, share doesn\'t exist')); + throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist')); } try { - $share->getNode()->lock(ILockingProvider::LOCK_SHARED); + $this->lock($share->getNode()); } catch (LockedException $e) { - return new \OC_OCS_Result(null, 404, 'could not delete share'); + throw new OCSNotFoundException($this->l->t('could not delete share')); } if (!$this->canAccessShare($share, false)) { - $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return new \OC_OCS_Result(null, 404, $this->l->t('Could not delete share')); + throw new OCSNotFoundException($this->l->t('Could not delete share')); } $this->shareManager->deleteShare($share); - $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - - return new \OC_OCS_Result(); + return new DataResponse(); } /** - * @return \OC_OCS_Result + * @NoAdminRequired + * + * @return DataResponse + * @throws OCSNotFoundException + * @throws OCSForbiddenException + * @throws OCSBadRequestException + * @throws OCSException */ public function createShare() { $share = $this->shareManager->newShare(); - if (!$this->shareManager->shareApiEnabled()) { - return new \OC_OCS_Result(null, 404, $this->l->t('Share API is disabled')); - } - // Verify path $path = $this->request->getParam('path', null); if ($path === null) { - return new \OC_OCS_Result(null, 404, $this->l->t('Please specify a file or folder path')); + throw new OCSNotFoundException($this->l->t('Please specify a file or folder path')); } $userFolder = $this->rootFolder->getUserFolder($this->currentUser->getUID()); try { $path = $userFolder->get($path); } catch (NotFoundException $e) { - return new \OC_OCS_Result(null, 404, $this->l->t('Wrong path, file/folder doesn\'t exist')); + throw new OCSNotFoundException($this->l->t('Wrong path, file/folder doesn\'t exist')); } $share->setNode($path); try { - $share->getNode()->lock(ILockingProvider::LOCK_SHARED); + $this->lock($share->getNode()); } catch (LockedException $e) { - return new \OC_OCS_Result(null, 404, 'Could not create share'); + throw new OCSNotFoundException($this->l->t('Could not create share')); } // Parse permissions (if available) @@ -276,8 +285,7 @@ class Share20OCS { } if ($permissions < 0 || $permissions > \OCP\Constants::PERMISSION_ALL) { - $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return new \OC_OCS_Result(null, 404, 'invalid permissions'); + throw new OCSNotFoundException($this->l->t('invalid permissions')); } // Shares always require read permissions @@ -304,29 +312,25 @@ class Share20OCS { if ($shareType === \OCP\Share::SHARE_TYPE_USER) { // Valid user is required to share if ($shareWith === null || !$this->userManager->userExists($shareWith)) { - $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return new \OC_OCS_Result(null, 404, $this->l->t('Please specify a valid user')); + throw new OCSNotFoundException($this->l->t('Please specify a valid user')); } $share->setSharedWith($shareWith); $share->setPermissions($permissions); } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) { if (!$this->shareManager->allowGroupSharing()) { - $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return new \OC_OCS_Result(null, 404, $this->l->t('Group sharing is disabled by the administrator')); + throw new OCSNotFoundException($this->l->t('Group sharing is disabled by the administrator')); } // Valid group is required to share if ($shareWith === null || !$this->groupManager->groupExists($shareWith)) { - $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return new \OC_OCS_Result(null, 404, $this->l->t('Please specify a valid group')); + throw new OCSNotFoundException($this->l->t('Please specify a valid group')); } $share->setSharedWith($shareWith); $share->setPermissions($permissions); } else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) { //Can we even share links? if (!$this->shareManager->shareApiAllowLinks()) { - $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return new \OC_OCS_Result(null, 404, $this->l->t('Public link sharing is disabled by the administrator')); + throw new OCSNotFoundException($this->l->t('Public link sharing is disabled by the administrator')); } /* @@ -335,22 +339,19 @@ class Share20OCS { */ $existingShares = $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_LINK, $path, false, 1, 0); if (!empty($existingShares)) { - $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return new \OC_OCS_Result($this->formatShare($existingShares[0])); + return new DataResponse(['data' => $this->formatShare($existingShares[0])]); } $publicUpload = $this->request->getParam('publicUpload', null); if ($publicUpload === 'true') { // Check if public upload is allowed if (!$this->shareManager->shareApiLinkAllowPublicUpload()) { - $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return new \OC_OCS_Result(null, 403, $this->l->t('Public upload disabled by the administrator')); + throw new OCSForbiddenException($this->l->t('Public upload disabled by the administrator')); } // Public upload can only be set for folders if ($path instanceof \OCP\Files\File) { - $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return new \OC_OCS_Result(null, 404, $this->l->t('Public upload is only possible for publicly shared folders')); + throw new OCSNotFoundException($this->l->t('Public upload is only possible for publicly shared folders')); } $share->setPermissions( @@ -378,22 +379,19 @@ class Share20OCS { $expireDate = $this->parseDate($expireDate); $share->setExpirationDate($expireDate); } catch (\Exception $e) { - $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return new \OC_OCS_Result(null, 404, $this->l->t('Invalid date, date format must be YYYY-MM-DD')); + throw new OCSNotFoundException($this->l->t('Invalid date, date format must be YYYY-MM-DD')); } } } else if ($shareType === \OCP\Share::SHARE_TYPE_REMOTE) { if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) { - $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return new \OC_OCS_Result(null, 403, $this->l->t('Sharing %s failed because the back end does not allow shares from type %s', [$path->getPath(), $shareType])); + throw new OCSForbiddenException($this->l->t('Sharing %s failed because the back end does not allow shares from type %s', [$path->getPath(), $shareType])); } $share->setSharedWith($shareWith); $share->setPermissions($permissions); } else { - $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return new \OC_OCS_Result(null, 400, $this->l->t('Unknown share type')); + throw new OCSBadRequestException($this->l->t('Unknown share type')); } $share->setShareType($shareType); @@ -403,23 +401,19 @@ class Share20OCS { $share = $this->shareManager->createShare($share); } catch (GenericShareException $e) { $code = $e->getCode() === 0 ? 403 : $e->getCode(); - $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return new \OC_OCS_Result(null, $code, $e->getHint()); + throw new OCSException($e->getHint(), $code); }catch (\Exception $e) { - $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return new \OC_OCS_Result(null, 403, $e->getMessage()); + throw new OCSForbiddenException($e->getMessage()); } $output = $this->formatShare($share); - $share->getNode()->unlock(\OCP\Lock\ILockingProvider::LOCK_SHARED); - - return new \OC_OCS_Result($output); + return new DataResponse(['data' => $output]); } /** * @param \OCP\Files\File|\OCP\Files\Folder $node - * @return \OC_OCS_Result + * @return DataResponse */ private function getSharedWithMe($node = null) { $userShares = $this->shareManager->getSharedWith($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_USER, $node, -1, 0); @@ -438,16 +432,17 @@ class Share20OCS { } } - return new \OC_OCS_Result($formatted); + return new DataResponse(['data' => $formatted]); } /** * @param \OCP\Files\Folder $folder - * @return \OC_OCS_Result + * @return DataResponse + * @throws OCSBadRequestException */ private function getSharesInDir($folder) { if (!($folder instanceof \OCP\Files\Folder)) { - return new \OC_OCS_Result(null, 400, $this->l->t('Not a directory')); + throw new OCSBadRequestException($this->l->t('Not a directory')); } $nodes = $folder->getDirectoryListing(); @@ -471,25 +466,24 @@ class Share20OCS { } } - return new \OC_OCS_Result($formatted); + return new DataResponse(['data' => $formatted]); } /** * The getShares function. * + * @NoAdminRequired + * * - Get shares by the current user * - Get shares by the current user and reshares (?reshares=true) * - Get shares with the current user (?shared_with_me=true) * - Get shares for a specific path (?path=...) * - Get all shares in a folder (?subfiles=true&path=..) * - * @return \OC_OCS_Result + * @return DataResponse + * @throws OCSNotFoundException */ public function getShares() { - if (!$this->shareManager->shareApiEnabled()) { - return new \OC_OCS_Result(); - } - $sharedWithMe = $this->request->getParam('shared_with_me', null); $reshares = $this->request->getParam('reshares', null); $subfiles = $this->request->getParam('subfiles'); @@ -499,27 +493,21 @@ class Share20OCS { $userFolder = $this->rootFolder->getUserFolder($this->currentUser->getUID()); try { $path = $userFolder->get($path); - $path->lock(ILockingProvider::LOCK_SHARED); + $this->lock($path); } catch (\OCP\Files\NotFoundException $e) { - return new \OC_OCS_Result(null, 404, $this->l->t('Wrong path, file/folder doesn\'t exist')); + throw new OCSNotFoundException($this->l->t('Wrong path, file/folder doesn\'t exist')); } catch (LockedException $e) { - return new \OC_OCS_Result(null, 404, $this->l->t('Could not lock path')); + throw new OCSNotFoundException($this->l->t('Could not lock path')); } } if ($sharedWithMe === 'true') { $result = $this->getSharedWithMe($path); - if ($path !== null) { - $path->unlock(ILockingProvider::LOCK_SHARED); - } return $result; } if ($subfiles === 'true') { $result = $this->getSharesInDir($path); - if ($path !== null) { - $path->unlock(ILockingProvider::LOCK_SHARED); - } return $result; } @@ -549,33 +537,29 @@ class Share20OCS { } } - if ($path !== null) { - $path->unlock(ILockingProvider::LOCK_SHARED); - } - - return new \OC_OCS_Result($formatted); + return new DataResponse(['data' => $formatted]); } /** + * @NoAdminRequired + * * @param int $id - * @return \OC_OCS_Result + * @return DataResponse + * @throws OCSNotFoundException + * @throws OCSBadRequestException + * @throws OCSForbiddenException */ public function updateShare($id) { - if (!$this->shareManager->shareApiEnabled()) { - return new \OC_OCS_Result(null, 404, $this->l->t('Share API is disabled')); - } - try { $share = $this->getShareById($id); } catch (ShareNotFound $e) { - return new \OC_OCS_Result(null, 404, $this->l->t('Wrong share ID, share doesn\'t exist')); + throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist')); } - $share->getNode()->lock(\OCP\Lock\ILockingProvider::LOCK_SHARED); + $this->lock($share->getNode()); if (!$this->canAccessShare($share, false)) { - $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return new \OC_OCS_Result(null, 404, $this->l->t('Wrong share ID, share doesn\'t exist')); + throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist')); } $permissions = $this->request->getParam('permissions', null); @@ -588,8 +572,7 @@ class Share20OCS { */ if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { if ($permissions === null && $password === null && $publicUpload === null && $expireDate === null) { - $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return new \OC_OCS_Result(null, 400, 'Wrong or no update parameter given'); + throw new OCSBadRequestException($this->l->t('Wrong or no update parameter given')); } $newPermissions = null; @@ -611,8 +594,7 @@ class Share20OCS { \OCP\Constants::PERMISSION_CREATE, // hidden file list ]) ) { - $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return new \OC_OCS_Result(null, 400, $this->l->t('Can\'t change permissions for public share links')); + throw new OCSBadRequestException($this->l->t('Can\'t change permissions for public share links')); } if ( @@ -622,13 +604,11 @@ class Share20OCS { $newPermissions === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE) ) { if (!$this->shareManager->shareApiLinkAllowPublicUpload()) { - $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return new \OC_OCS_Result(null, 403, $this->l->t('Public upload disabled by the administrator')); + throw new OCSForbiddenException($this->l->t('Public upload disabled by the administrator')); } if (!($share->getNode() instanceof \OCP\Files\Folder)) { - $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return new \OC_OCS_Result(null, 400, $this->l->t('Public upload is only possible for publicly shared folders')); + throw new OCSBadRequestException($this->l->t('Public upload is only possible for publicly shared folders')); } // normalize to correct public upload permissions @@ -645,8 +625,7 @@ class Share20OCS { try { $expireDate = $this->parseDate($expireDate); } catch (\Exception $e) { - $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return new \OC_OCS_Result(null, 400, $e->getMessage()); + throw new OCSBadRequestException($e->getMessage()); } $share->setExpirationDate($expireDate); } @@ -660,8 +639,7 @@ class Share20OCS { } else { // For other shares only permissions is valid. if ($permissions === null) { - $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return new \OC_OCS_Result(null, 400, $this->l->t('Wrong or no update parameter given')); + throw new OCSBadRequestException($this->l->t('Wrong or no update parameter given')); } else { $permissions = (int)$permissions; $share->setPermissions($permissions); @@ -673,6 +651,7 @@ class Share20OCS { $incomingShares = $this->shareManager->getSharedWith($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_USER, $share->getNode(), -1, 0); $incomingShares = array_merge($incomingShares, $this->shareManager->getSharedWith($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_GROUP, $share->getNode(), -1, 0)); + /** @var \OCP\Share\IShare[] $incomingShares */ if (!empty($incomingShares)) { $maxPermissions = 0; foreach ($incomingShares as $incomingShare) { @@ -680,8 +659,7 @@ class Share20OCS { } if ($share->getPermissions() & ~$maxPermissions) { - $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return new \OC_OCS_Result(null, 404, $this->l->t('Cannot increase permissions')); + throw new OCSNotFoundException($this->l->t('Cannot increase permissions')); } } } @@ -690,13 +668,10 @@ class Share20OCS { try { $share = $this->shareManager->updateShare($share); } catch (\Exception $e) { - $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return new \OC_OCS_Result(null, 400, $e->getMessage()); + throw new OCSBadRequestException($e->getMessage()); } - $share->getNode()->unlock(\OCP\Lock\ILockingProvider::LOCK_SHARED); - - return new \OC_OCS_Result($this->formatShare($share)); + return new DataResponse(['data' => $this->formatShare($share)]); } /** @@ -782,4 +757,22 @@ class Share20OCS { return $share; } + + /** + * Lock a Node + * @param \OCP\Files\Node $node + */ + private function lock(\OCP\Files\Node $node) { + $node->lock(ILockingProvider::LOCK_SHARED); + $this->lockedNode = $node; + } + + /** + * Cleanup the remaining locks + */ + public function cleanup() { + if ($this->lockedNode !== null) { + $this->lockedNode->unlock(ILockingProvider::LOCK_SHARED); + } + } } diff --git a/apps/files_sharing/lib/AppInfo/Application.php b/apps/files_sharing/lib/AppInfo/Application.php index aff42cd43dc..b9598bd4a2b 100644 --- a/apps/files_sharing/lib/AppInfo/Application.php +++ b/apps/files_sharing/lib/AppInfo/Application.php @@ -28,6 +28,8 @@ namespace OCA\Files_Sharing\AppInfo; use OCA\FederatedFileSharing\DiscoveryManager; +use OCA\Files_Sharing\API\Share20OCS; +use OCA\Files_Sharing\Middleware\OCSShareAPIMiddleware; use OCA\Files_Sharing\MountProvider; use OCP\AppFramework\App; use OC\AppFramework\Utility\SimpleContainer; @@ -35,7 +37,6 @@ use OCA\Files_Sharing\Controllers\ExternalSharesController; use OCA\Files_Sharing\Controllers\ShareController; use OCA\Files_Sharing\Middleware\SharingCheckMiddleware; use \OCP\IContainer; -use OCA\Files_Sharing\Capabilities; class Application extends App { public function __construct(array $urlParams = array()) { @@ -73,6 +74,19 @@ class Application extends App { $c->query('HttpClientService') ); }); + $container->registerService('ShareAPIController', function (SimpleContainer $c) use ($server) { + return new Share20OCS( + $c->query('AppName'), + $c->query('Request'), + $server->getShareManager(), + $server->getGroupManager(), + $server->getUserManager(), + $server->getRootFolder(), + $server->getURLGenerator(), + $server->getUserSession()->getUser(), + $server->getL10N($c->query('AppName')) + ); + }); /** * Core class wrappers @@ -110,8 +124,16 @@ class Application extends App { ); }); + $container->registerService('OCSShareAPIMiddleware', function (SimpleContainer $c) use ($server) { + return new OCSShareAPIMiddleware( + $server->getShareManager(), + $server->getL10N($c->query('AppName')) + ); + }); + // Execute middlewares $container->registerMiddleware('SharingCheckMiddleware'); + $container->registerMiddleWare('OCSShareAPIMiddleware'); $container->registerService('MountProvider', function (IContainer $c) { /** @var \OCP\IServerContainer $server */ diff --git a/apps/files_sharing/lib/Middleware/OCSShareAPIMiddleware.php b/apps/files_sharing/lib/Middleware/OCSShareAPIMiddleware.php new file mode 100644 index 00000000000..04a6545c9a7 --- /dev/null +++ b/apps/files_sharing/lib/Middleware/OCSShareAPIMiddleware.php @@ -0,0 +1,52 @@ +<?php + +namespace OCA\Files_Sharing\Middleware; + +use OCA\Files_Sharing\API\Share20OCS; +use OCP\AppFramework\Http\Response; +use OCP\AppFramework\Middleware; +use OCP\AppFramework\OCS\OCSNotFoundException; +use OCP\IL10N; +use OCP\Share\IManager; + +class OCSShareAPIMiddleware extends Middleware { + /** @var IManager */ + private $shareManager; + /** @var IL10N */ + private $l; + + public function __construct(IManager $shareManager, + IL10N $l) { + $this->shareManager = $shareManager; + $this->l = $l; + } + + /** + * @param \OCP\AppFramework\Controller $controller + * @param string $methodName + * + * @throws OCSNotFoundException + */ + public function beforeController($controller, $methodName) { + if ($controller instanceof Share20OCS) { + if (!$this->shareManager->shareApiEnabled()) { + throw new OCSNotFoundException($this->l->t('Share API is disabled')); + } + } + } + + /** + * @param \OCP\AppFramework\Controller $controller + * @param string $methodName + * @param Response $response + * @return Response + */ + public function afterController($controller, $methodName, Response $response) { + if ($controller instanceof Share20OCS) { + /** @var Share20OCS $controller */ + $controller->cleanup(); + } + + return $response; + } +} |