-
Notifications
You must be signed in to change notification settings - Fork 112
feat: add maximum submissions limit for forms #3199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Forms\Migration; | ||
|
|
||
| use Closure; | ||
| use OCP\DB\ISchemaWrapper; | ||
| use OCP\DB\Types; | ||
| use OCP\Migration\IOutput; | ||
| use OCP\Migration\SimpleMigrationStep; | ||
|
|
||
| class Version050300Date20260303000000 extends SimpleMigrationStep { | ||
|
|
||
| /** | ||
| * @param IOutput $output | ||
| * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` | ||
| * @param array $options | ||
| * @return null|ISchemaWrapper | ||
| */ | ||
| public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { | ||
| /** @var ISchemaWrapper $schema */ | ||
| $schema = $schemaClosure(); | ||
| $table = $schema->getTable('forms_v2_forms'); | ||
|
|
||
| if (!$table->hasColumn('max_submissions')) { | ||
| $table->addColumn('max_submissions', Types::INTEGER, [ | ||
| 'notnull' => false, | ||
| 'default' => null, | ||
| 'comment' => 'Maximum number of submissions, null means unlimited', | ||
| ]); | ||
| } | ||
|
|
||
| return $schema; | ||
| } | ||
| } | ||
LAfricain marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -204,6 +204,10 @@ public function getForm(Form $form): array { | |
| $result['permissions'] = $this->getPermissions($form); | ||
| // Append canSubmit, to be able to show proper EmptyContent on internal view. | ||
| $result['canSubmit'] = $this->canSubmit($form); | ||
| // Append isMaxSubmissionsReached to show proper message on submit view. | ||
| $maxSubmissions = $form->getMaxSubmissions(); | ||
| $result['isMaxSubmissionsReached'] = $maxSubmissions !== null | ||
| && $this->submissionMapper->countSubmissions($form->getId()) >= $maxSubmissions; | ||
|
|
||
| // Append submissionCount if currentUser has permissions to see results | ||
| if (in_array(Constants::PERMISSION_RESULTS, $result['permissions'])) { | ||
|
|
@@ -484,6 +488,12 @@ public function canDeleteResults(Form $form): bool { | |
| * @return boolean | ||
| */ | ||
| public function canSubmit(Form $form): bool { | ||
| // Check if max submissions limit is reached | ||
| $maxSubmissions = $form->getMaxSubmissions(); | ||
| if ($maxSubmissions !== null && $this->submissionMapper->countSubmissions($form->getId()) >= $maxSubmissions) { | ||
| return false; | ||
| } | ||
LAfricain marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
Comment on lines
+491
to
+496
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check fails once the submission reaches the limit as this part of the code is called after inserting the submission. @susnux do you think a single check for maxSubmissions is enough in ApiController or do we need the double check here as well? If so, we need to change the condition from |
||
| // We cannot control how many time users can submit if public link available | ||
| if ($this->hasPublicLink($form)) { | ||
| return true; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.