Skip to content

Commit

Permalink
feat: delete re-shares when deleting the parent share
Browse files Browse the repository at this point in the history
Signed-off-by: Luka Trovic <luka@nextcloud.com>
  • Loading branch information
luka-nextcloud committed Apr 1, 2024
1 parent a40838b commit 007d650
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions lib/private/Share20/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,71 @@ protected function deleteChildren(IShare $share) {
return $deletedShares;
}

protected function deleteReshare(IShare $share) {
// If the user has another shares, we don't delete the shares by this user
if ($share->getShareType() === IShare::TYPE_USER) {
$groupShares = $this->getSharedWith($share->getSharedWith(), IShare::TYPE_GROUP, $share->getNode(), -1, 0);

if (count($groupShares) !== 0) {
return;
}
}

// Delete re-share records (shared by "share with user") inside folder
if ($share->getNodeType() === 'folder' && $share->getShareType() === IShare::TYPE_USER) {
$sharesInFolder = $this->getSharesInFolder($share->getSharedWith(), $share->getNode(), true, false);

foreach ($sharesInFolder as $nodeId => $shares) {
foreach ($shares as $child) {
$this->deleteShare($child);
}
}
}

$shareTypes = [
IShare::TYPE_GROUP,
IShare::TYPE_USER,
IShare::TYPE_LINK,
IShare::TYPE_REMOTE,
IShare::TYPE_EMAIL
];

// Delete re-share records which shared by "share with user"
if ($share->getShareType() === IShare::TYPE_USER || $share->getShareType() === IShare::TYPE_USERGROUP) {
foreach ($shareTypes as $shareType) {
$provider = $this->factory->getProviderForType($shareType);
$shares = $provider->getSharesBy($share->getSharedWith(), $shareType, $share->getNode(), false, -1, 0);
foreach ($shares as $child) {
$this->deleteShare($child);
}
}
}

// Delete re-share records which shared by users in "share with group"
if ($share->getShareType() === IShare::TYPE_GROUP) {
$group = $this->groupManager->get($share->getSharedWith());
$users = $group->getUsers();

foreach ($users as $user) {
$anotherShares = $this->getSharedWith($user->getUID(), IShare::TYPE_USER, $share->getNode(), -1, 0);
$groupShares = $this->getSharedWith($user->getUID(), IShare::TYPE_GROUP, $share->getNode(), -1, 0);

// If the user has another shares, we don't delete the shares by this user
if (count($anotherShares) !== 0 || count($groupShares) > 1) {
continue;
}

foreach ($shareTypes as $shareType) {
$provider = $this->factory->getProviderForType($shareType);
$shares = $provider->getSharesBy($user->getUID(), $shareType, $share->getNode(), false, -1, 0);
foreach ($shares as $child) {
$this->deleteShare($child);
}
}
}
}
}

/**
* Delete a share
*
Expand All @@ -1249,6 +1314,9 @@ public function deleteShare(IShare $share) {

$this->dispatcher->dispatchTyped(new BeforeShareDeletedEvent($share));

// Delete shares that shared by the "share with user/group"
$this->deleteReshare($share);

// Get all children and delete them as well
$this->deleteChildren($share);

Expand Down

0 comments on commit 007d650

Please sign in to comment.