From aae9b0b1bfc95d60bcc7c4a4b85a387a94ac9caa Mon Sep 17 00:00:00 2001 From: Sam Tuke Date: Tue, 26 Feb 2013 18:33:31 +0000 Subject: [PATCH] Started work on post unshare hook Development snapshot --- apps/files_encryption/hooks/hooks.php | 116 ++++++++++++++++------- apps/files_encryption/lib/keymanager.php | 24 +++++ apps/files_encryption/lib/util.php | 3 +- 3 files changed, 108 insertions(+), 35 deletions(-) diff --git a/apps/files_encryption/hooks/hooks.php b/apps/files_encryption/hooks/hooks.php index 6d982b2c3b..bf16a492e3 100644 --- a/apps/files_encryption/hooks/hooks.php +++ b/apps/files_encryption/hooks/hooks.php @@ -167,44 +167,60 @@ class Hooks { * @brief get all users with access to the file and encrypt the file key to each of them */ public static function postShared( $params ) { - - // NOTE: $params is an array with these keys: + + // NOTE: $params has keys: + // [itemType] => file // itemSource -> int, filecache file ID + // [parent] => + // [itemTarget] => /13 // shareWith -> string, uid of user being shared to // fileTarget -> path of file being shared // uidOwner -> owner of the original file being shared + // [shareType] => 0 + // [shareWith] => test1 + // [uidOwner] => admin + // [permissions] => 17 + // [fileSource] => 13 + // [fileTarget] => /test8 + // [id] => 10 + // [token] => - $view = new \OC_FilesystemView( '/' ); - $session = new Session(); - $userId = \OCP\User::getUser(); - $util = new Util( $view, $userId ); - $path = $util->fileIdToPath( $params['itemSource'] ); + // TODO: Should other kinds of item be encrypted too? + if ( $params['itemType'] === 'file' ) { - $usersSharing = \OCP\Share::getUsersSharingFile( $path, true ); - - $allPaths = $util->getPaths( $path ); - - $failed = array(); - - foreach ( $allPaths as $path ) { - - if ( ! $util->setSharedFileKeyfiles( $session, $usersSharing, $path ) ) { + $view = new \OC_FilesystemView( '/' ); + $session = new Session(); + $userId = \OCP\User::getUser(); + $util = new Util( $view, $userId ); + $path = $util->fileIdToPath( $params['itemSource'] ); - $failed[] = $path; + $usersSharing = \OCP\Share::getUsersSharingFile( $path, true ); + + $allPaths = $util->getPaths( $path ); + + $failed = array(); + + foreach ( $allPaths as $path ) { + + if ( ! $util->setSharedFileKeyfiles( $session, $usersSharing, $path ) ) { + + $failed[] = $path; + + } } - } - - // If no attempts to set keyfiles failed - if ( empty( $failed ) ) { - - return true; + // If no attempts to set keyfiles failed + if ( empty( $failed ) ) { - } else { - - return false; + return true; + + } else { + return false; + + } + } } @@ -213,15 +229,47 @@ class Hooks { * @brief */ public static function postUnshare( $params ) { - -// $view = new \OC_FilesystemView( '/' ); -// $session = new Session(); -// $userId = \OCP\User::getUser(); -// $util = new Util( $view, $userId ); -// $path = $util->fileIdToPath( $params['itemSource'] ); -// -// return Crypt::updateKeyfile( $view, $util, $session, $userId, $path ); + // NOTE: $params has keys: + // [itemType] => file + // [itemSource] => 13 + // [shareType] => 0 + // [shareWith] => test1 + + // TODO: Should other kinds of item be encrypted too? + if ( $params['itemType'] === 'file' ) { + + $view = new \OC_FilesystemView( '/' ); + $session = new Session(); + $userId = \OCP\User::getUser(); + $util = new Util( $view, $userId ); + $path = $util->fileIdToPath( $params['itemSource'] ); + + $allPaths = $util->getPaths( $path ); + + foreach ( $allPaths as $path ) { + + if ( ! Keymanager::delShareKey( $view, $userId, $path ) ) { + + $failed[] = $path; + + } + + } + + // If no attempts to set keyfiles failed + if ( empty( $failed ) ) { + + return true; + + } else { + + return false; + + } + + } + } /** diff --git a/apps/files_encryption/lib/keymanager.php b/apps/files_encryption/lib/keymanager.php index ec4057d098..22e2ffa500 100755 --- a/apps/files_encryption/lib/keymanager.php +++ b/apps/files_encryption/lib/keymanager.php @@ -350,6 +350,30 @@ class Keymanager { } + /** + * @brief Delete a single user's shareKey for a single file + */ + public static function delShareKey( \OC_FilesystemView $view, $userId, $filePath ) { + + $trimmed = ltrim( $filePath, '/' ); + $shareKeyPath = '/' . $userId . '/files_encryption/share-keys/' . $trimmed . '.shareKey'; + + // Unlink doesn't tell us if file was deleted (not found returns + // true), so we perform our own test + if ( $view->file_exists( $shareKeyPath ) ) { + + return $view->unlink( $shareKeyPath ); + + } else { + + \OC_Log::write( 'Encryption library', 'Could not delete shareKey; does not exist: "' . $shareKeyPath, \OC_Log::ERROR ); + + return false; + + } + + } + /** * @brief Make preparations to vars and filesystem for saving a keyfile */ diff --git a/apps/files_encryption/lib/util.php b/apps/files_encryption/lib/util.php index 920ff3eb15..02c62e160c 100644 --- a/apps/files_encryption/lib/util.php +++ b/apps/files_encryption/lib/util.php @@ -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 ) {