forked from d8-contrib-modules/field_encrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield_encrypt.module
109 lines (95 loc) · 3.89 KB
/
field_encrypt.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* Contains module hooks for field_encrypt
*/
/**
* Implements hook_form_alter.
*
* Add a field to the field storage configuration forms to allow setting the encryption state.
*/
function field_encrypt_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
// If this is the add or edit form for field_storage, we call our function.
if (in_array($form_id, ['field_storage_add_form', 'field_storage_config_edit_form'])) {
// Check permissions
$user = \Drupal::currentUser();
if ($user->hasPermission('administer field encryption')) {
/**
* @var $field \Drupal\field\Entity\FieldStorageConfig
*/
$field = $form_state->getFormObject()->getEntity();
// Add our encrypted field to the form.
$form['encrypt'] = [
'#type' => 'checkbox',
'#title' => t('Encrypted'),
'#description' => t('Makes the field storage encrypted.'),
'#default_value' => $field->getThirdPartySetting('field_encrypt', 'encrypt', FALSE),
];
// We add a function to process the form when it is saved.
$form['#entity_builders'][] = 'field_encrypt_form_field_add_form_builder';
}
}
}
/**
* Update the field storage configuration to set the encryption state.
*
* @param $entity_type
* @param \Drupal\field\Entity\FieldStorageConfig $fieldStorageConfig
* @param $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
*/
function field_encrypt_form_field_add_form_builder($entity_type, \Drupal\field\Entity\FieldStorageConfig $fieldStorageConfig, &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
$form_encryption = $form_state->getValue('encrypt');
$original_encryption = $fieldStorageConfig->getThirdPartySetting('field_encrypt', 'encrypt');
// If the form has the value, we set it.
if ($form_encryption === 1) {
$fieldStorageConfig->setThirdPartySetting('field_encrypt', 'encrypt', $form_encryption);
}
else {
// If there is no value, remove.
$fieldStorageConfig->unsetThirdPartySetting('field_encrypt', 'encrypt');
}
if ($original_encryption !== $fieldStorageConfig->getThirdPartySetting('field_encrypt', 'encrypt')) {
// We need to process the field to either encrypt or decrypt the stored fields if the setting was changed.
$field_name = $fieldStorageConfig->get('field_name');
$field_entity_type = $fieldStorageConfig->get('entity_type');
/**
* @var $field_encrypt_process_entities \Drupal\field_encrypt\FieldEncryptProcessEntities
*/
$field_encrypt_process_entities = \Drupal::service('field_encrypt.process_entities');
if ($form_encryption === 1) {
$field_encrypt_process_entities->encrypt_stored_field($field_entity_type, $field_name);
}
elseif ($form_encryption === 0) {
$field_encrypt_process_entities->decrypt_stored_field($field_entity_type, $field_name);
}
}
}
/**
* Encrypt fields before they are saved.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
*/
function field_encrypt_entity_presave(\Drupal\Core\Entity\EntityInterface $entity) {
if (!($entity instanceof Drupal\Core\Entity\ContentEntityInterface)) {return;}
/**
* @var $field_encrypt_process_entities \Drupal\field_encrypt\FieldEncryptProcessEntities
*/
$field_encrypt_process_entities = \Drupal::service('field_encrypt.process_entities');
$field_encrypt_process_entities->encrypt_entity($entity);
}
/**
* Decrypt fields before they are rendered.
*
* @param $entities
* @param $entity_type
*/
function field_encrypt_entity_load($entities, $entity_type) {
/**
* @var $field_encrypt_process_entities \Drupal\field_encrypt\FieldEncryptProcessEntities
*/
$field_encrypt_process_entities = \Drupal::service('field_encrypt.process_entities');
foreach($entities as &$entity) {
if (!($entity instanceof Drupal\Core\Entity\ContentEntityInterface)) {continue;}
$field_encrypt_process_entities->decrypt_entity($entity);
}
}