Skip to content

Commit

Permalink
Added emitting of hook post_unshareFromSelf to Share 2.0
Browse files Browse the repository at this point in the history
- This hook used to be present in Share 1.0 but was missing from 2.0
- Some new key-value pairs have been added to the hook parameter data as compared to 1.0. This is to make the hook more useful and to enable unified handling of the hooks post_unshare and post_unshareFromSelf.
- Fixes owncloud#28337

(cherry picked from commit 9a385ab)
  • Loading branch information
paulijar committed Jul 17, 2017
1 parent af4db97 commit 5a50013
Showing 1 changed file with 37 additions and 30 deletions.
67 changes: 37 additions & 30 deletions lib/private/Share20/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,32 @@ protected function deleteChildren(\OCP\Share\IShare $share) {
return $deletedShares;
}

protected static function formatUnshareHookParams(\OCP\Share\IShare $share) {
// Prepare hook
$shareType = $share->getShareType();
$sharedWith = '';
if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
$sharedWith = $share->getSharedWith();
} else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
$sharedWith = $share->getSharedWith();
} else if ($shareType === \OCP\Share::SHARE_TYPE_REMOTE) {
$sharedWith = $share->getSharedWith();
}

$hookParams = [
'id' => $share->getId(),
'itemType' => $share->getNodeType(),
'itemSource' => $share->getNodeId(),
'shareType' => $shareType,
'shareWith' => $sharedWith,
'itemparent' => method_exists($share, 'getParent') ? $share->getParent() : '',
'uidOwner' => $share->getSharedBy(),
'fileSource' => $share->getNodeId(),
'fileTarget' => $share->getTarget()
];
return $hookParams;
}

/**
* Delete a share
*
Expand All @@ -779,33 +805,7 @@ public function deleteShare(\OCP\Share\IShare $share) {
throw new \InvalidArgumentException('Share does not have a full id');
}

$formatHookParams = function(\OCP\Share\IShare $share) {
// Prepare hook
$shareType = $share->getShareType();
$sharedWith = '';
if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
$sharedWith = $share->getSharedWith();
} else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
$sharedWith = $share->getSharedWith();
} else if ($shareType === \OCP\Share::SHARE_TYPE_REMOTE) {
$sharedWith = $share->getSharedWith();
}

$hookParams = [
'id' => $share->getId(),
'itemType' => $share->getNodeType(),
'itemSource' => $share->getNodeId(),
'shareType' => $shareType,
'shareWith' => $sharedWith,
'itemparent' => method_exists($share, 'getParent') ? $share->getParent() : '',
'uidOwner' => $share->getSharedBy(),
'fileSource' => $share->getNodeId(),
'fileTarget' => $share->getTarget()
];
return $hookParams;
};

$hookParams = $formatHookParams($share);
$hookParams = self::formatUnshareHookParams($share);

// Emit pre-hook
\OC_Hook::emit('OCP\Share', 'pre_unshare', $hookParams);
Expand All @@ -821,9 +821,7 @@ public function deleteShare(\OCP\Share\IShare $share) {
$deletedShares[] = $share;

//Format hook info
$formattedDeletedShares = array_map(function($share) use ($formatHookParams) {
return $formatHookParams($share);
}, $deletedShares);
$formattedDeletedShares = array_map('self::formatUnshareHookParams', $deletedShares);

$hookParams['deletedShares'] = $formattedDeletedShares;

Expand All @@ -846,6 +844,15 @@ public function deleteFromSelf(\OCP\Share\IShare $share, $recipientId) {
$provider = $this->factory->getProvider($providerId);

$provider->deleteFromSelf($share, $recipientId);

// Emit post hook. The parameter data structure is slightly different
// from the post_unshare hook to maintain backward compatibility with
// Share 1.0: the array contains all the key-value pairs from the old
// library plus some new ones.
$hookParams = self::formatUnshareHookParams($share);
$hookParams['itemTarget'] = $hookParams['fileTarget'];
$hookParams['unsharedItems'] = [$hookParams];
\OC_Hook::emit('OCP\Share', 'post_unshareFromSelf', $hookParams);
}

/**
Expand Down

0 comments on commit 5a50013

Please sign in to comment.