diff --git a/composer.json b/composer.json index 1507722..38ab90b 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/modules/os2forms_encrypt/os2forms_encrypt.module b/modules/os2forms_encrypt/os2forms_encrypt.module index 516a7fe..9e43af3 100644 --- a/modules/os2forms_encrypt/os2forms_encrypt.module +++ b/modules/os2forms_encrypt/os2forms_encrypt.module @@ -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']); +} diff --git a/modules/os2forms_encrypt/os2forms_encrypt.services.yml b/modules/os2forms_encrypt/os2forms_encrypt.services.yml new file mode 100644 index 0000000..255a0ef --- /dev/null +++ b/modules/os2forms_encrypt/os2forms_encrypt.services.yml @@ -0,0 +1,4 @@ +services: + os2forms_encrypt.encryptor: + class: Drupal\os2forms_encrypt\Helper\Os2FormsEncryptor + arguments: ['@encryption', '@entity_type.manager'] diff --git a/modules/os2forms_encrypt/src/Helper/Os2FormsEncryptor.php b/modules/os2forms_encrypt/src/Helper/Os2FormsEncryptor.php new file mode 100644 index 0000000..b254f33 --- /dev/null +++ b/modules/os2forms_encrypt/src/Helper/Os2FormsEncryptor.php @@ -0,0 +1,66 @@ +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); + + } + +} diff --git a/modules/os2forms_encrypt/src/WebformOs2FormsEncryptSubmissionStorage.php b/modules/os2forms_encrypt/src/WebformOs2FormsEncryptSubmissionStorage.php index 0b0608f..30242be 100644 --- a/modules/os2forms_encrypt/src/WebformOs2FormsEncryptSubmissionStorage.php +++ b/modules/os2forms_encrypt/src/WebformOs2FormsEncryptSubmissionStorage.php @@ -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); @@ -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; @@ -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();