From f4ef37285b715a01a7fecdd8f62d44501cbd3c63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Mon, 5 Aug 2019 19:10:49 +0200 Subject: [PATCH 1/2] Emit moveToTrash event only for the deleting user MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- apps/files_trashbin/lib/Storage.php | 15 +++++++-------- apps/files_trashbin/tests/StorageTest.php | 5 ++++- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/apps/files_trashbin/lib/Storage.php b/apps/files_trashbin/lib/Storage.php index 0db634eeb9ea7..9c714d30aef53 100644 --- a/apps/files_trashbin/lib/Storage.php +++ b/apps/files_trashbin/lib/Storage.php @@ -125,10 +125,15 @@ public function rmdir($path) { * @return bool */ protected function shouldMoveToTrash($path) { + $normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path); + $parts = explode('/', $normalized); + if (count($parts) < 4 || !$this->userManager->userExists($parts[1])) { + return false; + } // check if there is a app which want to disable the trash bin for this file $fileId = $this->storage->getCache()->getId($path); - $nodes = $this->rootFolder->getById($fileId); + $nodes = $this->rootFolder->getUserFolder($parts[1])->getById($fileId); foreach ($nodes as $node) { $event = $this->createMoveToTrashEvent($node); $this->eventDispatcher->dispatch('OCA\Files_Trashbin::moveToTrash', $event); @@ -137,13 +142,7 @@ protected function shouldMoveToTrash($path) { } } - $normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path); - $parts = explode('/', $normalized); - if (count($parts) < 4) { - return false; - } - - if ($parts[2] === 'files' && $this->userManager->userExists($parts[1])) { + if ($parts[2] === 'files') { return true; } diff --git a/apps/files_trashbin/tests/StorageTest.php b/apps/files_trashbin/tests/StorageTest.php index 058d64d1aa0db..330b00af20cf1 100644 --- a/apps/files_trashbin/tests/StorageTest.php +++ b/apps/files_trashbin/tests/StorageTest.php @@ -36,6 +36,7 @@ use OCA\Files_Trashbin\Storage; use OCA\Files_Trashbin\Trash\ITrashManager; use OCP\Files\Cache\ICache; +use OCP\Files\Folder; use OCP\Files\IRootFolder; use OCP\Files\Node; use OCP\ILogger; @@ -546,12 +547,14 @@ public function testShouldMoveToTrash($mountPoint, $path, $userExists, $appDisab $eventDispatcher = $this->getMockBuilder(EventDispatcher::class) ->disableOriginalConstructor()->getMock(); $rootFolder = $this->createMock(IRootFolder::class); + $userFolder = $this->createMock(Folder::class); $node = $this->getMockBuilder(Node::class)->disableOriginalConstructor()->getMock(); $trashManager = $this->createMock(ITrashManager::class); $event = $this->getMockBuilder(MoveToTrashEvent::class)->disableOriginalConstructor()->getMock(); $event->expects($this->any())->method('shouldMoveToTrashBin')->willReturn(!$appDisablesTrash); - $rootFolder->expects($this->any())->method('getById')->with($fileID)->willReturn([$node]); + $userFolder->expects($this->any())->method('getById')->with($fileID)->willReturn([$node]); + $rootFolder->expects($this->any())->method('getUserFolder')->willReturn($userFolder); $storage = $this->getMockBuilder(Storage::class) ->setConstructorArgs( From 1db1d2ce9d98edc0303eb69e7aa8187be516a0d0 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Wed, 7 Aug 2019 16:47:43 +0200 Subject: [PATCH 2/2] Use the owner from the storage Signed-off-by: Roeland Jago Douma --- apps/files_trashbin/lib/Storage.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/apps/files_trashbin/lib/Storage.php b/apps/files_trashbin/lib/Storage.php index 9c714d30aef53..502add528398d 100644 --- a/apps/files_trashbin/lib/Storage.php +++ b/apps/files_trashbin/lib/Storage.php @@ -127,13 +127,19 @@ public function rmdir($path) { protected function shouldMoveToTrash($path) { $normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path); $parts = explode('/', $normalized); - if (count($parts) < 4 || !$this->userManager->userExists($parts[1])) { + if (count($parts) < 4) { return false; } // check if there is a app which want to disable the trash bin for this file $fileId = $this->storage->getCache()->getId($path); - $nodes = $this->rootFolder->getUserFolder($parts[1])->getById($fileId); + $owner = $this->storage->getOwner($path); + if ($owner === false) { + $nodes = $this->rootFolder->getById($fileId); + } else { + $nodes = $this->rootFolder->getUserFolder($owner)->getById($fileId); + } + foreach ($nodes as $node) { $event = $this->createMoveToTrashEvent($node); $this->eventDispatcher->dispatch('OCA\Files_Trashbin::moveToTrash', $event); @@ -142,7 +148,7 @@ protected function shouldMoveToTrash($path) { } } - if ($parts[2] === 'files') { + if ($parts[2] === 'files' && $this->userManager->userExists($parts[1])) { return true; }