From b50146eb57ddb37ab1f5bf8193ec78be1d669049 Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Mon, 9 Dec 2024 11:24:45 +0100 Subject: [PATCH 1/4] Added webform id to digital post audit log messages --- CHANGELOG.md | 2 ++ modules/os2forms_digital_post/src/Helper/DigitalPostHelper.php | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e95164e..7623a26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ 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. + ## [3.19.0] 2024-12-06 - Ensured installation of `os2web_audit` diff --git a/modules/os2forms_digital_post/src/Helper/DigitalPostHelper.php b/modules/os2forms_digital_post/src/Helper/DigitalPostHelper.php index 2e0d563..173aae0 100644 --- a/modules/os2forms_digital_post/src/Helper/DigitalPostHelper.php +++ b/modules/os2forms_digital_post/src/Helper/DigitalPostHelper.php @@ -76,7 +76,8 @@ public function sendDigitalPost(string $type, Message $message, ?ForsendelseI $f // RecipientID should be the same in Message and Forsendelse, // so fetch it from Message as it is always set. - $msg = sprintf('Sent digital post of type %s to %s', $type, $message->getMessageHeader()->getRecipient()->getRecipientID()); + $msg = sprintf('Sent digital post of type %s to %s.', $type, $message->getMessageHeader()->getRecipient()->getRecipientID()); + $msg .= $submission ? sprintf(' Webform id %s.', $submission->getWebform()->id()) : ''; $this->auditLogger->info('DigitalPost', $msg); return [$response, $service->getLastKombiMeMoMessage()]; From 4ea0f6277a511ffe12c92d413f34f715c4db4e9e Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Mon, 9 Dec 2024 11:28:05 +0100 Subject: [PATCH 2/4] Added audit logging to Fasit --- CHANGELOG.md | 1 + .../os2forms_fasit/os2forms_fasit.info.yml | 1 + .../os2forms_fasit.services.yml | 1 + .../os2forms_fasit/src/Helper/FasitHelper.php | 28 +++++++++++++++---- 4 files changed, 26 insertions(+), 5 deletions(-) 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; From 2ce46859889aa28c5075623d9d17a4c66e27efda Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Mon, 9 Dec 2024 12:28:19 +0100 Subject: [PATCH 3/4] Added audit logging to FBS handler --- CHANGELOG.md | 1 + .../os2forms_fbs_handler/os2forms_fbs_handler.info.yml | 1 + .../src/Plugin/AdvancedQueue/JobType/FbsCreateUser.php | 8 +++++++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b2b357..005317f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ before starting to add changes. Use example [placed in the end of the page](#exa - Added webform ID to digital post audit logging messages. - Added audit logging to `os2forms_fasit` +- Added audit logging to `os2forms_fbs_handler` ## [3.19.0] 2024-12-06 diff --git a/modules/os2forms_fbs_handler/os2forms_fbs_handler.info.yml b/modules/os2forms_fbs_handler/os2forms_fbs_handler.info.yml index 7d43a27..e6251ac 100644 --- a/modules/os2forms_fbs_handler/os2forms_fbs_handler.info.yml +++ b/modules/os2forms_fbs_handler/os2forms_fbs_handler.info.yml @@ -6,3 +6,4 @@ core_version_requirement: ^9 || ^10 dependencies: - 'webform:webform' - 'advancedqueue:advancedqueue' + - 'os2web:os2web_audit' diff --git a/modules/os2forms_fbs_handler/src/Plugin/AdvancedQueue/JobType/FbsCreateUser.php b/modules/os2forms_fbs_handler/src/Plugin/AdvancedQueue/JobType/FbsCreateUser.php index daf3479..3afe322 100644 --- a/modules/os2forms_fbs_handler/src/Plugin/AdvancedQueue/JobType/FbsCreateUser.php +++ b/modules/os2forms_fbs_handler/src/Plugin/AdvancedQueue/JobType/FbsCreateUser.php @@ -11,6 +11,7 @@ use Drupal\os2forms_fbs_handler\Client\FBS; use Drupal\os2forms_fbs_handler\Client\Model\Guardian; use Drupal\os2forms_fbs_handler\Client\Model\Patron; +use Drupal\os2web_audit\Service\Logger; use Drupal\webform\Entity\WebformSubmission; use GuzzleHttp\Client; use GuzzleHttp\Exception\GuzzleException; @@ -41,6 +42,7 @@ public function __construct( $plugin_definition, LoggerChannelFactoryInterface $loggerFactory, protected readonly Client $client, + protected readonly Logger $auditLogger, ) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->submissionLogger = $loggerFactory->get('webform_submission'); @@ -55,7 +57,8 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_id, $plugin_definition, $container->get('logger.factory'), - $container->get('http_client') + $container->get('http_client'), + $container->get('os2web_audit.logger'), ); } @@ -121,6 +124,9 @@ public function process(Job $job): JobResult { $this->submissionLogger->notice($this->t('The submission #@serial was successfully delivered', ['@serial' => $webformSubmission->serial()]), $logger_context); + $msg = sprintf('Successfully created FBS patron with cpr %s and guardian with cpr %s. Webform id %s.', $data['barn_cpr'], $data['cpr'], $webformSubmission->getWebform()->id()); + $this->auditLogger->info('FBS', $msg); + return JobResult::success(); } catch (\Exception | GuzzleException $e) { From fc49a163f7f00d7e2eb4a35804367b1907f111e4 Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Mon, 9 Dec 2024 12:43:42 +0100 Subject: [PATCH 4/4] Applied coding standards --- .../src/Helper/DigitalPostHelper.php | 1 + .../os2forms_fasit/src/Helper/FasitHelper.php | 21 ++++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/modules/os2forms_digital_post/src/Helper/DigitalPostHelper.php b/modules/os2forms_digital_post/src/Helper/DigitalPostHelper.php index 173aae0..8681cf3 100644 --- a/modules/os2forms_digital_post/src/Helper/DigitalPostHelper.php +++ b/modules/os2forms_digital_post/src/Helper/DigitalPostHelper.php @@ -77,6 +77,7 @@ public function sendDigitalPost(string $type, Message $message, ?ForsendelseI $f // RecipientID should be the same in Message and Forsendelse, // so fetch it from Message as it is always set. $msg = sprintf('Sent digital post of type %s to %s.', $type, $message->getMessageHeader()->getRecipient()->getRecipientID()); + // If the cause is a submission, add webform id to audit logging message. $msg .= $submission ? sprintf(' Webform id %s.', $submission->getWebform()->id()) : ''; $this->auditLogger->info('DigitalPost', $msg); diff --git a/modules/os2forms_fasit/src/Helper/FasitHelper.php b/modules/os2forms_fasit/src/Helper/FasitHelper.php index fb1b686..2bd84d6 100644 --- a/modules/os2forms_fasit/src/Helper/FasitHelper.php +++ b/modules/os2forms_fasit/src/Helper/FasitHelper.php @@ -35,11 +35,11 @@ class FasitHelper { ]; public function __construct( - private readonly ClientInterface $client, - private readonly EntityTypeManagerInterface $entityTypeManager, - private readonly Settings $settings, - private readonly CertificateLocatorHelper $certificateLocator, - private readonly Logger $auditLogger, + private readonly ClientInterface $client, + private readonly EntityTypeManagerInterface $entityTypeManager, + private readonly Settings $settings, + private readonly CertificateLocatorHelper $certificateLocator, + private readonly Logger $auditLogger, ) { } @@ -239,7 +239,7 @@ private function uploadDocument(array $uploads, string $submissionId, array $han 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()); + $msg = sprintf('Successfully uploaded document %s to cpr %s in Fasit. Webform id %s.', $fasitDocumentTitle, $fasitCpr, $submission->getWebform()->id()); $this->auditLogger->info('Fasit', $msg); } @@ -329,7 +329,7 @@ private function uploadAttachment(string $submissionId, array $handlerConfigurat * The temp filename. * @param string $webformId * The webform id. - * + * * @throws \Drupal\os2forms_fasit\Exception\CertificateLocatorException * Certificate locator exception. * @throws \Drupal\os2forms_fasit\Exception\FasitResponseException @@ -383,9 +383,10 @@ private function uploadFile(string $originalFilename, string $tempFilename, stri 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); + // 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']];