From d23e0d74363ccc77dd25c7c72d8d0f19c55f313b Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Tue, 28 Nov 2023 22:23:43 +0100 Subject: [PATCH] enh: Allow to set results delete permission on the frontend Signed-off-by: Ferdinand Thiessen --- lib/Controller/ApiController.php | 5 +- lib/Controller/ShareApiController.php | 5 + lib/Service/FormsService.php | 12 ++- .../SidebarTabs/SharingShareDiv.vue | 17 +++ src/views/Results.vue | 1 + tests/Unit/Controller/ApiControllerTest.php | 101 ++++++++++++++++++ .../Controller/ShareApiControllerTest.php | 46 ++++++++ tests/Unit/Service/FormsServiceTest.php | 67 ++++++++++++ 8 files changed, 251 insertions(+), 3 deletions(-) diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 7e4c6cfc0..d5abc9ed6 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -1016,8 +1016,9 @@ public function deleteSubmission(int $id): DataResponse { throw new OCSBadRequestException(); } - if ($form->getOwnerId() !== $this->currentUser->getUID()) { - $this->logger->debug('This form is not owned by the current user'); + // The current user has permissions to remove submissions + if (!$this->formsService->canDeleteResults($form)) { + $this->logger->debug('This form is not owned by the current user and user has no `results_delete` permission'); throw new OCSForbiddenException(); } diff --git a/lib/Controller/ShareApiController.php b/lib/Controller/ShareApiController.php index 75924e4e1..29fffefc0 100644 --- a/lib/Controller/ShareApiController.php +++ b/lib/Controller/ShareApiController.php @@ -313,6 +313,11 @@ protected function validatePermissions(array $permissions, int $shareType): bool return false; } + if (in_array(Constants::PERMISSION_RESULTS_DELETE, $sanitizedPermissions) && !in_array(Constants::PERMISSION_RESULTS, $sanitizedPermissions)) { + $this->logger->debug('Permission results_delete is only allowed when permission results is also set'); + return false; + } + // Make sure only users can have special permissions if (count($sanitizedPermissions) > 1) { switch ($shareType) { diff --git a/lib/Service/FormsService.php b/lib/Service/FormsService.php index 8a0a8e2ef..0af552978 100644 --- a/lib/Service/FormsService.php +++ b/lib/Service/FormsService.php @@ -252,6 +252,16 @@ public function canSeeResults(Form $form): bool { return in_array(Constants::PERMISSION_RESULTS, $this->getPermissions($form)); } + /** + * Can the current user delete results of a form + * + * @param Form $form + * @return boolean + */ + public function canDeleteResults(Form $form): bool { + return in_array(Constants::PERMISSION_RESULTS_DELETE, $this->getPermissions($form)); + } + /** * Can the user submit a form * @@ -475,7 +485,7 @@ public function notifyNewSubmission(Form $form, string $submitter): void { * * @param int $formId The form to query shares for * @param string $userId The user to check if shared with - * @return array + * @return Share[] */ protected function getSharesWithUser(int $formId, string $userId): array { $shareEntities = $this->shareMapper->findByForm($formId); diff --git a/src/components/SidebarTabs/SharingShareDiv.vue b/src/components/SidebarTabs/SharingShareDiv.vue index 462432c12..32795e70c 100644 --- a/src/components/SidebarTabs/SharingShareDiv.vue +++ b/src/components/SidebarTabs/SharingShareDiv.vue @@ -35,6 +35,9 @@ {{ t('forms', 'View responses') }} + + {{ t('forms', 'Delete responses') }} +