diff options
author | Roeland Jago Douma <rullzer@owncloud.com> | 2015-12-15 09:54:50 +0100 |
---|---|---|
committer | Roeland Jago Douma <rullzer@owncloud.com> | 2016-01-06 14:53:43 +0100 |
commit | 38d3a638ed2ec055ddf4a09da682961b6976bc4d (patch) | |
tree | 0ddc1acf2fc1734fab0b44cfcfbf47c59de26463 /apps/files_sharing/api/share20ocs.php | |
parent | b15be8f96fb875ab1834c15b9de80b10147e8863 (diff) | |
download | nextcloud-server-38d3a638ed2ec055ddf4a09da682961b6976bc4d.tar.gz nextcloud-server-38d3a638ed2ec055ddf4a09da682961b6976bc4d.zip |
[Share 2.0] Enable share creation via OCS API
Diffstat (limited to 'apps/files_sharing/api/share20ocs.php')
-rw-r--r-- | apps/files_sharing/api/share20ocs.php | 148 |
1 files changed, 147 insertions, 1 deletions
diff --git a/apps/files_sharing/api/share20ocs.php b/apps/files_sharing/api/share20ocs.php index 6c25b4a4426..bf644ce00f6 100644 --- a/apps/files_sharing/api/share20ocs.php +++ b/apps/files_sharing/api/share20ocs.php @@ -25,7 +25,6 @@ use OC\Share20\IShare; use OCP\IGroupManager; use OCP\IUserManager; use OCP\IRequest; -use OCP\Files\Folder; use OCP\IURLGenerator; use OCP\IUser; use OCP\Files\IRootFolder; @@ -192,6 +191,127 @@ class Share20OCS { } /** + * @return \OC_OCS_Result + */ + public function createShare() { + $share = $this->shareManager->newShare(); + + // Verify path + $path = $this->request->getParam('path', null); + if ($path === null) { + return new \OC_OCS_Result(null, 404, 'please specify a file or folder path'); + } + + $userFolder = $this->rootFolder->getUserFolder($this->currentUser->getUID()); + try { + $path = $userFolder->get($path); + } catch (\OCP\Files\NotFoundException $e) { + return new \OC_OCS_Result(null, 404, 'wrong path, file/folder doesn\'t exist'); + } + + $share->setPath($path); + + // Parse permissions (if available) + $permissions = $this->request->getParam('permissions', null); + if ($permissions === null) { + $permissions = \OCP\Constants::PERMISSION_ALL; + } else { + $permissions = (int)$permissions; + } + + if ($permissions < 0 || $permissions > \OCP\Constants::PERMISSION_ALL) { + return new \OC_OCS_Result(null, 404, 'invalid permissions'); + } + + // Shares always require read permissions + $permissions |= \OCP\Constants::PERMISSION_READ; + + if ($path instanceof \OCP\Files\File) { + // Single file shares should never have delete or create permissions + $permissions &= ~\OCP\Constants::PERMISSION_DELETE; + $permissions &= ~\OCP\Constants::PERMISSION_CREATE; + } + + $shareWith = $this->request->getParam('shareWith', null); + $shareType = (int)$this->request->getParam('shareType', '-1'); + + if ($shareType === \OCP\Share::SHARE_TYPE_USER) { + // Valid user is required to share + if ($shareWith === null || !$this->userManager->userExists($shareWith)) { + return new \OC_OCS_Result(null, 404, 'please specify a valid user'); + } + $share->setSharedWith($this->userManager->get($shareWith)); + $share->setPermissions($permissions); + } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) { + // Valid group is required to share + if ($shareWith === null || !$this->groupManager->groupExists($shareWith)) { + return new \OC_OCS_Result(null, 404, 'please specify a valid group'); + } + $share->setSharedWith($this->groupManager->get($shareWith)); + $share->setPermissions($permissions); + } else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) { + //Can we even share links? + if (!$this->shareManager->shareApiAllowLinks()) { + return new \OC_OCS_Result(null, 404, 'public link sharing is disabled by the administrator'); + } + + $publicUpload = $this->request->getParam('publicUpload', null); + if ($publicUpload === 'true') { + // Check if public upload is allowed + if (!$this->shareManager->shareApiLinkAllowPublicUpload()) { + return new \OC_OCS_Result(null, 403, '"public upload disabled by the administrator'); + } + + // Public upload can only be set for folders + if ($path instanceof \OCP\Files\File) { + return new \OC_OCS_Result(null, 404, '"public upload is only possible for public shared folders'); + } + + $share->setPermissions( + \OCP\Constants::PERMISSION_READ | + \OCP\Constants::PERMISSION_CREATE | + \OCP\Constants::PERMISSION_UPDATE + ); + } else { + $share->setPermissions(\OCP\Constants::PERMISSION_READ); + } + + // Set password + $share->setPassword($this->request->getParam('password', null)); + + //Expire date + $expireDate = $this->request->getParam('expireDate', null); + + if ($expireDate !== null) { + try { + $expireDate = $this->parseDate($expireDate); + $share->setExpirationDate($expireDate); + } catch (\Exception $e) { + return new \OC_OCS_Result(null, 404, 'Invalid Date. Format must be YYYY-MM-DD.'); + } + } + + } else if ($shareType === \OCP\Share::SHARE_TYPE_REMOTE) { + //fixme Remote shares are handled by old code path for now + return \OCA\Files_Sharing\API\Local::createShare([]); + } else { + return new \OC_OCS_Result(null, 400, "unknown share type"); + } + + $share->setShareType($shareType); + $share->setSharedBy($this->currentUser); + + try { + $share = $this->shareManager->createShare($share); + } catch (\Exception $e) { + return new \OC_OCS_Result(null, 404, $e->getMessage()); + } + + $share = $this->formatShare($share); + return new \OC_OCS_Result($share); + } + + /** * @param IShare $share * @return bool */ @@ -216,4 +336,30 @@ class Share20OCS { return false; } + + /** + * Make sure that the passed date is valid ISO 8601 + * So YYYY-MM-DD + * If not throw an exception + * + * @param string $expireDate + * + * @throws \Exception + * @return \DateTime + */ + private function parseDate($expireDate) { + try { + $date = new \DateTime($expireDate); + } catch (\Exception $e) { + throw new \Exception('Invalid date. Format must be YYYY-MM-DD'); + } + + if ($date === false) { + throw new \Exception('Invalid date. Format must be YYYY-MM-DD'); + } + + $date->setTime(0,0,0); + + return $date; + } } |