]> source.dussan.org Git - nextcloud-server.git/commitdiff
Fix moving share keys as non-owner to subdir
authorVincent Petry <pvince81@owncloud.com>
Wed, 29 Oct 2014 11:22:50 +0000 (12:22 +0100)
committerVincent Petry <pvince81@owncloud.com>
Wed, 29 Oct 2014 11:22:50 +0000 (12:22 +0100)
This fix gathers the share keys BEFORE a file is moved to make sure that
findShareKeys() is able to find all relevant keys when the file still
exists.

After the move/copy operation the keys are moved/copied to the target
dir.

Also: refactored preRename and preCopy into a single function to avoid
duplicate code.

apps/files_encryption/hooks/hooks.php

index e004d4a1d63a69f1b9ce091aca1cad251ce05c9a..3a0a37c0a591843bf4395eff8129d72a9f9a071e 100644 (file)
@@ -409,34 +409,18 @@ class Hooks {
         * @param array $params with the old path and the new path\r
         */\r
        public static function preRename($params) {\r
-               $user = \OCP\User::getUser();\r
-               $view = new \OC\Files\View('/');\r
-               $util = new Util($view, $user);\r
-               list($ownerOld, $pathOld) = $util->getUidAndFilename($params['oldpath']);\r
-\r
-               // we only need to rename the keys if the rename happens on the same mountpoint\r
-               // otherwise we perform a stream copy, so we get a new set of keys\r
-               $mp1 = $view->getMountPoint('/' . $user . '/files/' . $params['oldpath']);\r
-               $mp2 = $view->getMountPoint('/' . $user . '/files/' . $params['newpath']);\r
-\r
-               $type = $view->is_dir('/' . $user . '/files/' . $params['oldpath']) ? 'folder' : 'file';\r
-\r
-               if ($mp1 === $mp2) {\r
-                       self::$renamedFiles[$params['oldpath']] = array(\r
-                               'uid' => $ownerOld,\r
-                               'path' => $pathOld,\r
-                               'type' => $type,\r
-                               'operation' => 'rename',\r
-                               );\r
-\r
-               }\r
+               self::preRenameOrCopy($params, 'rename');\r
        }\r
 \r
        /**\r
-        * mark file as renamed so that we know the original source after the file was renamed\r
+        * mark file as copied so that we know the original source after the file was copied\r
         * @param array $params with the old path and the new path\r
         */\r
        public static function preCopy($params) {\r
+               self::preRenameOrCopy($params, 'copy');\r
+       }\r
+\r
+       private static function preRenameOrCopy($params, $operation) {\r
                $user = \OCP\User::getUser();\r
                $view = new \OC\Files\View('/');\r
                $util = new Util($view, $user);\r
@@ -450,11 +434,27 @@ class Hooks {
                $type = $view->is_dir('/' . $user . '/files/' . $params['oldpath']) ? 'folder' : 'file';\r
 \r
                if ($mp1 === $mp2) {\r
+                       if ($util->isSystemWideMountPoint($pathOld)) {\r
+                               $oldShareKeyPath = 'files_encryption/share-keys/' . $pathOld;\r
+                       } else {\r
+                               $oldShareKeyPath = $ownerOld . '/' . 'files_encryption/share-keys/' . $pathOld;\r
+                       }\r
+                       // gather share keys here because in postRename() the file will be moved already\r
+                       $oldShareKeys = Helper::findShareKeys($pathOld, $oldShareKeyPath, $view);\r
+                       if (count($oldShareKeys) === 0) {\r
+                               \OC_Log::write(\r
+                                       'Encryption library', 'No share keys found for "' . $pathOld . '"',\r
+                                       \OC_Log::WARN\r
+                               );\r
+                       }\r
                        self::$renamedFiles[$params['oldpath']] = array(\r
                                'uid' => $ownerOld,\r
                                'path' => $pathOld,\r
                                'type' => $type,\r
-                               'operation' => 'copy');\r
+                               'operation' => $operation,\r
+                               'sharekeys' => $oldShareKeys\r
+                               );\r
+\r
                }\r
        }\r
 \r
@@ -476,6 +476,7 @@ class Hooks {
                $view = new \OC\Files\View('/');\r
                $userId = \OCP\User::getUser();\r
                $util = new Util($view, $userId);\r
+               $oldShareKeys = null;\r
 \r
                if (isset(self::$renamedFiles[$params['oldpath']]['uid']) &&\r
                                isset(self::$renamedFiles[$params['oldpath']]['path'])) {\r
@@ -483,6 +484,7 @@ class Hooks {
                        $pathOld = self::$renamedFiles[$params['oldpath']]['path'];\r
                        $type =  self::$renamedFiles[$params['oldpath']]['type'];\r
                        $operation = self::$renamedFiles[$params['oldpath']]['operation'];\r
+                       $oldShareKeys = self::$renamedFiles[$params['oldpath']]['sharekeys'];\r
                        unset(self::$renamedFiles[$params['oldpath']]);\r
                } else {\r
                        \OCP\Util::writeLog('Encryption library', "can't get path and owner from the file before it was renamed", \OCP\Util::DEBUG);\r
@@ -522,15 +524,7 @@ class Hooks {
                        $oldKeyfilePath .= '.key';\r
                        $newKeyfilePath .= '.key';\r
 \r
-                       // handle share-keys\r
-                       $matches = Helper::findShareKeys($pathOld, $oldShareKeyPath, $view);\r
-                       if (count($matches) === 0) {\r
-                               \OC_Log::write(\r
-                                       'Encryption library', 'No share keys found for "' . $pathOld . '"',\r
-                                       \OC_Log::WARN\r
-                               );\r
-                       }\r
-                       foreach ($matches as $src) {\r
+                       foreach ($oldShareKeys as $src) {\r
                                $dst = \OC\Files\Filesystem::normalizePath(str_replace($pathOld, $pathNew, $src));\r
                                $view->$operation($src, $dst);\r
                        }\r