Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1547,7 +1547,6 @@ public function updateSubmission(int $formId, int $submissionId, array $answers)
throw new OCSBadRequestException($e->getMessage());
}

// get existing submission of this user
try {
$submission = $this->submissionMapper->findById($submissionId);
} catch (DoesNotExistException) {
Expand All @@ -1558,6 +1557,13 @@ public function updateSubmission(int $formId, int $submissionId, array $answers)
throw new OCSBadRequestException('Submission doesn\'t belong to given form');
}

if (
!in_array(Constants::PERMISSION_RESULTS_DELETE, $this->formsService->getPermissions($form))
&& $this->currentUser->getUID() !== $submission->getUserId()
) {
throw new OCSForbiddenException('You can only update your own submission!');
}

$submission->setTimestamp(time());
$this->submissionMapper->update($submission);

Expand Down
79 changes: 79 additions & 0 deletions tests/Unit/Controller/ApiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1908,6 +1908,11 @@ public function testUpdateSubmission_success() {
->with($form)
->willReturn(true);

$this->formsService->expects($this->once())
->method('getPermissions')
->with($form)
->willReturn([]);

$this->formsService->expects($this->once())
->method('getQuestions')
->with($formId)
Expand Down Expand Up @@ -2122,6 +2127,11 @@ public function testUpdateSubmission_canEditOthers_withDeletePermission() {
->with($form)
->willReturn(true);

$this->formsService->expects($this->once())
->method('getPermissions')
->with($form)
->willReturn([Constants::PERMISSION_RESULTS_DELETE]);

$this->formsService->expects($this->once())
->method('getQuestions')
->with($formId)
Expand Down Expand Up @@ -2289,6 +2299,11 @@ public function testUpdateSubmission_ownSubmissionWithAllowEditSubmissions() {
->with($form)
->willReturn(true);

$this->formsService->expects($this->once())
->method('getPermissions')
->with($form)
->willReturn([]);

$this->formsService->expects($this->once())
->method('getQuestions')
->with($formId)
Expand Down Expand Up @@ -2355,6 +2370,60 @@ public function testUpdateSubmission_ownSubmissionWithoutAllowEditSubmissions()
$this->apiController->updateSubmission($formId, $submissionId, $answers);
}

public function testUpdateSubmission_otherUsersSubmissionDeniedWhenAllowEditSubmissions() {
$formId = 1;
$submissionId = 42;
$answers = ['q1' => ['answer1']];

$form = new Form();
$form->setId($formId);
$form->setOwnerId('formOwner');
$form->setAllowEditSubmissions(true);

$submission = new Submission();
$submission->setId($submissionId);
$submission->setFormId($formId);
$submission->setUserId('anotherUser');

$this->formsService->expects($this->once())
->method('loadFormForSubmission')
->with(1)
->willReturn($form);

$this->formsService->expects($this->once())
->method('canDeleteResults')
->with($form)
->willReturn(true);

$this->formsService->expects($this->once())
->method('getPermissions')
->with($form)
->willReturn([Constants::PERMISSION_SUBMIT]);

$this->formsService->expects($this->once())
->method('getQuestions')
->with($formId)
->willReturn([['id' => 'q1', 'type' => Constants::ANSWER_TYPE_SHORT, 'options' => []]]);

$this->submissionService->expects($this->once())
->method('validateSubmission');

$this->submissionMapper->expects($this->once())
->method('findById')
->with($submissionId)
->willReturn($submission);

$this->submissionMapper->expects($this->never())
->method('update');

$this->answerMapper->expects($this->never())
->method('deleteBySubmission');

$this->expectException(OCSForbiddenException::class);
$this->expectExceptionMessage('You can only update your own submission!');
$this->apiController->updateSubmission($formId, $submissionId, $answers);
}

public function testUpdateSubmission_otherSubmissionWithoutPermission() {
// Test that user without RESULTS_DELETE cannot edit others' submissions even if allowEditSubmissions=true
$formId = 1;
Expand Down Expand Up @@ -2463,6 +2532,11 @@ public function testUpdateSubmission_formOwnerCanEditOthers() {
->with($form)
->willReturn(true);

$this->formsService->expects($this->once())
->method('getPermissions')
->with($form)
->willReturn([Constants::PERMISSION_RESULTS_DELETE]);

$this->formsService->expects($this->once())
->method('getQuestions')
->with($formId)
Expand Down Expand Up @@ -2524,6 +2598,11 @@ public function testUpdateSubmission_resultsDeleteShareCanEditOthers() {
->with($form)
->willReturn(true);

$this->formsService->expects($this->once())
->method('getPermissions')
->with($form)
->willReturn([Constants::PERMISSION_RESULTS_DELETE]);

$this->formsService->expects($this->once())
->method('getQuestions')
->with($formId)
Expand Down
Loading