From 5610429a02352df36e2e64f25e0a8af474915268 Mon Sep 17 00:00:00 2001 From: Florin Peter Date: Sun, 5 May 2013 23:41:42 +0200 Subject: [PATCH] handling for re-share and re-unshare should work now --- apps/files_encryption/hooks/hooks.php | 132 +++++++++++++++----------- apps/files_encryption/lib/util.php | 48 +++++++++- 2 files changed, 120 insertions(+), 60 deletions(-) diff --git a/apps/files_encryption/hooks/hooks.php b/apps/files_encryption/hooks/hooks.php index 0633a81a057..a72d339277a 100644 --- a/apps/files_encryption/hooks/hooks.php +++ b/apps/files_encryption/hooks/hooks.php @@ -23,10 +23,11 @@ namespace OCA\Encryption; +use OC\Files\Filesystem; + /** * Class for hook specific logic */ - class Hooks { // TODO: use passphrase for encrypting private key that is separate to @@ -253,10 +254,9 @@ class Hooks { } else { // prefix path with Shared - $path = '/Shared'.$parent['file_target']; + $path = '/Shared'.$parent['file_target'].$params['fileTarget']; } } - } $sharingEnabled = \OCP\Share::isEnabled(); @@ -282,10 +282,8 @@ class Hooks { // If no attempts to set keyfiles failed if (empty($failed)) { - return true; } else { - return false; } } @@ -294,73 +292,91 @@ class Hooks { /** * @brief */ - public static function postUnshare( $params ) { - - // NOTE: $params has keys: - // [itemType] => file - // [itemSource] => 13 - // [shareType] => 0 - // [shareWith] => test1 - - 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'] ); + public static function postUnshare($params) + { - // for group shares get a list of the group members - if ($params['shareType'] == \OCP\Share::SHARE_TYPE_GROUP) { - $userIds = \OC_Group::usersInGroup($params['shareWith']); - } else { - $userIds = array($params['shareWith']); - } + // NOTE: $params has keys: + // [itemType] => file + // [itemSource] => 13 + // [shareType] => 0 + // [shareWith] => test1 + // [itemParent] => - // if we unshare a folder we need a list of all (sub-)files - if ($params['itemType'] === 'folder') { - $allFiles = $util->getAllFiles($path); - } else { - $allFiles = array($path); - } + if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') { + + $view = new \OC_FilesystemView('/'); + $userId = \OCP\User::getUser(); + $util = new Util($view, $userId); + $path = $util->fileIdToPath($params['itemSource']); + // check if this is a re-share + if ($params['itemParent']) { + + // get the parent from current share + $parent = $util->getShareParent($params['itemParent']); - foreach ( $allFiles as $path ) { + // get target path + $targetPath = $util->fileIdToPath($params['itemSource']); + $targetPathSplit = array_reverse(explode('/', $targetPath)); - // check if the user still has access to the file, otherwise delete share key - $sharingUsers = $util->getSharingUsersArray(true, $path); + // init values + $path = ''; + $sharedPart = ltrim($parent['file_target'], '/'); + + // rebuild path + foreach ($targetPathSplit as $pathPart) { + if ($pathPart !== $sharedPart) { + $path = '/' . $pathPart . $path; + } else { + break; + } + } + + // prefix path with Shared + $path = '/Shared' . $parent['file_target'] . $path; + } + + // for group shares get a list of the group members + if ($params['shareType'] == \OCP\Share::SHARE_TYPE_GROUP) { + $userIds = \OC_Group::usersInGroup($params['shareWith']); + } else { + $userIds = array($params['shareWith']); + } + + // if we unshare a folder we need a list of all (sub-)files + if ($params['itemType'] === 'folder') { + $allFiles = $util->getAllFiles($path); + } else { + $allFiles = array($path); + } + + foreach ($allFiles as $path) { + + // check if the user still has access to the file, otherwise delete share key + $sharingUsers = $util->getSharingUsersArray(true, $path); // Unshare every user who no longer has access to the file - //TODO: does not work properly atm - $delUsers = array_diff($userIds, $sharingUsers); - if ( ! Keymanager::delShareKey( $view, $delUsers, $path ) ) { - - $failed[] = $path; - - } - - } - - // If no attempts to set keyfiles failed - if ( empty( $failed ) ) { - - return true; - - } else { - - return false; - - } + $delUsers = array_diff($userIds, $sharingUsers); + if (!Keymanager::delShareKey($view, $delUsers, $path)) { + $failed[] = $path; + } - } + } - } + // If no attempts to set keyfiles failed + if (empty($failed)) { + return true; + } else { + return false; + } + } + } /** * @brief */ public static function postUnshareAll( $params ) { - + // NOTE: It appears that this is never called for files, so // we may not need to implement it diff --git a/apps/files_encryption/lib/util.php b/apps/files_encryption/lib/util.php index 91502af4a3f..2e8e2af683b 100644 --- a/apps/files_encryption/lib/util.php +++ b/apps/files_encryption/lib/util.php @@ -1102,10 +1102,26 @@ class Util { } foreach ($content as $c) { + $sharedPart = $path_split[sizeof($path_split)-1]; + $targetPathSplit = array_reverse(explode('/', $c['path'])); + + $path = ''; + + // rebuild path + foreach ($targetPathSplit as $pathPart) { + if($pathPart !== $sharedPart) { + $path = '/'.$pathPart.$path; + } else { + break; + } + } + + $path = $dir.$path; + if ($c['type'] === "dir" ) { - $result = array_merge($result, $this->getAllFiles($shared.substr($c['path'],5))); + $result = array_merge($result, $this->getAllFiles($path)); } else { - $result[] = $shared.substr($c['path'], 5); + $result[] = $path; } } return $result; @@ -1130,4 +1146,32 @@ class Util { } + /** + * @brief get owner of the shared files. + * @param int $Id of a share + * @return owner + */ + public function getOwnerFromSharedFile($id) { + $query = \OC_DB::prepare('SELECT `parent`, `uid_owner` FROM `*PREFIX*share` WHERE `id` = ?', 1); + $source = $query->execute(array($id))->fetchRow(); + + if (isset($source['parent'])) { + $parent = $source['parent']; + while (isset($parent)) { + $query = \OC_DB::prepare('SELECT `parent`, `uid_owner` FROM `*PREFIX*share` WHERE `id` = ?', 1); + $item = $query->execute(array($parent))->fetchRow(); + if (isset($item['parent'])) { + $parent = $item['parent']; + } else { + $fileOwner = $item['uid_owner']; + break; + } + } + } else { + $fileOwner = $source['uid_owner']; + } + + return $fileOwner; + } + } -- 2.39.5