Skip to content

Commit

Permalink
Merge pull request #28413 from paulijar/stable10-share20-post_unshare…
Browse files Browse the repository at this point in the history
…FromSelf

[stable10] Add emitting of hook post_unshareFromSelf to Share 2.0
  • Loading branch information
Vincent Petry authored Jul 17, 2017
2 parents af4db97 + 5a50013 commit 545a7a1
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 545a7a1

Please sign in to comment.