]> source.dussan.org Git - nextcloud-server.git/commitdiff
make sure that all share keys get deleted if a file/folder gets unshared from a user...
authorBjörn Schießle <schiessle@owncloud.com>
Fri, 19 Apr 2013 11:17:08 +0000 (13:17 +0200)
committerBjörn Schießle <schiessle@owncloud.com>
Fri, 19 Apr 2013 11:17:08 +0000 (13:17 +0200)
apps/files_encryption/hooks/hooks.php
apps/files_encryption/lib/keymanager.php

index 265b90a87a1ef1213a2cf7fb0a2d7e87e23cd2fe..2731ee1112555e280d7fb37f7528801f5d3ae4ab 100644 (file)
@@ -240,22 +240,27 @@ class Hooks {
                // [shareType] => 0\r
                // [shareWith] => test1\r
        \r
-               // TODO: Should other kinds of item be encrypted too?\r
-               if ( $params['itemType'] === 'file' ) {\r
+               if ( $params['itemType'] === 'file' ||  $params['itemType'] === 'folder' ) {\r
                \r
                        $view = new \OC_FilesystemView( '/' );\r
-                       $session = new Session();\r
+                       $session = new Session($view);\r
                        $userId = \OCP\User::getUser();\r
                        $util = new Util( $view, $userId );\r
                        $path = $util->fileIdToPath( $params['itemSource'] );\r
-               \r
+\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 path is a folder, get all children\r
                        $allPaths = $util->getPaths( $path );\r
                        \r
                        foreach ( $allPaths as $path ) {\r
                        \r
                                // Unshare each child path\r
-                               if ( ! Keymanager::delShareKey( $view, $params['shareWith'], $path ) ) {\r
+                               if ( ! Keymanager::delShareKey( $view, $userIds, $path ) ) {\r
                                \r
                                        $failed[] = $path;\r
                                        \r
index 5c5a6c7ec5c473689ef4e0f08ef57e08421b8ed9..f23423062b97dcec6673456d0f572995707db7d2 100755 (executable)
@@ -395,27 +395,28 @@ class Keymanager {
        /**
         * @brief Delete a single user's shareKey for a single file
         */
-       public static function delShareKey( \OC_FilesystemView $view, $userId, $filePath ) {
+       public static function delShareKey( \OC_FilesystemView $view, $userIds, $filePath ) {
                
                \OC_FileProxy::$enabled = false;
 
-               $shareKeyPath = '/' . $userId . '/files_encryption/share-keys/' . $filePath;
+               //here we need the currently logged in user, while userId can be a different user
+               $util = new Util( $view, \OCP\User::getUser() );
+
+               list($owner, $filename) = $util->getUidAndFilename($filePath);
+
+               $shareKeyPath = '/' . $owner . '/files_encryption/share-keys/' . $filename;
 
                $result = false;
 
                if ( $view->is_dir($shareKeyPath) ) {
-                       $result = $view->unlink($shareKeyPath);
-               } else {
-                       $absPath = $view->getLocalFile($shareKeyPath);
-
-                       $matches = glob(preg_quote($absPath).'.*.shareKey' );
 
-                       if ( $matches ) {
+                       $localPath = \OC_Filesystem::normalizePath($view->getLocalFolder($shareKeyPath));
+                       $result = self::recursiveDelShareKeys($localPath, $userIds);
 
-                               foreach ( $matches as $ma ) {
-                                       unlink($ma);
-                               }
+               } else {
 
+                       foreach ($userIds as $userId) {
+                               $view->unlink($shareKeyPath.'.'.$userId.'.shareKey');
                        }
 
                        $result = true;
@@ -432,7 +433,28 @@ class Keymanager {
                return $result;
                
        }
-       
+
+       /**
+        * @brief recursively delete share keys from given users
+        *
+        * @param type $dir directory
+        * @param type $userIds user ids for which the share keys should be deleted
+        */
+       private static function recursiveDelShareKeys($dir, $userIds) {
+               foreach ($userIds as $userId) {
+                       $completePath = $dir.'/.*'.'.'.$userId.'.shareKey';
+                       $matches = glob(preg_quote($dir).'/*'.preg_quote('.'.$userId.'.shareKey'));
+               }
+               foreach ($matches as $ma)  {
+                       unlink($ma);
+               }
+               $subdirs = $directories = glob(preg_quote($dir) . '/*' , GLOB_ONLYDIR);
+               foreach ( $subdirs as $subdir ) {
+                       self::recursiveDelShareKeys($subdir, $userIds);
+               }
+               return $true;
+       }
+
        /**
         * @brief Make preparations to vars and filesystem for saving a keyfile
         */