]> source.dussan.org Git - nextcloud-server.git/commitdiff
More exceptions
authorRoeland Jago Douma <roeland@famdouma.nl>
Wed, 20 Jul 2016 08:41:09 +0000 (10:41 +0200)
committerRoeland Jago Douma <roeland@famdouma.nl>
Fri, 5 Aug 2016 12:17:31 +0000 (14:17 +0200)
apps/files_sharing/lib/API/Share20OCS.php

index 1a0e75f2749ddfcb88ffe5cc70e6e917526a76b5..aebda5d85129ed0567bf170ee29edc48d80b9d9c 100644 (file)
@@ -24,6 +24,9 @@
 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;
@@ -245,6 +248,9 @@ class Share20OCS extends OCSController {
         *
         * @return DataResponse
         * @throws OCSNotFoundException
+        * @throws OCSForbiddenException
+        * @throws OCSBadRequestException
+        * @throws OCSException
         */
        public function createShare() {
                $share = $this->shareManager->newShare();
@@ -340,10 +346,7 @@ class Share20OCS extends OCSController {
                        if ($publicUpload === 'true') {
                                // Check if public upload is allowed
                                if (!$this->shareManager->shareApiLinkAllowPublicUpload()) {
-                                       return [
-                                               'statuscode' => 403,
-                                               'message' => $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
@@ -382,19 +385,13 @@ class Share20OCS extends OCSController {
 
                } else if ($shareType === \OCP\Share::SHARE_TYPE_REMOTE) {
                        if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) {
-                               return [
-                                       'statuscode' => 403,
-                                       'message' => $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 {
-                       return [
-                               'statuscode' => 400,
-                               $this->l->t('Unknown share type')
-                       ];
+                       throw new OCSBadRequestException($this->l->t('Unknown share type');
                }
 
                $share->setShareType($shareType);
@@ -404,20 +401,14 @@ class Share20OCS extends OCSController {
                        $share = $this->shareManager->createShare($share);
                } catch (GenericShareException $e) {
                        $code = $e->getCode() === 0 ? 403 : $e->getCode();
-                       return [
-                               'statuscode' => $code,
-                               'message' => $e->getHint()
-                       ];
+                       throw new OCSException($e->getHint(), $code);
                }catch (\Exception $e) {
-                       return [
-                               'statuscode' => 403,
-                               'message' => $e->getMessage()
-                       ];
+                       throw new OCSForbiddenException($e->getMessage());
                }
 
                $output = $this->formatShare($share);
 
-               return ['data' => $output];
+               return new DataResponse($output);
        }
 
        /**
@@ -447,13 +438,11 @@ class Share20OCS extends OCSController {
        /**
         * @param \OCP\Files\Folder $folder
         * @return DataResponse
+        * @throws OCSBadRequestException
         */
        private function getSharesInDir($folder) {
                if (!($folder instanceof \OCP\Files\Folder)) {
-                       return [
-                               'statuscode' => 400,
-                               'message' => $this->l->t('Not a directory')
-                       ];
+                       throw new OCSBadRequestException($this->l->t('Not a directory'));
                }
 
                $nodes = $folder->getDirectoryListing();
@@ -557,6 +546,8 @@ class Share20OCS extends OCSController {
         * @param int $id
         * @return DataResponse
         * @throws OCSNotFoundException
+        * @throws OCSBadRequestException
+        * @throws OCSForbiddenException
         */
        public function updateShare($id) {
                try {
@@ -581,10 +572,7 @@ class Share20OCS extends OCSController {
                 */
                if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
                        if ($permissions === null && $password === null && $publicUpload === null && $expireDate === null) {
-                               return [
-                                       'statuscode' => 400,
-                                       'message' => 'Wrong or no update parameter given'
-                               ];
+                               throw new OCSBadRequestException($this->l->t('Wrong or no update parameter given'));
                        }
 
                        $newPermissions = null;
@@ -606,10 +594,7 @@ class Share20OCS extends OCSController {
                                        \OCP\Constants::PERMISSION_CREATE, // hidden file list
                                ])
                        ) {
-                               return [
-                                       'statuscode' => 400,
-                                       'message' => $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 (
@@ -619,17 +604,11 @@ class Share20OCS extends OCSController {
                                $newPermissions === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)
                        ) {
                                if (!$this->shareManager->shareApiLinkAllowPublicUpload()) {
-                                       return [
-                                               'statuscode' => 403,
-                                               'message' => $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)) {
-                                       return [
-                                               'statuscode' => 400,
-                                               'message' => $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
@@ -646,10 +625,7 @@ class Share20OCS extends OCSController {
                                try {
                                        $expireDate = $this->parseDate($expireDate);
                                } catch (\Exception $e) {
-                                       return [
-                                               'statuscode' => 400,
-                                               'message' => $e->getMessage()
-                                       ];
+                                       throw new OCSBadRequestException($e->getMessage());
                                }
                                $share->setExpirationDate($expireDate);
                        }
@@ -663,10 +639,7 @@ class Share20OCS extends OCSController {
                } else {
                        // For other shares only permissions is valid.
                        if ($permissions === null) {
-                               return [
-                                       'statuscode' => 400,
-                                       'message' => $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);
@@ -686,10 +659,7 @@ class Share20OCS extends OCSController {
                                }
 
                                if ($share->getPermissions() & ~$maxPermissions) {
-                                       return [
-                                               'statuscode' => 404,
-                                               'message' => $this->l->t('Cannot increase permissions')
-                                       ];
+                                       throw new OCSBadRequestException($this->l->t('Cannot increase permissions'));
                                }
                        }
                }
@@ -698,10 +668,7 @@ class Share20OCS extends OCSController {
                try {
                        $share = $this->shareManager->updateShare($share);
                } catch (\Exception $e) {
-                       return [
-                               'statuscode' => 400,
-                               'message' => $e->getMessage()
-                       ];
+                       throw new OCSBadRequestException($e->getMessage());
                }
 
                return new DataResponse($this->formatShare($share));