/**
  * {@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;
 }
Ejemplo n.º 2
0
 /**
  * {@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;
 }
Ejemplo n.º 3
0
 /**
  * Tests loadEncryptionMethods method.
  *
  * @covers ::__construct
  * @covers ::loadEncryptionMethods
  */
 public function testLoadEncryptionMethods()
 {
     $definitions = array('mcrypt_aes_256_test' => array('id' => 'mcrypt_aes_256_test', 'title' => "Mcrypt AES 256 test"));
     $this->encryptManager->expects($this->once())->method('getDefinitions')->will($this->returnValue($definitions));
     $service = new EncryptService($this->encryptManager, $this->keyRepository);
     $methods = $service->loadEncryptionMethods();
     $this->assertEquals(['mcrypt_aes_256_test'], array_keys($methods));
 }
Ejemplo n.º 4
0
 /**
  * {@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;
 }
Ejemplo n.º 6
0
 /**
  * {@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;
 }
Ejemplo n.º 7
0
 /**
  * Tests the encrypt & decrypt method.
  *
  * @covers ::__construct
  * @covers ::encrypt
  * @covers ::decrypt
  * @covers ::validate
  *
  * @dataProvider encryptionDataProvider
  */
 public function testEncryptDecrypt($key, $valid_key)
 {
     // Set up expectations for Key.
     $this->key->expects($this->any())->method('getKeyValue')->will($this->returnValue($key));
     if ($valid_key) {
         // Set up expectations for encryption method.
         $this->encryptionMethod->expects($this->once())->method('encrypt')->will($this->returnValue("encrypted_text"));
         $this->encryptionMethod->expects($this->once())->method('decrypt')->will($this->returnValue("decrypted_text"));
         // Set up expectations for encryption profile.
         $this->encryptionProfile->expects($this->any())->method('getEncryptionKey')->will($this->returnValue($this->key));
         $this->encryptionProfile->expects($this->any())->method('getEncryptionMethod')->will($this->returnValue($this->encryptionMethod));
         $this->encryptionProfile->expects($this->any())->method('validate')->will($this->returnValue(array()));
     } else {
         // Set up expectations for encryption profile.
         $this->encryptionProfile->expects($this->never())->method('getEncryptionKey');
         $this->encryptionProfile->expects($this->never())->method('getEncryptionMethod');
         $this->encryptionProfile->expects($this->any())->method('validate')->will($this->returnValue(array("Validation error")));
         $this->setExpectedException('\\Drupal\\encrypt\\Exception\\EncryptException');
     }
     $service = new EncryptService($this->encryptManager, $this->keyRepository);
     $encrypted_text = $service->encrypt("text_to_encrypt", $this->encryptionProfile);
     $decrypted_text = $service->decrypt("text_to_decrypt", $this->encryptionProfile);
     if ($valid_key) {
         $this->assertEquals("encrypted_text", $encrypted_text);
         $this->assertEquals("decrypted_text", $decrypted_text);
     }
 }
Ejemplo n.º 8
-5
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $trigger = $form_state->getTriggeringElement();
     switch ($trigger['#name']) {
         case 'encrypt':
             if ($to_encrypt = $form_state->getValue('to_encrypt')) {
                 $encrypted_text = $this->encryptService->encrypt($to_encrypt, $this->entity);
                 if ($form_state->getValue('encrypt_base64')) {
                     $encrypted_text = base64_encode($encrypted_text);
                 }
                 $form_state->setValue('encrypted', $encrypted_text);
             }
             break;
         case 'decrypt':
             if ($to_decrypt = $form_state->getValue('to_decrypt')) {
                 if ($form_state->getValue('decrypt_base64')) {
                     $to_decrypt = base64_decode($to_decrypt);
                 }
                 $decrypted_text = $this->encryptService->decrypt($to_decrypt, $this->entity);
                 $form_state->setValue('decrypted', $decrypted_text);
             }
             break;
     }
     $form_state->setRebuild();
 }