Skip to content

Commit 4ea0f62

Browse files
committed
Added audit logging to Fasit
1 parent b50146e commit 4ea0f62

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ before starting to add changes. Use example [placed in the end of the page](#exa
1212
## [Unreleased]
1313

1414
- Added webform ID to digital post audit logging messages.
15+
- Added audit logging to `os2forms_fasit`
1516

1617
## [3.19.0] 2024-12-06
1718

modules/os2forms_fasit/os2forms_fasit.info.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ dependencies:
77
- drupal:webform
88
- drupal:advancedqueue
99
- os2forms:os2forms_attachment
10+
- os2web:os2web_audit
1011
configure: os2forms_fasit.admin.settings

modules/os2forms_fasit/os2forms_fasit.services.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ services:
1313
- '@entity_type.manager'
1414
- "@Drupal\\os2forms_fasit\\Helper\\Settings"
1515
- "@Drupal\\os2forms_fasit\\Helper\\CertificateLocatorHelper"
16+
- "@os2web_audit.logger"

modules/os2forms_fasit/src/Helper/FasitHelper.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Drupal\os2forms_fasit\Exception\InvalidSettingException;
1212
use Drupal\os2forms_fasit\Exception\InvalidSubmissionException;
1313
use Drupal\os2forms_fasit\Plugin\WebformHandler\FasitWebformHandler;
14+
use Drupal\os2web_audit\Service\Logger;
1415
use Drupal\webform\Entity\WebformSubmission;
1516
use GuzzleHttp\ClientInterface;
1617
use GuzzleHttp\Exception\GuzzleException;
@@ -33,7 +34,13 @@ class FasitHelper {
3334
'managed_file',
3435
];
3536

36-
public function __construct(private readonly ClientInterface $client, private readonly EntityTypeManagerInterface $entityTypeManager, private readonly Settings $settings, private readonly CertificateLocatorHelper $certificateLocator) {
37+
public function __construct(
38+
private readonly ClientInterface $client,
39+
private readonly EntityTypeManagerInterface $entityTypeManager,
40+
private readonly Settings $settings,
41+
private readonly CertificateLocatorHelper $certificateLocator,
42+
private readonly Logger $auditLogger,
43+
) {
3744
}
3845

3946
/**
@@ -231,6 +238,9 @@ private function uploadDocument(array $uploads, string $submissionId, array $han
231238
if (Response::HTTP_OK !== $response->getStatusCode()) {
232239
throw new FasitResponseException(sprintf('Expected status code 200, received %d', $response->getStatusCode()));
233240
}
241+
242+
$msg = sprintf('Successfully uploaded document %s to cpr %s in Fasit. Webform id: %s', $fasitDocumentTitle, $fasitCpr, $submission->getWebform()->id());
243+
$this->auditLogger->info('Fasit', $msg);
234244
}
235245

236246
/**
@@ -307,7 +317,7 @@ private function uploadAttachment(string $submissionId, array $handlerConfigurat
307317
$tempAttachmentFilename = tempnam(sys_get_temp_dir(), 'attachment');
308318
file_put_contents($tempAttachmentFilename, $fileContent);
309319

310-
return $this->uploadFile($fileName, $tempAttachmentFilename);
320+
return $this->uploadFile($fileName, $tempAttachmentFilename, $submission->getWebform()->id());
311321
}
312322

313323
/**
@@ -317,15 +327,17 @@ private function uploadAttachment(string $submissionId, array $handlerConfigurat
317327
* The original filename.
318328
* @param string $tempFilename
319329
* The temp filename.
320-
*
330+
* @param string $webformId
331+
* The webform id.
332+
*
321333
* @throws \Drupal\os2forms_fasit\Exception\CertificateLocatorException
322334
* Certificate locator exception.
323335
* @throws \Drupal\os2forms_fasit\Exception\FasitResponseException
324336
* Fasit response exception.
325337
*
326338
* @phpstan-return array<string, mixed>
327339
*/
328-
private function uploadFile(string $originalFilename, string $tempFilename): array {
340+
private function uploadFile(string $originalFilename, string $tempFilename, string $webformId): array {
329341
$endpoint = sprintf('%s/%s/%s/documents/%s',
330342
$this->settings->getFasitApiBaseUrl(),
331343
$this->settings->getFasitApiTenant(),
@@ -371,6 +383,11 @@ private function uploadFile(string $originalFilename, string $tempFilename): arr
371383
throw new FasitResponseException('Could not get upload id from response');
372384
}
373385

386+
// Note, that this does not mean a document has been sent to a citizen case in Fasit yet.
387+
// This is done later by uploadDocument. The file has simply been made ready.
388+
$msg = sprintf('Successfully uploaded file %s to Fasit. Webform id: %s', $originalFilename, $webformId);
389+
$this->auditLogger->info('Fasit', $msg);
390+
374391
return ['filename' => $originalFilename, 'id' => $content['id']];
375392
}
376393

@@ -392,6 +409,7 @@ private function uploadFileElements(string $submissionId): array {
392409
// Fetch element ids that may contain pdf files.
393410
/** @var \Drupal\webform\Entity\WebformSubmission $submission */
394411
$submission = $this->getSubmission($submissionId);
412+
$webformId = $submission->getWebform()->id();
395413
$fileIds = $this->getFileElementKeysFromSubmission($submission);
396414
$fileStorage = $this->entityTypeManager->getStorage('file');
397415

@@ -412,7 +430,7 @@ private function uploadFileElements(string $submissionId): array {
412430
$tempFilename = tempnam(sys_get_temp_dir(), 'attachment');
413431
file_put_contents($tempFilename, $fileContent);
414432

415-
$uploads[] = $this->uploadFile($filename, $tempFilename);
433+
$uploads[] = $this->uploadFile($filename, $tempFilename, $webformId);
416434
}
417435

418436
return $uploads;

0 commit comments

Comments
 (0)