-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from OS2Forms/develop
Release 3.15.4
- Loading branch information
Showing
10 changed files
with
586 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# OS2Forms Encrypt module | ||
|
||
This module extends and modifies upon [Webform Encrypt](https://www.drupal.org/project/webform_encrypt) | ||
to provide encryption of webform element values in the database. | ||
|
||
## Modifications from the base Webform Encrypt module | ||
|
||
### Encryption time | ||
|
||
Any computed elements, e.g. Computed Twig, may cause issues as | ||
their values are attempted computed after encryption. If any calculations | ||
are done this could result in runtime TypeErrors. | ||
|
||
This is handled by modifying the time at which decryption is made, in | ||
`WebformOs2FormsEncryptSubmissionStorage`. | ||
|
||
### Permissions | ||
|
||
The Webform Encrypt module introduces a `view encrypted values` permission. | ||
This permission should be granted to roles that need to view encrypted values. | ||
|
||
**Note**, that in Drupal 9 and newer drush commands are ran as an | ||
anonymous user. This means the anonymous user needs this permission, if | ||
at any point they need values from submissions to do their job. | ||
|
||
### Configurable per element | ||
|
||
The Webform Encrypt module allows configuration on element level. That is, | ||
webform builders can actively enable and disable for each element. | ||
|
||
We want all elements to be encrypted whenever encryption is enabled. | ||
This is done by `os2forms_encrypt_webform_presave` and `os2forms_encrypt_form_alter` in | ||
`os2forms_encrypt.module`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
services: | ||
os2forms_encrypt.encryptor: | ||
class: Drupal\os2forms_encrypt\Helper\Os2FormsEncryptor | ||
arguments: ['@encryption', '@entity_type.manager', '@config.factory'] | ||
|
||
os2forms_encrypt.form_helper: | ||
class: Drupal\os2forms_encrypt\Helper\FormHelper | ||
|
||
os2forms_encrypt.settings_form: | ||
class: Drupal\os2forms_encrypt\Form\SettingsForm | ||
arguments: ['@config.factory', '@encrypt.encryption_profile.manager'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Drupal\os2forms_encrypt\Helper; | ||
|
||
use Drupal\Core\Form\FormStateInterface; | ||
|
||
/** | ||
* Form helper class. | ||
*/ | ||
class FormHelper { | ||
|
||
/** | ||
* Removes 'element_encrypt' element from element forms. | ||
* | ||
* @param array $form | ||
* The form. | ||
* @param \Drupal\Core\Form\FormStateInterface $form_state | ||
* The form state. | ||
* @param string $form_id | ||
* The form id. | ||
*/ | ||
public function formAlter(array &$form, FormStateInterface $form_state, string $form_id) { | ||
if ('webform_ui_element_form' === $form_id) { | ||
if (isset($form['element_encrypt'])) { | ||
$form['element_encrypt']['#access'] = FALSE; | ||
} | ||
} | ||
} | ||
|
||
} |
106 changes: 106 additions & 0 deletions
106
modules/os2forms_encrypt/src/Helper/Os2FormsEncryptor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
namespace Drupal\os2forms_encrypt\Helper; | ||
|
||
use Drupal\Core\Config\ConfigFactoryInterface; | ||
use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
use Drupal\encrypt\EncryptServiceInterface; | ||
use Drupal\encrypt\Entity\EncryptionProfile; | ||
use Drupal\os2forms_encrypt\Form\SettingsForm; | ||
use Drupal\webform\Entity\Webform; | ||
use Drupal\webform\WebformInterface; | ||
|
||
/** | ||
* The Os2FormsEncryptor class. | ||
*/ | ||
class Os2FormsEncryptor { | ||
|
||
/** | ||
* The encryption Service. | ||
* | ||
* @var \Drupal\encrypt\EncryptServiceInterface | ||
*/ | ||
private EncryptServiceInterface $encryptionService; | ||
|
||
/** | ||
* The entity type manager. | ||
* | ||
* @var \Drupal\Core\Entity\EntityTypeManagerInterface | ||
*/ | ||
private EntityTypeManagerInterface $entityTypeManager; | ||
|
||
/** | ||
* The config factory. | ||
* | ||
* @var \Drupal\Core\Config\ConfigFactoryInterface | ||
*/ | ||
private ConfigFactoryInterface $configFactory; | ||
|
||
public function __construct(EncryptServiceInterface $encryptService, EntityTypeManagerInterface $entityTypeManager, ConfigFactoryInterface $configFactory) { | ||
$this->encryptionService = $encryptService; | ||
$this->entityTypeManager = $entityTypeManager; | ||
$this->configFactory = $configFactory; | ||
} | ||
|
||
/** | ||
* Encrypts value if element is configured to be encrypted. | ||
* | ||
* @param string $value | ||
* The value that should be encrypted. | ||
* @param string $element | ||
* The element. | ||
* @param string $webformId | ||
* The webform id. | ||
* | ||
* @return string | ||
* The resulting value. | ||
*/ | ||
public function encryptValue(string $value, string $element, string $webformId): string { | ||
/** @var \Drupal\webform\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); | ||
} | ||
|
||
/** | ||
* Enables encrypt on all elements of webform. | ||
* | ||
* @param \Drupal\webform\WebformInterface $webform | ||
* The webform. | ||
*/ | ||
public function enableEncryption(WebformInterface $webform): void { | ||
|
||
// Check that encryption is enabled. | ||
$config = $this->configFactory->get(SettingsForm::$configName); | ||
if (!$config->get('enabled') || !$webform instanceof Webform) { | ||
return; | ||
} | ||
|
||
// Check that there are any elements to enable encryption on. | ||
$elements = $webform->getElementsDecoded(); | ||
|
||
if (empty($elements)) { | ||
return; | ||
} | ||
|
||
$encryptedElements = array_map(static fn () => [ | ||
'encrypt' => TRUE, | ||
'encrypt_profile' => $config->get('default_encryption_profile'), | ||
], $elements); | ||
|
||
$webform->setThirdPartySetting('webform_encrypt', 'element', $encryptedElements); | ||
} | ||
|
||
} |
Oops, something went wrong.