From 82b4e4a6cab1ce99267c2da1613c8f0f13630fef Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Fri, 22 Nov 2024 10:57:23 +0100 Subject: [PATCH 1/5] Ensured audit log is installed --- os2forms.install | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/os2forms.install b/os2forms.install index d7d9975..9bf96ab 100644 --- a/os2forms.install +++ b/os2forms.install @@ -222,3 +222,12 @@ function _os2form_install_init_area_terms() { ])->save(); } } + +/** + * Implements hook_update_N(). + * + * Enable os2web_audit module. + */ +function os2forms_update_103001() { + \Drupal::service('module_installer')->install(['os2web_audit']); +} From ddd0742e2e5d5d276417d1187fa023575c7d33f2 Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Fri, 22 Nov 2024 11:25:46 +0100 Subject: [PATCH 2/5] Audit logged digital post --- .../os2forms_digital_post/os2forms_digital_post.info.yml | 1 + .../os2forms_digital_post.services.yml | 1 + .../src/Helper/DigitalPostHelper.php | 8 ++++++++ 3 files changed, 10 insertions(+) diff --git a/modules/os2forms_digital_post/os2forms_digital_post.info.yml b/modules/os2forms_digital_post/os2forms_digital_post.info.yml index 39ebe91..71a1768 100644 --- a/modules/os2forms_digital_post/os2forms_digital_post.info.yml +++ b/modules/os2forms_digital_post/os2forms_digital_post.info.yml @@ -9,5 +9,6 @@ dependencies: - 'os2web_datalookup:os2web_datalookup' - 'webform:webform' - 'webform:webform_submission_log' + - 'os2web:os2web_audit' configure: os2forms_digital_post.admin.settings diff --git a/modules/os2forms_digital_post/os2forms_digital_post.services.yml b/modules/os2forms_digital_post/os2forms_digital_post.services.yml index a40d88a..c13fb96 100644 --- a/modules/os2forms_digital_post/os2forms_digital_post.services.yml +++ b/modules/os2forms_digital_post/os2forms_digital_post.services.yml @@ -37,6 +37,7 @@ services: - "@Drupal\\os2forms_digital_post\\Helper\\BeskedfordelerHelper" - "@logger.channel.os2forms_digital_post" - "@logger.channel.os2forms_digital_post_submission" + - "@os2web_audit.logger" Drupal\os2forms_digital_post\Helper\WebformHelperSF1601: arguments: diff --git a/modules/os2forms_digital_post/src/Helper/DigitalPostHelper.php b/modules/os2forms_digital_post/src/Helper/DigitalPostHelper.php index 07b81ab..2e0d563 100644 --- a/modules/os2forms_digital_post/src/Helper/DigitalPostHelper.php +++ b/modules/os2forms_digital_post/src/Helper/DigitalPostHelper.php @@ -5,6 +5,7 @@ use DigitalPost\MeMo\Message; use Drupal\Core\Logger\LoggerChannelInterface; use Drupal\os2forms_digital_post\Exception\RuntimeException; +use Drupal\os2web_audit\Service\Logger; use Drupal\os2web_datalookup\LookupResult\CompanyLookupResult; use Drupal\os2web_datalookup\LookupResult\CprLookupResult; use Drupal\os2web_datalookup\Plugin\DataLookupManager; @@ -35,6 +36,7 @@ public function __construct( private readonly BeskedfordelerHelper $beskedfordelerHelper, private readonly LoggerChannelInterface $logger, private readonly LoggerChannelInterface $submissionLogger, + private readonly Logger $auditLogger, ) { } @@ -64,6 +66,7 @@ public function sendDigitalPost(string $type, Message $message, ?ForsendelseI $f ]; $service = new SF1601($options); $transactionId = Serializer::createUuid(); + $response = $service->kombiPostAfsend($transactionId, $type, $message, $forsendelse); $content = (string) $response->getContent(); @@ -71,6 +74,11 @@ public function sendDigitalPost(string $type, Message $message, ?ForsendelseI $f $this->beskedfordelerHelper->createMessage($submission->id(), $message, $content); } + // 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()); + $this->auditLogger->info('DigitalPost', $msg); + return [$response, $service->getLastKombiMeMoMessage()]; } From 80d2ebd97a7a6fb8eecb411874e7a47e69dad389 Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Fri, 22 Nov 2024 15:28:40 +0100 Subject: [PATCH 3/5] Updated CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 47ebf14..9e4e340 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ before starting to add changes. Use example [placed in the end of the page](#exa ## [Unreleased] +- Ensured installation of `os2web_audit` + - `os2web_audit` has default logging through `watchdog` +- Adds audit logging to `os2forms_digital_post` + ## [3.17.0] 2024-11-21 - Updated `os2web/os2web_audit` version From 8ccdb914d7969ecb81c3bbe6775207bff99394e1 Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Fri, 6 Dec 2024 10:21:29 +0100 Subject: [PATCH 4/5] Added audit logging to os2forms_nemid --- CHANGELOG.md | 1 + .../os2forms_nemid/os2forms_nemid.info.yml | 1 + .../os2forms_nemid.services.yml | 2 +- .../src/Service/FormsHelper.php | 56 ++++++++++++++++++- 4 files changed, 57 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e4e340..fa29fb6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ before starting to add changes. Use example [placed in the end of the page](#exa - Ensured installation of `os2web_audit` - `os2web_audit` has default logging through `watchdog` - Adds audit logging to `os2forms_digital_post` +- Adds audit logging to `os2forms_nemid` ## [3.17.0] 2024-11-21 diff --git a/modules/os2forms_nemid/os2forms_nemid.info.yml b/modules/os2forms_nemid/os2forms_nemid.info.yml index 2a2e81f..3523807 100644 --- a/modules/os2forms_nemid/os2forms_nemid.info.yml +++ b/modules/os2forms_nemid/os2forms_nemid.info.yml @@ -8,3 +8,4 @@ dependencies: - 'drupal:os2forms' - 'drupal:os2web_nemlogin' - 'os2web_datalookup:os2web_datalookup' + - 'os2web:os2web_audit' diff --git a/modules/os2forms_nemid/os2forms_nemid.services.yml b/modules/os2forms_nemid/os2forms_nemid.services.yml index 5e159bb..7c07b50 100644 --- a/modules/os2forms_nemid/os2forms_nemid.services.yml +++ b/modules/os2forms_nemid/os2forms_nemid.services.yml @@ -6,4 +6,4 @@ services: - {name: event_subscriber} os2forms_nemid.forms_helper: class: Drupal\os2forms_nemid\Service\FormsHelper - arguments: ['@os2web_nemlogin.auth_provider', '@plugin.manager.os2web_datalookup', '@current_route_match'] + arguments: ['@os2web_nemlogin.auth_provider', '@plugin.manager.os2web_datalookup', '@current_route_match', '@os2web_audit.logger'] diff --git a/modules/os2forms_nemid/src/Service/FormsHelper.php b/modules/os2forms_nemid/src/Service/FormsHelper.php index a2c435c..74ab925 100644 --- a/modules/os2forms_nemid/src/Service/FormsHelper.php +++ b/modules/os2forms_nemid/src/Service/FormsHelper.php @@ -8,9 +8,11 @@ use Drupal\os2forms_nemid\Element\NemidCompanyCvrFetchData; use Drupal\os2forms_nemid\Element\NemidCompanyPNumber; use Drupal\os2forms_nemid\Element\NemidCprFetchData; +use Drupal\os2web_audit\Service\Logger; use Drupal\os2web_datalookup\LookupResult\CompanyLookupResult; use Drupal\os2web_datalookup\LookupResult\CprLookupResult; use Drupal\os2web_datalookup\Plugin\DataLookupManager; +use Drupal\os2web_nemlogin\Plugin\AuthProviderInterface; use Drupal\os2web_nemlogin\Service\AuthProviderService; use Drupal\webform\WebformSubmissionInterface; @@ -70,6 +72,13 @@ class FormsHelper { */ private RouteMatchInterface $routeMatch; + /** + * An audit logger. + * + * @var \Drupal\os2web_audit\Service\Logger + */ + private Logger $auditLogger; + /** * Constructor. * @@ -79,11 +88,19 @@ class FormsHelper { * Datalookup plugin manager. * @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch * Route match service. + * @param \Drupal\os2web_audit\Service\Logger $auditLogger + * Audit logger. */ - public function __construct(AuthProviderService $authProviderService, DataLookupManager $dataLookPluginManager, RouteMatchInterface $routeMatch) { + public function __construct( + AuthProviderService $authProviderService, + DataLookupManager $dataLookPluginManager, + RouteMatchInterface $routeMatch, + Logger $auditLogger, + ) { $this->authProviderService = $authProviderService; $this->dataLookManager = $dataLookPluginManager; $this->routeMatch = $routeMatch; + $this->auditLogger = $auditLogger; } /** @@ -126,6 +143,14 @@ public function retrieveCprLookupResult(FormStateInterface $form_state) { } } + // We need the auth provider for logging purposes. + $authProviderPlugin = $this->getAuthProvider($form_state); + + $userCpr = $authProviderPlugin->fetchValue('cpr'); + $lookedUpCpr = $cprLookupResult->getCpr(); + + $this->auditLogger->info('DataLookup', 'User with cpr ' . $userCpr . ' looked at cpr ' . $lookedUpCpr); + return $cprLookupResult; } @@ -196,7 +221,7 @@ public function lookupPersonData(FormStateInterface $form_state) { /** * Retrieves the CompanyLookupResult which is stored in form_state. * - * If there is no CBVRLookupResult, it is requested and saved for future uses. + * If there is no CVRLookupResult, it is requested and saved for future uses. * * @param \Drupal\Core\Form\FormStateInterface $form_state * Form state. @@ -408,4 +433,31 @@ public function webformSubmissionPrepareForm(WebformSubmissionInterface $webform } } + /** + * Get active auth provider plugin. + * + * @param \Drupal\Core\Form\FormStateInterface $form_state + * Form state. + * + * @return \Drupal\os2web_nemlogin\Plugin\AuthProviderInterface + * The active auth provider plugin. + * + * @throws \Drupal\Component\Plugin\Exception\PluginException + */ + private function getAuthProvider(FormStateInterface $form_state): AuthProviderInterface { + /** @var \Drupal\webform\WebformSubmissionInterface Interface $webformSubmission */ + $webformSubmission = $form_state->getFormObject()->getEntity(); + /** @var \Drupal\webform\WebformInterface $webform */ + $webform = $webformSubmission->getWebform(); + $webformNemidSettings = $webform->getThirdPartySetting('os2forms', 'os2forms_nemid'); + + // Getting auth plugin ID override. + $authPluginId = NULL; + if (!empty($webformNemidSettings['session_type'])) { + $authPluginId = $webformNemidSettings['session_type']; + } + + return ($authPluginId) ? $this->authProviderService->getPluginInstance($authPluginId) : $this->authProviderService->getActivePlugin(); + } + } From db18d910f6ba350732a8b4b6327009ff2f052d19 Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Fri, 6 Dec 2024 10:38:27 +0100 Subject: [PATCH 5/5] Adjusted audit logging message --- modules/os2forms_nemid/src/Service/FormsHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/os2forms_nemid/src/Service/FormsHelper.php b/modules/os2forms_nemid/src/Service/FormsHelper.php index 74ab925..d6788e8 100644 --- a/modules/os2forms_nemid/src/Service/FormsHelper.php +++ b/modules/os2forms_nemid/src/Service/FormsHelper.php @@ -149,7 +149,7 @@ public function retrieveCprLookupResult(FormStateInterface $form_state) { $userCpr = $authProviderPlugin->fetchValue('cpr'); $lookedUpCpr = $cprLookupResult->getCpr(); - $this->auditLogger->info('DataLookup', 'User with cpr ' . $userCpr . ' looked at cpr ' . $lookedUpCpr); + $this->auditLogger->info('DataLookup', 'User with cpr ' . $userCpr . ' looked up cpr ' . $lookedUpCpr); return $cprLookupResult; }