Skip to content

Commit b8cf7be

Browse files
authored
Merge pull request #3128 from nextcloud/fix/perms
fix: do not add result permissions to see own submissions
2 parents e745a4a + a97c0ec commit b8cf7be

6 files changed

Lines changed: 51 additions & 10 deletions

File tree

eslint.config.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,4 @@
66
import { recommendedVue2 } from '@nextcloud/eslint-config'
77
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended'
88

9-
export default [
10-
...recommendedVue2,
11-
eslintPluginPrettierRecommended,
12-
]
9+
export default [...recommendedVue2, eslintPluginPrettierRecommended]

lib/Controller/ApiController.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use OCA\Forms\Db\SubmissionMapper;
2323
use OCA\Forms\Db\UploadedFile;
2424
use OCA\Forms\Db\UploadedFileMapper;
25+
use OCA\Forms\Exception\NoSuchFormException;
2526
use OCA\Forms\ResponseDefinitions;
2627
use OCA\Forms\Service\ConfigService;
2728
use OCA\Forms\Service\FormsService;
@@ -1161,16 +1162,22 @@ public function reorderOptions(int $formId, int $questionId, array $newOrder, ?s
11611162
#[ApiRoute(verb: 'GET', url: '/api/v3/forms/{formId}/submissions')]
11621163
public function getSubmissions(int $formId, ?string $query = null, ?int $limit = null, int $offset = 0, ?string $fileFormat = null): DataResponse|DataDownloadResponse {
11631164
$form = $this->formsService->getFormIfAllowed($formId, Constants::PERMISSION_RESULTS);
1165+
$permissions = $this->formsService->getPermissions($form);
1166+
$canSeeAllSubmissions = in_array(Constants::PERMISSION_RESULTS, $permissions, true);
11641167

11651168
if ($fileFormat !== null) {
1169+
if (!$canSeeAllSubmissions) {
1170+
throw new NoSuchFormException('The current user has no permission to get the results for this form', Http::STATUS_FORBIDDEN);
1171+
}
1172+
11661173
$submissionsData = $this->submissionService->getSubmissionsData($form, $fileFormat);
11671174
$fileName = $this->formsService->getFileName($form, $fileFormat);
11681175

11691176
return new DataDownloadResponse($submissionsData, $fileName, Constants::SUPPORTED_EXPORT_FORMATS[$fileFormat]);
11701177
}
11711178

11721179
// Load submissions and currently active questions
1173-
if (in_array(Constants::PERMISSION_RESULTS, $this->formsService->getPermissions($form))) {
1180+
if ($canSeeAllSubmissions) {
11741181
$submissions = $this->submissionService->getSubmissions($formId, null, $query, $limit, $offset);
11751182
$filteredSubmissionsCount = $this->submissionMapper->countSubmissions($formId, null, $query);
11761183
} else {

lib/Service/FormsService.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,6 @@ public function getForm(Form $form): array {
212212
$userSubmissionCount = $this->submissionMapper->countSubmissions($form->getId(), $this->currentUser->getUID());
213213
if ($userSubmissionCount > 0) {
214214
$result['submissionCount'] = $userSubmissionCount;
215-
// Append `results` permission if user has submitted to the form
216-
$result['permissions'][] = Constants::PERMISSION_RESULTS;
217215
}
218216
}
219217

src/Forms.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,15 @@ export default {
269269
return false
270270
}
271271
272+
if (this.$route.name === 'results') {
273+
return (
274+
form.permissions.includes(this.$route.name)
275+
|| form.submissionCount > 0
276+
)
277+
}
278+
272279
// Return whether route is in the permissions-list
273-
return form?.permissions.includes(this.$route.name)
280+
return form.permissions.includes(this.$route.name)
274281
},
275282
276283
selectedForm: {

src/views/Results.vue

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050

5151
<!-- Action menu for cloud export and deletion -->
5252
<NcActions
53+
v-if="canExportSubmissions"
5354
:aria-label="t('forms', 'Options')"
5455
force-name
5556
:inline="isMobile ? 0 : 1"
@@ -449,6 +450,12 @@ export default {
449450
return this.form.state === FormState.FormArchived
450451
},
451452
453+
canExportSubmissions() {
454+
return this.form.permissions.includes(
455+
this.PERMISSION_TYPES.PERMISSION_RESULTS,
456+
)
457+
},
458+
452459
canDeleteSubmissions() {
453460
return (
454461
this.form.permissions.includes(

tests/Unit/Controller/ApiControllerTest.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ public function testExportSubmissions_invalidForm() {
304304
->willThrowException(new NoSuchFormException('Could not find form'));
305305

306306
$this->expectException(NoSuchFormException::class);
307-
$this->apiController->getSubmissions(99, 'csv');
307+
$this->apiController->getSubmissions(99, fileFormat: 'csv');
308308
}
309309

310310
public function testExportSubmissions_noPermissions() {
@@ -318,7 +318,27 @@ public function testExportSubmissions_noPermissions() {
318318
->willThrowException(new NoSuchFormException('The current user has no permission to get the results for this form'));
319319

320320
$this->expectException(NoSuchFormException::class);
321-
$this->apiController->getSubmissions(1, 'csv');
321+
$this->apiController->getSubmissions(1, fileFormat: 'csv');
322+
}
323+
324+
public function testExportSubmissions_noExportPermissions() {
325+
$form = new Form();
326+
$form->setId(1);
327+
$form->setOwnerId('currentUser');
328+
329+
$this->formsService->expects($this->once())
330+
->method('getFormIfAllowed')
331+
->with(1, Constants::PERMISSION_RESULTS)
332+
->willReturn($form);
333+
334+
$this->formsService->expects($this->once())
335+
->method('getPermissions')
336+
->with($form)
337+
->willReturn([Constants::PERMISSION_SUBMIT]);
338+
339+
340+
$this->expectException(NoSuchFormException::class);
341+
$this->apiController->getSubmissions(1, fileFormat: 'csv');
322342
}
323343

324344
public function testExportSubmissions() {
@@ -331,6 +351,11 @@ public function testExportSubmissions() {
331351
->with(1, Constants::PERMISSION_RESULTS)
332352
->willReturn($form);
333353

354+
$this->formsService->expects($this->once())
355+
->method('getPermissions')
356+
->with($form)
357+
->willReturn([Constants::PERMISSION_SUBMIT, Constants::PERMISSION_RESULTS]);
358+
334359
$csv = 'foo,bar';
335360
$this->submissionService->expects($this->once())
336361
->method('getSubmissionsData')

0 commit comments

Comments
 (0)