-
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.
- Loading branch information
Showing
5 changed files
with
87 additions
and
9 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,4 @@ | ||
services: | ||
os2forms_encrypt.encryptor: | ||
class: Drupal\os2forms_encrypt\Helper\Os2FormsEncryptor | ||
arguments: ['@encryption', '@entity_type.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
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); | ||
|
||
} | ||
|
||
} |
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