]> source.dussan.org Git - nextcloud-server.git/commitdiff
move unlink proxy to a hook which handles pre and post conditions
authorBjoern Schiessle <schiessle@owncloud.com>
Fri, 31 Jan 2014 19:38:35 +0000 (20:38 +0100)
committerBjoern Schiessle <schiessle@owncloud.com>
Fri, 31 Jan 2014 19:38:35 +0000 (20:38 +0100)
apps/files_encryption/hooks/hooks.php
apps/files_encryption/lib/helper.php
apps/files_encryption/lib/proxy.php

index 09d5687e2269f6d6851211c4613b4390ea3a0608..4c4b3f2040ffd84a6da6da45205d64242a78917f 100644 (file)
@@ -32,6 +32,8 @@ class Hooks {
 \r
        // file for which we want to rename the keys after the rename operation was successful\r
        private static $renamedFiles = array();\r
+       // file for which we want to delete the keys after the delete operation was successful\r
+       private static $deleteFiles = array();\r
 \r
        /**\r
         * @brief Startup encryption backend upon user login\r
@@ -630,4 +632,66 @@ class Hooks {
                }\r
        }\r
 \r
+       /**\r
+        * @brief if the file was really deleted we remove the encryption keys\r
+        * @param array $params\r
+        * @return boolean\r
+        */\r
+       public static function postDelete($params) {\r
+\r
+               if (!isset(self::$deleteFiles[$params[\OC\Files\Filesystem::signal_param_path]])) {\r
+                       return true;\r
+               }\r
+\r
+               $deletedFile = self::$deleteFiles[$params[\OC\Files\Filesystem::signal_param_path]];\r
+               $path = $deletedFile['path'];\r
+               $user = $deletedFile['uid'];\r
+\r
+               // we don't need to remember the file any longer\r
+               unset(self::$deleteFiles[$params[\OC\Files\Filesystem::signal_param_path]]);\r
+\r
+               $view = new \OC\Files\View('/');\r
+\r
+               // return if the file still exists and wasn't deleted correctly\r
+               if ($view->file_exists('/' . $user . '/files/' . $path)) {\r
+                       return true;\r
+               }\r
+\r
+               // Disable encryption proxy to prevent recursive calls\r
+               $proxyStatus = \OC_FileProxy::$enabled;\r
+               \OC_FileProxy::$enabled = false;\r
+\r
+               // Delete keyfile & shareKey so it isn't orphaned\r
+               if (!Keymanager::deleteFileKey($view, $path, $user)) {\r
+                       \OCP\Util::writeLog('Encryption library',\r
+                               'Keyfile or shareKey could not be deleted for file "' . $user.'/files/'.$path . '"', \OCP\Util::ERROR);\r
+               }\r
+\r
+               Keymanager::delAllShareKeys($view, $user, $path);\r
+\r
+               \OC_FileProxy::$enabled = $proxyStatus;\r
+       }\r
+\r
+       /**\r
+        * @brief remember the file which should be deleted and it's owner\r
+        * @param array $params\r
+        * @return boolean\r
+        */\r
+       public static function preDelete($params) {\r
+               $path = $params[\OC\Files\Filesystem::signal_param_path];\r
+\r
+               // skip this method if the trash bin is enabled or if we delete a file\r
+               // outside of /data/user/files\r
+               if (\OCP\App::isEnabled('files_trashbin')) {\r
+                       return true;\r
+               }\r
+\r
+               $util = new Util(new \OC_FilesystemView('/'), \OCP\USER::getUser());\r
+               list($owner, $ownerPath) = $util->getUidAndFilename($path);\r
+\r
+               self::$deleteFiles[$params[\OC\Files\Filesystem::signal_param_path]] = array(\r
+                       'uid' => $owner,\r
+                       'path' => $ownerPath);\r
+       }\r
+\r
 }\r
index 5dcb05fa1961cb3f69406cc16fefe60657b24e37..bb06a57c7143191ee801175a13617abe5df4b8c7 100755 (executable)
@@ -63,6 +63,8 @@ class Helper {
 
                \OCP\Util::connectHook('OC_Filesystem', 'rename', 'OCA\Encryption\Hooks', 'preRename');
                \OCP\Util::connectHook('OC_Filesystem', 'post_rename', 'OCA\Encryption\Hooks', 'postRename');
+               \OCP\Util::connectHook('OC_Filesystem', 'post_delete', 'OCA\Encryption\Hooks', 'postDelete');
+               \OCP\Util::connectHook('OC_Filesystem', 'delete', 'OCA\Encryption\Hooks', 'preDelete');
        }
 
        /**
index 4e71ab1dd5dedee81c5ed825f495e7c090b9445e..1104800596972c49f9809890072caad5d26fad98 100644 (file)
@@ -203,47 +203,6 @@ class Proxy extends \OC_FileProxy {
 
        }
 
-       /**
-        * @brief When a file is deleted, remove its keyfile also
-        */
-       public function preUnlink($path) {
-
-               $relPath = Helper::stripUserFilesPath($path);
-
-               // skip this method if the trash bin is enabled or if we delete a file
-               // outside of /data/user/files
-               if (\OCP\App::isEnabled('files_trashbin') || $relPath === false) {
-                       return true;
-               }
-
-               // Disable encryption proxy to prevent recursive calls
-               $proxyStatus = \OC_FileProxy::$enabled;
-               \OC_FileProxy::$enabled = false;
-
-               $view = new \OC_FilesystemView('/');
-
-               $userId = \OCP\USER::getUser();
-
-               $util = new Util($view, $userId);
-
-               list($owner, $ownerPath) = $util->getUidAndFilename($relPath);
-
-               // Delete keyfile & shareKey so it isn't orphaned
-               if (!Keymanager::deleteFileKey($view, $ownerPath)) {
-                       \OCP\Util::writeLog('Encryption library',
-                               'Keyfile or shareKey could not be deleted for file "' . $ownerPath . '"', \OCP\Util::ERROR);
-               }
-
-               Keymanager::delAllShareKeys($view, $owner, $ownerPath);
-
-               \OC_FileProxy::$enabled = $proxyStatus;
-
-               // If we don't return true then file delete will fail; better
-               // to leave orphaned keyfiles than to disallow file deletion
-               return true;
-
-       }
-
        /**
         * @param $path
         * @return bool