/** * {@inheritdoc} */ public function validate($module) { $reasons = []; $encryption_method_plugins = []; // Check if this module provides one or more EncryptionMethod plugins. $definitions = $this->encryptService->loadEncryptionMethods(); foreach ($definitions as $definition) { if ($definition['provider'] == $module) { $encryption_method_plugins[] = $definition['id']; } } // If the module provides EncryptionMethod plugins, check if they are used. if (!empty($encryption_method_plugins)) { foreach ($encryption_method_plugins as $plugin_id) { if ($profiles = $this->encryptionProfileManager->getEncryptionProfilesByEncryptionMethod($plugin_id)) { $used_in = []; foreach ($profiles as $encryption_profile) { $used_in[] = $encryption_profile->label(); } $reasons[] = $this->t('Provides an encryption method that is in use in the following encryption profiles: %profiles', ['%profiles' => implode(', ', $used_in)]); } } } return $reasons; }
/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); $keys = $this->key_repository->getKeys(); if (empty($keys)) { drupal_set_message('No system keys (admin/config/system/key) are installed to manage encryption profiles.'); } /** @var $encryption_profile \Drupal\encrypt\Entity\EncryptionProfile */ $encryption_profile = $this->entity; $form['label'] = array('#type' => 'textfield', '#title' => $this->t('Label'), '#maxlength' => 255, '#default_value' => $encryption_profile->label(), '#description' => $this->t("Label for the encryption profile."), '#required' => TRUE); $form['id'] = array('#type' => 'machine_name', '#default_value' => $encryption_profile->id(), '#machine_name' => array('exists' => '\\Drupal\\encrypt\\Entity\\EncryptionProfile::load'), '#disabled' => !$encryption_profile->isNew()); /** @var $key \Drupal\key\Entity\KeyInterface */ foreach ($keys as $key) { $key_id = $key->id(); $key_title = $key->label(); $keys[$key_id] = (string) $key_title; } if ($profile_key = $encryption_profile->getEncryptionKey()) { $default_key = $profile_key; } $form['encryption_key'] = array('#type' => 'select', '#title' => $this->t('Encryption Key'), '#description' => $this->t('Select the key used for encryption.'), '#options' => $keys, '#default_value' => empty($default_key) ? NULL : $default_key, '#required' => TRUE); $enc_methods = []; foreach ($this->encrypt_service->loadEncryptionMethods() as $plugin_id => $definition) { $enc_methods[$plugin_id] = (string) $definition['title']; } $form['encryption_method'] = array('#type' => 'select', '#title' => $this->t('Encryption Method'), '#description' => $this->t('Select the method used for encryption'), '#options' => $enc_methods, '#default_value' => $encryption_profile->getEncryptionMethod()); return $form; }
/** * Tests loadEncryptionMethods method. * * @covers ::__construct * @covers ::loadEncryptionMethods */ public function testLoadEncryptionMethods() { $definitions = array('test_encryption_method' => array('id' => 'test_encryption_method', 'title' => "Test encryption method")); $this->encryptManager->expects($this->once())->method('getDefinitions')->will($this->returnValue($definitions)); $service = new EncryptService($this->encryptManager, $this->keyRepository); $methods = $service->loadEncryptionMethods(); $this->assertEquals(['test_encryption_method'], array_keys($methods)); }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config('encrypt.settings'); $keys = []; foreach ($this->key_manager->getKeys() as $key) { $key_id = $key->id(); $key_title = $key->label(); $keys[$key_id] = (string) $key_title; } $form['encryption_key'] = array('#type' => 'select', '#title' => $this->t('Encryption Key'), '#description' => $this->t('Select the key used for encryption'), '#options' => $keys, '#default_value' => $config->get('encryption_key')); $enc_methods = []; foreach ($this->encrypt_service->loadEncryptionMethods() as $plugin_id => $definition) { $enc_methods[$plugin_id] = (string) $definition['title']; } $form['encryption_method'] = array('#type' => 'select', '#title' => $this->t('Encryption Method'), '#description' => $this->t('Select the method used for encryption'), '#options' => $enc_methods, '#default_value' => $config->get('encryption_method')); return parent::buildForm($form, $form_state); }
/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); /** @var $encryption_profile \Drupal\encrypt\Entity\EncryptionProfile */ $encryption_profile = $this->entity; $form['label'] = array('#type' => 'textfield', '#title' => $this->t('Label'), '#maxlength' => 255, '#default_value' => $encryption_profile->label(), '#description' => $this->t("Label for the encryption profile."), '#required' => TRUE); $form['id'] = array('#type' => 'machine_name', '#default_value' => $encryption_profile->id(), '#machine_name' => array('exists' => '\\Drupal\\encrypt\\Entity\\EncryptionProfile::load'), '#disabled' => !$encryption_profile->isNew()); // This is the element that contains all of the dynamic parts of the form. $form['encryption'] = array('#type' => 'container', '#prefix' => '<div id="encrypt-settings">', '#suffix' => '</div>'); $encryption_methods = $this->encrypt_service->loadEncryptionMethods(); $method_options = []; foreach ($encryption_methods as $plugin_id => $definition) { $method_options[$plugin_id] = (string) $definition['title']; } $form['encryption']['encryption_method'] = array('#type' => 'select', '#title' => $this->t('Encryption Method'), '#description' => $this->t('Select the method used for encryption'), '#options' => $method_options, '#required' => TRUE, '#default_value' => $encryption_profile->getEncryptionMethod(), '#ajax' => array('callback' => [$this, 'ajaxUpdateSettings'], 'event' => 'change', 'wrapper' => 'encrypt-settings')); $form['encryption']['encryption_key'] = array('#type' => 'key_select', '#title' => $this->t('Encryption Key'), '#required' => TRUE, '#default_value' => $encryption_profile->getEncryptionMethod()); if ($current_encryption_method = $encryption_profile->getEncryptionMethod()) { $key_type_filter = $encryption_methods[$current_encryption_method]['key_type']; if (!empty($key_type_filter)) { $form['encryption']['encryption_key']['#key_filters'] = ['type' => $key_type_filter]; } } return $form; }
/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); /* @var $encryption_profile \Drupal\encrypt\Entity\EncryptionProfile */ $encryption_profile = $this->entity; $disabled = FALSE; if ($this->operation == "edit" && !$this->editConfirmed) { $disabled = TRUE; } if ($disabled) { $form['confirm_edit'] = array('#type' => 'checkbox', '#title' => $this->t('I understand the risks of editing this encryption profile.'), '#description' => $this->t('Please acknowledge that you understand editing this encryption profile will make data that was previously encrypted with this profile <strong>unencryptable</strong>. After you checked this box and pressed the "Save" button, you will be able to edit this existing profile.')); } $form['label'] = array('#type' => 'textfield', '#title' => $this->t('Label'), '#maxlength' => 255, '#default_value' => $encryption_profile->label(), '#description' => $this->t("Label for the encryption profile."), '#required' => TRUE, '#disabled' => $disabled); $form['id'] = array('#type' => 'machine_name', '#default_value' => $encryption_profile->id(), '#machine_name' => array('exists' => '\\Drupal\\encrypt\\Entity\\EncryptionProfile::load'), '#disabled' => !$encryption_profile->isNew()); // This is the element that contains all of the dynamic parts of the form. $form['encryption'] = array('#type' => 'container', '#prefix' => '<div id="encrypt-settings">', '#suffix' => '</div>'); $encryption_methods = $this->encryptService->loadEncryptionMethods(); $method_options = []; foreach ($encryption_methods as $plugin_id => $definition) { $method_options[$plugin_id] = (string) $definition['title']; } $form['encryption']['encryption_method'] = array('#type' => 'select', '#title' => $this->t('Encryption Method'), '#description' => $this->t('Select the method used for encryption'), '#options' => $method_options, '#required' => TRUE, '#default_value' => $encryption_profile->getEncryptionMethodId(), '#ajax' => array('callback' => [$this, 'ajaxUpdateSettings'], 'event' => 'change', 'wrapper' => 'encrypt-settings'), '#disabled' => $disabled); $form['encryption']['encryption_method_configuration'] = array('#type' => 'container', '#title' => $this->t('Encryption method settings'), '#title_display' => FALSE, '#tree' => TRUE); if ($encryption_profile->getEncryptionMethod() instanceof EncryptionMethodPluginFormInterface) { $plugin_form_state = $this->createPluginFormState($form_state); $form['encryption']['encryption_method_configuration'] += $encryption_profile->getEncryptionMethod()->buildConfigurationForm([], $plugin_form_state); $form_state->setValue('encryption_method_configuration', $plugin_form_state->getValues()); } $form['encryption']['encryption_key'] = array('#type' => 'key_select', '#title' => $this->t('Encryption Key'), '#required' => TRUE, '#default_value' => $encryption_profile->getEncryptionKeyId(), '#disabled' => $disabled); if ($current_encryption_method = $encryption_profile->getEncryptionMethodId()) { $key_type_filter = $encryption_methods[$current_encryption_method]['key_type']; if (!empty($key_type_filter)) { $form['encryption']['encryption_key']['#key_filters'] = ['type' => $key_type_filter]; } } return $form; }