|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\uceap_logging\Form; |
| 4 | + |
| 5 | +use Drupal\Core\Form\ConfigFormBase; |
| 6 | +use Drupal\Core\Form\FormStateInterface; |
| 7 | + |
| 8 | +/** |
| 9 | + * Configure UCEAP Logging settings. |
| 10 | + */ |
| 11 | +class LoggingSettingsForm extends ConfigFormBase { |
| 12 | + |
| 13 | + /** |
| 14 | + * {@inheritdoc} |
| 15 | + */ |
| 16 | + public function getFormId() { |
| 17 | + return 'uceap_logging_settings'; |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * {@inheritdoc} |
| 22 | + */ |
| 23 | + protected function getEditableConfigNames() { |
| 24 | + return ['uceap_logging.settings']; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * {@inheritdoc} |
| 29 | + */ |
| 30 | + public function buildForm(array $form, FormStateInterface $form_state) { |
| 31 | + $config = $this->config('uceap_logging.settings'); |
| 32 | + |
| 33 | + $form['sensitive_fields'] = [ |
| 34 | + '#type' => 'textarea', |
| 35 | + '#title' => $this->t('Sensitive Fields'), |
| 36 | + '#description' => $this->t('Enter field machine names (one per line) that should have their values masked in entity change logs. When these fields are modified, they will appear in logs with masked values (e.g., ***MASKED***) instead of actual values.'), |
| 37 | + '#default_value' => implode("\n", $config->get('sensitive_fields') ?? []), |
| 38 | + '#rows' => 10, |
| 39 | + ]; |
| 40 | + |
| 41 | + $form['help'] = [ |
| 42 | + '#type' => 'details', |
| 43 | + '#title' => $this->t('Examples'), |
| 44 | + '#open' => FALSE, |
| 45 | + ]; |
| 46 | + |
| 47 | + $form['help']['examples'] = [ |
| 48 | + '#markup' => $this->t('<p>Common sensitive fields include:</p> |
| 49 | + <ul> |
| 50 | + <li><code>field_ssn</code> - Social Security Numbers</li> |
| 51 | + <li><code>pass</code> - User passwords</li> |
| 52 | + <li><code>field_bank_account</code> - Banking information</li> |
| 53 | + <li><code>field_credit_card</code> - Payment information</li> |
| 54 | + <li><code>field_api_key</code> - API keys or tokens</li> |
| 55 | + </ul> |
| 56 | + <p><strong>Note:</strong> The following fields are automatically excluded from logging: <code>changed</code>, <code>revision_timestamp</code>, <code>revision_uid</code>, <code>revision_log</code>. Additionally, computed and internal fields are never logged.</p>'), |
| 57 | + ]; |
| 58 | + |
| 59 | + return parent::buildForm($form, $form_state); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * {@inheritdoc} |
| 64 | + */ |
| 65 | + public function submitForm(array &$form, FormStateInterface $form_state) { |
| 66 | + // Convert textarea input to array. |
| 67 | + $sensitive_fields_raw = $form_state->getValue('sensitive_fields'); |
| 68 | + $sensitive_fields = array_filter( |
| 69 | + array_map('trim', explode("\n", $sensitive_fields_raw)), |
| 70 | + function ($field) { |
| 71 | + return !empty($field); |
| 72 | + } |
| 73 | + ); |
| 74 | + |
| 75 | + $this->config('uceap_logging.settings') |
| 76 | + ->set('sensitive_fields', array_values($sensitive_fields)) |
| 77 | + ->save(); |
| 78 | + |
| 79 | + parent::submitForm($form, $form_state); |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments