Skip to content

Commit

Permalink
Merge pull request #147 from OS2Forms/feature/more-audit-logging
Browse files Browse the repository at this point in the history
Feature/more audit logging
  • Loading branch information
jekuaitk authored Dec 9, 2024
2 parents d9f7776 + fc49a16 commit 78259d0
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ 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`
- Added audit logging to `os2forms_fbs_handler`

## [3.19.0] 2024-12-06

- Ensured installation of `os2web_audit`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ 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());
// 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);

return [$response, $service->getLastKombiMeMoMessage()];
Expand Down
1 change: 1 addition & 0 deletions modules/os2forms_fasit/os2forms_fasit.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ dependencies:
- drupal:webform
- drupal:advancedqueue
- os2forms:os2forms_attachment
- os2web:os2web_audit
configure: os2forms_fasit.admin.settings
1 change: 1 addition & 0 deletions modules/os2forms_fasit/os2forms_fasit.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ services:
- '@entity_type.manager'
- "@Drupal\\os2forms_fasit\\Helper\\Settings"
- "@Drupal\\os2forms_fasit\\Helper\\CertificateLocatorHelper"
- "@os2web_audit.logger"
27 changes: 23 additions & 4 deletions modules/os2forms_fasit/src/Helper/FasitHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
) {
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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());
}

/**
Expand All @@ -317,6 +327,8 @@ 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.
Expand All @@ -325,7 +337,7 @@ private function uploadAttachment(string $submissionId, array $handlerConfigurat
*
* @phpstan-return array<string, mixed>
*/
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(),
Expand Down Expand Up @@ -371,6 +383,12 @@ 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']];
}

Expand All @@ -392,6 +410,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');

Expand All @@ -412,7 +431,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;
Expand Down
1 change: 1 addition & 0 deletions modules/os2forms_fbs_handler/os2forms_fbs_handler.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ core_version_requirement: ^9 || ^10
dependencies:
- 'webform:webform'
- 'advancedqueue:advancedqueue'
- 'os2web:os2web_audit'
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand All @@ -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'),
);
}

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 78259d0

Please sign in to comment.