From 754b819f8c457974d79183cef60f812916e64976 Mon Sep 17 00:00:00 2001 From: Maksim Sukharev Date: Tue, 9 May 2023 16:54:12 +0200 Subject: [PATCH] check for folder existence when applying changes to it Signed-off-by: Maksim Sukharev --- lib/Controller/FolderController.php | 59 ++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 5 deletions(-) diff --git a/lib/Controller/FolderController.php b/lib/Controller/FolderController.php index d7dd65986..61e4b4358 100644 --- a/lib/Controller/FolderController.php +++ b/lib/Controller/FolderController.php @@ -26,6 +26,7 @@ use OCA\GroupFolders\Mount\MountProvider; use OCA\GroupFolders\Service\DelegationService; use OCA\GroupFolders\Service\FoldersFilter; +use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\OCSController; use OCP\Files\IRootFolder; @@ -84,10 +85,28 @@ public function getFolders(): DataResponse { * @RequireGroupFolderAdmin */ public function getFolder(int $id): DataResponse { - return new DataResponse($this->manager->getFolder($id, $this->getRootFolderStorageId())); + $response = $this->checkFolderExists($id); + if ($response) { + return $response; + } + + $storageId = $this->getRootFolderStorageId(); + return new DataResponse($this->manager->getFolder($id, $storageId)); + } + + private function checkFolderExists(int $id): ?DataResponse { + $storageId = $this->getRootFolderStorageId(); + if ($storageId === null) { + return new DataResponse([], Http::STATUS_NOT_FOUND); + } + $folder = $this->manager->getFolder($id, $storageId); + if ($folder === false) { + return new DataResponse([], Http::STATUS_NOT_FOUND); + } + return null; } - private function getRootFolderStorageId(): int { + private function getRootFolderStorageId(): ?int { return $this->rootFolder->getMountPoint()->getNumericStorageId(); } @@ -105,10 +124,12 @@ public function addFolder(string $mountpoint): DataResponse { * @RequireGroupFolderAdmin */ public function removeFolder(int $id): DataResponse { - $folder = $this->mountProvider->getFolder($id); - if ($folder) { - $folder->delete(); + $response = $this->checkFolderExists($id); + if ($response) { + return $response; } + $folder = $this->mountProvider->getFolder($id); + $folder->delete(); $this->manager->removeFolder($id); return new DataResponse(['success' => true]); } @@ -127,6 +148,10 @@ public function setMountPoint(int $id, string $mountPoint): DataResponse { * @RequireGroupFolderAdmin */ public function addGroup(int $id, string $group): DataResponse { + $response = $this->checkFolderExists($id); + if ($response) { + return $response; + } $this->manager->addApplicableGroup($id, $group); return new DataResponse(['success' => true]); } @@ -136,6 +161,10 @@ public function addGroup(int $id, string $group): DataResponse { * @RequireGroupFolderAdmin */ public function removeGroup(int $id, string $group): DataResponse { + $response = $this->checkFolderExists($id); + if ($response) { + return $response; + } $this->manager->removeApplicableGroup($id, $group); return new DataResponse(['success' => true]); } @@ -145,6 +174,10 @@ public function removeGroup(int $id, string $group): DataResponse { * @RequireGroupFolderAdmin */ public function setPermissions(int $id, string $group, int $permissions): DataResponse { + $response = $this->checkFolderExists($id); + if ($response) { + return $response; + } $this->manager->setGroupPermissions($id, $group, $permissions); return new DataResponse(['success' => true]); } @@ -155,6 +188,10 @@ public function setPermissions(int $id, string $group, int $permissions): DataRe * @throws \OCP\DB\Exception */ public function setManageACL(int $id, string $mappingType, string $mappingId, bool $manageAcl): DataResponse { + $response = $this->checkFolderExists($id); + if ($response) { + return $response; + } $this->manager->setManageACL($id, $mappingType, $mappingId, $manageAcl); return new DataResponse(['success' => true]); } @@ -164,6 +201,10 @@ public function setManageACL(int $id, string $mappingType, string $mappingId, bo * @RequireGroupFolderAdmin */ public function setQuota(int $id, int $quota): DataResponse { + $response = $this->checkFolderExists($id); + if ($response) { + return $response; + } $this->manager->setFolderQuota($id, $quota); return new DataResponse(['success' => true]); } @@ -173,6 +214,10 @@ public function setQuota(int $id, int $quota): DataResponse { * @RequireGroupFolderAdmin */ public function setACL(int $id, bool $acl): DataResponse { + $response = $this->checkFolderExists($id); + if ($response) { + return $response; + } $this->manager->setFolderACL($id, $acl); return new DataResponse(['success' => true]); } @@ -182,6 +227,10 @@ public function setACL(int $id, bool $acl): DataResponse { * @RequireGroupFolderAdmin */ public function renameFolder(int $id, string $mountpoint): DataResponse { + $response = $this->checkFolderExists($id); + if ($response) { + return $response; + } $this->manager->renameFolder($id, $mountpoint); return new DataResponse(['success' => true]); }