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
This commit is contained in:
parent
8e004cc3e1
commit
a6ef25ba08
|
@ -180,10 +180,39 @@ 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
|
* @brief
|
||||||
*/
|
*/
|
||||||
public static function preShared( $params ) {
|
public static function postShared($params) {
|
||||||
|
|
||||||
// NOTE: $params has keys:
|
// NOTE: $params has keys:
|
||||||
// [itemType] => file
|
// [itemType] => file
|
||||||
|
@ -204,26 +233,25 @@ class Hooks {
|
||||||
// [run] => whether emitting script should continue to run
|
// [run] => whether emitting script should continue to run
|
||||||
// TODO: Should other kinds of item be encrypted too?
|
// TODO: Should other kinds of item be encrypted too?
|
||||||
|
|
||||||
if ( $params['itemType'] === 'file' || $params['itemType'] === 'folder' ) {
|
if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
|
||||||
|
|
||||||
$view = new \OC_FilesystemView( '/' );
|
$view = new \OC_FilesystemView('/');
|
||||||
$session = new Session($view);
|
$session = new Session($view);
|
||||||
$userId = \OCP\User::getUser();
|
$userId = \OCP\User::getUser();
|
||||||
$util = new Util($view, $userId);
|
$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 parent is set, then this is a re-share action
|
||||||
if( $params['parent'] ) {
|
if ($params['parent']) {
|
||||||
|
|
||||||
// get the parent from current share
|
// 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 is file the it is an 1:1 share
|
||||||
if($parent['item_type'] === 'file') {
|
if ($parent['item_type'] === 'file') {
|
||||||
|
|
||||||
// prefix path with Shared
|
// prefix path with Shared
|
||||||
$path = '/Shared'.$parent['file_target'];
|
$path = '/Shared' . $parent['file_target'];
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
// NOTE: parent is folder but shared was a file!
|
// NOTE: parent is folder but shared was a file!
|
||||||
|
@ -238,37 +266,28 @@ class Hooks {
|
||||||
// /Shared/subfolder1/subsubfolder1/somefile.txt
|
// /Shared/subfolder1/subsubfolder1/somefile.txt
|
||||||
// while user3 is sharing
|
// while user3 is sharing
|
||||||
|
|
||||||
if ( $params['itemType'] === 'file' ) {
|
if ($params['itemType'] === 'file') {
|
||||||
// get target path
|
// get target path
|
||||||
$targetPath = $util->fileIdToPath( $params['fileSource'] );
|
$targetPath = $util->fileIdToPath($params['fileSource']);
|
||||||
$targetPathSplit = array_reverse( explode( '/', $targetPath ) );
|
$targetPathSplit = array_reverse(explode('/', $targetPath));
|
||||||
|
|
||||||
// init values
|
// init values
|
||||||
$path = '';
|
$path = '';
|
||||||
$sharedPart = ltrim( $parent['file_target'], '/' );
|
$sharedPart = ltrim($parent['file_target'], '/');
|
||||||
|
|
||||||
// rebuild path
|
// rebuild path
|
||||||
foreach ( $targetPathSplit as $pathPart ) {
|
foreach ($targetPathSplit as $pathPart) {
|
||||||
|
if ($pathPart !== $sharedPart) {
|
||||||
if ( $pathPart !== $sharedPart ) {
|
|
||||||
|
|
||||||
$path = '/' . $pathPart . $path;
|
$path = '/' . $pathPart . $path;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// prefix path with Shared
|
// prefix path with Shared
|
||||||
$path = '/Shared'.$parent['file_target'].$path;
|
$path = '/Shared' . $parent['file_target'] . $path;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
// prefix path with Shared
|
// 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();
|
$sharingEnabled = \OCP\Share::isEnabled();
|
||||||
|
|
||||||
// if a folder was shared, get a list if all (sub-)folders
|
// if a folder was shared, get a list if all (sub-)folders
|
||||||
if ( $params['itemType'] === 'folder' ) {
|
if ($params['itemType'] === 'folder') {
|
||||||
|
$allFiles = $util->getAllFiles($path);
|
||||||
$allFiles = $util->getAllFiles( $path );
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
$allFiles = array($path);
|
||||||
$allFiles = array( $path );
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set array for collecting paths which can't be shared
|
foreach ($allFiles as $path) {
|
||||||
$failed = array();
|
$usersSharing = $util->getSharingUsersArray($sharingEnabled, $path);
|
||||||
|
$util->setSharedFileKeyfiles( $session, $usersSharing, $path );
|
||||||
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
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,6 +35,7 @@ class Helper {
|
||||||
public static function registerShareHooks() {
|
public static function registerShareHooks() {
|
||||||
|
|
||||||
\OCP\Util::connectHook( 'OCP\Share', 'pre_shared', 'OCA\Encryption\Hooks', 'preShared' );
|
\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_unshare', 'OCA\Encryption\Hooks', 'postUnshare' );
|
||||||
\OCP\Util::connectHook( 'OCP\Share', 'post_unshareAll', 'OCA\Encryption\Hooks', 'postUnshareAll' );
|
\OCP\Util::connectHook( 'OCP\Share', 'post_unshareAll', 'OCA\Encryption\Hooks', 'postUnshareAll' );
|
||||||
}
|
}
|
||||||
|
|
|
@ -165,44 +165,6 @@ class Proxy extends \OC_FileProxy {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 $path Path of file from which has been read
|
||||||
* @param string $data Data that has been read from file
|
* @param string $data Data that has been read from file
|
||||||
|
|
Loading…
Reference in New Issue