From 985758305f66d80f2b0625423b0bee68fce2a2d9 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Wed, 4 Sep 2013 14:32:05 +0200 Subject: [PATCH 01/35] initial commit, start implementing the ocs share api --- apps/files_sharing/appinfo/app.php | 1 + apps/files_sharing/appinfo/routes.php | 53 ++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/apps/files_sharing/appinfo/app.php b/apps/files_sharing/appinfo/app.php index 895d446a336..ffdcbf05109 100644 --- a/apps/files_sharing/appinfo/app.php +++ b/apps/files_sharing/appinfo/app.php @@ -7,6 +7,7 @@ OC::$CLASSPATH['OC\Files\Cache\Shared_Cache'] = 'files_sharing/lib/cache.php'; OC::$CLASSPATH['OC\Files\Cache\Shared_Permissions'] = 'files_sharing/lib/permissions.php'; OC::$CLASSPATH['OC\Files\Cache\Shared_Updater'] = 'files_sharing/lib/updater.php'; OC::$CLASSPATH['OC\Files\Cache\Shared_Watcher'] = 'files_sharing/lib/watcher.php'; +OC::$CLASSPATH['OCA\Files\Share\Api'] = 'files_sharing/lib/api.php'; OCP\Util::connectHook('OC_Filesystem', 'setup', '\OC\Files\Storage\Shared', 'setup'); OCP\Share::registerBackend('file', 'OC_Share_Backend_File'); OCP\Share::registerBackend('folder', 'OC_Share_Backend_Folder', 'file'); diff --git a/apps/files_sharing/appinfo/routes.php b/apps/files_sharing/appinfo/routes.php index 02815b5eb42..15af5226e18 100644 --- a/apps/files_sharing/appinfo/routes.php +++ b/apps/files_sharing/appinfo/routes.php @@ -2,4 +2,55 @@ $this->create('core_ajax_public_preview', '/publicpreview.png')->action( function() { require_once __DIR__ . '/../ajax/publicpreview.php'; -}); \ No newline at end of file +}); + +//TODO: GET: share status of a given file/folder +//TODO: GET: share status of all files in a given folder? +//TODO: SET: share (unshare) +//TODO: SET: permissions +//TODO: SET: expire date +//TODO: SET: mail notification + +OC_API::register('get', + '/apps/files_sharing/api/share/{path}', + array('\OCA\Files\Share\Api', 'getShare'), + 'files_sharing', + OC_API::USER_AUTH, + array('path' => ''), + array('path' => '.+')); //allow slashes in parameter path +/* +OC_API::register('get', + '/apps/files_sharing/api/share/group/{path}', + array('\OCA\Files\Share\Api', 'getShare'), + 'files_sharing', + OC_API::USER_AUTH, + array('path' => '')); + +OC_API::register('get', + '/apps/files_sharing/api/share/user/{path}', + array('\OCA\Files\Share\Api', 'getShare'), + 'files_sharing', + OC_API::USER_AUTH, + array('path' => '')); + +OC_API::register('get', + '/apps/files_sharing/api/permission/{path}', + array('\OCA\Files\Share\Api', 'getShare'), + 'files_sharing', + OC_API::USER_AUTH, + array('path' => '')); + +OC_API::register('get', + '/apps/files_sharing/api/expire/{path}', + array('\OCA\Files\Share\Api', 'getShare'), + 'files_sharing', + OC_API::USER_AUTH, + array('path' => '')); + +OC_API::register('get', + '/apps/files_sharing/api/notify/{path}', + array('\OCA\Files\Share\Api', 'getShare'), + 'files_sharing', + OC_API::USER_AUTH, + array('path' => '')); +*/ From 21a0a96e4395fedb7fae8fe5f731ca283ce937b1 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Wed, 4 Sep 2013 17:25:15 +0200 Subject: [PATCH 02/35] intermediate results, share api --- apps/files_sharing/appinfo/routes.php | 14 -------- apps/files_sharing/lib/api.php | 48 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 14 deletions(-) create mode 100644 apps/files_sharing/lib/api.php diff --git a/apps/files_sharing/appinfo/routes.php b/apps/files_sharing/appinfo/routes.php index 15af5226e18..2e26033cad0 100644 --- a/apps/files_sharing/appinfo/routes.php +++ b/apps/files_sharing/appinfo/routes.php @@ -19,20 +19,6 @@ OC_API::register('get', array('path' => ''), array('path' => '.+')); //allow slashes in parameter path /* -OC_API::register('get', - '/apps/files_sharing/api/share/group/{path}', - array('\OCA\Files\Share\Api', 'getShare'), - 'files_sharing', - OC_API::USER_AUTH, - array('path' => '')); - -OC_API::register('get', - '/apps/files_sharing/api/share/user/{path}', - array('\OCA\Files\Share\Api', 'getShare'), - 'files_sharing', - OC_API::USER_AUTH, - array('path' => '')); - OC_API::register('get', '/apps/files_sharing/api/permission/{path}', array('\OCA\Files\Share\Api', 'getShare'), diff --git a/apps/files_sharing/lib/api.php b/apps/files_sharing/lib/api.php new file mode 100644 index 00000000000..cfe1fc2da46 --- /dev/null +++ b/apps/files_sharing/lib/api.php @@ -0,0 +1,48 @@ +. + * + */ + +namespace OCA\Files\Share; + +class Api { + + /** + * @brief get share information for a given file/folder + * + * @param array $params which contains a 'path' to a file/folder + * @return \OC_OCS_Result share information + */ + public static function getShare($params) { + $path = $params['path']; + + $view = new \OC\Files\View('/'.\OCP\User::getUser().'/files'); + $fileInfo = $view->getFileInfo($path); + if ($fileInfo) { + $share = \OCP\Share::getItemShared('file', $fileInfo['fileid']); + } else { + \OCP\Util::writeLog('files_sharing', 'OCS API getShare, file ' . $path . ' does not exists', \OCP\Util::WARN); + $share = array(); + } + + return new \OC_OCS_Result($share); + } + +} \ No newline at end of file From b6ee727399c3d0eced5b2ee2bce9f17a813a1bb2 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Fri, 6 Sep 2013 10:49:21 +0200 Subject: [PATCH 03/35] intermediate result ocs api --- apps/files_sharing/appinfo/routes.php | 22 +++++- apps/files_sharing/lib/api.php | 106 ++++++++++++++++++++++++-- 2 files changed, 118 insertions(+), 10 deletions(-) diff --git a/apps/files_sharing/appinfo/routes.php b/apps/files_sharing/appinfo/routes.php index 2e26033cad0..1c7f5b4a1fc 100644 --- a/apps/files_sharing/appinfo/routes.php +++ b/apps/files_sharing/appinfo/routes.php @@ -4,12 +4,11 @@ function() { require_once __DIR__ . '/../ajax/publicpreview.php'; }); -//TODO: GET: share status of a given file/folder -//TODO: GET: share status of all files in a given folder? -//TODO: SET: share (unshare) +//TODO: SET: unshare //TODO: SET: permissions //TODO: SET: expire date //TODO: SET: mail notification +//TODO: SET: can upload OC_API::register('get', '/apps/files_sharing/api/share/{path}', @@ -18,6 +17,23 @@ OC_API::register('get', OC_API::USER_AUTH, array('path' => ''), array('path' => '.+')); //allow slashes in parameter path + +OC_API::register('post', + '/apps/files_sharing/api/share/{path}', + array('\OCA\Files\Share\Api', 'setShare'), + 'files_sharing', + OC_API::USER_AUTH, + array('path' => ''), + array('path' => '.+')); + +OC_API::register('post', + '/apps/files_sharing/api/permission/{path}', + array('\OCA\Files\Share\Api', 'setPermission'), + '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 cfe1fc2da46..7f7f925eb23 100644 --- a/apps/files_sharing/lib/api.php +++ b/apps/files_sharing/lib/api.php @@ -33,16 +33,108 @@ class Api { public static function getShare($params) { $path = $params['path']; - $view = new \OC\Files\View('/'.\OCP\User::getUser().'/files'); - $fileInfo = $view->getFileInfo($path); - if ($fileInfo) { - $share = \OCP\Share::getItemShared('file', $fileInfo['fileid']); + $fileId = self::getFileId($path); + if ($fileId !== null) { + $share = \OCP\Share::getItemShared('file', $fileId); } else { - \OCP\Util::writeLog('files_sharing', 'OCS API getShare, file ' . $path . ' does not exists', \OCP\Util::WARN); - $share = array(); + $share = null; } - return new \OC_OCS_Result($share); + if ($share !== null) { + return new \OC_OCS_Result($share); + } else { + return new \OC_OCS_Result(null, 404, 'file/folder doesn\'t exists'); + } + } + + /** + * @brief share file with a user/group + * + * @param array $params which contains a 'path' to a file/folder + * @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); + + $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; + } + + + $token = null; + if (($shareWith !== null || $shareType === \OCP\Share::SHARE_TYPE_LINK) + && $shareType !== false + && $itemType !== false) { + $token = \OCP\Share::shareItem( + $itemType, + $itemSource, + $shareType, + $shareWith, + $permissions + ); + } 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; + if(is_string($token)) { //public link share + $url = \OCP\Util::linkToPublic('files&t='.$token); + $data = array('url' => $url, // '&' gets encoded to $amp; + 'token' => $token); + + } + return new \OC_OCS_Result($data); + } else { + return new \OC_OCS_Result(null, 404, $errorMessage); + } + } + + /** + * @brief get file ID from a given path + * @param string $path + * @return string fileID or null + */ + private static function getFileId($path) { + $view = new \OC\Files\View('/'.\OCP\User::getUser().'/files'); + $fileId = null; + + $fileInfo = $view->getFileInfo($path); + if ($fileInfo) { + $fileId = $fileInfo['fileid']; + } + + return $fileId; + } + + /** + * @brief get itemType + * @param string $path + * @return string type 'file', 'folder' or null of file/folder doesn't exists + */ + private static function getItemType($path) { + $view = new \OC\Files\View('/'.\OCP\User::getUser().'/files'); + $itemType = null; + + if ($view->is_dir($path)) { + $itemType = "folder"; + } elseif ($view->is_file($path)) { + $itemType = "file"; + } + + return $itemType; } } \ No newline at end of file From 3861c9bce185e0f38b4941afd752c9da73985570 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Fri, 6 Sep 2013 16:00:01 +0200 Subject: [PATCH 04/35] 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 1c7f5b4a1fc..3f80614cc0c 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 7f7f925eb23..90d8a93d3a4 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 From 14437ffd159db79eaccee4fc88d91084e10ac3c6 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Mon, 16 Sep 2013 17:04:49 +0200 Subject: [PATCH 05/35] ocs api for file sharing --- apps/files_sharing/appinfo/routes.php | 32 ++------ apps/files_sharing/lib/api.php | 113 +++++++++++++++++--------- 2 files changed, 84 insertions(+), 61 deletions(-) diff --git a/apps/files_sharing/appinfo/routes.php b/apps/files_sharing/appinfo/routes.php index 3f80614cc0c..cf0a69dc7ec 100644 --- a/apps/files_sharing/appinfo/routes.php +++ b/apps/files_sharing/appinfo/routes.php @@ -4,10 +4,9 @@ function() { require_once __DIR__ . '/../ajax/publicpreview.php'; }); -//TODO: SET: unshare -//TODO: SET: expire date -//TODO: SET: mail notification -//TODO: SET: can upload +// OCS API + +//TODO: SET: mail notification, waiting for PR #4689 to be accepted OC_API::register('get', '/apps/files_sharing/api/share/{path}', @@ -41,25 +40,10 @@ OC_API::register('post', array('path' => ''), array('path' => '.+')); -/* -OC_API::register('get', - '/apps/files_sharing/api/permission/{path}', - array('\OCA\Files\Share\Api', 'getShare'), +OC_API::register('post', + '/apps/files_sharing/api/unshare/{path}', + array('\OCA\Files\Share\Api', 'setUnshare'), 'files_sharing', OC_API::USER_AUTH, - array('path' => '')); - -OC_API::register('get', - '/apps/files_sharing/api/expire/{path}', - array('\OCA\Files\Share\Api', 'getShare'), - 'files_sharing', - OC_API::USER_AUTH, - array('path' => '')); - -OC_API::register('get', - '/apps/files_sharing/api/notify/{path}', - array('\OCA\Files\Share\Api', 'getShare'), - 'files_sharing', - OC_API::USER_AUTH, - array('path' => '')); -*/ + array('path' => ''), + array('path' => '.+')); diff --git a/apps/files_sharing/lib/api.php b/apps/files_sharing/lib/api.php index 90d8a93d3a4..6f05d46cbdf 100644 --- a/apps/files_sharing/lib/api.php +++ b/apps/files_sharing/lib/api.php @@ -50,7 +50,8 @@ class Api { /** * @brief share file with a user/group, path to file is encoded in URL * - * @param array $params with following parameters 'shareWith', 'shareType' + * @param array $params with following parameters 'shareWith', 'shareType', 'path' + * optional 'publicUpload' and 'password' for public shares * @return \OC_OCS_Result result of share operation */ public static function setShare($params) { @@ -69,32 +70,29 @@ class Api { 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; + //allow password protection + $shareWith = isset($_POST['password']) ? $_POST['password'] : null; + $publicUpload = isset($_POST['publicUpload']) ? $_POST['publicUpload'] : 'no'; + $permission = self::getPublicLinkSharePermissions($publicUpload); break; - default: - return new \OC_OCS_Result(null, 404, "unknown share type"); } - - $token = \OCP\Share::shareItem( + try { + $token = \OCP\Share::shareItem( $itemType, $itemSource, $shareType, $shareWith, $permission ); + } catch (\Exception $e) { + return new \OC_OCS_Result(null, 404, $e->getMessage()); + } if ($token) { $data = null; @@ -127,32 +125,18 @@ class Api { $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"); + try { + $return = \OCP\Share::setPermissions( + $itemType, + $itemSource, + $shareType, + $shareWith, + $permission + ); + } catch (\Exception $e) { + return new \OC_OCS_Result(null, 404, $e->getMessage()); } - - $return = \OCP\Share::setPermissions( - $itemType, - $itemSource, - $shareType, - $shareWith, - $permission - ); - if ($return) { return new \OC_OCS_Result(); } else { @@ -187,8 +171,63 @@ class Api { $msg = "Failed, please check the expire date, expected format 'DD-MM-YYYY'."; return new \OC_OCS_Result(null, 404, $msg); } + } + /** + * @brief unshare a file/folder + * @param array $params with following parameters 'shareWith', 'shareType', 'path' + * @return \OC_OCS_Result + */ + public static function setUnshare($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; + + if( $shareType == \OCP\Share::SHARE_TYPE_LINK) { + $shareWith = null; + } + + try { + $return = \OCP\Share::unshare( + $itemType, + $itemSource, + $shareType, + $shareWith); + } catch (\Exception $e) { + return new \OC_OCS_Result(null, 404, $e->getMessage()); + } + + if ($return) { + return new \OC_OCS_Result(); + } else { + $msg = "Unshare Failed"; + return new \OC_OCS_Result(null, 404, $msg); + } + } + + /** + * @brief get public link share permissions to allow/forbid public uploads + * @param string $publicUpload 'yes' or 'no' + * @return int permissions read (1) or create,update,read (7) + */ + private static function getPublicLinkSharePermissions($publicUpload) { + + $publicUploadEnabled = \OC_Appconfig::getValue('core', 'shareapi_allow_public_upload', 'yes'); + + if(\OC_App::isEnabled('files_encryption') || + $publicUploadEnabled !== 'yes' || + $publicUpload === 'no') { + return 1; // read + } else { + return 7; // create, update, read + } } /** From 7aed24fa6c3df13d553f5b83b7de57e89f119d15 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Mon, 16 Sep 2013 17:28:17 +0200 Subject: [PATCH 06/35] allow to set a different permission during initial share operation --- apps/files_sharing/lib/api.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/files_sharing/lib/api.php b/apps/files_sharing/lib/api.php index 6f05d46cbdf..ba186094311 100644 --- a/apps/files_sharing/lib/api.php +++ b/apps/files_sharing/lib/api.php @@ -69,10 +69,10 @@ class Api { switch($shareType) { case \OCP\Share::SHARE_TYPE_USER: - $permission = 31; + $permission = isset($_POST['permission']) ? (int)$_POST['permission'] : 31; break; case \OCP\Share::SHARE_TYPE_GROUP: - $permission = 31; + $permission = isset($_POST['permission']) ? (int)$_POST['permission'] : 31; break; case \OCP\Share::SHARE_TYPE_LINK: //allow password protection @@ -265,4 +265,4 @@ class Api { return $itemType; } -} \ No newline at end of file +} From ef3307f0996f1025a75a697a549166b26576e670 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Mon, 16 Sep 2013 17:42:56 +0200 Subject: [PATCH 07/35] return error if public upload is disabled --- apps/files_sharing/lib/api.php | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/apps/files_sharing/lib/api.php b/apps/files_sharing/lib/api.php index ba186094311..f641623ac10 100644 --- a/apps/files_sharing/lib/api.php +++ b/apps/files_sharing/lib/api.php @@ -77,8 +77,17 @@ class Api { case \OCP\Share::SHARE_TYPE_LINK: //allow password protection $shareWith = isset($_POST['password']) ? $_POST['password'] : null; + //check public link share + $publicUploadEnabled = \OC_Appconfig::getValue('core', 'shareapi_allow_public_upload', 'yes'); + $encryptionEnabled = \OC_App::isEnabled('files_encryption'); + if(isset($_POST['publicUpload']) && + ($encryptionEnabled || $publicUploadEnabled !== 'yes')) { + return new \OC_OCS_Result(null, 404, "public upload disabled by the administrator"); + } $publicUpload = isset($_POST['publicUpload']) ? $_POST['publicUpload'] : 'no'; - $permission = self::getPublicLinkSharePermissions($publicUpload); + // read, create, update (7) if public upload is enabled or + // read (1) if public upload is disabled + $permission = $publicUpload === 'yes' ? 7 : 1; break; } @@ -212,24 +221,6 @@ class Api { } } - /** - * @brief get public link share permissions to allow/forbid public uploads - * @param string $publicUpload 'yes' or 'no' - * @return int permissions read (1) or create,update,read (7) - */ - private static function getPublicLinkSharePermissions($publicUpload) { - - $publicUploadEnabled = \OC_Appconfig::getValue('core', 'shareapi_allow_public_upload', 'yes'); - - if(\OC_App::isEnabled('files_encryption') || - $publicUploadEnabled !== 'yes' || - $publicUpload === 'no') { - return 1; // read - } else { - return 7; // create, update, read - } - } - /** * @brief get file ID from a given path * @param string $path From 86dbb13823087f316e9962ca9453303b73b55bde Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Tue, 17 Sep 2013 11:53:06 +0200 Subject: [PATCH 08/35] more rest-style API --- apps/files_sharing/appinfo/routes.php | 40 +++---- apps/files_sharing/lib/api.php | 147 ++++++++++++++++++++------ 2 files changed, 134 insertions(+), 53 deletions(-) diff --git a/apps/files_sharing/appinfo/routes.php b/apps/files_sharing/appinfo/routes.php index cf0a69dc7ec..381a1091e0a 100644 --- a/apps/files_sharing/appinfo/routes.php +++ b/apps/files_sharing/appinfo/routes.php @@ -9,40 +9,34 @@ function() { //TODO: SET: mail notification, waiting for PR #4689 to be accepted OC_API::register('get', - '/apps/files_sharing/api/share/{path}', + '/apps/files_sharing/api/shares', + array('\OCA\Files\Share\Api', 'getAllShare'), + 'files_sharing'); + +OC_API::register('post', + '/apps/files_sharing/api/shares', + array('\OCA\Files\Share\Api', 'createShare'), + 'files_sharing'); + +OC_API::register('get', + '/apps/files_sharing/api/shares/{path}', array('\OCA\Files\Share\Api', 'getShare'), 'files_sharing', OC_API::USER_AUTH, array('path' => ''), array('path' => '.+')); //allow slashes in parameter path -OC_API::register('post', - '/apps/files_sharing/api/share/{path}', - array('\OCA\Files\Share\Api', 'setShare'), +OC_API::register('put', + '/apps/files_sharing/api/shares/{path}', + array('\OCA\Files\Share\Api', 'updateShare'), 'files_sharing', OC_API::USER_AUTH, array('path' => ''), array('path' => '.+')); -OC_API::register('post', - '/apps/files_sharing/api/permission/{path}', - array('\OCA\Files\Share\Api', 'setPermission'), - 'files_sharing', - OC_API::USER_AUTH, - 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('post', - '/apps/files_sharing/api/unshare/{path}', - array('\OCA\Files\Share\Api', 'setUnshare'), +OC_API::register('delete', + '/apps/files_sharing/api/shares/{path}', + array('\OCA\Files\Share\Api', 'deleteShare'), 'files_sharing', OC_API::USER_AUTH, array('path' => ''), diff --git a/apps/files_sharing/lib/api.php b/apps/files_sharing/lib/api.php index f641623ac10..1cfe9a67a25 100644 --- a/apps/files_sharing/lib/api.php +++ b/apps/files_sharing/lib/api.php @@ -24,6 +24,23 @@ namespace OCA\Files\Share; class Api { + /** + * @brief get all shares + * + * @param array $params + * @return \OC_OCS_Result share information + */ + public static function getAllShare($params) { + + $share = \OCP\Share::getItemShared('file', null); + + if ($share !== null) { + return new \OC_OCS_Result($share); + } else { + return new \OC_OCS_Result(null, 404, 'no shares available'); + } + } + /** * @brief get share information for a given file/folder path is encoded in URL * @@ -48,14 +65,17 @@ class Api { } /** - * @brief share file with a user/group, path to file is encoded in URL - * - * @param array $params with following parameters 'shareWith', 'shareType', 'path' - * optional 'publicUpload' and 'password' for public shares - * @return \OC_OCS_Result result of share operation + * @breif create a new share + * @param array $params 'path', 'shareWith', 'shareType' + * @return \OC_OCS_Result */ - public static function setShare($params) { - $path = $params['path']; + public static function createShare($params) { + + $path = isset($_POST['path']) ? $_POST['path'] : null; + + if($path === null) { + return new \OC_OCS_Result(null, 404, "please specify a file or folder path"); + } $itemSource = self::getFileId($path); $itemType = self::getItemType($path); @@ -69,10 +89,10 @@ class Api { switch($shareType) { case \OCP\Share::SHARE_TYPE_USER: - $permission = isset($_POST['permission']) ? (int)$_POST['permission'] : 31; + $permissions = isset($_POST['permissions']) ? (int)$_POST['permissions'] : 31; break; case \OCP\Share::SHARE_TYPE_GROUP: - $permission = isset($_POST['permission']) ? (int)$_POST['permission'] : 31; + $permissions = isset($_POST['permissions']) ? (int)$_POST['permissions'] : 31; break; case \OCP\Share::SHARE_TYPE_LINK: //allow password protection @@ -87,7 +107,7 @@ class Api { $publicUpload = isset($_POST['publicUpload']) ? $_POST['publicUpload'] : 'no'; // read, create, update (7) if public upload is enabled or // read (1) if public upload is disabled - $permission = $publicUpload === 'yes' ? 7 : 1; + $permissions = $publicUpload === 'yes' ? 7 : 1; break; } @@ -97,7 +117,7 @@ class Api { $itemSource, $shareType, $shareWith, - $permission + $permissions ); } catch (\Exception $e) { return new \OC_OCS_Result(null, 404, $e->getMessage()); @@ -116,13 +136,17 @@ class Api { 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' + * update shares, e.g. expire date, permissions, etc + * @param array $params 'path', 'shareWith', 'shareType' and + * 'permissions' or 'expire' or 'password' * @return \OC_OCS_Result */ - public static function setPermission($params) { + public static function updateShare($params) { + $path = $params['path']; + $itemSource = self::getFileId($path); $itemType = self::getItemType($path); @@ -130,9 +154,34 @@ class Api { 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; + try { + if(isset($params['_put']['permissions'])) { + return self::updatePermissions($itemSource, $itemType, $params); + } elseif (isset($params['_put']['expire'])) { + return self::updateExpire($itemSource, $itemType, $params); + } elseif (isset($params['_put']['password'])) { + return self::updatePassword($itemSource, $itemType, $params); + } + } catch (\Exception $e) { + return new \OC_OCS_Result(null, 404, $e->getMessage()); + } + + return new \OC_OCS_Result(null, 404, "Couldn't find a parameter to update"); + + } + + /** + * @brief update permissions for a share + * @param int $itemSource file ID + * @param string $itemType 'file' or 'folder' + * @param array $params contain 'shareWith', 'shareType', 'permissions' + * @return \OC_OCS_Result + */ + private static function updatePermissions($itemSource, $itemType, $params) { + + $shareWith = isset($params['_put']['shareWith']) ? $params['_put']['shareWith'] : null; + $shareType = isset($params['_put']['shareType']) ? (int)$params['_put']['shareType'] : null; + $permissions = isset($params['_put']['permissions']) ? (int)$params['_put']['permissions'] : null; try { $return = \OCP\Share::setPermissions( @@ -140,7 +189,7 @@ class Api { $itemSource, $shareType, $shareWith, - $permission + $permissions ); } catch (\Exception $e) { return new \OC_OCS_Result(null, 404, $e->getMessage()); @@ -153,21 +202,59 @@ class Api { } } + /** + * @brief update password for public link share + * @param int $itemSource file ID + * @param string $itemType 'file' or 'folder' + * @param type $params 'password' + * @return \OC_OCS_Result + */ + private static function updatePassword($itemSource, $itemType, $params) { + error_log("update password"); + $shareWith = isset($params['_put']['password']) ? $params['_put']['password'] : null; + + if($shareWith === '') { + $shareWith = null; + } + + $items = \OCP\Share::getItemShared($itemType, $itemSource); + + $checkExists = false; + foreach ($items as $item) { + if($item['share_type'] === \OCP\Share::SHARE_TYPE_LINK) { + $checkExists = true; + $permissions = $item['permissions']; + } + } + + if (!$checkExists) { + return new \OC_OCS_Result(null, 404, "share doesn't exists, can't change password"); + } + + $result = \OCP\Share::shareItem( + $itemType, + $itemSource, + \OCP\Share::SHARE_TYPE_LINK, + $shareWith, + $permissions + ); + if($result) { + return new \OC_OCS_Result(); + } + + return new \OC_OCS_Result(null, 404, "couldn't set password"); + } + /** * @brief set expire date, path to file is encoded in URL + * @param int $itemSource file ID + * @param string $itemType 'file' or 'folder' * @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); + private static function updateExpire($itemSource, $itemType, $params) { - 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; + $expire = isset($params['_put']['expire']) ? (int)$params['_put']['expire'] : null; $return = false; if ($expire) { @@ -187,7 +274,7 @@ class Api { * @param array $params with following parameters 'shareWith', 'shareType', 'path' * @return \OC_OCS_Result */ - public static function setUnshare($params) { + public static function deleteShare($params) { $path = $params['path']; $itemSource = self::getFileId($path); $itemType = self::getItemType($path); @@ -196,8 +283,8 @@ class Api { 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; + $shareWith = isset($params['_delete']['shareWith']) ? $params['_delete']['shareWith'] : null; + $shareType = isset($params['_delete']['shareType']) ? (int)$params['_delete']['shareType'] : null; if( $shareType == \OCP\Share::SHARE_TYPE_LINK) { $shareWith = null; From e52639e4e64d31c6aec3bb4b865bee8a84db08f2 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Tue, 17 Sep 2013 15:27:10 +0200 Subject: [PATCH 09/35] use share ID as REST resource --- apps/files_sharing/appinfo/routes.php | 25 +-- apps/files_sharing/lib/api.php | 229 ++++++++++++++++++-------- 2 files changed, 167 insertions(+), 87 deletions(-) diff --git a/apps/files_sharing/appinfo/routes.php b/apps/files_sharing/appinfo/routes.php index 381a1091e0a..a373bff4dad 100644 --- a/apps/files_sharing/appinfo/routes.php +++ b/apps/files_sharing/appinfo/routes.php @@ -9,35 +9,26 @@ function() { //TODO: SET: mail notification, waiting for PR #4689 to be accepted OC_API::register('get', - '/apps/files_sharing/api/shares', + '/apps/files_sharing/api/v1/shares', array('\OCA\Files\Share\Api', 'getAllShare'), 'files_sharing'); OC_API::register('post', - '/apps/files_sharing/api/shares', + '/apps/files_sharing/api/v1/shares', array('\OCA\Files\Share\Api', 'createShare'), 'files_sharing'); OC_API::register('get', - '/apps/files_sharing/api/shares/{path}', + '/apps/files_sharing/api/v1/shares/{id}', array('\OCA\Files\Share\Api', 'getShare'), - 'files_sharing', - OC_API::USER_AUTH, - array('path' => ''), - array('path' => '.+')); //allow slashes in parameter path + 'files_sharing'); OC_API::register('put', - '/apps/files_sharing/api/shares/{path}', + '/apps/files_sharing/api/v1/shares/{id}', array('\OCA\Files\Share\Api', 'updateShare'), - 'files_sharing', - OC_API::USER_AUTH, - array('path' => ''), - array('path' => '.+')); + 'files_sharing'); OC_API::register('delete', - '/apps/files_sharing/api/shares/{path}', + '/apps/files_sharing/api/v1/shares/{id}', array('\OCA\Files\Share\Api', 'deleteShare'), - 'files_sharing', - OC_API::USER_AUTH, - array('path' => ''), - array('path' => '.+')); + 'files_sharing'); diff --git a/apps/files_sharing/lib/api.php b/apps/files_sharing/lib/api.php index 1cfe9a67a25..87841150de7 100644 --- a/apps/files_sharing/lib/api.php +++ b/apps/files_sharing/lib/api.php @@ -27,11 +27,17 @@ class Api { /** * @brief get all shares * - * @param array $params + * @param array $params option 'file' to limit the result to a specific file/folder * @return \OC_OCS_Result share information */ public static function getAllShare($params) { + // if a file is specified, get the share for this file + if (isset($_GET['file'])) { + $params['itemSource'] = self::getFileId($_GET['file']); + return self::getShare($params); + } + $share = \OCP\Share::getItemShared('file', null); if ($share !== null) { @@ -42,25 +48,43 @@ class Api { } /** - * @brief get share information for a given file/folder path is encoded in URL + * @brief get share information for a given share * - * @param array $params which contains a 'path' to a file/folder + * @param array $params which contains a 'id' * @return \OC_OCS_Result share information */ public static function getShare($params) { - $path = $params['path']; - $fileId = self::getFileId($path); - if ($fileId !== null) { - $share = \OCP\Share::getItemShared('file', $fileId); + // either the $params already contains a itemSource if we come from + // getAllShare() or we need to translate the shareID to a itemSource + if(isset($params['itemSource'])) { + $itemSource = $params['itemSource']; + $getAll = true; } else { - $share = null; + $s = self::getShareFromId($params['id']); + $itemSource = $s['item_source']; + $getAll = false; } - if ($share !== null) { - return new \OC_OCS_Result($share); + if ($itemSource !== null) { + $shares = \OCP\Share::getItemShared('file', $itemSource); + // if a specific share was specified only return this one + if ($getAll === false) { + foreach ($shares as $share) { + if ($share['id'] === (int)$params['id']) { + $shares = array('element' => $share); + break; + } + } + } } else { - return new \OC_OCS_Result(null, 404, 'file/folder doesn\'t exists'); + $shares = null; + } + + if ($shares === null || empty($shares)) { + return new \OC_OCS_Result(null, 404, 'share doesn\'t exists'); + } else { + return new \OC_OCS_Result($shares); } } @@ -74,7 +98,7 @@ class Api { $path = isset($_POST['path']) ? $_POST['path'] : null; if($path === null) { - return new \OC_OCS_Result(null, 404, "please specify a file or folder path"); + return new \OC_OCS_Result(null, 400, "please specify a file or folder path"); } $itemSource = self::getFileId($path); @@ -125,11 +149,27 @@ class Api { if ($token) { $data = null; + $shares = \OCP\Share::getItemShared('file', $itemSource); if(is_string($token)) { //public link share + foreach ($shares as $share) { + if ($share['token'] === $token) { + $shareId = $share['id']; + break; + } + } $url = \OCP\Util::linkToPublic('files&t='.$token); $data = array('url' => $url, // '&' gets encoded to $amp; - 'token' => $token); + 'token' => $token, + 'id' => $shareId); + } else { + foreach ($shares as $share) { + if ($share['share_with'] === $shareWith && $share['share_type'] === $shareType) { + $shareId = $share['id']; + $data = array('id' => $shareId); + break; + } + } } return new \OC_OCS_Result($data); } else { @@ -138,51 +178,65 @@ class Api { } /** - * update shares, e.g. expire date, permissions, etc - * @param array $params 'path', 'shareWith', 'shareType' and - * 'permissions' or 'expire' or 'password' + * update shares, e.g. password, permissions, etc + * @param array $params shareId 'id' and the parameter we want to update + * currently supported: permissions, password, publicUpload * @return \OC_OCS_Result */ public static function updateShare($params) { - $path = $params['path']; - - $itemSource = self::getFileId($path); - $itemType = self::getItemType($path); + $share = self::getShareFromId($params['id']); + $itemSource = isset($share['item_source']) ? $share['item_source'] : null; if($itemSource === null) { - return new \OC_OCS_Result(null, 404, "wrong path, file/folder doesn't exist."); + return new \OC_OCS_Result(null, 404, "wrong share Id, share doesn't exist."); } try { if(isset($params['_put']['permissions'])) { - return self::updatePermissions($itemSource, $itemType, $params); - } elseif (isset($params['_put']['expire'])) { - return self::updateExpire($itemSource, $itemType, $params); + return self::updatePermissions($share, $params); } elseif (isset($params['_put']['password'])) { - return self::updatePassword($itemSource, $itemType, $params); + return self::updatePassword($share, $params); + } elseif (isset($params['_put']['publicUpload'])) { + return self::updatePublicUpload($share, $params); } } catch (\Exception $e) { - return new \OC_OCS_Result(null, 404, $e->getMessage()); + return new \OC_OCS_Result(null, 400, $e->getMessage()); } - return new \OC_OCS_Result(null, 404, "Couldn't find a parameter to update"); + return new \OC_OCS_Result(null, 400, "Wrong or no update parameter given"); } /** * @brief update permissions for a share - * @param int $itemSource file ID - * @param string $itemType 'file' or 'folder' - * @param array $params contain 'shareWith', 'shareType', 'permissions' + * @param array $share information about the share + * @param array $params contains 'permissions' * @return \OC_OCS_Result */ - private static function updatePermissions($itemSource, $itemType, $params) { + private static function updatePermissions($share, $params) { - $shareWith = isset($params['_put']['shareWith']) ? $params['_put']['shareWith'] : null; - $shareType = isset($params['_put']['shareType']) ? (int)$params['_put']['shareType'] : null; + $itemSource = $share['item_source']; + $itemType = $share['item_type']; + $shareWith = $share['share_with']; + $shareType = $share['share_type']; $permissions = isset($params['_put']['permissions']) ? (int)$params['_put']['permissions'] : null; + $publicUploadStatus = \OC_Appconfig::getValue('core', 'shareapi_allow_public_upload', 'yes'); + $encryptionEnabled = \OC_App::isEnabled('files_encryption'); + $publicUploadEnabled = false; + if(!$encryptionEnabled && $publicUploadStatus === 'yes') { + $publicUploadEnabled = true; + } + + // only change permissions for public shares if public upload is enabled + // and we want to set permissions to 1 (read only) or 7 (allow upload) + if ( (int)$shareType === \OCP\Share::SHARE_TYPE_LINK ) { + if ($publicUploadEnabled === false || ($permissions !== 7 && $permissions !== 1)) { + return new \OC_OCS_Result(null, 400, "can't change permission for public link share"); + } + } + try { $return = \OCP\Share::setPermissions( $itemType, @@ -202,15 +256,48 @@ class Api { } } + /** + * @brief enable/disable public upload + * @param array $share information about the share + * @param array $params contains 'publicUpload' which can be 'yes' or 'no' + * @return \OC_OCS_Result + */ + private static function updatePublicUpload($share, $params) { + + $publicUploadEnabled = \OC_Appconfig::getValue('core', 'shareapi_allow_public_upload', 'yes'); + $encryptionEnabled = \OC_App::isEnabled('files_encryption'); + if($encryptionEnabled || $publicUploadEnabled !== 'yes') { + return new \OC_OCS_Result(null, 404, "public upload disabled by the administrator"); + } + + if ($share['item_type'] !== 'folder' || + (int)$share['share_type'] !== \OCP\Share::SHARE_TYPE_LINK ) { + return new \OC_OCS_Result(null, 404, "public upload is only possible for public shared folders"); + } + + // read, create, update (7) if public upload is enabled or + // read (1) if public upload is disabled + $params['_put']['permissions'] = $params['_put']['publicUpload'] === 'yes' ? 7 : 1; + + return self::updatePermissions($share, $params); + + } + /** * @brief update password for public link share - * @param int $itemSource file ID - * @param string $itemType 'file' or 'folder' + * @param array $share information about the share * @param type $params 'password' * @return \OC_OCS_Result */ - private static function updatePassword($itemSource, $itemType, $params) { - error_log("update password"); + private static function updatePassword($share, $params) { + + $itemSource = $share['item_source']; + $itemType = $share['item_type']; + + if( (int)$share['share_type'] !== \OCP\Share::SHARE_TYPE_LINK) { + return new \OC_OCS_Result(null, 400, "password protection is only supported for public shares"); + } + $shareWith = isset($params['_put']['password']) ? $params['_put']['password'] : null; if($shareWith === '') { @@ -230,7 +317,7 @@ class Api { if (!$checkExists) { return new \OC_OCS_Result(null, 404, "share doesn't exists, can't change password"); } - + error_log("type: $itemType"); $result = \OCP\Share::shareItem( $itemType, $itemSource, @@ -245,48 +332,25 @@ class Api { return new \OC_OCS_Result(null, 404, "couldn't set password"); } - /** - * @brief set expire date, path to file is encoded in URL - * @param int $itemSource file ID - * @param string $itemType 'file' or 'folder' - * @param array $params contains 'expire' (format DD-MM-YYYY) - * @return \OC_OCS_Result - */ - private static function updateExpire($itemSource, $itemType, $params) { - - $expire = isset($params['_put']['expire']) ? (int)$params['_put']['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 unshare a file/folder - * @param array $params with following parameters 'shareWith', 'shareType', 'path' + * @param array $params contains the shareID 'id' which should be unshared * @return \OC_OCS_Result */ public static function deleteShare($params) { - $path = $params['path']; - $itemSource = self::getFileId($path); - $itemType = self::getItemType($path); + + $share = self::getShareFromId($params['id']); + $itemSource = isset($share['item_source']) ? $share['item_source'] : null; + $itemType = isset($share['item_type']) ? $share['item_type'] : null;; if($itemSource === null) { - return new \OC_OCS_Result(null, 404, "wrong path, file/folder doesn't exist."); + return new \OC_OCS_Result(null, 404, "wrong share ID, share doesn't exist."); } - $shareWith = isset($params['_delete']['shareWith']) ? $params['_delete']['shareWith'] : null; - $shareType = isset($params['_delete']['shareType']) ? (int)$params['_delete']['shareType'] : null; + $shareWith = isset($share['share_with']) ? $share['share_with'] : null; + $shareType = isset($share['share_type']) ? (int)$share['share_type'] : null; - if( $shareType == \OCP\Share::SHARE_TYPE_LINK) { + if( $shareType === \OCP\Share::SHARE_TYPE_LINK) { $shareWith = null; } @@ -343,4 +407,29 @@ class Api { return $itemType; } + /** + * @brief get some information from a given share + * @param int $shareID + * @return array with: item_source, share_type, share_with, item_type, permissions + */ + private static function getShareFromId($shareID) { + $sql = 'SELECT `item_source`, `share_type`, `share_with`, `item_type`, `permissions` FROM `*PREFIX*share` WHERE `id` = ?'; + $args = array($shareID); + $query = \OCP\DB::prepare($sql); + $result = $query->execute($args); + + $share = Null; + + if (\OCP\DB::isError($result)) { + \OCP\Util::writeLog('files_sharing', \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); + } else { + if ($result->numRows() > 0) { + $share = $result->fetchRow(); + } + } + + return $share; + + } + } From 171b7ebffe96c4f6bd326652a6c12118956e39ca Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Wed, 18 Sep 2013 10:11:20 +0200 Subject: [PATCH 10/35] remove debug output --- apps/files_sharing/lib/api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/files_sharing/lib/api.php b/apps/files_sharing/lib/api.php index 87841150de7..b88850bf17d 100644 --- a/apps/files_sharing/lib/api.php +++ b/apps/files_sharing/lib/api.php @@ -317,7 +317,7 @@ class Api { if (!$checkExists) { return new \OC_OCS_Result(null, 404, "share doesn't exists, can't change password"); } - error_log("type: $itemType"); + $result = \OCP\Share::shareItem( $itemType, $itemSource, From 199121134fabb885dce52645b291c82c1bf4c970 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Thu, 19 Sep 2013 10:33:04 +0200 Subject: [PATCH 11/35] return error if wrong shareType is given --- apps/files_sharing/lib/api.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/apps/files_sharing/lib/api.php b/apps/files_sharing/lib/api.php index b88850bf17d..0f9347e34c6 100644 --- a/apps/files_sharing/lib/api.php +++ b/apps/files_sharing/lib/api.php @@ -133,6 +133,8 @@ class Api { // read (1) if public upload is disabled $permissions = $publicUpload === 'yes' ? 7 : 1; break; + default: + return new \OC_OCS_Result(null, 404, "unknown share type"); } try { From b947aab80290dc17a50aa714f07bb1981e5307d5 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Thu, 19 Sep 2013 14:39:51 +0200 Subject: [PATCH 12/35] some unit test for the new ocs share api --- apps/files_sharing/appinfo/routes.php | 2 +- apps/files_sharing/lib/api.php | 11 +- apps/files_sharing/tests/api.php | 318 ++++++++++++++++++++++++++ tests/enable_all.php | 1 + 4 files changed, 326 insertions(+), 6 deletions(-) create mode 100644 apps/files_sharing/tests/api.php diff --git a/apps/files_sharing/appinfo/routes.php b/apps/files_sharing/appinfo/routes.php index a373bff4dad..3469829b6f7 100644 --- a/apps/files_sharing/appinfo/routes.php +++ b/apps/files_sharing/appinfo/routes.php @@ -10,7 +10,7 @@ function() { OC_API::register('get', '/apps/files_sharing/api/v1/shares', - array('\OCA\Files\Share\Api', 'getAllShare'), + array('\OCA\Files\Share\Api', 'getAllShares'), 'files_sharing'); OC_API::register('post', diff --git a/apps/files_sharing/lib/api.php b/apps/files_sharing/lib/api.php index 0f9347e34c6..8ed9b4e2345 100644 --- a/apps/files_sharing/lib/api.php +++ b/apps/files_sharing/lib/api.php @@ -30,7 +30,7 @@ class Api { * @param array $params option 'file' to limit the result to a specific file/folder * @return \OC_OCS_Result share information */ - public static function getAllShare($params) { + public static function getAllShares($params) { // if a file is specified, get the share for this file if (isset($_GET['file'])) { @@ -59,17 +59,17 @@ class Api { // getAllShare() or we need to translate the shareID to a itemSource if(isset($params['itemSource'])) { $itemSource = $params['itemSource']; - $getAll = true; + $getSpecificShare = true; } else { $s = self::getShareFromId($params['id']); $itemSource = $s['item_source']; - $getAll = false; + $getSpecificShare = false; } if ($itemSource !== null) { $shares = \OCP\Share::getItemShared('file', $itemSource); // if a specific share was specified only return this one - if ($getAll === false) { + if ($getSpecificShare === false) { foreach ($shares as $share) { if ($share['id'] === (int)$params['id']) { $shares = array('element' => $share); @@ -90,7 +90,7 @@ class Api { /** * @breif create a new share - * @param array $params 'path', 'shareWith', 'shareType' + * @param array $params * @return \OC_OCS_Result */ public static function createShare($params) { @@ -380,6 +380,7 @@ class Api { * @return string fileID or null */ private static function getFileId($path) { + $view = new \OC\Files\View('/'.\OCP\User::getUser().'/files'); $fileId = null; diff --git a/apps/files_sharing/tests/api.php b/apps/files_sharing/tests/api.php new file mode 100644 index 00000000000..fa65eac445b --- /dev/null +++ b/apps/files_sharing/tests/api.php @@ -0,0 +1,318 @@ + + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This library 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 along with this library. If not, see . + * + */ + +require_once __DIR__ . '/../../../lib/base.php'; + +use OCA\Files\Share; + +/** + * Class Test_Encryption_Share + */ +class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { + + const TEST_FILES_SHARING_API_USER1 = "test-share-user1"; + const TEST_FILES_SHARING_API_USER2 = "test-share-user2"; + const TEST_FILES_SHARING_API_USER3 = "test-share-user3"; + const TEST_FILES_SHARING_API_USER4 = "test-share-user4"; + const TEST_FILES_SHARING_API_GROUP1 = "test-share-group1"; + + public $filename; + public $data; + /** + * @var OC_FilesystemView + */ + public $view; + public $folder; + + public static function setUpBeforeClass() { + // reset backend + \OC_User::clearBackends(); + \OC_User::useBackend('database'); + + // enable resharing + \OC_Appconfig::setValue('core', 'shareapi_allow_resharing', 'yes'); + + // clear share hooks + \OC_Hook::clear('OCP\\Share'); + \OC::registerShareHooks(); + \OCP\Util::connectHook('OC_Filesystem', 'setup', '\OC\Files\Storage\Shared', 'setup'); + + // create users + self::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1, true); + self::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, true); + + } + + function setUp() { + $this->data = 'foobar'; + $this->view = new \OC_FilesystemView('/' . \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1 . '/files'); + + $this->folder = '/folder'; + + $this->filename = 'share-api-test.txt'; + + // save file with content + $this->view->file_put_contents($this->filename, $this->data); + $this->view->mkdir($this->folder); + $this->view->file_put_contents($this->folder.'/'.$this->filename, $this->data); + } + + function tearDown() { + $this->view->unlink($this->filename); + $this->view->deleteAll($this->folder); + } + + public static function tearDownAfterClass() { + + // cleanup users + \OC_User::deleteUser(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1); + \OC_User::deleteUser(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2); + } + + /** + * @medium + */ + function testCreateShare() { + + //login as user1 + \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1); + + // share to user + + // simulate a post request + $_POST['path'] = $this->filename; + $_POST['shareWith'] = \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2; + $_POST['shareType'] = \OCP\Share::SHARE_TYPE_USER; + + $result = Share\Api::createShare(array()); + + $this->assertTrue($result->succeeded()); + $data = $result->getData(); + + $share = $this->getShareFromId($data['id']); + + $items = \OCP\Share::getItemShared('file', $share['item_source']); + + $this->assertTrue(!empty($items)); + + // share link + + // simulate a post request + $_POST['path'] = $this->folder; + $_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK; + + $result = Share\Api::createShare(array()); + + // check if API call was successful + $this->assertTrue($result->succeeded()); + + $data = $result->getData(); + + // check if we have a token + $this->assertTrue(is_string($data['token'])); + + $share = $this->getShareFromId($data['id']); + + $items = \OCP\Share::getItemShared('file', $share['item_source']); + + $this->assertTrue(!empty($items)); + + + + } + + /** + * @medium + * @depends testCreateShare + */ + function testGetAllShares() { + + $result = Share\Api::getAllShares(array()); + + $this->assertTrue($result->succeeded()); + + // test should return two shares created from testCreateShare() + $this->assertTrue(count($result->getData()) === 2); + } + + /** + * @medium + * @depends testCreateShare + */ + function testGetShare() { + + $fileInfo = $this->view->getFileInfo($this->filename); + + $params = array('itemSource' => $fileInfo['fileid']); + + $result = Share\Api::getShare($params); + + $this->assertTrue($result->succeeded()); + + // test should return one share created from testCreateShare() + $this->assertTrue(count($result->getData()) === 1); + + } + + /** + * @medium + * @depends testCreateShare + */ + function testUpdateShare() { + + $items = \OCP\Share::getItemShared('file', null); + + $linkShare = null; + $userShare = null; + + foreach ($items as $item) { + if ($item['share_type'] === \OCP\Share::SHARE_TYPE_LINK) { + $linkShare = $item; + } + if ($item['share_type'] === \OCP\Share::SHARE_TYPE_USER) { + $userShare = $item; + } + } + + // make sure that we found a link share and a user share + $this->assertTrue(is_array($linkShare)); + $this->assertTrue(is_array($userShare)); + + // update permissions + + $this->assertEquals($userShare['permissions'], '31'); + + $params = array(); + $params['id'] = $userShare['id']; + $params['_put'] = array(); + $params['_put']['permissions'] = 1; + + $result = Share\Api::updateShare($params); + + $this->assertTrue($result->succeeded()); + + $items = \OCP\Share::getItemShared('file', $userShare['file_source']); + + $newUserShare = null; + foreach ($items as $item) { + if ($item['share_with'] === \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2) { + $newUserShare = $item; + break; + } + } + + $this->assertTrue(is_array($newUserShare)); + + $this->assertEquals($newUserShare['permissions'], '1'); + + // update password for link share + + $this->assertTrue(empty($linkShare['share_with'])); + + $params = array(); + $params['id'] = $linkShare['id']; + $params['_put'] = array(); + $params['_put']['password'] = 'foo'; + + $result = Share\Api::updateShare($params); + + $this->assertTrue($result->succeeded()); + + $items = \OCP\Share::getItemShared('file', $linkShare['file_source']); + + $newLinkShare = null; + foreach ($items as $item) { + if ($item['share_type'] === \OCP\Share::SHARE_TYPE_LINK) { + $newLinkShare = $item; + break; + } + } + + $this->assertTrue(is_array($newLinkShare)); + $this->assertTrue(!empty($newLinkShare['share_with'])); + + } + + /** + * @medium + * @depends testCreateShare + */ + function testDeleteShare() { + $items = \OCP\Share::getItemShared('file', null); + + foreach ($items as $item) { + $result = Share\Api::deleteShare(array('id' => $item['id'])); + + $this->assertTrue($result->succeeded()); + } + + $itemsAfterDelete = \OCP\Share::getItemShared('file', null); + + $this->assertTrue(empty($itemsAfterDelete)); + } + + /** + * @param $user + * @param bool $create + * @param bool $password + */ + private static function loginHelper($user, $create = false, $password = false) { + if ($create) { + \OC_User::createUser($user, $user); + } + + if ($password === false) { + $password = $user; + } + + \OC_Util::tearDownFS(); + \OC_User::setUserId(''); + \OC\Files\Filesystem::tearDown(); + \OC_Util::setupFS($user); + \OC_User::setUserId($user); + + $params['uid'] = $user; + $params['password'] = $password; + } + + /** + * @brief get some information from a given share + * @param int $shareID + * @return array with: item_source, share_type, share_with, item_type, permissions + */ + private function getShareFromId($shareID) { + $sql = 'SELECT `item_source`, `share_type`, `share_with`, `item_type`, `permissions` FROM `*PREFIX*share` WHERE `id` = ?'; + $args = array($shareID); + $query = \OCP\DB::prepare($sql); + $result = $query->execute($args); + + $share = Null; + + if ($result && $result->numRows() > 0) { + $share = $result->fetchRow(); + } + + return $share; + + } + +} diff --git a/tests/enable_all.php b/tests/enable_all.php index 111ed0e1357..43ee16a72f8 100644 --- a/tests/enable_all.php +++ b/tests/enable_all.php @@ -8,6 +8,7 @@ require_once __DIR__.'/../lib/base.php'; +OC_App::enable('files_sharing'); OC_App::enable('files_encryption'); OC_App::enable('calendar'); OC_App::enable('contacts'); From 8cf8c0b16168c2bc13d5593abcf7958ed39d27cb Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Thu, 19 Sep 2013 14:42:34 +0200 Subject: [PATCH 13/35] remove some unused variables --- apps/files_sharing/tests/api.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/apps/files_sharing/tests/api.php b/apps/files_sharing/tests/api.php index fa65eac445b..e9d689ed43e 100644 --- a/apps/files_sharing/tests/api.php +++ b/apps/files_sharing/tests/api.php @@ -31,9 +31,6 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { const TEST_FILES_SHARING_API_USER1 = "test-share-user1"; const TEST_FILES_SHARING_API_USER2 = "test-share-user2"; - const TEST_FILES_SHARING_API_USER3 = "test-share-user3"; - const TEST_FILES_SHARING_API_USER4 = "test-share-user4"; - const TEST_FILES_SHARING_API_GROUP1 = "test-share-group1"; public $filename; public $data; @@ -48,9 +45,6 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { \OC_User::clearBackends(); \OC_User::useBackend('database'); - // enable resharing - \OC_Appconfig::setValue('core', 'shareapi_allow_resharing', 'yes'); - // clear share hooks \OC_Hook::clear('OCP\\Share'); \OC::registerShareHooks(); From c9f1538c1f5d9f950da77c869d16b3dfec8e46d8 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Thu, 19 Sep 2013 16:41:29 +0200 Subject: [PATCH 14/35] extend API to get the shares from all files in a given folder --- apps/files_sharing/lib/api.php | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/apps/files_sharing/lib/api.php b/apps/files_sharing/lib/api.php index 8ed9b4e2345..06da492ba15 100644 --- a/apps/files_sharing/lib/api.php +++ b/apps/files_sharing/lib/api.php @@ -35,6 +35,11 @@ class Api { // if a file is specified, get the share for this file if (isset($_GET['file'])) { $params['itemSource'] = self::getFileId($_GET['file']); + $params['path'] = $_GET['file']; + if (isset($_GET['subfiles']) && $_GET['subfiles'] === 'yes') { + error_log("get shares from folder"); + return self::getSharesFromFolder($params); + } return self::getShare($params); } @@ -88,6 +93,33 @@ class Api { } } + /** + * @brief get share from all files in a given folder (non-recursive) + * @param array $params contains 'path' to the folder + * @return \OC_OCS_Result + */ + private static function getSharesFromFolder($params) { + $path = $params['path']; + $view = new \OC\Files\View('/'.\OCP\User::getUser().'/files'); + + if(!$view->is_dir($path)) { + return new \OC_OCS_Result(null, 404, "not a directory"); + } + + $content = $view->getDirectoryContent($path); + + $result = array(); + foreach ($content as $file) { + $share = \OCP\Share::getItemShared('file', $file['fileid']); + if ($share) { + $share['filename'] = $file['name']; + $result[] = $share; + } + } + + return new \OC_OCS_Result($result); + } + /** * @breif create a new share * @param array $params From 39920955f25f7b7f04395324b886d361725436ed Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Thu, 19 Sep 2013 16:52:44 +0200 Subject: [PATCH 15/35] set share id to 'unknown' if we can't retrive one --- apps/files_sharing/lib/api.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/apps/files_sharing/lib/api.php b/apps/files_sharing/lib/api.php index 06da492ba15..3aa87b6e1d1 100644 --- a/apps/files_sharing/lib/api.php +++ b/apps/files_sharing/lib/api.php @@ -182,25 +182,24 @@ class Api { } if ($token) { - $data = null; + $data = array(); + $data['id'] = 'unknown'; $shares = \OCP\Share::getItemShared('file', $itemSource); if(is_string($token)) { //public link share foreach ($shares as $share) { if ($share['token'] === $token) { - $shareId = $share['id']; + $data['id'] = $share['id']; break; } } $url = \OCP\Util::linkToPublic('files&t='.$token); - $data = array('url' => $url, // '&' gets encoded to $amp; - 'token' => $token, - 'id' => $shareId); + $data['url'] = $url; // '&' gets encoded to $amp; + $data['token'] = $token; } else { foreach ($shares as $share) { if ($share['share_with'] === $shareWith && $share['share_type'] === $shareType) { - $shareId = $share['id']; - $data = array('id' => $shareId); + $data['id'] = $share['id']; break; } } From 57092e817f3bb45c4ca030ba135d5f8c7d2a096b Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Thu, 19 Sep 2013 20:14:34 +0200 Subject: [PATCH 16/35] disable encryption app for share API tests --- apps/files_sharing/tests/api.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/apps/files_sharing/tests/api.php b/apps/files_sharing/tests/api.php index e9d689ed43e..3597fbec617 100644 --- a/apps/files_sharing/tests/api.php +++ b/apps/files_sharing/tests/api.php @@ -32,6 +32,7 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { const TEST_FILES_SHARING_API_USER1 = "test-share-user1"; const TEST_FILES_SHARING_API_USER2 = "test-share-user2"; + public $stateFilesEncryption; public $filename; public $data; /** @@ -68,11 +69,24 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { $this->view->file_put_contents($this->filename, $this->data); $this->view->mkdir($this->folder); $this->view->file_put_contents($this->folder.'/'.$this->filename, $this->data); + + + // remember files_encryption state + $this->stateFilesEncryption = OC_App::isEnabled('files_encryption'); + + // we don't want to tests with app files_encryption enabled + \OC_App::disable('files_encryption'); } function tearDown() { $this->view->unlink($this->filename); $this->view->deleteAll($this->folder); + // reset app files_trashbin + if ($this->stateFilesTrashbin) { + OC_App::enable('files_encryption'); + } else { + OC_App::disable('files_encryption'); + } } public static function tearDownAfterClass() { From 96c27ead9256999f3bcdf0647a77589851acd381 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Fri, 20 Sep 2013 11:21:34 +0200 Subject: [PATCH 17/35] check if encryption app is enabled --- apps/files_encryption/lib/proxy.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/apps/files_encryption/lib/proxy.php b/apps/files_encryption/lib/proxy.php index eb7ba60cb9d..33851381919 100644 --- a/apps/files_encryption/lib/proxy.php +++ b/apps/files_encryption/lib/proxy.php @@ -51,8 +51,7 @@ class Proxy extends \OC_FileProxy { if (is_null(self::$enableEncryption)) { - if ( - \OCP\Config::getAppValue('files_encryption', 'enable_encryption', 'true') === 'true' + if (\OCP\Config::getAppValue('files_encryption', 'enabled', 'true') === 'true' && Crypt::mode() === 'server' ) { @@ -200,7 +199,7 @@ class Proxy extends \OC_FileProxy { */ public function preUnlink($path) { - // let the trashbin handle this + // let the trashbin handle this if (\OCP\App::isEnabled('files_trashbin')) { return true; } @@ -291,7 +290,7 @@ class Proxy extends \OC_FileProxy { // Close the original encrypted file fclose($result); - // Open the file using the crypto stream wrapper + // Open the file using the crypto stream wrapper // protocol and let it do the decryption work instead $result = fopen('crypt://' . $path, $meta['mode']); From bd09910f67f3bf1928d0dad803e32621d4fc030f Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Fri, 20 Sep 2013 11:24:18 +0200 Subject: [PATCH 18/35] disable encrpytion app before performing tests --- apps/files_sharing/tests/api.php | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/apps/files_sharing/tests/api.php b/apps/files_sharing/tests/api.php index 3597fbec617..6abc6fc63ea 100644 --- a/apps/files_sharing/tests/api.php +++ b/apps/files_sharing/tests/api.php @@ -61,31 +61,34 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { $this->data = 'foobar'; $this->view = new \OC_FilesystemView('/' . \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1 . '/files'); - $this->folder = '/folder'; + $this->folder = '/folder_share_api_test'; $this->filename = 'share-api-test.txt'; + // remember files_encryption state + $this->stateFilesEncryption = \OC_App::isEnabled('files_encryption'); + + //we don't want to tests with app files_encryption enabled + \OC_App::disable('files_encryption'); + + + $this->assertTrue(!\OC_App::isEnabled('files_encryption')); + // save file with content $this->view->file_put_contents($this->filename, $this->data); $this->view->mkdir($this->folder); $this->view->file_put_contents($this->folder.'/'.$this->filename, $this->data); - - // remember files_encryption state - $this->stateFilesEncryption = OC_App::isEnabled('files_encryption'); - - // we don't want to tests with app files_encryption enabled - \OC_App::disable('files_encryption'); } function tearDown() { $this->view->unlink($this->filename); $this->view->deleteAll($this->folder); - // reset app files_trashbin - if ($this->stateFilesTrashbin) { - OC_App::enable('files_encryption'); + // reset app files_encryption + if ($this->stateFilesEncryption) { + \OC_App::enable('files_encryption'); } else { - OC_App::disable('files_encryption'); + \OC_App::disable('files_encryption'); } } From 762654067985b5f487b2c74e675a1966f9c0a6c6 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Fri, 20 Sep 2013 12:38:23 +0200 Subject: [PATCH 19/35] make sure that files are shared correctly before each test and unshared again after the test --- apps/files_sharing/tests/api.php | 45 ++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/apps/files_sharing/tests/api.php b/apps/files_sharing/tests/api.php index 6abc6fc63ea..a112ae7b27c 100644 --- a/apps/files_sharing/tests/api.php +++ b/apps/files_sharing/tests/api.php @@ -147,6 +147,15 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { $this->assertTrue(!empty($items)); + $fileinfo = $this->view->getFileInfo($this->filename); + + \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER, + \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2); + + $fileinfo = $this->view->getFileInfo($this->folder); + + \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null); + } @@ -157,12 +166,20 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { */ function testGetAllShares() { + $fileinfo = $this->view->getFileInfo($this->filename); + + \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER, + \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31); + $result = Share\Api::getAllShares(array()); $this->assertTrue($result->succeeded()); // test should return two shares created from testCreateShare() - $this->assertTrue(count($result->getData()) === 2); + $this->assertTrue(count($result->getData()) === 1); + + \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER, + \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2); } /** @@ -173,6 +190,12 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { $fileInfo = $this->view->getFileInfo($this->filename); + \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, + \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31); + + \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, + null, 1); + $params = array('itemSource' => $fileInfo['fileid']); $result = Share\Api::getShare($params); @@ -180,7 +203,12 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { $this->assertTrue($result->succeeded()); // test should return one share created from testCreateShare() - $this->assertTrue(count($result->getData()) === 1); + $this->assertTrue(count($result->getData()) === 2); + + \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, + \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2); + + \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null); } @@ -190,6 +218,14 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { */ function testUpdateShare() { + $fileInfo = $this->view->getFileInfo($this->filename); + + \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, + \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31); + + \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, + null, 1); + $items = \OCP\Share::getItemShared('file', null); $linkShare = null; @@ -261,6 +297,11 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { $this->assertTrue(is_array($newLinkShare)); $this->assertTrue(!empty($newLinkShare['share_with'])); + \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, + \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2); + + \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null); + } /** From 259f03bee28eee019dbd0b23d33040bf9a76242d Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Mon, 23 Sep 2013 14:10:09 +0200 Subject: [PATCH 20/35] remove debug output --- apps/files_sharing/lib/api.php | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/files_sharing/lib/api.php b/apps/files_sharing/lib/api.php index 3aa87b6e1d1..1fee3960f6f 100644 --- a/apps/files_sharing/lib/api.php +++ b/apps/files_sharing/lib/api.php @@ -37,7 +37,6 @@ class Api { $params['itemSource'] = self::getFileId($_GET['file']); $params['path'] = $_GET['file']; if (isset($_GET['subfiles']) && $_GET['subfiles'] === 'yes') { - error_log("get shares from folder"); return self::getSharesFromFolder($params); } return self::getShare($params); From 333d3eda993a2af26639cb73d60a79fb9322826b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Wed, 25 Sep 2013 09:43:16 +0200 Subject: [PATCH 21/35] add error message to the assert --- apps/files_sharing/tests/api.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/files_sharing/tests/api.php b/apps/files_sharing/tests/api.php index a112ae7b27c..02adff7dfaa 100644 --- a/apps/files_sharing/tests/api.php +++ b/apps/files_sharing/tests/api.php @@ -25,7 +25,7 @@ require_once __DIR__ . '/../../../lib/base.php'; use OCA\Files\Share; /** - * Class Test_Encryption_Share + * Class Test_Files_Sharing_Api */ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { @@ -255,7 +255,7 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { $result = Share\Api::updateShare($params); - $this->assertTrue($result->succeeded()); + $this->assertTrue($result->succeeded(), $result->getMeta()['message']); $items = \OCP\Share::getItemShared('file', $userShare['file_source']); From 4077ed6ddbd11a4013284ea1a15455a68827f16d Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Wed, 25 Sep 2013 15:05:23 +0200 Subject: [PATCH 22/35] add some additional assertions to the tests --- apps/files_sharing/tests/api.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/apps/files_sharing/tests/api.php b/apps/files_sharing/tests/api.php index 02adff7dfaa..475f203471e 100644 --- a/apps/files_sharing/tests/api.php +++ b/apps/files_sharing/tests/api.php @@ -220,14 +220,23 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { $fileInfo = $this->view->getFileInfo($this->filename); - \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, + $result = \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31); - \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, + // share was successful? + $this->assertTrue($result); + + $result = \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1); + // share was successful? + $this->assertTrue(is_string($result)); + $items = \OCP\Share::getItemShared('file', null); + // make sure that we found a link share and a user share + $this->assertEquals(count($items), 2); + $linkShare = null; $userShare = null; From 4d2dfa98c0e96f7829127744a741b03819a1666a Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Wed, 25 Sep 2013 17:44:05 +0200 Subject: [PATCH 23/35] check if key exists before reading it --- apps/files_encryption/lib/keymanager.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/apps/files_encryption/lib/keymanager.php b/apps/files_encryption/lib/keymanager.php index 5386de486e1..6863b0871c7 100755 --- a/apps/files_encryption/lib/keymanager.php +++ b/apps/files_encryption/lib/keymanager.php @@ -40,11 +40,14 @@ class Keymanager { public static function getPrivateKey(\OC_FilesystemView $view, $user) { $path = '/' . $user . '/' . 'files_encryption' . '/' . $user . '.private.key'; + $key = false; $proxyStatus = \OC_FileProxy::$enabled; \OC_FileProxy::$enabled = false; - $key = $view->file_get_contents($path); + if ($view->file_exists($path)) { + $key = $view->file_get_contents($path); + } \OC_FileProxy::$enabled = $proxyStatus; From 663009f895f63235d65b32f32dba532e46b9057d Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Wed, 25 Sep 2013 19:23:07 +0200 Subject: [PATCH 24/35] fix check if app is enabled --- apps/files_encryption/lib/proxy.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/files_encryption/lib/proxy.php b/apps/files_encryption/lib/proxy.php index 33851381919..e6d6841d396 100644 --- a/apps/files_encryption/lib/proxy.php +++ b/apps/files_encryption/lib/proxy.php @@ -51,7 +51,8 @@ class Proxy extends \OC_FileProxy { if (is_null(self::$enableEncryption)) { - if (\OCP\Config::getAppValue('files_encryption', 'enabled', 'true') === 'true' + if ( + \OCP\App::isEnabled('files_encryption') === true && Crypt::mode() === 'server' ) { From 62b8c36a40228192a936e823cda83c269147af91 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Thu, 26 Sep 2013 16:27:14 +0200 Subject: [PATCH 25/35] check if encryption app is enabled before trying to calculate file size --- apps/files_encryption/lib/proxy.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/files_encryption/lib/proxy.php b/apps/files_encryption/lib/proxy.php index 4ec810a5199..6f630c83a3f 100644 --- a/apps/files_encryption/lib/proxy.php +++ b/apps/files_encryption/lib/proxy.php @@ -317,7 +317,7 @@ class Proxy extends \OC_FileProxy { public function postGetFileInfo($path, $data) { // if path is a folder do nothing - if (is_array($data) && array_key_exists('size', $data)) { + if (\OCP\App::isEnabled('files_encryption') && is_array($data) && array_key_exists('size', $data)) { // Disable encryption proxy to prevent recursive calls $proxyStatus = \OC_FileProxy::$enabled; From 5b4f515e7bc0d2e74094b2caa94b130cf4ec1fda Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Fri, 27 Sep 2013 14:01:04 +0200 Subject: [PATCH 26/35] add test getShareFromId --- apps/files_sharing/tests/api.php | 37 +++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/apps/files_sharing/tests/api.php b/apps/files_sharing/tests/api.php index 475f203471e..51d5da5ac05 100644 --- a/apps/files_sharing/tests/api.php +++ b/apps/files_sharing/tests/api.php @@ -186,7 +186,7 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { * @medium * @depends testCreateShare */ - function testGetShare() { + function testGetShareFromSource() { $fileInfo = $this->view->getFileInfo($this->filename); @@ -212,6 +212,41 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { } + /** + * @medium + * @depends testCreateShare + */ + function testGetShareFromId() { + + $fileInfo = $this->view->getFileInfo($this->filename); + + $result = \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, + \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31); + + // share was successful? + $this->assertTrue($result); + + $result = \OCP\Share::getItemShared('file', $fileInfo['fileid']); + + $this->assertEquals(count($result), 1); + + // get first element + $share = reset($result); + + $params = array('id' => $share['id']); + + $result = Share\Api::getShare($params); + + $this->assertTrue($result->succeeded()); + + // test should return one share created from testCreateShare() + $this->assertEquals(count($result->getData()), 1); + + \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, + \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2); + + } + /** * @medium * @depends testCreateShare From f935a3855944055fd7047675d07e6094b3736e85 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Fri, 27 Sep 2013 14:04:28 +0200 Subject: [PATCH 27/35] add some comments --- apps/files_sharing/tests/api.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/apps/files_sharing/tests/api.php b/apps/files_sharing/tests/api.php index 51d5da5ac05..04640ab726a 100644 --- a/apps/files_sharing/tests/api.php +++ b/apps/files_sharing/tests/api.php @@ -226,6 +226,7 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { // share was successful? $this->assertTrue($result); + // get item to determine share ID $result = \OCP\Share::getItemShared('file', $fileInfo['fileid']); $this->assertEquals(count($result), 1); @@ -233,13 +234,13 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { // get first element $share = reset($result); + // call getShare() with share ID $params = array('id' => $share['id']); - $result = Share\Api::getShare($params); $this->assertTrue($result->succeeded()); - // test should return one share created from testCreateShare() + // test should return one share $this->assertEquals(count($result->getData()), 1); \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, From 63ea6844e8a943fe8dfa02a0b8d1fbef288d7278 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Sun, 29 Sep 2013 22:16:48 +0200 Subject: [PATCH 28/35] - adding unit test testGetShareFromUnknownId() - fixing getShareFromId() for Oracle - fixing error message --- apps/files_sharing/lib/api.php | 14 ++++++-------- apps/files_sharing/tests/api.php | 20 +++++++++++++++++--- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/apps/files_sharing/lib/api.php b/apps/files_sharing/lib/api.php index 1fee3960f6f..455784558b5 100644 --- a/apps/files_sharing/lib/api.php +++ b/apps/files_sharing/lib/api.php @@ -86,7 +86,7 @@ class Api { } if ($shares === null || empty($shares)) { - return new \OC_OCS_Result(null, 404, 'share doesn\'t exists'); + return new \OC_OCS_Result(null, 404, 'share doesn\'t exist'); } else { return new \OC_OCS_Result($shares); } @@ -451,17 +451,15 @@ class Api { $query = \OCP\DB::prepare($sql); $result = $query->execute($args); - $share = Null; - if (\OCP\DB::isError($result)) { \OCP\Util::writeLog('files_sharing', \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); - } else { - if ($result->numRows() > 0) { - $share = $result->fetchRow(); - } + return null; + } + if ($share = $result->fetchRow()) { + return $share; } - return $share; + return null; } diff --git a/apps/files_sharing/tests/api.php b/apps/files_sharing/tests/api.php index 51d5da5ac05..1b278be3032 100644 --- a/apps/files_sharing/tests/api.php +++ b/apps/files_sharing/tests/api.php @@ -221,7 +221,7 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { $fileInfo = $this->view->getFileInfo($this->filename); $result = \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, - \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31); + \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31); // share was successful? $this->assertTrue($result); @@ -239,11 +239,25 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { $this->assertTrue($result->succeeded()); - // test should return one share created from testCreateShare() + // test should return one share created from testCreateShare() $this->assertEquals(count($result->getData()), 1); \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, - \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2); + \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2); + + } + + /** + * @medium + */ + function testGetShareFromUnknownId() { + + $params = array('id' => 0); + + $result = Share\Api::getShare($params); + + $this->assertEquals(404, $result->getStatusCode()); + $this->assertEquals('share doesn\'t exist', $result->getMeta()['message']); } From 258e8d58da8ee1abcf4a89049c75f24a398daa75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Sun, 29 Sep 2013 22:20:42 +0200 Subject: [PATCH 29/35] flipping arguments on assertEquals() - first argument contains the expected value and second argument contains the value to be asserted --- apps/files_sharing/tests/api.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/files_sharing/tests/api.php b/apps/files_sharing/tests/api.php index 1b278be3032..f5897a22d8f 100644 --- a/apps/files_sharing/tests/api.php +++ b/apps/files_sharing/tests/api.php @@ -228,7 +228,7 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { $result = \OCP\Share::getItemShared('file', $fileInfo['fileid']); - $this->assertEquals(count($result), 1); + $this->assertEquals(1, count($result)); // get first element $share = reset($result); @@ -240,7 +240,7 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { $this->assertTrue($result->succeeded()); // test should return one share created from testCreateShare() - $this->assertEquals(count($result->getData()), 1); + $this->assertEquals(1, count($result->getData())); \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2); @@ -304,7 +304,7 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { // update permissions - $this->assertEquals($userShare['permissions'], '31'); + $this->assertEquals('31', $userShare['permissions']); $params = array(); $params['id'] = $userShare['id']; @@ -327,7 +327,7 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { $this->assertTrue(is_array($newUserShare)); - $this->assertEquals($newUserShare['permissions'], '1'); + $this->assertEquals('1', $newUserShare['permissions']); // update password for link share From 03db954e1a21951cbf451f32fd6234b7d27861f8 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Mon, 30 Sep 2013 12:38:36 +0200 Subject: [PATCH 30/35] add getShareFromFolder() test --- apps/files_sharing/tests/api.php | 37 ++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/apps/files_sharing/tests/api.php b/apps/files_sharing/tests/api.php index 271484f8d44..d91422bab24 100644 --- a/apps/files_sharing/tests/api.php +++ b/apps/files_sharing/tests/api.php @@ -248,6 +248,43 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { } + /** + * @medium + */ + function testGetShareFromFolder() { + + $fileInfo1 = $this->view->getFileInfo($this->filename); + $fileInfo2 = $this->view->getFileInfo($this->folder.'/'.$this->filename); + + $result = \OCP\Share::shareItem('file', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER, + \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31); + + // share was successful? + $this->assertTrue($result); + + $result = \OCP\Share::shareItem('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, + null, 1); + + // share was successful? + $this->assertTrue(is_string($result)); + + $_GET['file'] = $this->folder; + $_GET['subfiles'] = 'yes'; + + $result = Share\Api::getAllShares(array()); + + $this->assertTrue($result->succeeded()); + + // test should return one share within $this->folder + $this->assertTrue(count($result->getData()) === 1); + + \OCP\Share::unshare('file', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER, + \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2); + + \OCP\Share::unshare('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null); + + } + /** * @medium */ From 6d830087db71318fb3669cbe5bb38384e5d5a46e Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Mon, 30 Sep 2013 12:57:55 +0200 Subject: [PATCH 31/35] add testUpdateShareUpload() --- apps/files_sharing/tests/api.php | 65 ++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/apps/files_sharing/tests/api.php b/apps/files_sharing/tests/api.php index d91422bab24..7cbfe03b49e 100644 --- a/apps/files_sharing/tests/api.php +++ b/apps/files_sharing/tests/api.php @@ -154,7 +154,7 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { $fileinfo = $this->view->getFileInfo($this->folder); - \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null); + \OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null); @@ -262,7 +262,7 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { // share was successful? $this->assertTrue($result); - $result = \OCP\Share::shareItem('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, + $result = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1); // share was successful? @@ -281,7 +281,7 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { \OCP\Share::unshare('file', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER, \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2); - \OCP\Share::unshare('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null); + \OCP\Share::unshare('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null); } @@ -400,6 +400,65 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { } + /** + * @medium + */ + function testUpdateShareUpload() { + + $fileInfo = $this->view->getFileInfo($this->folder); + + $result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, + null, 1); + + // share was successful? + $this->assertTrue(is_string($result)); + + $items = \OCP\Share::getItemShared('file', null); + + // make sure that we found a link share and a user share + $this->assertEquals(count($items), 1); + + $linkShare = null; + + foreach ($items as $item) { + if ($item['share_type'] === \OCP\Share::SHARE_TYPE_LINK) { + $linkShare = $item; + } + } + + // make sure that we found a link share + $this->assertTrue(is_array($linkShare)); + + // update public upload + + $params = array(); + $params['id'] = $linkShare['id']; + $params['_put'] = array(); + $params['_put']['publicUpload'] = 'yes'; + + $result = Share\Api::updateShare($params); + + $this->assertTrue($result->succeeded()); + + $items = \OCP\Share::getItemShared('file', $linkShare['file_source']); + + $updatedLinkShare = null; + foreach ($items as $item) { + if ($item['share_type'] === \OCP\Share::SHARE_TYPE_LINK) { + $updatedLinkShare = $item; + break; + } + } + + $this->assertTrue(is_array($updatedLinkShare)); + $this->assertEquals(7, $updatedLinkShare['permissions']); + + // cleanup + + \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null); + + } + /** * @medium * @depends testCreateShare From 513dc20d4b851381a9096807a930fd7b371a553b Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Mon, 30 Sep 2013 13:05:34 +0200 Subject: [PATCH 32/35] also return success if no shares are found. --- apps/files_sharing/lib/api.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/apps/files_sharing/lib/api.php b/apps/files_sharing/lib/api.php index 455784558b5..c3048da9a41 100644 --- a/apps/files_sharing/lib/api.php +++ b/apps/files_sharing/lib/api.php @@ -44,11 +44,12 @@ class Api { $share = \OCP\Share::getItemShared('file', null); - if ($share !== null) { - return new \OC_OCS_Result($share); + if ($share === false) { + return new \OC_OCS_Result(null, 404, 'could not get shares'); } else { - return new \OC_OCS_Result(null, 404, 'no shares available'); + return new \OC_OCS_Result($share); } + } /** From a1fe5148b54e9ba130efd0d0d2bebabfa61a4457 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Fri, 4 Oct 2013 12:10:11 +0200 Subject: [PATCH 33/35] use always path as parameter to specify a file/folder --- apps/files_sharing/lib/api.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/files_sharing/lib/api.php b/apps/files_sharing/lib/api.php index c3048da9a41..8615d51160f 100644 --- a/apps/files_sharing/lib/api.php +++ b/apps/files_sharing/lib/api.php @@ -33,9 +33,9 @@ class Api { public static function getAllShares($params) { // if a file is specified, get the share for this file - if (isset($_GET['file'])) { - $params['itemSource'] = self::getFileId($_GET['file']); - $params['path'] = $_GET['file']; + if (isset($_GET['path'])) { + $params['itemSource'] = self::getFileId($_GET['path']); + $params['path'] = $_GET['path']; if (isset($_GET['subfiles']) && $_GET['subfiles'] === 'yes') { return self::getSharesFromFolder($params); } From dd2bf691520319e6ad129fc6ab44fb3a8f6427c5 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Fri, 4 Oct 2013 12:16:47 +0200 Subject: [PATCH 34/35] use true/false instead of yes/no for publicUpload and subfiles parameter --- apps/files_sharing/lib/api.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/files_sharing/lib/api.php b/apps/files_sharing/lib/api.php index 8615d51160f..e6624624898 100644 --- a/apps/files_sharing/lib/api.php +++ b/apps/files_sharing/lib/api.php @@ -36,7 +36,7 @@ class Api { if (isset($_GET['path'])) { $params['itemSource'] = self::getFileId($_GET['path']); $params['path'] = $_GET['path']; - if (isset($_GET['subfiles']) && $_GET['subfiles'] === 'yes') { + if (isset($_GET['subfiles']) && $_GET['subfiles'] === 'true') { return self::getSharesFromFolder($params); } return self::getShare($params); @@ -160,10 +160,10 @@ class Api { ($encryptionEnabled || $publicUploadEnabled !== 'yes')) { return new \OC_OCS_Result(null, 404, "public upload disabled by the administrator"); } - $publicUpload = isset($_POST['publicUpload']) ? $_POST['publicUpload'] : 'no'; + $publicUpload = isset($_POST['publicUpload']) ? $_POST['publicUpload'] : 'false'; // read, create, update (7) if public upload is enabled or // read (1) if public upload is disabled - $permissions = $publicUpload === 'yes' ? 7 : 1; + $permissions = $publicUpload === 'true' ? 7 : 1; break; default: return new \OC_OCS_Result(null, 404, "unknown share type"); @@ -310,7 +310,7 @@ class Api { // read, create, update (7) if public upload is enabled or // read (1) if public upload is disabled - $params['_put']['permissions'] = $params['_put']['publicUpload'] === 'yes' ? 7 : 1; + $params['_put']['permissions'] = $params['_put']['publicUpload'] === 'true' ? 7 : 1; return self::updatePermissions($share, $params); From 2e7f481e6bcc9b9d2aea986c86af123f1ec265c0 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Fri, 4 Oct 2013 14:32:15 +0200 Subject: [PATCH 35/35] update tests --- apps/files_sharing/tests/api.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/files_sharing/tests/api.php b/apps/files_sharing/tests/api.php index 7cbfe03b49e..c55c186f089 100644 --- a/apps/files_sharing/tests/api.php +++ b/apps/files_sharing/tests/api.php @@ -268,8 +268,8 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { // share was successful? $this->assertTrue(is_string($result)); - $_GET['file'] = $this->folder; - $_GET['subfiles'] = 'yes'; + $_GET['path'] = $this->folder; + $_GET['subfiles'] = 'true'; $result = Share\Api::getAllShares(array()); @@ -434,7 +434,7 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase { $params = array(); $params['id'] = $linkShare['id']; $params['_put'] = array(); - $params['_put']['publicUpload'] = 'yes'; + $params['_put']['publicUpload'] = 'true'; $result = Share\Api::updateShare($params);