Skip to content

Commit

Permalink
Fix ACLs on subfolders in Trashbin
Browse files Browse the repository at this point in the history
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
  • Loading branch information
come-nc committed Nov 23, 2023
1 parent f5abcf4 commit 805b5cf
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions lib/Trash/TrashBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function restoreItem(ITrashItem $item) {
if ($node === null) {
throw new NotFoundException();
}
if (!$this->userHasAccessToPath($item->getUser(), $folderId . '/' . $item->getOriginalLocation(), Constants::PERMISSION_UPDATE)) {
if (!$this->userHasAccessToPath($item->getUser(), $folderId, $item->getOriginalLocation(), $item->getPath(), Constants::PERMISSION_UPDATE)) {
throw new NotPermittedException();
}
$folderPermissions = $this->folderManager->getFolderPermissionsForUser($item->getUser(), (int)$folderId);
Expand Down Expand Up @@ -191,7 +191,7 @@ public function removeItem(ITrashItem $item) {
if ($node->getStorage()->unlink($node->getInternalPath()) === false) {
throw new \Exception('Failed to remove item from trashbin');
}
if (!$this->userHasAccessToPath($item->getUser(), $folderId . '/' . $item->getOriginalLocation(), Constants::PERMISSION_DELETE)) {
if (!$this->userHasAccessToPath($item->getUser(), $folderId, $item->getOriginalLocation(), $item->getPath(), Constants::PERMISSION_DELETE)) {
throw new NotPermittedException();
}

Expand Down Expand Up @@ -249,12 +249,16 @@ private function userHasAccessToFolder(IUser $user, int $folderId): bool {

private function userHasAccessToPath(
IUser $user,
int $folderId,
string $originalLocation,
string $path,
int $permission = Constants::PERMISSION_READ
): bool {
$activePermissions = $this->aclManagerFactory->getACLManager($user)
->getACLPermissionsForPath('__groupfolders/' . ltrim($path, '/'));
return (bool)($activePermissions & $permission);
->getACLPermissionsForPath('__groupfolders/' . $folderId . '/' . ltrim($originalLocation, '/'));
$activePermissionsOnTrash = $this->aclManagerFactory->getACLManager($user)
->getACLPermissionsForPath($path);
return (bool)($activePermissions & $activePermissionsOnTrash & $permission);
}

private function getNodeForTrashItem(IUser $user, ITrashItem $trashItem): ?Node {
Expand All @@ -265,7 +269,7 @@ private function getNodeForTrashItem(IUser $user, ITrashItem $trashItem): ?Node
$trashRoot = $this->getTrashFolder((int)$folderId);
try {
$node = $trashRoot->get($path);
if (!$this->userHasAccessToPath($user, $folderId . '/' . $trashItem->getOriginalLocation())) {
if (!$this->userHasAccessToPath($user, $folderId, $trashItem->getOriginalLocation(), $trashItem->getPath())) {
return null;
}
return $node;
Expand Down Expand Up @@ -322,7 +326,7 @@ private function getTrashForFolders(IUser $user, array $folders): array {
$name = $pathParts['filename'];
$key = $folderId . '/' . $name . '/' . $timestamp;
$originalLocation = isset($indexedRows[$key]) ? $indexedRows[$key]['original_location'] : '';
if (!$this->userHasAccessToPath($user, $folderId . '/' . $originalLocation)) {
if (!$this->userHasAccessToPath($user, $folderId, $originalLocation, $item->getPath())) {
continue;
}
$info = $item->getFileInfo();
Expand Down Expand Up @@ -359,8 +363,8 @@ public function getTrashNodeById(IUser $user, int $fileId): ?Node {
$fileId = $trashFolder->get($folderId . "/" . $nameAndTime)->getId();
}
$trashItem = $this->trashManager->getTrashItemByFileId($fileId);
$originalPath = $folderId . '/' . ($trashItem ? $trashItem['original_location'] : '/');
if ($this->userHasAccessToFolder($user, (int)$folderId) && $this->userHasAccessToPath($user, $originalPath)) {
$originalPath = ($trashItem ? $trashItem['original_location'] : '/');
if ($this->userHasAccessToFolder($user, (int)$folderId) && $this->userHasAccessToPath($user, $folderId, $originalPath, $absolutePath)) {
return $trashFolder->get($relativePath);
} else {
return null;
Expand Down

0 comments on commit 805b5cf

Please sign in to comment.