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
2 changes: 2 additions & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,8 @@
<command>OCA\Integriq\Command\MigrateInlineSecrets</command>
<!-- Reports source and rule secrets stored as the export placeholder; blanks them only with the apply option. -->
<command>OCA\Integriq\Command\BlankPlaceholderSecrets</command>
<!-- open-formulieren-submits-into-its-case: drains the staged Open Formulieren submissions into their cases; a dry run without the apply option. -->
<command>OCA\Integriq\Command\OpenFormulierenDrain</command>
<!-- ocon#232 (ocon#151 follow-up): audits the VESTIGIAL source
authenticationConfig field. No code authenticates from it, so removal
(not a credentialRef migration) is the correct treatment. The default
Expand Down
119 changes: 119 additions & 0 deletions lib/Command/OpenFormulierenDrain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

/**
* Integriq OpenFormulierenDrain command.
*
* `occ integriq:openformulieren:drain`: pushes every staged `received` and
* `failed` Open Formulieren submission through the inbound submit path.
*
* @category Command
* @package OCA\Integriq\Command
*
* @author Conduction Development Team <info@conduction.nl>
* @copyright 2026 Conduction B.V.
* @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
*
* SPDX-FileCopyrightText: 2026 Conduction B.V. <info@conduction.nl>
* SPDX-License-Identifier: EUPL-1.2
*
* @link https://conduction.nl
*/

declare(strict_types=1);

namespace OCA\Integriq\Command;

use OCA\Integriq\Service\OpenFormulieren\SubmissionDrain;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Throwable;

/**
* Drain the Open Formulieren staging schema into cases; a dry run by default.
*
* @spec openspec/changes/open-formulieren-submits-into-its-case/tasks.md
*/
class OpenFormulierenDrain extends Command {

/**
* Constructor.
*
* @param SubmissionDrain $drain The drain.
*/
public function __construct(
private readonly SubmissionDrain $drain,
) {
parent::__construct();
}//end __construct()

/**
* Name, description and options.
*
* @return void
*/
protected function configure(): void {
$this->setName(name: 'integriq:openformulieren:drain')
->setDescription(
description: 'Report (dry-run) the staged Open Formulieren submissions without a case; --apply submits them into their cases'
)
->addOption(
name: 'apply',
shortcut: null,
mode: InputOption::VALUE_NONE,
description: 'Submit and mark them. Without it nothing is written.'
)
->addOption(
name: 'limit',
shortcut: null,
mode: InputOption::VALUE_REQUIRED,
description: 'Maximum number of submissions per status',
default: '1000'
);
}//end configure()

/**
* Run the drain and print each submission's outcome.
*
* @param InputInterface $input The input.
* @param OutputInterface $output The output.
*
* @return int 0 when nothing failed; 1 otherwise.
*
* @spec openspec/changes/open-formulieren-submits-into-its-case/tasks.md
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$io = new SymfonyStyle($input, $output);
$limit = (string)$input->getOption('limit');
if (preg_match('/^\d+$/', $limit) !== 1 || (int)$limit < 1 || (int)$limit > 100000) {
$io->error(sprintf('limit must be an integer in [1, 100000], got "%s"', $limit));
return Command::FAILURE;
}

$apply = ((bool)$input->getOption('apply') === true);
try {
$report = $this->drain->run(apply: $apply, limit: (int)$limit);
} catch (Throwable $exception) {
$io->error('The drain could not run: ' . $exception->getMessage());
return Command::FAILURE;
}

foreach ($report['rows'] as $row) {
$io->writeln(sprintf('%s %s: %s', $row['formSlug'], $row['uuid'], $row['outcome']));
}

if ($apply === false) {
$io->note(sprintf('Dry run: %d submission(s) wait for a case. Run with --apply to submit them.', $report['pending']));
return Command::SUCCESS;
}

$io->success(sprintf('%d submission(s) became a case, %d failed; %d were pending.', $report['drained'], $report['failed'], $report['pending']));
if ($report['failed'] === 0) {
return Command::SUCCESS;
}

return Command::FAILURE;
}//end execute()
}//end class
111 changes: 44 additions & 67 deletions lib/Controller/OpenFormulierenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@
use OCA\Integriq\Service\ActionAuthServiceInterface;
use OCA\Integriq\Service\Portal\PortalConnectionAlertsInterface;
use OCA\Integriq\Service\Dso\DsoIdentity;
use OCA\Integriq\Service\OpenFormulieren\DestinationSubmitterInterface;
use OCA\Integriq\Service\OpenFormulieren\OpenFormulierenConnection;
use OCA\Integriq\Service\OpenFormulierenIntakeServiceInterface;
use OCA\OpenRegister\Db\ObjectEntity;
use OCA\OpenRegister\Exception\FormSubmitRefusedException;
use OCA\OpenRegister\Exception\HandoffException;
use OCA\OpenRegister\Exception\NotAuthorizedException;
use OCP\AppFramework\Controller;
Expand Down Expand Up @@ -78,6 +79,7 @@ class OpenFormulierenController extends Controller {
* @param ActionAuthServiceInterface $actionAuth The action authorization service.
* @param IL10N $l The localization service.
* @param LoggerInterface $logger Logger for non-fatal diagnostics.
* @param DestinationSubmitterInterface $submitter Creates the case in the inbound request.
*/
public function __construct(
string $appName,
Expand All @@ -89,6 +91,7 @@ public function __construct(
private readonly ActionAuthServiceInterface $actionAuth,
private readonly IL10N $l,
private readonly LoggerInterface $logger,
private readonly DestinationSubmitterInterface $submitter,
) {
parent::__construct(appName: $appName, request: $request);

Expand All @@ -103,10 +106,14 @@ public function __construct(
* who every write runs as, inside OpenRegister's `runAs()`. A bad signature
* answers 401 before any state change. A missing connection, account or
* right answers 503 before anything is written, so Open Formulieren
* delivers again. A write OpenRegister refuses anyway answers 503 too.
* delivers again. The submission is mapped and its case created in this
* request through OpenRegister's form submit path, with the submission id
* as idempotency key and `externalReference`: 201 with the case reference,
* or the refusal's status (422 with findings) so Open Formulieren reports
* the failure on its side. No submission is stored to process later.
* Expected JSON body: see design.md §5 of open-formulieren-intake.
*
* @return JSONResponse The persisted `openformulieren_submission` record, or a 400/401/503 error envelope.
* @return JSONResponse 201 with OpenRegister's answer (reference, id, ...), or a 400/401/422/503 error envelope.
*
* @spec openspec/specs/open-formulieren-intake/spec.md#requirement-signed-inbound-submission-webhook-req-001
* @spec openspec/changes/openformulieren-intake-through-an-integriq-connection/specs/open-formulieren-intake/spec.md#requirement-the-intake-acts-as-the-open-formulieren-connections-account-req-006
Expand All @@ -115,7 +122,8 @@ public function __construct(
#[PublicPage]
#[AnonRateLimit(limit: 300, period: 60)]
public function inbound(): JSONResponse {
$identity = $this->identify(rawBody: $this->getRawContent());
$rawBody = $this->getRawContent();
$identity = $this->identify(rawBody: $rawBody);
if ($identity instanceof JSONResponse) {
return $identity;
}
Expand All @@ -131,88 +139,57 @@ public function inbound(): JSONResponse {
);
}

return $this->storeSubmission(identity: $identity, body: $body);
return $this->submitCase(identity: $identity, body: $body, payloadHash: hash('sha256', $rawBody));
}//end inbound()

/**
* Store the submission as the connection's account and answer the stored
* record, or 503 when it was not stored.
* Create the case as the connection's account and answer 201, or the refusal.
*
* @param DsoIdentity $identity The authenticated connection.
* @param array<string, mixed> $body The submission payload.
* @param DsoIdentity $identity The authenticated consumer.
* @param array<string, mixed> $body The decoded body.
* @param string $payloadHash The sha256 of the raw body, for the audit log.
*
* @return JSONResponse The record with its id, or the 503 answer.
* @return JSONResponse
*
* @spec openspec/changes/open-formulieren-submits-into-its-case/specs/open-formulieren-intake/spec.md#requirement-an-open-formulieren-submit-must-create-its-case-in-the-inbound-request
*/
private function storeSubmission(DsoIdentity $identity, array $body): JSONResponse {
private function submitCase(DsoIdentity $identity, array $body, string $payloadHash): JSONResponse {
$submissionMeta = (array)($body['submission'] ?? []);
try {
$submission = $this->ingestAs(identity: $identity, body: $body);
$answer = $this->connection->runAs(
$identity->account,
fn (): array => $this->submitter->submit(
formSlug: (string)($body['form']['slug'] ?? ''),
submissionId: (string)($submissionMeta['uuid'] ?? ''),
values: (array)($body['values'] ?? []),
payloadHash: $payloadHash
)
);
} catch (FormSubmitRefusedException $refusal) {
return $this->refused(refusal: $refusal, submissionMeta: $submissionMeta);
} catch (Throwable $exception) {
return $this->notStored(submissionMeta: $submissionMeta, reason: $exception->getMessage());
}

if ($submission instanceof ObjectEntity === false || (string)$submission->getUuid() === '') {
return $this->notStored(submissionMeta: $submissionMeta, reason: 'ingest returned an object without a uuid');
}

return new JSONResponse($submission->getObject() + ['id' => $submission->getUuid()]);
}//end storeSubmission()
return new JSONResponse($answer, Http::STATUS_CREATED);
}//end submitCase()

/**
* Ingest the submission inside OpenRegister's `runAs()` for the
* connection's account.
* The answer to a refused submit: OpenRegister's status and body; a 5xx
* refusal is answered as "not stored" so Open Formulieren delivers again.
*
* @param DsoIdentity $identity The authenticated connection.
* @param array<string, mixed> $body The submission payload.
*
* @return mixed What the intake service stored.
*/
private function ingestAs(DsoIdentity $identity, array $body): mixed {
return $this->connection->runAs(
$identity->account,
fn (): mixed => $this->intakeService->ingest(
formSlug: (string)($body['form']['slug'] ?? ''),
formUuid: $this->formUuidOf(body: $body),
submissionMeta: (array)($body['submission'] ?? []),
values: (array)($body['values'] ?? []),
attachmentRefs: (array)($body['attachments'] ?? []),
authContext: $this->authContextOf(body: $body),
identity: $identity
)
);
}//end ingestAs()

/**
* The form uuid as a string, or null when the payload has none.
* @param FormSubmitRefusedException $refusal The refusal.
* @param array<string, mixed> $submissionMeta The submission metadata.
*
* @param array<string, mixed> $body The submission payload.
*
* @return string|null
*/
private function formUuidOf(array $body): ?string {
$formUuid = ($body['form']['uuid'] ?? null);
if ($formUuid === null) {
return null;
}

return (string)$formUuid;
}//end formUuidOf()

/**
* The payload's `auth` block, or null when it is not an object.
*
* @param array<string, mixed> $body The submission payload.
*
* @return array<string, mixed>|null
* @return JSONResponse
*/
private function authContextOf(array $body): ?array {
$authContext = ($body['auth'] ?? null);
if (is_array($authContext) === false) {
return null;
private function refused(FormSubmitRefusedException $refusal, array $submissionMeta): JSONResponse {
if ($refusal->getStatus() >= 500) {
return $this->notStored(submissionMeta: $submissionMeta, reason: $refusal->getMessage());
}

return $authContext;
}//end authContextOf()
return new JSONResponse(['error' => 'submission_refused'] + $refusal->toBody(), $refusal->getStatus());
}//end refused()

/**
* Authenticate the submission against the Open Formulieren connection.
Expand Down
4 changes: 4 additions & 0 deletions lib/EventListener/EventListenerBindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ private function registerObjectSaveGuards(IEventDispatcher $dispatcher): void {
// imowId; refused on OpenRegister's own save path.
$dispatcher->addServiceListener(eventName: ObjectCreatingEvent::class, className: DsoActivityMappingGuardListener::class);
$dispatcher->addServiceListener(eventName: ObjectUpdatingEvent::class, className: DsoActivityMappingGuardListener::class);
// open-formulieren-submits-into-its-case: an Open Formulieren form mapping is
// checked against its destination schema before it may be active.
$dispatcher->addServiceListener(eventName: ObjectCreatingEvent::class, className: OpenFormulierenMappingGuardListener::class);
$dispatcher->addServiceListener(eventName: ObjectUpdatingEvent::class, className: OpenFormulierenMappingGuardListener::class);
$dispatcher->addServiceListener(eventName: ObjectCreatingEvent::class, className: ProductChannelSlugGuardListener::class);
$dispatcher->addServiceListener(eventName: ObjectUpdatingEvent::class, className: ProductChannelSlugGuardListener::class);
// REQ-SOW-001 (signed-outbound-webhooks): the Webhooks page saves a
Expand Down
Loading