Skip to content

Commit

Permalink
check folder permissions when restoring a trashbin item
Browse files Browse the repository at this point in the history
not just the acl permissions

Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 authored and juliusknorr committed Feb 25, 2021
1 parent 1d5a790 commit 8fcff71
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
19 changes: 19 additions & 0 deletions lib/Folder/FolderManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -565,4 +565,23 @@ public function getFoldersForUser(IUser $user, $rootStorageId = 0) {

return array_values($mergedFolders);
}

/**
* @param IUser $user
* @param int $folderId
* @return int
*/
public function getFolderPermissionsForUser(IUser $user, int $folderId): int {
$groups = $this->groupManager->getUserGroupIds($user);
$folders = $this->getFoldersForGroups($groups);

$permissions = 0;
foreach ($folders as $folder) {
if ($folderId === (int)$folder['folder_id']) {
$permissions |= $folder['permissions'];
}
}

return $permissions;
}
}
8 changes: 6 additions & 2 deletions lib/Trash/TrashBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,16 @@ public function listTrashFolder(ITrashItem $trashItem): array {

public function restoreItem(ITrashItem $item) {
$user = $item->getUser();
list(, $folderId) = explode('/', $item->getTrashPath());
[, $folderId] = explode('/', $item->getTrashPath());
$node = $this->getNodeForTrashItem($user, $item);
if ($node === null) {
throw new NotFoundException();
}
if (!$this->userHasAccessToPath($item->getUser(), $folderId . '/' . $item->getOriginalLocation(), Constants::PERMISSION_UPDATE)) {
if (!$this->userHasACLAccessToPath($item->getUser(), $folderId . '/' . $item->getOriginalLocation(), Constants::PERMISSION_UPDATE)) {
throw new NotPermittedException();
}
$folderPermissions = $this->folderManager->getFolderPermissionsForUser($item->getUser(), (int)$folderId);
if (($folderPermissions & Constants::PERMISSION_UPDATE) !== Constants::PERMISSION_UPDATE) {
throw new NotPermittedException();
}

Expand Down
32 changes: 32 additions & 0 deletions tests/Folder/FolderManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,36 @@ public function testGetFoldersForUserMerge() {
]
], $folders);
}

public function testGetFolderPermissionsForUserMerge() {
$db = $this->createMock(IDBConnection::class);
/** @var FolderManager|\PHPUnit_Framework_MockObject_MockObject $manager */
$manager = $this->getMockBuilder(FolderManager::class)
->setConstructorArgs([$db, $this->groupManager, $this->mimeLoader])
->setMethods(['getFoldersForGroups'])
->getMock();

$folder1 = [
'folder_id' => 1,
'mount_point' => 'foo',
'permissions' => 3,
'quota' => 1000
];
$folder2 = [
'folder_id' => 1,
'mount_point' => 'foo',
'permissions' => 8,
'quota' => 1000
];

$manager->expects($this->any())
->method('getFoldersForGroups')
->willReturn([$folder1, $folder2]);

$permissions = $manager->getFolderPermissionsForUser($this->getUser(['g1', 'g2', 'g3']), 1);
$this->assertEquals(11, $permissions);

$permissions = $manager->getFolderPermissionsForUser($this->getUser(['g1', 'g2', 'g3']), 2);
$this->assertEquals(0, $permissions);
}
}

0 comments on commit 8fcff71

Please sign in to comment.