From 60a95aa0325f51b0fdecca72bcf3b44c68b9e941 Mon Sep 17 00:00:00 2001 From: Jordan Dukart Date: Mon, 1 Jun 2020 19:48:20 +0000 Subject: [PATCH 1/2] Broker config. --- src/Form/IslandoraSettingsForm.php | 84 ++++++++++++++++++++++++++---- src/StompFactory.php | 5 +- 2 files changed, 79 insertions(+), 10 deletions(-) diff --git a/src/Form/IslandoraSettingsForm.php b/src/Form/IslandoraSettingsForm.php index 27514c3bd..7b15c6c37 100644 --- a/src/Form/IslandoraSettingsForm.php +++ b/src/Form/IslandoraSettingsForm.php @@ -22,6 +22,8 @@ class IslandoraSettingsForm extends ConfigFormBase { const CONFIG_NAME = 'islandora.settings'; const BROKER_URL = 'broker_url'; + const BROKER_USER = 'broker_user'; + const BROKER_PASSWORD = 'broker_password'; const JWT_EXPIRY = 'jwt_expiry'; const GEMINI_URL = 'gemini_url'; const GEMINI_PSEUDO = 'gemini_pseudo_bundles'; @@ -34,6 +36,12 @@ class IslandoraSettingsForm extends ConfigFormBase { */ private $entityTypeBundleInfo; + /** + * The saved password (if set). + * @var string + */ + private $brokerPassword; + /** * Constructs a \Drupal\system\ConfigFormBase object. * @@ -45,6 +53,7 @@ class IslandoraSettingsForm extends ConfigFormBase { public function __construct(ConfigFactoryInterface $config_factory, EntityTypeBundleInfoInterface $entity_type_bundle_info) { $this->setConfigFactory($config_factory); $this->entityTypeBundleInfo = $entity_type_bundle_info; + $this->brokerPassword = $this->config(self::CONFIG_NAME)->get(self::BROKER_PASSWORD); } /** @@ -79,12 +88,46 @@ protected function getEditableConfigNames() { public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config(self::CONFIG_NAME); - $form[self::BROKER_URL] = [ + $form['broker_info'] = [ + '#type' => 'details', + '#title' => $this->t('Broker'), + '#open' => TRUE, + ]; + $form['broker_info'][self::BROKER_URL] = [ '#type' => 'textfield', - '#title' => $this->t('Broker URL'), + '#title' => $this->t('URL'), '#default_value' => $config->get(self::BROKER_URL), ]; - + $broker_user = $config->get(self::BROKER_USER); + $form['broker_info']['provide_user_creds'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Provide user identification'), + '#default_value' => $broker_user ? TRUE : FALSE, + ]; + $state_selector = 'input[name="provide_user_creds"]'; + $form['broker_info'][self::BROKER_USER] = [ + '#type' => 'textfield', + '#title' => $this->t('User'), + '#default_value' => $broker_user, + '#states' => [ + 'visible' => [ + $state_selector => ['checked' => TRUE], + ], + 'required' => [ + $state_selector => ['checked' => TRUE], + ] + ], + ]; + $form['broker_info'][self::BROKER_PASSWORD] = [ + '#type' => 'password', + '#title' => $this->t('Password'), + '#description' => $this->t('If this field is left blank and the user is filled out, the current password will not be changed.'), + '#states' => [ + 'visible' => [ + $state_selector => ['checked' => TRUE], + ], + ], + ]; $form[self::JWT_EXPIRY] = [ '#type' => 'textfield', '#title' => $this->t('JWT Expiry'), @@ -142,14 +185,22 @@ public function buildForm(array $form, FormStateInterface $form_state) { public function validateForm(array &$form, FormStateInterface $form_state) { // Validate broker url by actually connecting with a stomp client. $brokerUrl = $form_state->getValue(self::BROKER_URL); - // Attempt to subscribe to a dummy queue. try { - $stomp = new StatefulStomp( - new Client( - $brokerUrl - ) - ); + $client = new Client($brokerUrl); + if ($form_state->getValue('provide_user_creds')) { + $broker_password = $form_state->getValue(self::BROKER_PASSWORD); + if (!$broker_password) { + if (!$this->brokerPassword) { + $form_state->setErrorByName(self::BROKER_PASSWORD, $this->t('A password must be supplied')); + } + else { + $broker_password = $this->brokerPassword; + } + } + $client->setLogin($form_state->getValue(self::BROKER_USER), $broker_password); + } + $stomp = new StatefulStomp($client); $stomp->subscribe('dummy-queue-for-validation'); $stomp->unsubscribe(); } @@ -224,6 +275,21 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $pseudo_types = array_filter($form_state->getValue(self::GEMINI_PSEUDO)); + $broker_password = $form_state->getValue(self::BROKER_PASSWORD); + + // If there's no user set delete what may have been here before as password fields will also be blank when default valuing. + if (!$form_state->getValue('provide_user_creds')) { + $config->delete(self::BROKER_USER); + $config->delete(self::BROKER_PASSWORD); + } + else { + $config->set(self::BROKER_USER, $form_state->getValue(self::BROKER_USER)); + // If the password has changed update it as well. + if ($broker_password && $broker_password != $this->brokerPassword) { + $config->set(self::BROKER_PASSWORD, $broker_password); + } + } + $config ->set(self::BROKER_URL, $form_state->getValue(self::BROKER_URL)) ->set(self::JWT_EXPIRY, $form_state->getValue(self::JWT_EXPIRY)) diff --git a/src/StompFactory.php b/src/StompFactory.php index 0e3528c1a..b63ca90b2 100644 --- a/src/StompFactory.php +++ b/src/StompFactory.php @@ -25,13 +25,16 @@ public static function create(ConfigFactoryInterface $config) { // Get broker url from config. $settings = $config->get(IslandoraSettingsForm::CONFIG_NAME); $brokerUrl = $settings->get(IslandoraSettingsForm::BROKER_URL); - + $brokerUser = $settings->get(IslandoraSettingsForm::BROKER_USER); // Try a sensible default if one hasn't been configured. if (empty($brokerUrl)) { $brokerUrl = "tcp://localhost:61613"; } $client = new Client($brokerUrl); + if ($brokerUser) { + $client->setLogin($brokerUser, $settings->get(IslandoraSettingsForm::BROKER_PASSWORD)); + } return new StatefulStomp($client); } From 6fd7b908c58d819d9ac202b88576360364de88a9 Mon Sep 17 00:00:00 2001 From: Jordan Dukart Date: Mon, 1 Jun 2020 17:36:32 -0300 Subject: [PATCH 2/2] Coding standards, clear not delete. --- src/Form/IslandoraSettingsForm.php | 32 +++++++++++++++++------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/Form/IslandoraSettingsForm.php b/src/Form/IslandoraSettingsForm.php index 7b15c6c37..4ee828d5d 100644 --- a/src/Form/IslandoraSettingsForm.php +++ b/src/Form/IslandoraSettingsForm.php @@ -38,6 +38,7 @@ class IslandoraSettingsForm extends ConfigFormBase { /** * The saved password (if set). + * * @var string */ private $brokerPassword; @@ -115,7 +116,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { ], 'required' => [ $state_selector => ['checked' => TRUE], - ] + ], ], ]; $form['broker_info'][self::BROKER_PASSWORD] = [ @@ -132,7 +133,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#type' => 'textfield', '#title' => $this->t('JWT Expiry'), '#default_value' => $config->get(self::JWT_EXPIRY), - '#description' => 'Eg: 60, "2 days", "10h", "7d". A numeric value is interpreted as a seconds count. If you use a string be sure you provide the time units (days, hours, etc), otherwise milliseconds unit is used by default ("120" is equal to "120ms").', + '#description' => $this->t('Eg: 60, "2 days", "10h", "7d". A numeric value is interpreted as a seconds count. If you use a string be sure you provide the time units (days, hours, etc), otherwise milliseconds unit is used by default ("120" is equal to "120ms").'), ]; $form[self::GEMINI_URL] = [ @@ -190,15 +191,17 @@ public function validateForm(array &$form, FormStateInterface $form_state) { $client = new Client($brokerUrl); if ($form_state->getValue('provide_user_creds')) { $broker_password = $form_state->getValue(self::BROKER_PASSWORD); - if (!$broker_password) { - if (!$this->brokerPassword) { - $form_state->setErrorByName(self::BROKER_PASSWORD, $this->t('A password must be supplied')); - } - else { - $broker_password = $this->brokerPassword; - } - } - $client->setLogin($form_state->getValue(self::BROKER_USER), $broker_password); + // When stored password type fields aren't rendered again. + if (!$broker_password) { + // Use the stored password if it exists. + if (!$this->brokerPassword) { + $form_state->setErrorByName(self::BROKER_PASSWORD, $this->t('A password must be supplied')); + } + else { + $broker_password = $this->brokerPassword; + } + } + $client->setLogin($form_state->getValue(self::BROKER_USER), $broker_password); } $stomp = new StatefulStomp($client); $stomp->subscribe('dummy-queue-for-validation'); @@ -277,10 +280,11 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $broker_password = $form_state->getValue(self::BROKER_PASSWORD); - // If there's no user set delete what may have been here before as password fields will also be blank when default valuing. + // If there's no user set delete what may have been here before as password + // fields will also be blank. if (!$form_state->getValue('provide_user_creds')) { - $config->delete(self::BROKER_USER); - $config->delete(self::BROKER_PASSWORD); + $config->clear(self::BROKER_USER); + $config->clear(self::BROKER_PASSWORD); } else { $config->set(self::BROKER_USER, $form_state->getValue(self::BROKER_USER));