public function IdpForm::createCertsFieldset in SAML Service Provider 8.2
Same name and namespace in other branches
- 8.3 src/Form/IdpForm.php \Drupal\saml_sp\Form\IdpForm::createCertsFieldset()
- 4.x src/Form/IdpForm.php \Drupal\saml_sp\Form\IdpForm::createCertsFieldset()
- 3.x src/Form/IdpForm.php \Drupal\saml_sp\Form\IdpForm::createCertsFieldset()
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 140 - Contains \Drupal\saml_sp\Form\SamlSpIdpAdd.
Class
Namespace
Drupal\saml_sp\FormCode
public function createCertsFieldset(FormStateInterface $form_state) {
$idp = $this->entity;
$certs = $idp
->x509_cert();
if (!is_array($certs)) {
$certs = array(
$idp
->x509_cert(),
);
}
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 = array(
'#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] = array(
'#type' => 'textarea',
'#title' => $this
->t('New Certificate'),
'#default_value' => $encoded_cert,
'#max_length' => 1024,
);
continue;
}
$title = t('Certificate');
if (function_exists('openssl_x509_parse')) {
$cert = openssl_x509_parse(\OneLogin_Saml2_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', array(
'%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] = array(
'#type' => 'textarea',
'#title' => $title,
'#default_value' => $encoded_cert,
'#max_length' => 1024,
);
}
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['add_cert'] = array(
'#type' => 'submit',
'#value' => t('Add one more'),
'#submit' => array(
'::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' => array(
'::removeCertCallback',
),
'#ajax' => [
'callback' => '::addMoreCertsCallback',
'wrapper' => 'certs-fieldset-wrapper',
],
];
}
return $form;
}