Skip to content

Commit

Permalink
[apigee#415] Refine authentication form.
Browse files Browse the repository at this point in the history
  • Loading branch information
minnur committed Aug 3, 2020
1 parent 6f75214 commit 20b92d5
Showing 1 changed file with 30 additions and 36 deletions.
66 changes: 30 additions & 36 deletions src/Plugin/KeyInput/ApigeeAuthKeyInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,7 @@ class ApigeeAuthKeyInput extends KeyInputBase {
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
// When AJAX rebuilds the the form (f.e.: the "Send request" button) the
// submitted data is only available in $form_state->getUserInput() and not
// in $form_state->getValues(). Key is not prepared to handle this out of
// the box this is the reason why we have to manually process the user
// input and retrieve the submitted values here.
$key_value = $form_state->get('key_value')['current'];
// Either null or an empty string.
if (empty($key_value)) {
// When "Test connection" reloads the page they are not yet processed.
// @see \Drupal\key\Form\KeyFormBase::createPluginFormState()
$key_input_plugin_form_state = clone $form_state;
$key_input_plugin_form_state->setValues($form_state->getUserInput()['key_input_settings']);
// @see \Drupal\key\Form\KeyFormBase::validateForm()
$key_input_processed_values = $form_state->getFormObject()->getEntity()->getKeyInput()->processSubmittedKeyValue($key_input_plugin_form_state);
$key_value = $key_input_processed_values['processed_submitted'];
}

// Could be an empty array.
$values = Json::decode($key_value);
$values['authorization_server_type'] = empty($values['authorization_server']) ? 'default' : 'custom';
$values = $this->getFormDefaultValues($form_state);

$state_for_public = [
':input[name="key_input_settings[instance_type]"]' => ['value' => EdgeKeyTypeInterface::INSTANCE_TYPE_PUBLIC],
Expand Down Expand Up @@ -260,21 +241,6 @@ public function validateConfigurationForm(array &$form, FormStateInterface $form
* {@inheritdoc}
*/
public function processSubmittedKeyValue(FormStateInterface $form_state) {
$key_value = $form_state->get('key_value')['current'];
// Either null or an empty string.
if (empty($key_value)) {
// When "Test connection" reloads the page they are not yet processed.
// @see \Drupal\key\Form\KeyFormBase::createPluginFormState()
$key_input_plugin_form_state = clone $form_state;
$key_input_plugin_form_state->setValues($form_state->getUserInput()['key_input_settings']);
// @see \Drupal\key\Form\KeyFormBase::validateForm()
$key_input_processed_values = $form_state->getFormObject()->getEntity()->getKeyInput()->processSubmittedKeyValue($key_input_plugin_form_state);
$key_value = $key_input_processed_values['processed_submitted'];
}

// Could be an empty array.
$values = Json::decode($key_value);

// Get input values.
$input_values = $form_state->getValues();

Expand Down Expand Up @@ -304,7 +270,8 @@ public function processSubmittedKeyValue(FormStateInterface $form_state) {
else {
$input_values['account_json_key'] = '';
// If password field is empty we just skip it and preserve initial password.
if (empty($input_values['password'])) {
$values = $this->getFormDefaultValues($form_state);
if (empty($input_values['password']) && !empty($values['password'])) {
$input_values['password'] = $values['password'];
}
}
Expand All @@ -318,4 +285,31 @@ public function processSubmittedKeyValue(FormStateInterface $form_state) {
return parent::processSubmittedKeyValue($form_state);
}

/**
* Get authentication from values.
*/
protected function getFormDefaultValues(FormStateInterface $form_state) {
// When AJAX rebuilds the the form (f.e.: the "Send request" button) the
// submitted data is only available in $form_state->getUserInput() and not
// in $form_state->getValues(). Key is not prepared to handle this out of
// the box this is the reason why we have to manually process the user
// input and retrieve the submitted values here.
$key_value = $form_state->get('key_value')['current'];
// Either null or an empty string.
if (empty($key_value)) {
// When "Test connection" reloads the page they are not yet processed.
// @see \Drupal\key\Form\KeyFormBase::createPluginFormState()
$key_input_plugin_form_state = clone $form_state;
$key_input_plugin_form_state->setValues($form_state->getUserInput()['key_input_settings']);
// @see \Drupal\key\Form\KeyFormBase::validateForm()
$key_input_processed_values = $form_state->getFormObject()->getEntity()->getKeyInput()->processSubmittedKeyValue($key_input_plugin_form_state);
$key_value = $key_input_processed_values['processed_submitted'];
}

// Could be an empty array.
$values = Json::decode($key_value);
$values['authorization_server_type'] = empty($values['authorization_server']) ? 'default' : 'custom';
return $values;
}

}

0 comments on commit 20b92d5

Please sign in to comment.