diff --git a/eslint.config.js b/eslint.config.js index 3a3e1e156..5e99844f3 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -6,7 +6,4 @@ import { recommendedVue2 } from '@nextcloud/eslint-config' import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended' -export default [ - ...recommendedVue2, - eslintPluginPrettierRecommended, -] +export default [...recommendedVue2, eslintPluginPrettierRecommended] diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 7c2e8226d..789abf5f5 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -22,6 +22,7 @@ use OCA\Forms\Db\SubmissionMapper; use OCA\Forms\Db\UploadedFile; use OCA\Forms\Db\UploadedFileMapper; +use OCA\Forms\Exception\NoSuchFormException; use OCA\Forms\ResponseDefinitions; use OCA\Forms\Service\ConfigService; use OCA\Forms\Service\FormsService; @@ -1161,8 +1162,14 @@ public function reorderOptions(int $formId, int $questionId, array $newOrder, ?s #[ApiRoute(verb: 'GET', url: '/api/v3/forms/{formId}/submissions')] public function getSubmissions(int $formId, ?string $query = null, ?int $limit = null, int $offset = 0, ?string $fileFormat = null): DataResponse|DataDownloadResponse { $form = $this->formsService->getFormIfAllowed($formId, Constants::PERMISSION_RESULTS); + $permissions = $this->formsService->getPermissions($form); + $canSeeAllSubmissions = in_array(Constants::PERMISSION_RESULTS, $permissions, true); if ($fileFormat !== null) { + if (!$canSeeAllSubmissions) { + throw new NoSuchFormException('The current user has no permission to get the results for this form', Http::STATUS_FORBIDDEN); + } + $submissionsData = $this->submissionService->getSubmissionsData($form, $fileFormat); $fileName = $this->formsService->getFileName($form, $fileFormat); @@ -1170,7 +1177,7 @@ public function getSubmissions(int $formId, ?string $query = null, ?int $limit = } // Load submissions and currently active questions - if (in_array(Constants::PERMISSION_RESULTS, $this->formsService->getPermissions($form))) { + if ($canSeeAllSubmissions) { $submissions = $this->submissionService->getSubmissions($formId, null, $query, $limit, $offset); $filteredSubmissionsCount = $this->submissionMapper->countSubmissions($formId, null, $query); } else { diff --git a/lib/Service/FormsService.php b/lib/Service/FormsService.php index 72492d81d..5a7492329 100644 --- a/lib/Service/FormsService.php +++ b/lib/Service/FormsService.php @@ -212,8 +212,6 @@ public function getForm(Form $form): array { $userSubmissionCount = $this->submissionMapper->countSubmissions($form->getId(), $this->currentUser->getUID()); if ($userSubmissionCount > 0) { $result['submissionCount'] = $userSubmissionCount; - // Append `results` permission if user has submitted to the form - $result['permissions'][] = Constants::PERMISSION_RESULTS; } } diff --git a/src/Forms.vue b/src/Forms.vue index dc091d218..15245a93e 100644 --- a/src/Forms.vue +++ b/src/Forms.vue @@ -269,8 +269,15 @@ export default { return false } + if (this.$route.name === 'results') { + return ( + form.permissions.includes(this.$route.name) + || form.submissionCount > 0 + ) + } + // Return whether route is in the permissions-list - return form?.permissions.includes(this.$route.name) + return form.permissions.includes(this.$route.name) }, selectedForm: { diff --git a/src/views/Results.vue b/src/views/Results.vue index 218902894..8180c15ec 100644 --- a/src/views/Results.vue +++ b/src/views/Results.vue @@ -50,6 +50,7 @@ willThrowException(new NoSuchFormException('Could not find form')); $this->expectException(NoSuchFormException::class); - $this->apiController->getSubmissions(99, 'csv'); + $this->apiController->getSubmissions(99, fileFormat: 'csv'); } public function testExportSubmissions_noPermissions() { @@ -318,7 +318,27 @@ public function testExportSubmissions_noPermissions() { ->willThrowException(new NoSuchFormException('The current user has no permission to get the results for this form')); $this->expectException(NoSuchFormException::class); - $this->apiController->getSubmissions(1, 'csv'); + $this->apiController->getSubmissions(1, fileFormat: 'csv'); + } + + public function testExportSubmissions_noExportPermissions() { + $form = new Form(); + $form->setId(1); + $form->setOwnerId('currentUser'); + + $this->formsService->expects($this->once()) + ->method('getFormIfAllowed') + ->with(1, Constants::PERMISSION_RESULTS) + ->willReturn($form); + + $this->formsService->expects($this->once()) + ->method('getPermissions') + ->with($form) + ->willReturn([Constants::PERMISSION_SUBMIT]); + + + $this->expectException(NoSuchFormException::class); + $this->apiController->getSubmissions(1, fileFormat: 'csv'); } public function testExportSubmissions() { @@ -331,6 +351,11 @@ public function testExportSubmissions() { ->with(1, Constants::PERMISSION_RESULTS) ->willReturn($form); + $this->formsService->expects($this->once()) + ->method('getPermissions') + ->with($form) + ->willReturn([Constants::PERMISSION_SUBMIT, Constants::PERMISSION_RESULTS]); + $csv = 'foo,bar'; $this->submissionService->expects($this->once()) ->method('getSubmissionsData')