Skip to content

Commit c692276

Browse files
authored
Merge pull request #3786 from nextcloud/backport/3785/stable5.4
[stable5.4] fix: respondent check
2 parents 43744e1 + 99a0866 commit c692276

2 files changed

Lines changed: 86 additions & 1 deletion

File tree

‎lib/Controller/ApiController.php‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1519,7 +1519,6 @@ public function updateSubmission(int $formId, int $submissionId, array $answers)
15191519
throw new OCSBadRequestException($e->getMessage());
15201520
}
15211521

1522-
// get existing submission of this user
15231522
try {
15241523
$submission = $this->submissionMapper->findById($submissionId);
15251524
} catch (DoesNotExistException) {
@@ -1530,6 +1529,13 @@ public function updateSubmission(int $formId, int $submissionId, array $answers)
15301529
throw new OCSBadRequestException('Submission doesn\'t belong to given form');
15311530
}
15321531

1532+
if (
1533+
!in_array(Constants::PERMISSION_RESULTS_DELETE, $this->formsService->getPermissions($form))
1534+
&& $this->currentUser->getUID() !== $submission->getUserId()
1535+
) {
1536+
throw new OCSForbiddenException('You can only update your own submission!');
1537+
}
1538+
15331539
$submission->setTimestamp(time());
15341540
$this->submissionMapper->update($submission);
15351541

‎tests/Unit/Controller/ApiControllerTest.php‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,6 +1673,11 @@ public function testUpdateSubmission_success() {
16731673
->with($form)
16741674
->willReturn(true);
16751675

1676+
$this->formsService->expects($this->once())
1677+
->method('getPermissions')
1678+
->with($form)
1679+
->willReturn([]);
1680+
16761681
$this->formsService->expects($this->once())
16771682
->method('getQuestions')
16781683
->with($formId)
@@ -1883,6 +1888,11 @@ public function testUpdateSubmission_canEditOthers_withDeletePermission() {
18831888
->with($form)
18841889
->willReturn(true);
18851890

1891+
$this->formsService->expects($this->once())
1892+
->method('getPermissions')
1893+
->with($form)
1894+
->willReturn([Constants::PERMISSION_RESULTS_DELETE]);
1895+
18861896
$this->formsService->expects($this->once())
18871897
->method('getQuestions')
18881898
->with($formId)
@@ -2050,6 +2060,11 @@ public function testUpdateSubmission_ownSubmissionWithAllowEditSubmissions() {
20502060
->with($form)
20512061
->willReturn(true);
20522062

2063+
$this->formsService->expects($this->once())
2064+
->method('getPermissions')
2065+
->with($form)
2066+
->willReturn([]);
2067+
20532068
$this->formsService->expects($this->once())
20542069
->method('getQuestions')
20552070
->with($formId)
@@ -2116,6 +2131,60 @@ public function testUpdateSubmission_ownSubmissionWithoutAllowEditSubmissions()
21162131
$this->apiController->updateSubmission($formId, $submissionId, $answers);
21172132
}
21182133

2134+
public function testUpdateSubmission_otherUsersSubmissionDeniedWhenAllowEditSubmissions() {
2135+
$formId = 1;
2136+
$submissionId = 42;
2137+
$answers = ['q1' => ['answer1']];
2138+
2139+
$form = new Form();
2140+
$form->setId($formId);
2141+
$form->setOwnerId('formOwner');
2142+
$form->setAllowEditSubmissions(true);
2143+
2144+
$submission = new Submission();
2145+
$submission->setId($submissionId);
2146+
$submission->setFormId($formId);
2147+
$submission->setUserId('anotherUser');
2148+
2149+
$this->formsService->expects($this->once())
2150+
->method('loadFormForSubmission')
2151+
->with(1)
2152+
->willReturn($form);
2153+
2154+
$this->formsService->expects($this->once())
2155+
->method('canDeleteResults')
2156+
->with($form)
2157+
->willReturn(true);
2158+
2159+
$this->formsService->expects($this->once())
2160+
->method('getPermissions')
2161+
->with($form)
2162+
->willReturn([Constants::PERMISSION_SUBMIT]);
2163+
2164+
$this->formsService->expects($this->once())
2165+
->method('getQuestions')
2166+
->with($formId)
2167+
->willReturn([['id' => 'q1', 'type' => Constants::ANSWER_TYPE_SHORT, 'options' => []]]);
2168+
2169+
$this->submissionService->expects($this->once())
2170+
->method('validateSubmission');
2171+
2172+
$this->submissionMapper->expects($this->once())
2173+
->method('findById')
2174+
->with($submissionId)
2175+
->willReturn($submission);
2176+
2177+
$this->submissionMapper->expects($this->never())
2178+
->method('update');
2179+
2180+
$this->answerMapper->expects($this->never())
2181+
->method('deleteBySubmission');
2182+
2183+
$this->expectException(OCSForbiddenException::class);
2184+
$this->expectExceptionMessage('You can only update your own submission!');
2185+
$this->apiController->updateSubmission($formId, $submissionId, $answers);
2186+
}
2187+
21192188
public function testUpdateSubmission_otherSubmissionWithoutPermission() {
21202189
// Test that user without RESULTS_DELETE cannot edit others' submissions even if allowEditSubmissions=true
21212190
$formId = 1;
@@ -2224,6 +2293,11 @@ public function testUpdateSubmission_formOwnerCanEditOthers() {
22242293
->with($form)
22252294
->willReturn(true);
22262295

2296+
$this->formsService->expects($this->once())
2297+
->method('getPermissions')
2298+
->with($form)
2299+
->willReturn([Constants::PERMISSION_RESULTS_DELETE]);
2300+
22272301
$this->formsService->expects($this->once())
22282302
->method('getQuestions')
22292303
->with($formId)
@@ -2285,6 +2359,11 @@ public function testUpdateSubmission_resultsDeleteShareCanEditOthers() {
22852359
->with($form)
22862360
->willReturn(true);
22872361

2362+
$this->formsService->expects($this->once())
2363+
->method('getPermissions')
2364+
->with($form)
2365+
->willReturn([Constants::PERMISSION_RESULTS_DELETE]);
2366+
22882367
$this->formsService->expects($this->once())
22892368
->method('getQuestions')
22902369
->with($formId)

0 commit comments

Comments
 (0)