]> source.dussan.org Git - nextcloud-server.git/commitdiff
handling for re-share and re-unshare should work now
authorFlorin Peter <github@florin-peter.de>
Sun, 5 May 2013 21:41:42 +0000 (23:41 +0200)
committerFlorin Peter <github@florin-peter.de>
Sun, 5 May 2013 21:41:42 +0000 (23:41 +0200)
apps/files_encryption/hooks/hooks.php
apps/files_encryption/lib/util.php

index 0633a81a057fec2b63484de2c8bc628ad37f86fd..a72d339277ae05ee2be1d3390230e52a4f998224 100644 (file)
 \r
 namespace OCA\Encryption;\r
 \r
+use OC\Files\Filesystem;\r
+\r
 /**\r
  * Class for hook specific logic\r
  */\r
-\r
 class Hooks {\r
 \r
        // TODO: use passphrase for encrypting private key that is separate to \r
@@ -253,10 +254,9 @@ class Hooks {
                     } else {\r
 \r
                         // prefix path with Shared\r
-                        $path = '/Shared'.$parent['file_target'];\r
+                        $path = '/Shared'.$parent['file_target'].$params['fileTarget'];\r
                     }\r
                 }\r
-\r
             }\r
 \r
                $sharingEnabled = \OCP\Share::isEnabled();\r
@@ -282,10 +282,8 @@ class Hooks {
 \r
                        // If no attempts to set keyfiles failed\r
                        if (empty($failed)) {\r
-\r
                                return true;\r
                        } else {\r
-\r
                                return false;\r
                        }\r
                }\r
@@ -294,73 +292,91 @@ class Hooks {
        /**\r
         * @brief \r
         */\r
-       public static function postUnshare( $params ) {\r
-               \r
-               // NOTE: $params has keys:\r
-               // [itemType] => file\r
-               // [itemSource] => 13\r
-               // [shareType] => 0\r
-               // [shareWith] => test1\r
-       \r
-               if ( $params['itemType'] === 'file' ||  $params['itemType'] === 'folder' ) {\r
-               \r
-                       $view = new \OC_FilesystemView( '/' );\r
-                       $session = new Session($view);\r
-                       $userId = \OCP\User::getUser();\r
-                       $util = new Util( $view, $userId );\r
-                       $path = $util->fileIdToPath( $params['itemSource'] );\r
+    public static function postUnshare($params)\r
+    {\r
 \r
-                       // for group shares get a list of the group members\r
-                       if ($params['shareType'] == \OCP\Share::SHARE_TYPE_GROUP) {\r
-                               $userIds = \OC_Group::usersInGroup($params['shareWith']);\r
-                       } else {\r
-                               $userIds = array($params['shareWith']);\r
-                       }\r
+        // NOTE: $params has keys:\r
+        // [itemType] => file\r
+        // [itemSource] => 13\r
+        // [shareType] => 0\r
+        // [shareWith] => test1\r
+        // [itemParent] =>\r
 \r
-                       // if we unshare a folder we need a list of all (sub-)files\r
-                       if ($params['itemType'] === 'folder') {\r
-                               $allFiles = $util->getAllFiles($path);\r
-                       } else {\r
-                               $allFiles = array($path);\r
-                       }\r
+        if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {\r
+\r
+            $view = new \OC_FilesystemView('/');\r
+            $userId = \OCP\User::getUser();\r
+            $util = new Util($view, $userId);\r
+            $path = $util->fileIdToPath($params['itemSource']);\r
 \r
+            // check if this is a re-share\r
+            if ($params['itemParent']) {\r
+\r
+                // get the parent from current share\r
+                $parent = $util->getShareParent($params['itemParent']);\r
 \r
-                       foreach ( $allFiles as $path ) {\r
+                // get target path\r
+                $targetPath = $util->fileIdToPath($params['itemSource']);\r
+                $targetPathSplit = array_reverse(explode('/', $targetPath));\r
 \r
-                               // check if the user still has access to the file, otherwise delete share key\r
-                               $sharingUsers = $util->getSharingUsersArray(true, $path);\r
+                // init values\r
+                $path = '';\r
+                $sharedPart = ltrim($parent['file_target'], '/');\r
+\r
+                // rebuild path\r
+                foreach ($targetPathSplit as $pathPart) {\r
+                    if ($pathPart !== $sharedPart) {\r
+                        $path = '/' . $pathPart . $path;\r
+                    } else {\r
+                        break;\r
+                    }\r
+                }\r
+\r
+                // prefix path with Shared\r
+                $path = '/Shared' . $parent['file_target'] . $path;\r
+            }\r
+\r
+            // for group shares get a list of the group members\r
+            if ($params['shareType'] == \OCP\Share::SHARE_TYPE_GROUP) {\r
+                $userIds = \OC_Group::usersInGroup($params['shareWith']);\r
+            } else {\r
+                $userIds = array($params['shareWith']);\r
+            }\r
+\r
+            // if we unshare a folder we need a list of all (sub-)files\r
+            if ($params['itemType'] === 'folder') {\r
+                $allFiles = $util->getAllFiles($path);\r
+            } else {\r
+                $allFiles = array($path);\r
+            }\r
+\r
+            foreach ($allFiles as $path) {\r
+\r
+                // check if the user still has access to the file, otherwise delete share key\r
+                $sharingUsers = $util->getSharingUsersArray(true, $path);\r
 \r
                 // Unshare every user who no longer has access to the file\r
-                //TODO: does not work properly atm\r
-                               $delUsers = array_diff($userIds, $sharingUsers);\r
-                               if ( ! Keymanager::delShareKey( $view, $delUsers, $path ) ) {\r
-                               \r
-                                       $failed[] = $path;\r
-                                       \r
-                               }\r
-                               \r
-                       }\r
-                       \r
-                       // If no attempts to set keyfiles failed\r
-                       if ( empty( $failed ) ) {\r
-                       \r
-                               return true;\r
-                               \r
-                       } else {\r
-                       \r
-                               return false;\r
-                               \r
-                       }\r
+                $delUsers = array_diff($userIds, $sharingUsers);\r
+                if (!Keymanager::delShareKey($view, $delUsers, $path)) {\r
+                    $failed[] = $path;\r
+                }\r
 \r
-               }\r
+            }\r
 \r
-       }\r
+            // If no attempts to set keyfiles failed\r
+            if (empty($failed)) {\r
+                return true;\r
+            } else {\r
+                return false;\r
+            }\r
+        }\r
+    }\r
        \r
        /**\r
         * @brief \r
         */\r
        public static function postUnshareAll( $params ) {\r
-       \r
+\r
                // NOTE: It appears that this is never called for files, so \r
                // we may not need to implement it\r
                \r
index 91502af4a3fddf8adcd25556f367af4a969098a5..2e8e2af683b9c3b8df17678631b1b4123ce4fd7b 100644 (file)
@@ -1102,10 +1102,26 @@ class Util {
         }
 
                foreach ($content as $c) {
+            $sharedPart = $path_split[sizeof($path_split)-1];
+            $targetPathSplit = array_reverse(explode('/', $c['path']));
+
+            $path = '';
+
+            // rebuild path
+            foreach ($targetPathSplit as $pathPart) {
+                if($pathPart !== $sharedPart) {
+                    $path = '/'.$pathPart.$path;
+                } else {
+                    break;
+                }
+            }
+
+            $path = $dir.$path;
+
                        if ($c['type'] === "dir" ) {
-                $result = array_merge($result, $this->getAllFiles($shared.substr($c['path'],5)));
+                $result = array_merge($result, $this->getAllFiles($path));
                        } else {
-                $result[] = $shared.substr($c['path'], 5);
+                $result[] = $path;
                        }
                }
                return $result;
@@ -1130,4 +1146,32 @@ class Util {
 
     }
 
+    /**
+     * @brief get owner of the shared files.
+     * @param int $Id of a share
+     * @return owner
+     */
+    public function getOwnerFromSharedFile($id) {
+        $query = \OC_DB::prepare('SELECT `parent`, `uid_owner` FROM `*PREFIX*share` WHERE `id` = ?', 1);
+        $source = $query->execute(array($id))->fetchRow();
+
+        if (isset($source['parent'])) {
+            $parent = $source['parent'];
+            while (isset($parent)) {
+                $query = \OC_DB::prepare('SELECT `parent`, `uid_owner` FROM `*PREFIX*share` WHERE `id` = ?', 1);
+                $item = $query->execute(array($parent))->fetchRow();
+                if (isset($item['parent'])) {
+                    $parent = $item['parent'];
+                } else {
+                    $fileOwner = $item['uid_owner'];
+                    break;
+                }
+            }
+        } else {
+            $fileOwner = $source['uid_owner'];
+        }
+
+        return $fileOwner;
+    }
+
 }