From a6ef25ba08e9f026892a2715af479f0ff1299cce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Schie=C3=9Fle?= Date: Mon, 13 May 2013 14:28:45 +0200 Subject: [PATCH] use preShare hook only to check if all pub keys are available and the postShare hook to finaly update the shareKeys if the file was shared successfully --- apps/files_encryption/hooks/hooks.php | 130 +++++++++++--------------- apps/files_encryption/lib/helper.php | 1 + apps/files_encryption/lib/proxy.php | 38 -------- 3 files changed, 57 insertions(+), 112 deletions(-) diff --git a/apps/files_encryption/hooks/hooks.php b/apps/files_encryption/hooks/hooks.php index 2d48198939..e3890ce1d1 100644 --- a/apps/files_encryption/hooks/hooks.php +++ b/apps/files_encryption/hooks/hooks.php @@ -179,11 +179,40 @@ class Hooks { } } - + + /* + * @brief check if files can be encrypted to every user. + */ + public static function preShared($params) { + + $users = array(); + $view = new \OC\Files\View('/public-keys/'); + + switch ($params['shareType']) { + case \OCP\Share::SHARE_TYPE_USER: + $users[] = $params['shareWith']; + break; + case \OCP\Share::SHARE_TYPE_GROUP: + $users = \OC_Group::usersInGroup($params['shareWith']); + break; + } + + foreach ($users as $user) { + if (!$view->file_exists($user . '.public.key')) { + // Set flag var 'run' to notify emitting + // script that hook execution failed + $params['run']->run = false; + // TODO: Make sure files_sharing provides user + // feedback on failed share + break; + } + } + } + /** * @brief */ - public static function preShared( $params ) { + public static function postShared($params) { // NOTE: $params has keys: // [itemType] => file @@ -203,29 +232,28 @@ class Hooks { // [token] => // [run] => whether emitting script should continue to run // TODO: Should other kinds of item be encrypted too? - - if ( $params['itemType'] === 'file' || $params['itemType'] === 'folder' ) { - $view = new \OC_FilesystemView( '/' ); + if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') { + + $view = new \OC_FilesystemView('/'); $session = new Session($view); $userId = \OCP\User::getUser(); $util = new Util($view, $userId); - $path = $util->fileIdToPath( $params['itemSource'] ); + $path = $util->fileIdToPath($params['itemSource']); //if parent is set, then this is a re-share action - if( $params['parent'] ) { + if ($params['parent']) { // get the parent from current share - $parent = $util->getShareParent( $params['parent'] ); + $parent = $util->getShareParent($params['parent']); // if parent is file the it is an 1:1 share - if($parent['item_type'] === 'file') { - - // prefix path with Shared - $path = '/Shared'.$parent['file_target']; + if ($parent['item_type'] === 'file') { + // prefix path with Shared + $path = '/Shared' . $parent['file_target']; } else { - + // NOTE: parent is folder but shared was a file! // we try to rebuild the missing path // some examples we face here @@ -237,38 +265,29 @@ class Hooks { // so our path should be // /Shared/subfolder1/subsubfolder1/somefile.txt // while user3 is sharing - - if ( $params['itemType'] === 'file' ) { + + if ($params['itemType'] === 'file') { // get target path - $targetPath = $util->fileIdToPath( $params['fileSource'] ); - $targetPathSplit = array_reverse( explode( '/', $targetPath ) ); + $targetPath = $util->fileIdToPath($params['fileSource']); + $targetPathSplit = array_reverse(explode('/', $targetPath)); // init values $path = ''; - $sharedPart = ltrim( $parent['file_target'], '/' ); + $sharedPart = ltrim($parent['file_target'], '/'); // rebuild path - foreach ( $targetPathSplit as $pathPart ) { - - if ( $pathPart !== $sharedPart ) { - + foreach ($targetPathSplit as $pathPart) { + if ($pathPart !== $sharedPart) { $path = '/' . $pathPart . $path; - } else { - break; - } - } - // prefix path with Shared - $path = '/Shared'.$parent['file_target'].$path; - + $path = '/Shared' . $parent['file_target'] . $path; } else { - // prefix path with Shared - $path = '/Shared'.$parent['file_target'].$params['fileTarget']; + $path = '/Shared' . $parent['file_target'] . $params['fileTarget']; } } } @@ -276,52 +295,15 @@ class Hooks { $sharingEnabled = \OCP\Share::isEnabled(); // if a folder was shared, get a list if all (sub-)folders - if ( $params['itemType'] === 'folder' ) { - - $allFiles = $util->getAllFiles( $path ); - + if ($params['itemType'] === 'folder') { + $allFiles = $util->getAllFiles($path); } else { - - $allFiles = array( $path ); - + $allFiles = array($path); } - - // Set array for collecting paths which can't be shared - $failed = array(); - foreach ( $allFiles as $path ) { - - $usersSharing = $util->getSharingUsersArray( $sharingEnabled, $path ); - - // check if we share to a group - if($params['shareType'] === \OCP\Share::SHARE_TYPE_GROUP) { - $usersSharing[] = reset(\OC_Group::usersInGroup($params['shareWith'])); - // check if we share with link - } else if($params['shareType'] === \OCP\Share::SHARE_TYPE_LINK) { - $usersSharing[] = 'owncloud'; - } else { - // Because this is a pre_share hook, the user - // being shared to is not yet included; add them - $usersSharing[] = $params['shareWith']; - } - - - // Attempt to set shareKey - if ( ! $util->setSharedFileKeyfiles( $session, $usersSharing, $path ) ) { - - $failed[] = $path; - } - } - - // If some attempts to set keyfiles failed - if ( ! empty( $failed ) ) { - - // Set flag var 'run' to notify emitting - // script that hook execution failed - $params['run']->run = false; - // TODO: Make sure files_sharing provides user - // feedback on failed share - + foreach ($allFiles as $path) { + $usersSharing = $util->getSharingUsersArray($sharingEnabled, $path); + $util->setSharedFileKeyfiles( $session, $usersSharing, $path ); } } } diff --git a/apps/files_encryption/lib/helper.php b/apps/files_encryption/lib/helper.php index b294a71ec1..9b8d9ffc5b 100755 --- a/apps/files_encryption/lib/helper.php +++ b/apps/files_encryption/lib/helper.php @@ -35,6 +35,7 @@ class Helper { public static function registerShareHooks() { \OCP\Util::connectHook( 'OCP\Share', 'pre_shared', 'OCA\Encryption\Hooks', 'preShared' ); + \OCP\Util::connectHook( 'OCP\Share', 'post_shared', 'OCA\Encryption\Hooks', 'postShared' ); \OCP\Util::connectHook( 'OCP\Share', 'post_unshare', 'OCA\Encryption\Hooks', 'postUnshare' ); \OCP\Util::connectHook( 'OCP\Share', 'post_unshareAll', 'OCA\Encryption\Hooks', 'postUnshareAll' ); } diff --git a/apps/files_encryption/lib/proxy.php b/apps/files_encryption/lib/proxy.php index 3f8b857125..36d05d7e0f 100644 --- a/apps/files_encryption/lib/proxy.php +++ b/apps/files_encryption/lib/proxy.php @@ -164,45 +164,7 @@ class Proxy extends \OC_FileProxy { return true; } - - public function postFile_put_contents( $path, $length ) { - - $userId = \OCP\USER::getUser(); - $view = new \OC_FilesystemView( '/' ); - $util = new Util( $view, $userId ); - - // Check if recoveryAdmin is enabled for system and user - // TODO: Consider storing recoveryAdmin status for user in session - if ( - \OC_Appconfig::getValue( 'files_encryption', 'recoveryAdminEnabled' ) - && $util->recoveryEnabledForUser() - ) { - - // Get owner UID and filepath - list( $owner, $ownerPath ) = $util->getUidAndFilename( $path ); - $recoveryAdminUid = \OC_Appconfig::getValue( 'files_encryption', 'recoveryAdminEnabled' ); - $usersSharing = \OCP\Share::getUsersSharingFile( $ownerPath, $owner,true, true, true ); - - // Check if file is already shared to recoveryAdmin - if ( ! in_array( $recoveryAdminUid, $usersSharing ) ) { - - $relPath = $util->stripFilesPath( $path ); - - // Get file info from filecache - $fileInfo = \OC\Files\Filesystem::getFileInfo( $path ); - - // Register share to recoveryAdmin with share API - // FIXME: Some of these vars aren't set - // FIXME: What should the permission number be to grant all rights? -// \OCP\Share::shareItem( $itemType, $itemSource, 0, $recoveryAdminUid, 17 ); - - } - - } - - } - /** * @param string $path Path of file from which has been read * @param string $data Data that has been read from file