Skip to content

Commit

Permalink
1062: Encrypt computed elements
Browse files Browse the repository at this point in the history
  • Loading branch information
jekuaitk committed Jun 25, 2024
1 parent f5090b0 commit 34432c5
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 9 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@
"2733781 - Add Export to Word Support": "https://www.drupal.org/files/issues/2019-11-22/2733781-47.patch"
},
"drupal/webform": {
"Unlock possibility of using Entity print module export to Word": "https://www.drupal.org/files/issues/2020-02-29/3096552-6.patch"
"Unlock possibility of using Entity print module export to Word": "https://www.drupal.org/files/issues/2020-02-29/3096552-6.patch",
"Webform computed element post save alter": "https://www.drupal.org/files/issues/2024-06-25/webform_computed_post_save_field_alter.patch"
},
"drupal/user_default_page": {
"Warning: in_array() expects parameter 2 to be array, null given in user_default_page_user_logout() (https://www.drupal.org/node/3246986)": "https://www.drupal.org/files/issues/2021-11-01/user_default_page-3246986-2.patch"
Expand Down
12 changes: 12 additions & 0 deletions modules/os2forms_encrypt/os2forms_encrypt.module
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,15 @@ function os2forms_encrypt_entity_type_alter(array &$entity_types): void {
$entity_types['webform_submission']->setStorageClass('Drupal\os2forms_encrypt\WebformOs2FormsEncryptSubmissionStorage');
}
}

/**
* Implements hook_webform_computed_post_save_field_alter().
*
* Ensure encryption of computed element values.
*/
function os2forms_encrypt_webform_computed_post_save_field_alter(array &$fields): void {
/** @var \Drupal\os2forms_encrypt\Helper\Os2FormsEncryptor $os2formsEncryptor */
$os2formsEncryptor = \Drupal::service('os2forms_encrypt.encryptor');

$fields['value'] = $os2formsEncryptor->encryptValue($fields['value'], $fields['name'], $fields['webform_id']);
}
4 changes: 4 additions & 0 deletions modules/os2forms_encrypt/os2forms_encrypt.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
services:
os2forms_encrypt.encryptor:
class: Drupal\os2forms_encrypt\Helper\Os2FormsEncryptor
arguments: ['@encryption', '@entity_type.manager']
66 changes: 66 additions & 0 deletions modules/os2forms_encrypt/src/Helper/Os2FormsEncryptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Drupal\os2forms_encrypt\Helper;

use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\encrypt\EncryptServiceInterface;
use Drupal\encrypt\Entity\EncryptionProfile;
use Drupal\webform\Entity\WebformSubmission;
use Drupal\webform\WebformInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class Os2FormsEncryptor {

/**
* The encryption Service.
*
* @var \Drupal\encrypt\EncryptServiceInterface
*/
private EncryptServiceInterface $encryptionService;

/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
private EntityTypeManagerInterface $entityTypeManager;


public function __construct(EncryptServiceInterface $encryptService, EntityTypeManagerInterface $entityTypeManager) {
$this->encryptionService = $encryptService;
$this->entityTypeManager = $entityTypeManager;
}

/**
* @param string $value
* The value that should be encrypted.
* @param string $element
* The element.
* @param string $webformId
* The webform id.
*
* @return string
* The encrypted string if element is configured to be encrypted.
*/
public function encryptValue(string $value, string $element, string $webformId): string {
/** @var WebformInterface $webform */
$webform = $this->entityTypeManager->getStorage('webform')->load($webformId);

$config = $webform->getThirdPartySetting('webform_encrypt', 'element');
$encryption_profile = isset($config[$element]) ? EncryptionProfile::load($config[$element]['encrypt_profile']) : FALSE;

if (!$encryption_profile) {
return $value;
}

$encrypted_data = [
'data' => base64_encode($this->encryptionService->encrypt($value, $encryption_profile)),
'encrypt_profile' => $encryption_profile->id(),
];

return serialize($encrypted_data);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,6 @@ protected function loadData(array &$webform_submissions) {
}
}


foreach ($submissions_data as $sid => $submission_data) {
$this->decryptChildren($submission_data);
$webform_submissions[$sid]->setData($submission_data);
Expand Down Expand Up @@ -372,7 +371,8 @@ public function encryptElements(array $data, WebformInterface $webform) {
else {
if (is_null($value)) {
$data[$element_name] = $value;
} else {
}
else {
$encrypted_value = $this->encrypt($value, $encryption_profile);
// Save the encrypted data value.
$data[$element_name] = $encrypted_value;
Expand All @@ -392,14 +392,9 @@ public function encryptElements(array $data, WebformInterface $webform) {
* @see Drupal\webform\WebformSubmissionStorage::doPreSave
* @see Drupal\Core\Entity\ContentEntityStorageBase::doPreSave
* @see Drupal\Core\Entity\EntityStorageBase::doPreSave
*
*/
protected function doPreSave(EntityInterface $entity)
{
protected function doPreSave(EntityInterface $entity) {
/** @var \Drupal\webform\WebformSubmissionInterface $entity */

// From ContentEntityStorageBase.php.

// Sync the changes made in the fields array to the internal values array.
$entity->updateOriginalValues();

Expand Down

0 comments on commit 34432c5

Please sign in to comment.