]> source.dussan.org Git - nextcloud-server.git/commitdiff
Started work on post unshare hook
authorSam Tuke <samtuke@owncloud.com>
Tue, 26 Feb 2013 18:33:31 +0000 (18:33 +0000)
committerSam Tuke <samtuke@owncloud.com>
Tue, 26 Feb 2013 18:33:31 +0000 (18:33 +0000)
Development snapshot

apps/files_encryption/hooks/hooks.php
apps/files_encryption/lib/keymanager.php
apps/files_encryption/lib/util.php

index 6d982b2c3b05debce6abfea95d66f60b94ac934b..bf16a492e3318a3e96d26c7678da9218d2c1b148 100644 (file)
@@ -167,44 +167,60 @@ class Hooks {
         * @brief get all users with access to the file and encrypt the file key to each of them\r
         */\r
        public static function postShared( $params ) {\r
-\r
-               // NOTE: $params is an array with these keys:\r
+       \r
+               // NOTE: $params has keys:\r
+               // [itemType] => file\r
                // itemSource -> int, filecache file ID\r
+               // [parent] => \r
+               // [itemTarget] => /13\r
                // shareWith -> string, uid of user being shared to\r
                // fileTarget -> path of file being shared\r
                // uidOwner -> owner of the original file being shared\r
+               // [shareType] => 0\r
+               // [shareWith] => test1\r
+               // [uidOwner] => admin\r
+               // [permissions] => 17\r
+               // [fileSource] => 13\r
+               // [fileTarget] => /test8\r
+               // [id] => 10\r
+               // [token] => \r
                \r
-               $view = new \OC_FilesystemView( '/' );\r
-               $session = new Session();\r
-               $userId = \OCP\User::getUser();\r
-               $util = new Util( $view, $userId );\r
-               $path = $util->fileIdToPath( $params['itemSource'] );\r
-               \r
-               $usersSharing = \OCP\Share::getUsersSharingFile( $path, true );\r
-               \r
-               $allPaths = $util->getPaths( $path );\r
+               // TODO: Should other kinds of item be encrypted too?\r
+               if ( $params['itemType'] === 'file' ) {\r
                \r
-               $failed = array();\r
-               \r
-               foreach ( $allPaths as $path ) {\r
-               \r
-                       if ( ! $util->setSharedFileKeyfiles( $session, $usersSharing, $path ) ) {\r
+                       $view = new \OC_FilesystemView( '/' );\r
+                       $session = new Session();\r
+                       $userId = \OCP\User::getUser();\r
+                       $util = new Util( $view, $userId );\r
+                       $path = $util->fileIdToPath( $params['itemSource'] );\r
+                       \r
+                       $usersSharing = \OCP\Share::getUsersSharingFile( $path, true );\r
+                       \r
+                       $allPaths = $util->getPaths( $path );\r
+                       \r
+                       $failed = array();\r
                        \r
-                               $failed[] = $path;\r
+                       foreach ( $allPaths as $path ) {\r
+                       \r
+                               if ( ! $util->setSharedFileKeyfiles( $session, $usersSharing, $path ) ) {\r
+                               \r
+                                       $failed[] = $path;\r
+                                       \r
+                               }\r
                                \r
                        }\r
                        \r
-               }\r
-               \r
-               // If no attempts to set keyfiles failed\r
-               if ( empty( $failed ) ) {\r
-               \r
-                       return true;\r
+                       // If no attempts to set keyfiles failed\r
+                       if ( empty( $failed ) ) {\r
                        \r
-               } else {\r
-               \r
-                       return false;\r
+                               return true;\r
+                               \r
+                       } else {\r
                        \r
+                               return false;\r
+                               \r
+                       }\r
+               \r
                }\r
                \r
        }\r
@@ -213,15 +229,47 @@ class Hooks {
         * @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
-//             $view = new \OC_FilesystemView( '/' );\r
-//             $session = new Session();\r
-//             $userId = \OCP\User::getUser();\r
-//             $util = new Util( $view, $userId );\r
-//             $path = $util->fileIdToPath( $params['itemSource'] );\r
-//             \r
-//             return Crypt::updateKeyfile( $view, $util, $session, $userId, $path );\r
+               // TODO: Should other kinds of item be encrypted too?\r
+               if ( $params['itemType'] === 'file' ) {\r
+               \r
+                       $view = new \OC_FilesystemView( '/' );\r
+                       $session = new Session();\r
+                       $userId = \OCP\User::getUser();\r
+                       $util = new Util( $view, $userId );\r
+                       $path = $util->fileIdToPath( $params['itemSource'] );\r
                \r
+                       $allPaths = $util->getPaths( $path );\r
+                       \r
+                       foreach ( $allPaths as $path ) {\r
+                       \r
+                               if ( ! Keymanager::delShareKey( $view, $userId, $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
+\r
+               }\r
+\r
        }\r
        \r
        /**\r
index ec4057d0983c6b1422b3e69ec5889375772e6046..22e2ffa500e055ea9affc8745952ce03c246dc82 100755 (executable)
@@ -350,6 +350,30 @@ class Keymanager {
                \r
        }\r
        \r
+       /**\r
+        * @brief Delete a single user's shareKey for a single file\r
+        */\r
+       public static function delShareKey( \OC_FilesystemView $view, $userId, $filePath ) {\r
+               \r
+               $trimmed = ltrim( $filePath, '/' );\r
+               $shareKeyPath =  '/' . $userId . '/files_encryption/share-keys/' . $trimmed . '.shareKey';\r
+               \r
+               // Unlink doesn't tell us if file was deleted (not found returns\r
+               // true), so we perform our own test\r
+               if ( $view->file_exists( $shareKeyPath ) ) {\r
+               \r
+                       return $view->unlink( $shareKeyPath );\r
+                       \r
+               } else {\r
+                       \r
+                       \OC_Log::write( 'Encryption library', 'Could not delete shareKey; does not exist: "' . $shareKeyPath, \OC_Log::ERROR );\r
+                       \r
+                       return false;\r
+                       \r
+               }\r
+               \r
+       }\r
+       \r
        /**\r
         * @brief Make preparations to vars and filesystem for saving a keyfile\r
         */\r
index 920ff3eb15999faf347ef626c1fe412ec1fa3098..02c62e160c8cea7daf41ec6112ef51f67f6bdf8e 100644 (file)
@@ -580,7 +580,8 @@ class Util {
         * @brief Expand given path to all sub files & folders
         * @param Session $session
         * @param string $path path which needs to be updated
-        * @return bool outcome of attempt to set keyfiles
+        * @return array $pathsArray all found file paths
+        * @note Paths of directories excluded, only *file* paths are returned
         */
        public function getPaths( $path ) {