You are here

public function IdpForm::createCertsFieldset in SAML Service Provider 3.x

Same name and namespace in other branches
  1. 8.3 src/Form/IdpForm.php \Drupal\saml_sp\Form\IdpForm::createCertsFieldset()
  2. 8.2 src/Form/IdpForm.php \Drupal\saml_sp\Form\IdpForm::createCertsFieldset()
  3. 4.x src/Form/IdpForm.php \Drupal\saml_sp\Form\IdpForm::createCertsFieldset()

Creates a fieldset for managing certificates.

1 call to IdpForm::createCertsFieldset()
IdpForm::form in src/Form/IdpForm.php
Gets the actual form array to be built.

File

src/Form/IdpForm.php, line 145

Class

IdpForm
Provides the form to configure the IdP.

Namespace

Drupal\saml_sp\Form

Code

public function createCertsFieldset(FormStateInterface $form_state) {
  $idp = $this->entity;
  $certs = $idp
    ->getX509Cert();
  if (!is_array($certs)) {
    $certs = [
      $certs,
    ];
  }
  foreach ($certs as $key => $value) {
    if (is_string($value) && empty(trim($value)) || $value == 'Array') {
      unset($certs[$key]);
    }
  }
  $values = $form_state
    ->getValues();
  if (!empty($values['idp']['x509_cert'])) {
    $certs = $values['idp']['x509_cert'];
    unset($certs['actions']);
  }
  $form = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('X.509 certificates'),
    '#description' => t('Enter the application certificate(s) provided by the IdP.'),
    '#prefix' => '<div id="certs-fieldset-wrapper">',
    '#suffix' => '</div>',
  ];

  // Gather the number of certs in the form already.
  $num_certs = $form_state
    ->get('num_certs');

  // We have to ensure that there is at least one cert field.
  if ($num_certs === NULL) {
    $num_certs = count($certs) ?: 1;
    $cert_field = $form_state
      ->set('num_certs', $num_certs);
  }
  for ($i = 0; $i < $num_certs; $i++) {
    if (isset($certs[$i])) {
      $encoded_cert = trim($certs[$i]);
    }
    else {
      $encoded_cert = '';
    }
    if (empty($encoded_cert)) {
      $form[$i] = [
        '#type' => 'textarea',
        '#title' => $this
          ->t('New Certificate'),
        '#default_value' => $encoded_cert,
      ];
      continue;
    }
    $title = t('Certificate');
    if (function_exists('openssl_x509_parse')) {
      $cert = openssl_x509_parse(Utils::formatCert($encoded_cert));
      if ($cert) {

        // Flatten the issuer array.
        foreach ($cert['issuer'] as $key => &$value) {
          if (is_array($value)) {
            $value = implode("/", $value);
          }
        }
        $title = t('Name: %cert-name<br/>Issued by: %issuer<br/>Valid: %valid-from - %valid-to', [
          '%cert-name' => $cert['name'],
          '%issuer' => implode('/', $cert['issuer']),
          '%valid-from' => date('c', $cert['validFrom_time_t']),
          '%valid-to' => date('c', $cert['validTo_time_t']),
        ]);
      }
    }
    $form[$i] = [
      '#type' => 'textarea',
      '#title' => $title,
      '#default_value' => $encoded_cert,
    ];
  }
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['add_cert'] = [
    '#type' => 'submit',
    '#value' => t('Add one more'),
    '#submit' => [
      '::addCertCallback',
    ],
    '#ajax' => [
      'callback' => '::addMoreCertsCallback',
      'wrapper' => 'certs-fieldset-wrapper',
    ],
  ];

  // If there is more than one name, add the remove button.
  if ($num_certs > 1) {
    $form['actions']['remove_cert'] = [
      '#type' => 'submit',
      '#value' => t('Remove one'),
      '#submit' => [
        '::removeCertCallback',
      ],
      '#ajax' => [
        'callback' => '::addMoreCertsCallback',
        'wrapper' => 'certs-fieldset-wrapper',
      ],
    ];
  }
  return $form;
}