Skip to content

Commit e635783

Browse files
authored
Merge pull request #3133 from nextcloud/fix/perms-5.2
[stable5.2] fix: do not add result permissions to see own submissions
2 parents 37f284d + bddcd68 commit e635783

5 files changed

Lines changed: 50 additions & 6 deletions

File tree

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;
@@ -1155,16 +1156,22 @@ public function reorderOptions(int $formId, int $questionId, array $newOrder) {
11551156
#[ApiRoute(verb: 'GET', url: '/api/v3/forms/{formId}/submissions')]
11561157
public function getSubmissions(int $formId, ?string $query = null, ?int $limit = null, int $offset = 0, ?string $fileFormat = null): DataResponse|DataDownloadResponse {
11571158
$form = $this->formsService->getFormIfAllowed($formId, Constants::PERMISSION_RESULTS);
1159+
$permissions = $this->formsService->getPermissions($form);
1160+
$canSeeAllSubmissions = in_array(Constants::PERMISSION_RESULTS, $permissions, true);
11581161

11591162
if ($fileFormat !== null) {
1163+
if (!$canSeeAllSubmissions) {
1164+
throw new NoSuchFormException('The current user has no permission to get the results for this form', Http::STATUS_FORBIDDEN);
1165+
}
1166+
11601167
$submissionsData = $this->submissionService->getSubmissionsData($form, $fileFormat);
11611168
$fileName = $this->formsService->getFileName($form, $fileFormat);
11621169

11631170
return new DataDownloadResponse($submissionsData, $fileName, Constants::SUPPORTED_EXPORT_FORMATS[$fileFormat]);
11641171
}
11651172

11661173
// Load submissions and currently active questions
1167-
if (in_array(Constants::PERMISSION_RESULTS, $this->formsService->getPermissions($form))) {
1174+
if ($canSeeAllSubmissions) {
11681175
$submissions = $this->submissionService->getSubmissions($formId, null, $query, $limit, $offset);
11691176
$filteredSubmissionsCount = $this->submissionMapper->countSubmissions($formId, null, $query);
11701177
} 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
@@ -272,8 +272,15 @@ export default {
272272
return false
273273
}
274274
275+
if (this.$route.name === 'results') {
276+
return (
277+
form.permissions.includes(this.$route.name)
278+
|| form.submissionCount > 0
279+
)
280+
}
281+
275282
// Return whether route is in the permissions-list
276-
return form?.permissions.includes(this.$route.name)
283+
return form.permissions.includes(this.$route.name)
277284
},
278285
279286
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)