]> source.dussan.org Git - nextcloud-server.git/commitdiff
Added pre_share hook
authorSam Tuke <samtuke@owncloud.com>
Wed, 8 May 2013 14:22:08 +0000 (16:22 +0200)
committerSam Tuke <samtuke@owncloud.com>
Wed, 8 May 2013 14:22:08 +0000 (16:22 +0200)
Switched it for post_share hook in encryption hooks
Stop a file from being shared if the encryption procedure fails for any users

apps/files_encryption/appinfo/app.php
apps/files_encryption/hooks/hooks.php
apps/files_encryption/lib/util.php
lib/public/share.php

index a7253c433324433ca7931f2478ab2159dea0d406..7f01aaeebebe61b54bb3cced427e31a9cfc633e0 100644 (file)
@@ -16,7 +16,7 @@ OCP\Util::connectHook( 'OC_User', 'post_login', 'OCA\Encryption\Hooks', 'login'
 OCP\Util::connectHook( 'OC_User', 'pre_setPassword', 'OCA\Encryption\Hooks', 'setPassphrase' );
 
 // Sharing-related hooks
-OCP\Util::connectHook( 'OCP\Share', 'post_shared', 'OCA\Encryption\Hooks', 'postShared' );
+OCP\Util::connectHook( 'OCP\Share', 'pre_shared', 'OCA\Encryption\Hooks', 'preShared' );
 OCP\Util::connectHook( 'OCP\Share', 'post_unshare', 'OCA\Encryption\Hooks', 'postUnshare' );
 OCP\Util::connectHook( 'OCP\Share', 'post_unshareAll', 'OCA\Encryption\Hooks', 'postUnshareAll' );
 
index ddec839acdc45351cf4bd6bfc8b5cd7704f379c8..1f642f484157fe003681eb12c4945a1cb80ccf3f 100644 (file)
@@ -183,7 +183,7 @@ class Hooks {
        /**\r
         * @brief \r
         */\r
-       public static function postShared( $params ) {\r
+       public static function preShared( $params ) {\r
 \r
                // NOTE: $params has keys:\r
                // [itemType] => file\r
@@ -201,6 +201,7 @@ class Hooks {
                // [fileTarget] => /test8\r
                // [id] => 10\r
                // [token] =>\r
+               // [run] => whether emitting script should continue to run\r
                // TODO: Should other kinds of item be encrypted too?\r
                \r
                if ( $params['itemType'] === 'file' || $params['itemType'] === 'folder' ) {\r
@@ -249,7 +250,7 @@ class Hooks {
                                                // rebuild path\r
                                                foreach ( $targetPathSplit as $pathPart ) {\r
                                                \r
-                                                       if( $pathPart !== $sharedPart ) {\r
+                                                       if ( $pathPart !== $sharedPart ) {\r
                                                                \r
                                                                $path = '/' . $pathPart . $path;\r
                                                                \r
@@ -295,15 +296,13 @@ class Hooks {
                                        $failed[] = $path;\r
                                }\r
                        }\r
-\r
-                       // If no attempts to set keyfiles failed\r
-                       if ( empty( $failed ) ) {\r
                        \r
-                               return true;\r
+                       // If some attempts to set keyfiles failed\r
+                       if ( ! empty( $failed ) ) {\r
                                \r
-                       } else {\r
-                       \r
-                               return false;\r
+                               // Set flag var 'run' to notify emitting \r
+                               // script that hook execution failed\r
+                               $params['run']->run = false;\r
                                \r
                        }\r
                }\r
index 5ab5ea6425f7ed4e3f58b8bac2bab04d3b21803c..871d3bcfc37bde7de7fc9af00d2519dbcf647123 100644 (file)
@@ -146,7 +146,7 @@ class Util {
                        return false;
                        
                } else {
-               
+                       
                        return true;
                        
                }
@@ -855,15 +855,14 @@ class Util {
         * @param string $filePath path of the file to be shared
         */
        public function setSharedFileKeyfiles( Session $session, array $users, $filePath ) {
-       
+               
                // Make sure users are capable of sharing
                $filteredUids = $this->filterShareReadyUsers( $users );
                
                if ( ! empty( $filteredUids['unready'] ) ) {
                
-                       // Notify user of unready userDir
-                       // TODO: Move this out of here; it belongs somewhere else
-                       \OCP\JSON::error();
+                       // TODO: Notify user of unready userDir
+                       \OC_Log::write( 'Encryption library', 'Sharing to these user(s) failed as they are unready for encryption:"'.print_r( $filteredUids['unready'], 1 ), \OC_Log::WARN );
                        
                }
                
index fee906d18776dcf21c8b7aecd935a1bf015a0390..cb151b9f4bf497b4964d3f705f27853f901c4680 100644 (file)
@@ -1223,42 +1223,12 @@ class Share {
                        } else {
                                $groupFileTarget = null;
                        }
-                       $query->execute(array($itemType, $itemSource, $groupItemTarget, $parent, $shareType,
-                               $shareWith['group'], $uidOwner, $permissions, time(), $fileSource, $groupFileTarget, $token));
-                       // Save this id, any extra rows for this group share will need to reference it
-                       $parent = \OC_DB::insertid('*PREFIX*share');
-                       // Loop through all users of this group in case we need to add an extra row
-                       foreach ($shareWith['users'] as $uid) {
-                               $itemTarget = self::generateTarget($itemType, $itemSource, self::SHARE_TYPE_USER, $uid,
-                                       $uidOwner, $suggestedItemTarget, $parent);
-                               if (isset($fileSource)) {
-                                       if ($parentFolder) {
-                                               if ($parentFolder === true) {
-                                                       $fileTarget = self::generateTarget('file', $filePath, self::SHARE_TYPE_USER, $uid,
-                                                               $uidOwner, $suggestedFileTarget, $parent);
-                                                       if ($fileTarget != $groupFileTarget) {
-                                                               $parentFolders[$uid]['folder'] = $fileTarget;
-                                                       }
-                                               } else if (isset($parentFolder[$uid])) {
-                                                       $fileTarget = $parentFolder[$uid]['folder'].$itemSource;
-                                                       $parent = $parentFolder[$uid]['id'];
-                                               }
-                                       } else {
-                                               $fileTarget = self::generateTarget('file', $filePath, self::SHARE_TYPE_USER,
-                                                       $uid, $uidOwner, $suggestedFileTarget, $parent);
-                                       }
-                               } else {
-                                       $fileTarget = null;
-                               }
-                               // Insert an extra row for the group share if the item or file target is unique for this user
-                               if ($itemTarget != $groupItemTarget || (isset($fileSource) && $fileTarget != $groupFileTarget)) {
-                                       $query->execute(array($itemType, $itemSource, $itemTarget, $parent,
-                                               self::$shareTypeGroupUserUnique, $uid, $uidOwner, $permissions, time(),
-                                                       $fileSource, $fileTarget, $token));
-                                       $id = \OC_DB::insertid('*PREFIX*share');
-                               }
-                       }
-                       \OC_Hook::emit('OCP\Share', 'post_shared', array(
+                       // Trigger hooks before the share is added to DB
+                       // Set flag indicating if execution should continue. 
+                       // Use an object as workaround for pass by reference issues
+                       $run = new \stdClass();
+                       $run->run = true;
+                       $params = array(
                                'itemType' => $itemType,
                                'itemSource' => $itemSource,
                                'itemTarget' => $groupItemTarget,
@@ -1270,11 +1240,73 @@ class Share {
                                'fileSource' => $fileSource,
                                'fileTarget' => $groupFileTarget,
                                'id' => $parent,
-                               'token' => $token
-                       ));
-                       if ($parentFolder === true) {
-                               // Return parent folders to preserve file target paths for potential children
-                               return $parentFolders;
+                               'token' => $token, 
+                               'run' => $run
+                       );
+                       $run = \OC_Hook::emit(
+                               'OCP\Share'
+                               , 'pre_shared'
+                               , $params
+                       );
+                       // If hook execution didn't encounter errors
+                       if ( ! $run->run ) {
+                               $message = 'Sharing '.$itemSource.' failed, because pre share hooks failed';
+                               \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
+                               return false;
+                       } else {
+                               $query->execute(array($itemType, $itemSource, $groupItemTarget, $parent, $shareType,
+                                       $shareWith['group'], $uidOwner, $permissions, time(), $fileSource, $groupFileTarget, $token));
+                               // Save this id, any extra rows for this group share will need to reference it
+                               $parent = \OC_DB::insertid('*PREFIX*share');
+                               // Loop through all users of this group in case we need to add an extra row
+                               foreach ($shareWith['users'] as $uid) {
+                                       $itemTarget = self::generateTarget($itemType, $itemSource, self::SHARE_TYPE_USER, $uid,
+                                               $uidOwner, $suggestedItemTarget, $parent);
+                                       if (isset($fileSource)) {
+                                               if ($parentFolder) {
+                                                       if ($parentFolder === true) {
+                                                               $fileTarget = self::generateTarget('file', $filePath, self::SHARE_TYPE_USER, $uid,
+                                                                       $uidOwner, $suggestedFileTarget, $parent);
+                                                               if ($fileTarget != $groupFileTarget) {
+                                                                       $parentFolders[$uid]['folder'] = $fileTarget;
+                                                               }
+                                                       } else if (isset($parentFolder[$uid])) {
+                                                               $fileTarget = $parentFolder[$uid]['folder'].$itemSource;
+                                                               $parent = $parentFolder[$uid]['id'];
+                                                       }
+                                               } else {
+                                                       $fileTarget = self::generateTarget('file', $filePath, self::SHARE_TYPE_USER,
+                                                               $uid, $uidOwner, $suggestedFileTarget, $parent);
+                                               }
+                                       } else {
+                                               $fileTarget = null;
+                                       }
+                                       // Insert an extra row for the group share if the item or file target is unique for this user
+                                       if ($itemTarget != $groupItemTarget || (isset($fileSource) && $fileTarget != $groupFileTarget)) {
+                                               $query->execute(array($itemType, $itemSource, $itemTarget, $parent,
+                                                       self::$shareTypeGroupUserUnique, $uid, $uidOwner, $permissions, time(),
+                                                               $fileSource, $fileTarget, $token));
+                                               $id = \OC_DB::insertid('*PREFIX*share');
+                                       }
+                               }
+                               \OC_Hook::emit('OCP\Share', 'post_shared', array(
+                                       'itemType' => $itemType,
+                                       'itemSource' => $itemSource,
+                                       'itemTarget' => $groupItemTarget,
+                                       'parent' => $parent,
+                                       'shareType' => $shareType,
+                                       'shareWith' => $shareWith['group'],
+                                       'uidOwner' => $uidOwner,
+                                       'permissions' => $permissions,
+                                       'fileSource' => $fileSource,
+                                       'fileTarget' => $groupFileTarget,
+                                       'id' => $parent,
+                                       'token' => $token
+                               ));
+                               if ($parentFolder === true) {
+                                       // Return parent folders to preserve file target paths for potential children
+                                       return $parentFolders;
+                               }
                        }
                } else {
                        $itemTarget = self::generateTarget($itemType, $itemSource, $shareType, $shareWith, $uidOwner,
@@ -1296,10 +1328,14 @@ class Share {
                        } else {
                                $fileTarget = null;
                        }
-                       $query->execute(array($itemType, $itemSource, $itemTarget, $parent, $shareType, $shareWith, $uidOwner,
-                               $permissions, time(), $fileSource, $fileTarget, $token));
-                       $id = \OC_DB::insertid('*PREFIX*share');
-                       \OC_Hook::emit('OCP\Share', 'post_shared', array(
+                       // Trigger hooks before the share is added to DB
+                       // Set flag indicating if execution should continue. 
+                       // Use an object as workaround for pass by reference issues
+                       $run = new \stdClass();
+                       $run->run = true;
+                       // NOTE: [id] isn't included as it's not yet available 
+                       // (hasn't been inserted)
+                       $params = array(
                                'itemType' => $itemType,
                                'itemSource' => $itemSource,
                                'itemTarget' => $itemTarget,
@@ -1310,13 +1346,42 @@ class Share {
                                'permissions' => $permissions,
                                'fileSource' => $fileSource,
                                'fileTarget' => $fileTarget,
-                               'id' => $id,
-                               'token' => $token
-                       ));
-                       if ($parentFolder === true) {
-                               $parentFolders['id'] = $id;
-                               // Return parent folder to preserve file target paths for potential children
-                               return $parentFolders;
+                               'token' => $token, 
+                               'run' => $run
+                       );
+                       \OC_Hook::emit(
+                               'OCP\Share'
+                               , 'pre_shared'
+                               , $params
+                       );
+                       // If hook execution didn't encounter errors
+                       if ( ! $run->run ) {
+                               $message = 'Sharing '.$itemSource.' failed, because pre share hooks failed';
+                               \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
+                               return false;
+                       } else {
+                               $query->execute(array($itemType, $itemSource, $itemTarget, $parent, $shareType, $shareWith, $uidOwner,
+                                       $permissions, time(), $fileSource, $fileTarget, $token));
+                               $id = \OC_DB::insertid('*PREFIX*share');
+                               \OC_Hook::emit('OCP\Share', 'post_shared', array(
+                                       'itemType' => $itemType,
+                                       'itemSource' => $itemSource,
+                                       'itemTarget' => $itemTarget,
+                                       'parent' => $parent,
+                                       'shareType' => $shareType,
+                                       'shareWith' => $shareWith,
+                                       'uidOwner' => $uidOwner,
+                                       'permissions' => $permissions,
+                                       'fileSource' => $fileSource,
+                                       'fileTarget' => $fileTarget,
+                                       'id' => $id,
+                                       'token' => $token
+                               ));
+                               if ($parentFolder === true) {
+                                       $parentFolders['id'] = $id;
+                                       // Return parent folder to preserve file target paths for potential children
+                                       return $parentFolders;
+                               }
                        }
                }
                return true;