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 <[email protected]>
  • Loading branch information
luka-nextcloud committed Feb 5, 2024
1 parent 3a556ac commit 0828727
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions lib/private/Share20/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,59 @@ protected function deleteChildren(IShare $share) {
return $deletedShares;
}

protected function deleteReshare(IShare $share) {
if ($share->getNodeType() === 'folder' && $share->getShareType() === IShare::TYPE_USER) {
$sharesInFolder = $this->getSharesInFolder($share->getSharedWith(), $share->getNode());

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
];

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);
}
}
}

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_USERGROUP, $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 @@ -1239,6 +1292,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 0828727

Please sign in to comment.