]> source.dussan.org Git - nextcloud-server.git/commitdiff
some more OCS calls for sharing
authorBjoern Schiessle <schiessle@owncloud.com>
Fri, 6 Sep 2013 14:00:01 +0000 (16:00 +0200)
committerBjoern Schiessle <schiessle@owncloud.com>
Fri, 6 Sep 2013 14:00:01 +0000 (16:00 +0200)
apps/files_sharing/appinfo/routes.php
apps/files_sharing/lib/api.php

index 1c7f5b4a1fc3f07bd59122c4040835e7b0bfa1a0..3f80614cc0c4314cfca4128a7191410c1be3f1b6 100644 (file)
@@ -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}',
index 7f7f925eb231ba719d1cb20b11ffb338a7b71868..90d8a93d3a425f0c9d90dfd51574d513daac5637 100644 (file)
@@ -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