From 8b160077f1a2ea7851dab8ad3b2b55bac0ae5766 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Wed, 20 Jul 2016 09:55:43 +0200 Subject: [PATCH] Throw OCSNotFoundExceptions --- apps/files_sharing/lib/API/Share20OCS.php | 131 +++++++--------------- 1 file changed, 40 insertions(+), 91 deletions(-) diff --git a/apps/files_sharing/lib/API/Share20OCS.php b/apps/files_sharing/lib/API/Share20OCS.php index f8e8108cb52..a468fd3fc14 100644 --- a/apps/files_sharing/lib/API/Share20OCS.php +++ b/apps/files_sharing/lib/API/Share20OCS.php @@ -23,6 +23,8 @@ */ namespace OCA\Files_Sharing\API; +use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\OCS\OCSNotFoundException; use OCP\AppFramework\OCSController; use OCP\Files\NotFoundException; use OCP\IGroupManager; @@ -183,7 +185,8 @@ class Share20OCS extends OCSController { * @NoAdminRequired * * @param string $id - * @return array + * @return DataResponse + * @throws OCSNotFoundException */ public function getShare($id) { if (!$this->shareManager->shareApiEnabled()) { @@ -196,27 +199,19 @@ class Share20OCS extends OCSController { try { $share = $this->getShareById($id); } catch (ShareNotFound $e) { - return [ - 'statuscode' => 404, - 'message' => $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 [ - 'data' => [$share], - ]; + return new DataResponse([$share]); } catch (NotFoundException $e) { //Fall trough } } - return [ - 'statuscode' => 404, - 'message' => $this->l->t('Wrong share ID, share doesn\'t exist') - ]; + throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist')); } /** @@ -226,6 +221,7 @@ class Share20OCS extends OCSController { * * @param string $id * @return \OC\OCS\Result + * @throws OCSNotFoundException */ public function deleteShare($id) { if (!$this->shareManager->shareApiEnabled()) { @@ -238,40 +234,32 @@ class Share20OCS extends OCSController { try { $share = $this->getShareById($id); } catch (ShareNotFound $e) { - return [ - 'statuscode' => 404, - 'message' => $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); } catch (LockedException $e) { - return [ - 'statuscode' => 404, - 'message' => $this->l->t('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 [ - 'statuscode' => 404, - 'message' => $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 []; + return new DataResponse([]); } /** * @NoAdminRequired * - * @return array + * @return DataResponse + * @throws OCSNotFoundException */ public function createShare() { $share = $this->shareManager->newShare(); @@ -286,20 +274,14 @@ class Share20OCS extends OCSController { // Verify path $path = $this->request->getParam('path', null); if ($path === null) { - return [ - 'statuscode' => 404, - 'message' => $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 [ - 'statuscode' => 404, - 'messagecode' => $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); @@ -307,10 +289,7 @@ class Share20OCS extends OCSController { try { $share->getNode()->lock(ILockingProvider::LOCK_SHARED); } catch (LockedException $e) { - return [ - 'statuscode' => 404, - 'message' => $this->l->t('Could not create share') - ]; + throw new OCSNotFoundException($this->l->t('Could not create share')); } // Parse permissions (if available) @@ -323,10 +302,7 @@ class Share20OCS extends OCSController { if ($permissions < 0 || $permissions > \OCP\Constants::PERMISSION_ALL) { $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return [ - 'statuscode' => 404, - 'message' => 'invalid permissions' - ]; + throw new OCSNotFoundException($this->l->t('invalid permissions')); } // Shares always require read permissions @@ -354,29 +330,20 @@ class Share20OCS extends OCSController { // Valid user is required to share if ($shareWith === null || !$this->userManager->userExists($shareWith)) { $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return [ - 'statuscode' => 404, - 'message' => $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 [ - 'statuscode' => 404, - 'message' => $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 [ - 'statuscode' => 404, - 'message' => $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); @@ -384,10 +351,7 @@ class Share20OCS extends OCSController { //Can we even share links? if (!$this->shareManager->shareApiAllowLinks()) { $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return [ - 'statuscode' => 404, - 'message' => $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')); } /* @@ -397,7 +361,7 @@ class Share20OCS extends OCSController { $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 ['data' => $this->formatShare($existingShares[0])]; + return new DataResponse($this->formatShare($existingShares[0])); } $publicUpload = $this->request->getParam('publicUpload', null); @@ -414,10 +378,7 @@ class Share20OCS extends OCSController { // Public upload can only be set for folders if ($path instanceof \OCP\Files\File) { $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return [ - 'statuscode' => 404, - 'message' => $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( @@ -446,10 +407,7 @@ class Share20OCS extends OCSController { $share->setExpirationDate($expireDate); } catch (\Exception $e) { $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return [ - 'statuscode' => 404, - 'message' => $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')); } } @@ -501,7 +459,7 @@ class Share20OCS extends OCSController { /** * @param \OCP\Files\File|\OCP\Files\Folder $node - * @return array + * @return DataResponse */ private function getSharedWithMe($node = null) { $userShares = $this->shareManager->getSharedWith($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_USER, $node, -1, 0); @@ -520,12 +478,12 @@ class Share20OCS extends OCSController { } } - return ['data' => $formatted]; + return new DataResponse($formatted); } /** * @param \OCP\Files\Folder $folder - * @return array + * @return DataResponse */ private function getSharesInDir($folder) { if (!($folder instanceof \OCP\Files\Folder)) { @@ -556,7 +514,7 @@ class Share20OCS extends OCSController { } } - return ['data' => $formatted]; + return new DataResponse($formatted); } /** @@ -570,7 +528,8 @@ class Share20OCS extends OCSController { * - Get shares for a specific path (?path=...) * - Get all shares in a folder (?subfiles=true&path=..) * - * @return array + * @return DataResponse + * @throws OCSNotFoundException */ public function getShares() { if (!$this->shareManager->shareApiEnabled()) { @@ -588,15 +547,9 @@ class Share20OCS extends OCSController { $path = $userFolder->get($path); $path->lock(ILockingProvider::LOCK_SHARED); } catch (\OCP\Files\NotFoundException $e) { - return [ - 'statuscode' => 404, - 'message' => $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 [ - 'statuscode' => 404, - 'message' => $this->l->t('Could not lock path') - ]; + throw new OCSNotFoundException($this->l->t('Could not lock path')); } } @@ -646,14 +599,15 @@ class Share20OCS extends OCSController { $path->unlock(ILockingProvider::LOCK_SHARED); } - return ['data' => $formatted]; + return new DataResponse($formatted); } /** * @NoAdminRequired * * @param int $id - * @return array + * @return DataResponse + * @throws OCSNotFoundException */ public function updateShare($id) { if (!$this->shareManager->shareApiEnabled()) { @@ -666,20 +620,14 @@ class Share20OCS extends OCSController { try { $share = $this->getShareById($id); } catch (ShareNotFound $e) { - return [ - 'statuscode' => 404, - 'message' => $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); if (!$this->canAccessShare($share, false)) { $share->getNode()->unlock(ILockingProvider::LOCK_SHARED); - return [ - 'statuscode' => 404, - 'message' => $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); @@ -795,6 +743,7 @@ class Share20OCS extends OCSController { $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) { @@ -824,7 +773,7 @@ class Share20OCS extends OCSController { $share->getNode()->unlock(\OCP\Lock\ILockingProvider::LOCK_SHARED); - return ['data' => $this->formatShare($share)]; + return new DataResponse($this->formatShare($share)); } /** -- 2.39.5