diff --git a/CHANGELOG.md b/CHANGELOG.md index 7623a26..2b2b357 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ before starting to add changes. Use example [placed in the end of the page](#exa ## [Unreleased] - Added webform ID to digital post audit logging messages. +- Added audit logging to `os2forms_fasit` ## [3.19.0] 2024-12-06 diff --git a/modules/os2forms_fasit/os2forms_fasit.info.yml b/modules/os2forms_fasit/os2forms_fasit.info.yml index 0142493..43309b4 100644 --- a/modules/os2forms_fasit/os2forms_fasit.info.yml +++ b/modules/os2forms_fasit/os2forms_fasit.info.yml @@ -7,4 +7,5 @@ dependencies: - drupal:webform - drupal:advancedqueue - os2forms:os2forms_attachment + - os2web:os2web_audit configure: os2forms_fasit.admin.settings diff --git a/modules/os2forms_fasit/os2forms_fasit.services.yml b/modules/os2forms_fasit/os2forms_fasit.services.yml index 23e41d4..4397c83 100644 --- a/modules/os2forms_fasit/os2forms_fasit.services.yml +++ b/modules/os2forms_fasit/os2forms_fasit.services.yml @@ -13,3 +13,4 @@ services: - '@entity_type.manager' - "@Drupal\\os2forms_fasit\\Helper\\Settings" - "@Drupal\\os2forms_fasit\\Helper\\CertificateLocatorHelper" + - "@os2web_audit.logger" diff --git a/modules/os2forms_fasit/src/Helper/FasitHelper.php b/modules/os2forms_fasit/src/Helper/FasitHelper.php index fb14351..fb1b686 100644 --- a/modules/os2forms_fasit/src/Helper/FasitHelper.php +++ b/modules/os2forms_fasit/src/Helper/FasitHelper.php @@ -11,6 +11,7 @@ use Drupal\os2forms_fasit\Exception\InvalidSettingException; use Drupal\os2forms_fasit\Exception\InvalidSubmissionException; use Drupal\os2forms_fasit\Plugin\WebformHandler\FasitWebformHandler; +use Drupal\os2web_audit\Service\Logger; use Drupal\webform\Entity\WebformSubmission; use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\GuzzleException; @@ -33,7 +34,13 @@ class FasitHelper { 'managed_file', ]; - public function __construct(private readonly ClientInterface $client, private readonly EntityTypeManagerInterface $entityTypeManager, private readonly Settings $settings, private readonly CertificateLocatorHelper $certificateLocator) { + public function __construct( + private readonly ClientInterface $client, + private readonly EntityTypeManagerInterface $entityTypeManager, + private readonly Settings $settings, + private readonly CertificateLocatorHelper $certificateLocator, + private readonly Logger $auditLogger, + ) { } /** @@ -231,6 +238,9 @@ private function uploadDocument(array $uploads, string $submissionId, array $han if (Response::HTTP_OK !== $response->getStatusCode()) { throw new FasitResponseException(sprintf('Expected status code 200, received %d', $response->getStatusCode())); } + + $msg = sprintf('Successfully uploaded document %s to cpr %s in Fasit. Webform id: %s', $fasitDocumentTitle, $fasitCpr, $submission->getWebform()->id()); + $this->auditLogger->info('Fasit', $msg); } /** @@ -307,7 +317,7 @@ private function uploadAttachment(string $submissionId, array $handlerConfigurat $tempAttachmentFilename = tempnam(sys_get_temp_dir(), 'attachment'); file_put_contents($tempAttachmentFilename, $fileContent); - return $this->uploadFile($fileName, $tempAttachmentFilename); + return $this->uploadFile($fileName, $tempAttachmentFilename, $submission->getWebform()->id()); } /** @@ -317,7 +327,9 @@ private function uploadAttachment(string $submissionId, array $handlerConfigurat * The original filename. * @param string $tempFilename * The temp filename. - * + * @param string $webformId + * The webform id. + * * @throws \Drupal\os2forms_fasit\Exception\CertificateLocatorException * Certificate locator exception. * @throws \Drupal\os2forms_fasit\Exception\FasitResponseException @@ -325,7 +337,7 @@ private function uploadAttachment(string $submissionId, array $handlerConfigurat * * @phpstan-return array */ - private function uploadFile(string $originalFilename, string $tempFilename): array { + private function uploadFile(string $originalFilename, string $tempFilename, string $webformId): array { $endpoint = sprintf('%s/%s/%s/documents/%s', $this->settings->getFasitApiBaseUrl(), $this->settings->getFasitApiTenant(), @@ -371,6 +383,11 @@ private function uploadFile(string $originalFilename, string $tempFilename): arr throw new FasitResponseException('Could not get upload id from response'); } + // Note, that this does not mean a document has been sent to a citizen case in Fasit yet. + // This is done later by uploadDocument. The file has simply been made ready. + $msg = sprintf('Successfully uploaded file %s to Fasit. Webform id: %s', $originalFilename, $webformId); + $this->auditLogger->info('Fasit', $msg); + return ['filename' => $originalFilename, 'id' => $content['id']]; } @@ -392,6 +409,7 @@ private function uploadFileElements(string $submissionId): array { // Fetch element ids that may contain pdf files. /** @var \Drupal\webform\Entity\WebformSubmission $submission */ $submission = $this->getSubmission($submissionId); + $webformId = $submission->getWebform()->id(); $fileIds = $this->getFileElementKeysFromSubmission($submission); $fileStorage = $this->entityTypeManager->getStorage('file'); @@ -412,7 +430,7 @@ private function uploadFileElements(string $submissionId): array { $tempFilename = tempnam(sys_get_temp_dir(), 'attachment'); file_put_contents($tempFilename, $fileContent); - $uploads[] = $this->uploadFile($filename, $tempFilename); + $uploads[] = $this->uploadFile($filename, $tempFilename, $webformId); } return $uploads;