From f7ff39e1ff64674081f99765627b20b7b1ed75df Mon Sep 17 00:00:00 2001 From: Jesper Kristensen Date: Wed, 24 Apr 2024 15:17:57 +0200 Subject: [PATCH] ITKDev: Added user id to logging helper function --- .../Element/WebformAttachmentConsentXml.php | 2 +- os2forms.module | 20 +++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/modules/os2forms_consent/src/Element/WebformAttachmentConsentXml.php b/modules/os2forms_consent/src/Element/WebformAttachmentConsentXml.php index 9f7eaa6f..35e81977 100644 --- a/modules/os2forms_consent/src/Element/WebformAttachmentConsentXml.php +++ b/modules/os2forms_consent/src/Element/WebformAttachmentConsentXml.php @@ -45,7 +45,7 @@ public static function getFileContent(array $element, WebformSubmissionInterface /** @var \Drupal\os2web_datalookup\Plugin\DataLookupManager $os2web_datalookup_plugins */ $os2web_datalookup_plugins = \Drupal::service('plugin.manager.os2web_datalookup'); - /** @var \Drupal\os2web_datalookup\Plugin\os2web\DataLookup\DataLookupCPRInterface $cprPlugin */ + /** @var \Drupal\os2web_datalookup\Plugin\os2web\DataLookup\DataLookupInterfaceCpr $cprPlugin */ $cprPlugin = $os2web_datalookup_plugins->createDefaultInstanceByGroup('cpr_lookup'); if (!empty($nemid_cpr) && $cprPlugin->isReady()) { diff --git a/os2forms.module b/os2forms.module index 8f22ac25..d3751ba8 100644 --- a/os2forms.module +++ b/os2forms.module @@ -370,15 +370,27 @@ function os2forms_module_implements_alter(&$implementations, $hook) { * The timestamp of the log entry. * @param string $line * The log message. + * @param bool $logUser + * Log information about the current logged-in user (need to track who has + * lookup information in external services). Default: false. * @param array $metadata * Additional metadata for the log entry. - * - * @throws \Drupal\Component\Plugin\Exception\PluginException */ -function os2forms_audit_log(string $type, int $timestamp, string $line, array $metadata = []): void { +function os2forms_audit_log(string $type, int $timestamp, string $line, bool $logUser = false, array $metadata = []): void { if ( \Drupal::service('module_handler')->moduleExists('os2form_audit')) { + if ($logUser) { + // Add user id to the log message. + $user = \Drupal::currentUser(); + $metadata['userId'] = $user->id(); + } + /** @var \Drupal\os2forms_audit\Service\Logger $logger */ $logger = \Drupal::service('os2form_audit.logger'); - $logger->log($type, $timestamp, $line, $metadata); + try { + $logger->log($type, $timestamp, $line, $metadata); + } + catch (\Drupal\Component\Plugin\Exception\PluginException $e) { + // @todo: what should we do when plugins fails. + } } }