From 3861c9bce185e0f38b4941afd752c9da73985570 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Fri, 6 Sep 2013 16:00:01 +0200 Subject: [PATCH] some more OCS calls for sharing --- apps/files_sharing/appinfo/routes.php | 9 +- apps/files_sharing/lib/api.php | 131 +++++++++++++++++++++----- 2 files changed, 118 insertions(+), 22 deletions(-) diff --git a/apps/files_sharing/appinfo/routes.php b/apps/files_sharing/appinfo/routes.php index 1c7f5b4a1f..3f80614cc0 100644 --- a/apps/files_sharing/appinfo/routes.php +++ b/apps/files_sharing/appinfo/routes.php @@ -5,7 +5,6 @@ function() { }); //TODO: SET: unshare -//TODO: SET: permissions //TODO: SET: expire date //TODO: SET: mail notification //TODO: SET: can upload @@ -34,6 +33,14 @@ OC_API::register('post', array('path' => ''), array('path' => '.+')); +OC_API::register('post', + '/apps/files_sharing/api/expire/{path}', + array('\OCA\Files\Share\Api', 'setExpire'), + 'files_sharing', + OC_API::USER_AUTH, + array('path' => ''), + array('path' => '.+')); + /* OC_API::register('get', '/apps/files_sharing/api/permission/{path}', diff --git a/apps/files_sharing/lib/api.php b/apps/files_sharing/lib/api.php index 7f7f925eb2..90d8a93d3a 100644 --- a/apps/files_sharing/lib/api.php +++ b/apps/files_sharing/lib/api.php @@ -25,7 +25,7 @@ namespace OCA\Files\Share; class Api { /** - * @brief get share information for a given file/folder + * @brief get share information for a given file/folder path is encoded in URL * * @param array $params which contains a 'path' to a file/folder * @return \OC_OCS_Result share information @@ -48,45 +48,53 @@ class Api { } /** - * @brief share file with a user/group + * @brief share file with a user/group, path to file is encoded in URL * - * @param array $params which contains a 'path' to a file/folder + * @param array $params with following parameters 'shareWith', 'shareType' * @return \OC_OCS_Result result of share operation */ public static function setShare($params) { $path = $params['path']; - $errorMessage = ''; $itemSource = self::getFileId($path); $itemType = self::getItemType($path); + if($itemSource === null) { + return new \OC_OCS_Result(null, 404, "wrong path, file/folder doesn't exist."); + } + $shareWith = isset($_POST['shareWith']) ? $_POST['shareWith'] : null; $shareType = isset($_POST['shareType']) ? (int)$_POST['shareType'] : null; - if($shareType === \OCP\Share::SHARE_TYPE_LINK) { - $permissions = 1; - $shareWith = null; - } else { - $permissions = 31; + switch($shareType) { + case \OCP\Share::SHARE_TYPE_USER: + $permission = 31; + if (!\OCP\User::userExists($shareWith)) { + return new \OC_OCS_Result(null, 404, "user doesn't exist"); + } + break; + case \OCP\Share::SHARE_TYPE_GROUP: + $permission = 31; + if (!\OC_Group::groupExists($shareWith)) { + return new \OC_OCS_Result(null, 404, "group doesn't exist"); + } + break; + case \OCP\Share::SHARE_TYPE_LINK: + $permission = 1; + $shareWith = null; + break; + default: + return new \OC_OCS_Result(null, 404, "unknown share type"); } - $token = null; - if (($shareWith !== null || $shareType === \OCP\Share::SHARE_TYPE_LINK) - && $shareType !== false - && $itemType !== false) { - $token = \OCP\Share::shareItem( + $token = \OCP\Share::shareItem( $itemType, $itemSource, $shareType, $shareWith, - $permissions + $permission ); - } else { - $errorMessage = "You need to specify at least 'shareType' and provide a correct file/folder path." - . " For non public shares you also need specify 'shareWith'."; - } - if ($token) { $data = null; @@ -98,9 +106,90 @@ class Api { } return new \OC_OCS_Result($data); } else { - return new \OC_OCS_Result(null, 404, $errorMessage); + return new \OC_OCS_Result(null, 404, "couldn't share file"); } } + /** + * @brief set permission for a share, path to file is encoded in URL + * @param array $params contain 'shareWith', 'shareType', 'permission' + * @return \OC_OCS_Result + */ + public static function setPermission($params) { + $path = $params['path']; + $itemSource = self::getFileId($path); + $itemType = self::getItemType($path); + + if($itemSource === null) { + return new \OC_OCS_Result(null, 404, "wrong path, file/folder doesn't exist."); + } + + $shareWith = isset($_POST['shareWith']) ? $_POST['shareWith'] : null; + $shareType = isset($_POST['shareType']) ? (int)$_POST['shareType'] : null; + $permission = isset($_POST['permission']) ? (int)$_POST['permission'] : null; + + switch($shareType) { + case \OCP\Share::SHARE_TYPE_USER: + if (!\OCP\User::userExists($shareWith)) { + return new \OC_OCS_Result(null, 404, "user doesn't exist"); + } + break; + case \OCP\Share::SHARE_TYPE_GROUP: + if (!\OC_Group::groupExists($shareWith)) { + return new \OC_OCS_Result(null, 404, "group doesn't exist"); + } + break; + case \OCP\Share::SHARE_TYPE_LINK: + break; + default: + return new \OC_OCS_Result(null, 404, "unknown share type"); + } + + + $return = \OCP\Share::setPermissions( + $itemType, + $itemSource, + $shareType, + $shareWith, + $permission + ); + + if ($return) { + return new \OC_OCS_Result(); + } else { + return new \OC_OCS_Result(null, 404, "couldn't set permissions"); + } + } + + /** + * @brief set expire date, path to file is encoded in URL + * @param array $params contains 'expire' (format DD-MM-YYYY) + * @return \OC_OCS_Result + */ + public static function setExpire($params) { + $path = $params['path']; + $itemSource = self::getFileId($path); + $itemType = self::getItemType($path); + + if($itemSource === null) { + return new \OC_OCS_Result(null, 404, "wrong path, file/folder doesn't exist."); + } + + $expire = isset($_POST['expire']) ? (int)$_POST['expire'] : null; + + $return = false; + if ($expire) { + $return = \OCP\Share::setExpirationDate($itemType, $itemSource, $expire); + } + + if ($return) { + return new \OC_OCS_Result(); + } else { + $msg = "Failed, please check the expire date, expected format 'DD-MM-YYYY'."; + return new \OC_OCS_Result(null, 404, $msg); + } + + + } /** * @brief get file ID from a given path