Skip to content

Commit fb6c06a

Browse files
committed
delete submissions
Signed-off-by: Christian Hartmann <chris-hartmann@gmx.de>
1 parent dd601c0 commit fb6c06a

2 files changed

Lines changed: 28 additions & 13 deletions

File tree

lib/Controller/ApiController.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,6 +1474,13 @@ public function deleteSubmission(int $formId, int $submissionId): DataResponse {
14741474
throw new OCSBadRequestException('Submission doesn\'t belong to given form');
14751475
}
14761476

1477+
if (
1478+
!in_array(Constants::PERMISSION_RESULTS_DELETE, $this->formsService->getPermissions($form))
1479+
&& $this->currentUser->getUID() !== $submission->getUserId()
1480+
) {
1481+
throw new OCSForbiddenException('Can only delete your own submissions');
1482+
}
1483+
14771484
// Delete submission (incl. Answers)
14781485
$this->submissionMapper->deleteById($submissionId);
14791486
$this->formMapper->update($form);

lib/Service/FormsService.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -331,31 +331,39 @@ public function canEditForm(Form $form): bool {
331331
* @param Form $form The form for which the results visibility is being checked.
332332
* @return bool True if the user can see the results, false otherwise.
333333
*/
334-
/**
335-
* Can the current user see results of a form
336-
*
337-
* @param Form $form
338-
* @return boolean
339-
*/
340334
public function canSeeResults(Form $form): bool {
341335
return $this->submissionMapper->countSubmissions($form->getId(), $this->currentUser->getUID()) > 0
342336
|| in_array(Constants::PERMISSION_RESULTS, $this->getPermissions($form));
343337
}
344338

345339
/**
346-
* Can the current user delete results of a form
340+
* Determines if the current user has permission to delete the results of a given form.
347341
*
348-
* @param Form $form
349-
* @return boolean
342+
* A user can delete the results of a form if the form is not archived and one of the following conditions is met:
343+
* - The user has the "results_delete" permission.
344+
* - The user has not submitted any responses, and the form allows editing.
345+
* - The form is not archived.
346+
*
347+
* @param Form $form The form for which the results deletion permission is being checked.
348+
* @return bool True if the user can delete the results, false otherwise.
350349
*/
351350
public function canDeleteResults(Form $form): bool {
352-
// Check permissions
353-
if (!in_array(Constants::PERMISSION_RESULTS_DELETE, $this->getPermissions($form))) {
351+
// Do not allow deleting results on archived forms
352+
if ($this->isFormArchived($form)) {
354353
return false;
355354
}
355+
356+
// Allow deleting results if the current user has the "results_delete" permission
357+
if (in_array(Constants::PERMISSION_RESULTS_DELETE, $this->getPermissions($form))) {
358+
return true;
359+
}
356360

357-
// Do not allow deleting results on archived forms
358-
return !$this->isFormArchived($form);
361+
// Allow deleting results if the current user has already submitted
362+
if ($form->getAllowEdit() && $this->submissionMapper->countSubmissions($form->getId(), $this->currentUser->getUID()) > 0) {
363+
return true;
364+
}
365+
366+
return false;
359367
}
360368

361369
/**

0 commit comments

Comments
 (0)