]> source.dussan.org Git - nextcloud-server.git/commitdiff
fixed re-share problems
authorFlorin Peter <github@florin-peter.de>
Sat, 4 May 2013 01:37:22 +0000 (03:37 +0200)
committerFlorin Peter <github@florin-peter.de>
Sat, 4 May 2013 01:37:22 +0000 (03:37 +0200)
apps/files_encryption/hooks/hooks.php
apps/files_encryption/lib/util.php

index c0e493752a0d72bb9961efc5707a71046c1d1315..c21b9d69f69ff88575e688481ba3373ff0f48286 100644 (file)
@@ -200,16 +200,57 @@ class Hooks {
                        $util = new Util($view, $userId);\r
                        $path = $util->fileIdToPath($params['itemSource']);\r
 \r
-            //check if this is a reshare action, that's true if the item source is already shared with me\r
-                       $sharedItem = \OCP\Share::getItemSharedWithBySource($params['itemType'], $params['fileSource']);\r
-                       if ($sharedItem) {\r
-                               // if it is a re-share than the file is located in my Shared folder\r
-                               $path = '/Shared'.$sharedItem['file_target'];\r
-                       } else {\r
-                               $path = $util->fileIdToPath($params['fileSource']);\r
-                       }\r
+            //if parent is set, then this is a re-share action\r
+            if($params['parent']) {\r
+\r
+                // get the parent from current share\r
+                $parent = $util->getShareParent($params['parent']);\r
+\r
+                // if parent is file the it is an 1:1 share\r
+                if($parent['item_type'] === 'file') {\r
+\r
+                    // prefix path with Shared\r
+                    $path = '/Shared'.$parent['file_target'];\r
+\r
+                } else {\r
+                    // parent is folder but shared was a file!\r
+                    // we try to rebuild the missing path\r
+                    // some examples we face here\r
+                    // user1 share folder1 with user2 folder1 has the following structure /folder1/subfolder1/subsubfolder1/somefile.txt\r
+                    // user2 re-share subfolder2 with user3\r
+                    // user3 re-share somefile.txt user4\r
+                    // so our path should be /Shared/subfolder1/subsubfolder1/somefile.txt while user3 is sharing\r
+                    if($params['itemType'] === 'file') {\r
+                        // get target path\r
+                        $targetPath = $util->fileIdToPath($params['fileSource']);\r
+                        $targetPathSplit = array_reverse(explode('/', $targetPath));\r
+\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
+                    } else {\r
+\r
+                        // prefix path with Shared\r
+                        $path = '/Shared'.$parent['file_target'];\r
+                    }\r
+                }\r
+\r
+            }\r
 \r
-                       $sharingEnabled = \OCP\Share::isEnabled();\r
+               $sharingEnabled = \OCP\Share::isEnabled();\r
 \r
                        // if a folder was shared, get a list if all (sub-)folders\r
                        if ($params['itemType'] === 'folder') {\r
@@ -274,13 +315,14 @@ class Hooks {
                                $allFiles = array($path);\r
                        }\r
 \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
+                // 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
index 015125370bc3f6eea56ac7ebb422bf3b2afb3c07..f442a89f6f96844bb29ba739ece3b019471aea4e 100644 (file)
@@ -1028,4 +1028,23 @@ class Util {
                return $result;
        }
 
+    /**
+     * @brief get shares parent.
+     * @param int $Id of the current share
+     * @return array of the parent
+     */
+    public static function getShareParent($Id) {
+
+        $query = \OC_DB::prepare( 'SELECT `file_target`, `item_type`'
+            .' FROM `*PREFIX*share`'
+            .' WHERE `id` = ?' );
+
+        $result = $query->execute( array( $Id ) );
+
+        $row = $result->fetchRow();
+
+        return $row;
+
+    }
+
 }