You are here

public function SamlauthConfigureForm::submitForm in SAML Authentication 4.x

Same name and namespace in other branches
  1. 8.3 src/Form/SamlauthConfigureForm.php \Drupal\samlauth\Form\SamlauthConfigureForm::submitForm()
  2. 8 src/Form/SamlauthConfigureForm.php \Drupal\samlauth\Form\SamlauthConfigureForm::submitForm()
  3. 8.2 src/Form/SamlauthConfigureForm.php \Drupal\samlauth\Form\SamlauthConfigureForm::submitForm()

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides ConfigFormBase::submitForm

File

src/Form/SamlauthConfigureForm.php, line 1415

Class

SamlauthConfigureForm
Provides a configuration form for samlauth module settings and IdP/SP info.

Namespace

Drupal\samlauth\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $config = $this
    ->configFactory()
    ->getEditable(SamlController::CONFIG_OBJECT_NAME);
  $sp_key_type = $form_state
    ->getValue('sp_key_cert_type');
  if ($sp_key_type) {
    list($sp_key_type, $sp_cert_type) = explode('_', $sp_key_type, 2);
  }
  else {
    $sp_cert_type = '';
  }

  // We validated that max. 1 of the values is set if $sp_key/cert_type == ''.
  // If $sp_key/cert_type is nonempty, other values may be set which we must
  // explicitly skip.
  $sp_private_key = $form_state
    ->getValue('sp_key_key');
  if ($sp_private_key && in_array($sp_key_type, [
    '',
    'key',
  ])) {

    // If 'key', the value was changed to the appropriate one in the
    // validate function (if necessary).
    $sp_private_key = "key:{$sp_private_key}";
  }
  if (!$sp_private_key && in_array($sp_key_type, [
    '',
    'file',
  ])) {
    $sp_private_key = $form_state
      ->getValue('sp_key_file');
    if ($sp_private_key) {
      $sp_private_key = "file:{$sp_private_key}";
    }
  }
  if (!$sp_private_key && in_array($sp_key_type, [
    '',
    'config',
  ])) {
    $sp_private_key = $form_state
      ->getValue('sp_private_key');
    if ($sp_private_key) {
      $sp_private_key = $this
        ->formatKeyOrCert($sp_private_key, FALSE, TRUE);
    }
  }
  $sp_cert = $form_state
    ->getValue('sp_cert_key');
  if ($sp_cert && in_array($sp_cert_type, [
    '',
    'key',
  ])) {

    // If 'key', the value was changed to the appropriate one in the
    // validate function (if necessary).
    $sp_cert = "key:{$sp_cert}";
  }
  if (!$sp_cert && in_array($sp_cert_type, [
    '',
    'file',
  ])) {
    $sp_cert = $form_state
      ->getValue('sp_cert_file');
    if ($sp_cert) {
      $sp_cert = "file:{$sp_cert}";
    }
  }
  if (!$sp_cert && in_array($sp_cert_type, [
    '',
    'config',
  ])) {
    $sp_cert = $form_state
      ->getValue('sp_x509_certificate');
    if ($sp_cert) {
      $sp_cert = $this
        ->formatKeyOrCert($sp_cert, FALSE);
    }
  }
  $sp_new_cert = $form_state
    ->getValue('sp_new_cert_key');
  if ($sp_new_cert && in_array($sp_cert_type, [
    '',
    'key',
  ])) {

    // If 'key', the value was changed to the appropriate one in the
    // validate function (if necessary).
    $sp_new_cert = "key:{$sp_new_cert}";
  }
  if (!$sp_new_cert && in_array($sp_cert_type, [
    '',
    'file',
  ])) {
    $sp_new_cert = $form_state
      ->getValue('sp_new_cert_file');
    if ($sp_new_cert) {
      $sp_new_cert = "file:{$sp_new_cert}";
    }
  }
  if (!$sp_new_cert && in_array($sp_cert_type, [
    '',
    'config',
  ])) {
    $sp_new_cert = $form_state
      ->getValue('sp_new_cert');
    if ($sp_new_cert) {
      $sp_new_cert = $this
        ->formatKeyOrCert($sp_new_cert, FALSE);
    }
  }
  $idp_cert_type = $form_state
    ->getValue('idp_cert_type');
  $idp_certs = [];
  foreach ($form_state
    ->getValue('idp_certs') as $item) {

    // We validated that max. 1 of the values is set if $idp_cert_type == ''.
    if (!empty($item['key']) && in_array($idp_cert_type, [
      '',
      'key',
    ])) {
      $idp_certs[] = "key:{$item['key']}";
    }
    if (!empty($item['file']) && in_array($idp_cert_type, [
      '',
      'file',
    ])) {
      $idp_certs[] = "file:{$item['file']}";
    }
    if (!empty($item['cert']) && in_array($idp_cert_type, [
      '',
      'config',
    ])) {
      $idp_certs[] = $this
        ->formatKeyOrCert($item['cert'], FALSE);
    }
  }
  $idp_cert_encryption = $form_state
    ->getValue('idp_certkey_encryption');
  if ($idp_cert_encryption && in_array($idp_cert_type, [
    '',
    'key',
  ])) {

    // If 'key', the value was changed to the appropriate one in the
    // validate function (if necessary).
    $idp_cert_encryption = "key:{$idp_cert_encryption}";
  }
  if (!$idp_cert_encryption && in_array($idp_cert_type, [
    '',
    'file',
  ])) {
    $idp_cert_encryption = $form_state
      ->getValue('idp_certfile_encryption');
    if ($idp_cert_encryption) {
      $idp_cert_encryption = "file:{$idp_cert_encryption}";
    }
  }
  if (!$idp_cert_encryption && in_array($idp_cert_type, [
    '',
    'config',
  ])) {
    $idp_cert_encryption = $form_state
      ->getValue('idp_cert_encryption');
    if ($idp_cert_encryption) {
      $idp_cert_encryption = $this
        ->formatKeyOrCert($idp_cert_encryption, FALSE);
    }
  }
  $config
    ->set('sp_x509_certificate', $sp_cert)
    ->set('sp_new_certificate', $sp_new_cert)
    ->set('sp_private_key', $sp_private_key)
    ->set('idp_certs', $idp_certs)
    ->set('idp_cert_encryption', $idp_cert_encryption)
    ->clear('sp_cert_folder');

  // This is never 0 but can be ''. (NULL would mean same as ''.) Unlike
  // others, this value needs to be unset if empty.
  $metadata_valid = $form_state
    ->getValue('metadata_valid_secs');
  if ($metadata_valid) {
    $config
      ->set('metadata_valid_secs', $this
      ->parseReadableDuration($metadata_valid));
  }
  else {
    $config
      ->clear('metadata_valid_secs');
  }
  foreach ([
    'login_menu_item_title',
    'logout_menu_item_title',
    'logout_different_user',
    'local_login_saml_error',
    'login_redirect_url',
    'logout_redirect_url',
    'drupal_login_roles',
    'error_redirect_url',
    'error_throw',
    'sp_entity_id',
    'sp_name_id_format',
    'metadata_cache_http',
    'idp_entity_id',
    'idp_single_sign_on_service',
    'idp_single_log_out_service',
    'idp_change_password_service',
    'unique_id_attribute',
    'map_users',
    'map_users_name',
    'map_users_mail',
    'map_users_roles',
    'create_users',
    'sync_name',
    'sync_mail',
    'user_name_attribute',
    'user_mail_attribute',
    'security_metadata_sign',
    'security_authn_requests_sign',
    'security_logout_requests_sign',
    'security_logout_responses_sign',
    'security_assertions_encrypt',
    'security_nameid_encrypt',
    'security_nameid_encrypted',
    'security_assertions_signed',
    'security_lowercase_url_encoding',
    'security_messages_sign',
    'request_set_name_id_policy',
    'security_want_name_id',
    'security_logout_reuse_sigs',
    'security_request_authn_context',
    'security_signature_algorithm',
    'security_encryption_algorithm',
    'strict',
    'use_proxy_headers',
    'use_base_url',
    'debug_display_error_details',
    'debug_log_saml_out',
    'debug_log_saml_in',
    'debug_log_in',
    'debug_phpsaml',
  ] as $config_value) {
    $config
      ->set($config_value, $form_state
      ->getValue($config_value));
  }
  $config
    ->save();
  parent::submitForm($form, $form_state);
}