Skip to content

Commit

Permalink
Added audit logging to Fasit
Browse files Browse the repository at this point in the history
  • Loading branch information
jekuaitk committed Dec 9, 2024
1 parent b50146e commit 4ea0f62
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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`

## [3.19.0] 2024-12-06

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"
28 changes: 23 additions & 5 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,15 +327,17 @@ 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.
* @throws \Drupal\os2forms_fasit\Exception\FasitResponseException
* Fasit response exception.
*
* @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,11 @@ 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 +409,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 +430,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

0 comments on commit 4ea0f62

Please sign in to comment.