Skip to content

Commit

Permalink
Added auditl logging to emails
Browse files Browse the repository at this point in the history
  • Loading branch information
jekuaitk committed Dec 9, 2024
1 parent 82056a2 commit 07eff8d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Nedenfor ses dato for release og beskrivelse af opgaver som er implementeret.

## [Under udvikling]

* Audit logging af e-mails.
* Tilføjede update site tjek til GitHub Actions.
* Opdaterede
[OS2Forms GetOrganized](https://github.com/OS2Forms/os2forms_get_organized) version.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace Drupal\os2forms_email_handler\Plugin\WebformHandler;

use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Site\Settings;
use Drupal\file\Entity\File;
use Drupal\os2forms_email_handler\Helper\WebformHelper;
use Drupal\os2web_audit\Service\Logger;
use Drupal\webform\Plugin\WebformHandler\EmailWebformHandler;
use Drupal\webform\WebformSubmissionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Emails a webform submission.
Expand All @@ -21,8 +24,7 @@
* tokens = TRUE,
* )
*/
class OS2FormsEmailWebformHandler extends EmailWebformHandler {

class OS2FormsEmailWebformHandler extends EmailWebformHandler implements ContainerFactoryPluginInterface {
/**
* File element types.
*/
Expand All @@ -38,6 +40,33 @@ class OS2FormsEmailWebformHandler extends EmailWebformHandler {
private const DEFAULT_FROM_EMAIL = '[email protected]';
private const DEFAULT_FROM_NAME = 'Selvbetjening';

/**
* The audit logger.
*
* @var \Drupal\os2web_audit\Service\Logger
*/
protected Logger $auditLogger;
// /**
// * {@inheritdoc}
// */
// public function __construct(
// array $configuration,
// $plugin_id,
// $plugin_definition,
// private readonly Logger $auditLogger
// ) {
// parent::__construct($configuration, $plugin_id, $plugin_definition);
// }

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->auditLogger = $container->get('os2web_audit.logger');
return $instance;
}

/**
* Sends extra notification based on attachment file size.
*
Expand All @@ -47,7 +76,6 @@ class OS2FormsEmailWebformHandler extends EmailWebformHandler {
* An array of message parameters.
*/
public function sendMessage(WebformSubmissionInterface $webform_submission, array $message) {

$webform = $webform_submission->getWebform();
$settings = $webform->getThirdPartySetting('os2forms', WebformHelper::MODULE_NAME);

Expand All @@ -57,7 +85,19 @@ public function sendMessage(WebformSubmissionInterface $webform_submission, arra
$sendOriginalMessage = !$this->handleAttachmentNotification($webform_submission, $message, $settings['email_recipients']);
}

return $sendOriginalMessage ? parent::sendMessage($webform_submission, $message) : FALSE;
if ($sendOriginalMessage) {
$result = parent::sendMessage($webform_submission, $message);

if ($result) {
$msg = sprintf('Email, %s, sent to %s. Webform id %s.', $message['subject'], $message['to_mail'], $webform_submission->getWebform()->id());
$this->auditLogger->info('Email', $msg);
}

return $result;
}
else {
return FALSE;
}
}

/**
Expand Down Expand Up @@ -261,6 +301,11 @@ private function sendFileSizeNotification(WebformSubmissionInterface $webform_su

$result = parent::sendMessage($webform_submission, $notificationMessage);

if ($result) {
$msg = sprintf('Email, %s, sent to %s. Webform id %s.', $notificationMessage['subject'], $notificationMessage['to_mail'], $webform_submission->getWebform()->id());
$this->auditLogger->info('Email', $msg);
}

if ($webform_submission->getWebform()->hasSubmissionLog() && $result) {
// Log detailed message to the 'webform_submission' log.
$this->getLogger('webform_submission')->notice("Email notification advising surpassed file sizes sent to '@email'.", $context);
Expand Down

0 comments on commit 07eff8d

Please sign in to comment.