take group shares into account if we retrieve the list a all recipients

This commit is contained in:
Björn Schießle 2013-02-12 12:08:34 +01:00
parent 5a64c96d06
commit 8eef919a75
3 changed files with 65 additions and 69 deletions

View file

@ -167,43 +167,20 @@ class Hooks {
* @brief
*/
public static function postShared( $params ) {
error_log("post shared triggered!");
// NOTE: $params is an array with these keys:
// itemSource -> int, filecache file ID
// shareWith -> string, uid of user being shared to
// fileTarget -> path of file being shared
// uidOwner -> owner of the original file being shared
//TODO: We don't deal with shared folder yet, need to recursively update every file in the folder
$view = new \OC_FilesystemView( '/' );
$userId = \OCP\User::getUser();
$util = new Util( $view, $userId );
$shares = \OCP\Share::getUsersSharingFile( $params['fileTarget'], 1 );
$userIds = array();
foreach ( $shares as $share ) {
$util = new Util( $view, $share['userId'] );
// Check that the user is encryption capable
// TODO create encryption key when user gets created
if ( $util->ready() ) {
// Construct array of just UIDs for Keymanager{}
$userIds[] = $share['userId'];
} else {
// Log warning; we can't do necessary setup here
// because we don't have the user passphrase
// TODO: Provide user feedback indicating that
// sharing failed
\OC_Log::write( 'Encryption library', 'File cannot be shared: user "'.$share['userId'].'" is not setup for encryption', \OC_Log::WARN );
}
}
$shares = \OCP\Share::getUsersSharingFile( $params['itemSource'], 1 );
return Crypt::encKeyfileToMultipleUsers($shares, $params['fileTarget']);
@ -213,11 +190,11 @@ class Hooks {
* @brief
*/
public static function preUnshare( $params ) {
$items = \OCP\Share::getItemSharedWithBySource($params['itemType'], $params['itemSource']);
$shares = \OCP\Share::getUsersSharingFile( $item[0]['file_target'], 1 );
$shares = \OCP\Share::getUsersSharingFile( $params['itemSource'], 1 );
$userIds = array();
foreach ( $shares as $share ) {
error_log("keek user id: " . $share['userId']);
$userIds[] = $share['userId'];
}

View file

@ -752,16 +752,40 @@ class Crypt {
*/
public static function encKeyfileToMultipleUsers($users, $fileTarget) {
$view = new \OC_FilesystemView( '/' );
$userId = \OCP\User::getUser();
$owner = \OCP\User::getUser();
$util = new Util( $view, $userId );
$session = new Session();
$userPubKeys = Keymanager::getPublicKeys( $view, $users );
$userIds = array();
foreach ( $users as $user ) {
$util = new Util( $view, $user );
// Check that the user is encryption capable
if ( $util->ready() ) {
// Construct array of just UIDs for Keymanager{}
$userIds[] = $user;
} else {
// Log warning; we can't do necessary setup here
// because we don't have the user passphrase
// TODO: Provide user feedback indicating that
// sharing failed
\OC_Log::write( 'Encryption library', 'File cannot be shared: user "'.$user.'" is not setup for encryption', \OC_Log::WARN );
}
}
$userPubKeys = Keymanager::getPublicKeys( $view, $userIds );
\OC_FileProxy::$enabled = false;
// get the keyfile
$encKeyfile = Keymanager::getFileKey( $view, $userId, $fileTarget );
$encKeyfile = Keymanager::getFileKey( $view, $owner, $fileTarget );
$privateKey = $session->getPrivateKey();

View file

@ -149,62 +149,57 @@ class Share {
* @note $path needs to be relative to user data dir, e.g. 'file.txt'
* not '/admin/data/file.txt'
*/
public static function getUsersSharingFile( $path, $includeOwner = 0 ) {
$fPath = self::prepFileTarget( $path );
public static function getUsersSharingFile( $source, $includeOwner = 0 ) {
//TODO get also the recipients from folders which are shared above the current file
// Fetch all shares of this file path from DB
$query = \OC_DB::prepare(
'SELECT
share_type
, share_with
, uid_owner
, permissions
'SELECT share_with
FROM
`*PREFIX*share`
WHERE
file_target = ?'
item_source = ? AND share_type = ? AND uid_owner = ?'
);
$result = $query->execute( array( $fPath ) );
$result = $query->execute( array( $source, self::SHARE_TYPE_USER, \OCP\User::getUser() ) );
if ( \OC_DB::isError( $result ) ) {
\OC_Log::write( 'OCP\Share', \OC_DB::getErrorMessage($result) . ', path=' . $fPath, \OC_Log::ERROR );
\OC_Log::write( 'OCP\Share', \OC_DB::getErrorMessage($result), \OC_Log::ERROR );
}
$shares = array();
while( $row = $result->fetchRow() ) {
$shares[] = $row['share_with'];
}
// Set helpful array keys
$shares[] = array(
'userId' => $row['share_with']
, 'owner' => $row['uid_owner'] // we just set this so it can be used once, hugly hack :/
, 'shareType' => $row['share_type']
, 'permissions' => $row['permissions']
);
// We also need to take group shares into account
$query = \OC_DB::prepare(
'SELECT share_with
FROM
`*PREFIX*share`
WHERE
item_source = ? AND share_type = ? AND uid_owner = ?'
);
$result = $query->execute( array( $source, self::SHARE_TYPE_GROUP, \OCP\User::getUser() ) );
if ( \OC_DB::isError( $result ) ) {
\OC_Log::write( 'OCP\Share', \OC_DB::getErrorMessage($result), \OC_Log::ERROR );
}
while( $row = $result->fetchRow() ) {
$usersInGroup = \OC_Group::usersInGroup($row['share_with']);
$shares = array_merge($shares, $usersInGroup);
}
if ( ! empty( $shares ) ) {
// Include owner in list of users, if requested
if ( $includeOwner == 1 ) {
// NOTE: The values are incorrect for shareType and
// permissions of the owner; we just include them for
// optional convenience
$shares[] = array(
'userId' => $shares[0]['owner']
, 'shareType' => 0
, 'permissions' => 0
);
$shares[] = \OCP\User::getUser();
}
return $shares;
return array_unique($shares);
} else {