You are here

SamlSpConfigSPForm.php in SAML Service Provider 7.8

File

src/Form/SamlSpConfigSPForm.php
View source
<?php

/**
 * @file
 * Contains \Drupal\saml_sp\Form\SamlSpConfigSPForm.
 */
namespace Drupal\saml_sp\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
class SamlSpConfigSPForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'saml_sp_config_sp';
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this
      ->config('saml_sp.settings');
    $values = $form_state
      ->getValues();
    $this
      ->configRecurse($config, $values['contact'], 'contact');
    $this
      ->configRecurse($config, $values['organization'], 'organization');
    $this
      ->configRecurse($config, $values['security'], 'security');
    $config
      ->set('strict', $values['strict']);
    $config
      ->set('key_location', $values['key_location']);
    $config
      ->set('cert_location', $values['cert_location']);
    $config
      ->save();
    if (method_exists($this, '_submitForm')) {
      $this
        ->_submitForm($form, $form_state);
    }
    parent::submitForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {

    // ensure the cert and key files are provided and exist in the system
    $values = $form_state
      ->getValues();
    foreach ([
      'key_location',
      'cert_location',
    ] as $key) {
      if (empty($values[$key])) {
        $form_state
          ->setError($form[$key], $this
          ->t('The %field must be provided.', array(
          '%field' => $form[$key]['#title'],
        )));
      }
      else {
        if (!file_exists($values[$key])) {
          $form_state
            ->setError($form[$key], $this
            ->t('The %input file does not exist.', array(
            '%input' => $values[$key],
          )));
        }
      }
    }
  }

  /**
   * recursively go through the set values to set the configuration
   */
  protected function configRecurse($config, $values, $base = '') {
    foreach ($values as $var => $value) {
      if (!empty($base)) {
        $v = $base . '.' . $var;
      }
      else {
        $v = $var;
      }
      if (!is_array($value)) {
        $config
          ->set($v, $value);
      }
      else {
        $this
          ->configRecurse($config, $value, $v);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'saml_sp.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form = [], FormStateInterface $form_state) {
    $config = $this
      ->config('saml_sp.settings');
    $form['contact'] = array(
      '#type' => 'fieldset',
      '#title' => t('Contact Information'),
      '#description' => t('Information to be included in the federation metadata.'),
      '#tree' => TRUE,
    );
    $form['contact']['technical'] = array(
      '#type' => 'fieldset',
      '#title' => t('Technical'),
    );
    $form['contact']['technical']['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Name'),
      '#default_value' => $config
        ->get('contact.technical.name'),
    );
    $form['contact']['technical']['email'] = array(
      '#type' => 'textfield',
      '#title' => t('Email'),
      '#default_value' => $config
        ->get('contact.technical.email'),
    );
    $form['contact']['support'] = array(
      '#type' => 'fieldset',
      '#title' => t('Support'),
    );
    $form['contact']['support']['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Name'),
      '#default_value' => $config
        ->get('contact.support.name'),
    );
    $form['contact']['support']['email'] = array(
      '#type' => 'textfield',
      '#title' => t('Email'),
      '#default_value' => $config
        ->get('contact.support.email'),
    );
    $form['organization'] = array(
      '#type' => 'fieldset',
      '#title' => t('Organization'),
      '#description' => t('Organization information for the federation metadata'),
      '#tree' => TRUE,
    );
    $form['organization']['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Name'),
      '#description' => t('This is a short name for the organization'),
      '#default_value' => $config
        ->get('organization.name'),
    );
    $form['organization']['display_name'] = array(
      '#type' => 'textfield',
      '#title' => t('Display Name'),
      '#description' => t('This is a long name for the organization'),
      '#default_value' => $config
        ->get('organization.display_name'),
    );
    $form['organization']['url'] = array(
      '#type' => 'textfield',
      '#title' => t('URL'),
      '#description' => t('This is a URL for the organization'),
      '#default_value' => $config
        ->get('organization.url'),
    );
    $form['strict'] = array(
      '#type' => 'checkbox',
      '#title' => t('Strict Protocol'),
      '#description' => t('SAML 2 Strict protocol will be used.'),
      '#default_value' => $config
        ->get('strict'),
    );
    $form['security'] = array(
      '#type' => 'fieldset',
      '#title' => t('Security'),
      '#tree' => TRUE,
    );
    $form['security']['offered'] = array(
      '#markup' => t('Signatures and Encryptions Offered:'),
    );
    $form['security']['nameIdEncrypted'] = array(
      '#type' => 'checkbox',
      '#title' => t('NameID Encrypted'),
      '#default_value' => $config
        ->get('security.nameIdEncrypted'),
    );
    $form['security']['authnRequestsSigned'] = array(
      '#type' => 'checkbox',
      '#title' => t('Authn Requests Signed'),
      '#default_value' => $config
        ->get('security.authnRequestsSigned'),
    );
    $form['security']['logoutRequestSigned'] = array(
      '#type' => 'checkbox',
      '#title' => t('Logout Requests Signed'),
      '#default_value' => $config
        ->get('security.logoutRequestSigned'),
    );
    $form['security']['logoutResponseSigned'] = array(
      '#type' => 'checkbox',
      '#title' => t('Logout Response Signed'),
      '#default_value' => $config
        ->get('security.logoutResponseSigned'),
    );
    $form['security']['required'] = array(
      '#markup' => t('Signatures and Encryptions Required:'),
    );
    $form['security']['wantMessagesSigned'] = array(
      '#type' => 'checkbox',
      '#title' => t('Want Messages Signed'),
      '#default_value' => $config
        ->get('security.wantMessagesSigned'),
    );
    $form['security']['wantAssertionsSigned'] = array(
      '#type' => 'checkbox',
      '#title' => t('Want Assertions Signed'),
      '#default_value' => $config
        ->get('security.wantAssertionsSigned'),
    );
    $form['security']['wantNameIdEncrypted'] = array(
      '#type' => 'checkbox',
      '#title' => t('Want NameID Encrypted'),
      '#default_value' => $config
        ->get('security.wantNameIdEncrypted'),
    );
    $form['security']['metadata'] = array(
      //'#type'           => 'markup',
      '#markup' => t('Metadata:'),
    );
    $form['security']['signMetaData'] = array(
      '#type' => 'checkbox',
      '#title' => t('Sign Meta Data'),
      '#default_value' => $config
        ->get('security.signMetaData'),
    );
    $form['cert_location'] = array(
      '#type' => 'textfield',
      '#title' => t('Certificate Location'),
      '#description' => t('The location of the x.509 certificate file on the server. This must be a location that PHP can read.'),
      '#default_value' => $config
        ->get('cert_location'),
    );
    $form['key_location'] = array(
      '#type' => 'textfield',
      '#title' => t('Key Location'),
      '#description' => t('The location of the x.509 key file on the server. This must be a location that PHP can read.'),
      '#default_value' => $config
        ->get('key_location'),
    );
    $error = FALSE;
    try {
      $metadata = saml_sp__get_metadata(NULL, FALSE);
      if (is_array($metadata)) {
        if (isset($metadata[1])) {
          $errors = $metadata[1];
        }
        $metadata = $metadata[0];
      }
    } catch (Exception $e) {
      drupal_set_message(t('Attempt to create metadata failed: %message.', array(
        '%message' => $e
          ->getMessage(),
      )), 'error');
      $metadata = '';
      $error = $e;
    }
    if (empty($metadata) && $error) {
      $no_metadata = t('There is currently no metadata because of the following error: %error. Please resolve the error and  return here for your metadata.', array(
        '!url' => url('admin/config/people/saml_sp/drupal_login'),
        '%error' => $error
          ->getMessage(),
      ));
    }
    $form['metadata'] = array(
      '#type' => 'fieldset',
      '#collapsed' => TRUE,
      '#collapsible' => TRUE,
      '#title' => t('Metadata'),
      '#description' => t('This is the Federation Metadata for this IdP'),
    );
    if ($metadata) {
      $form['metadata']['data'] = array(
        '#type' => 'textarea',
        '#title' => t('XML'),
        '#description' => t('This metadata for @idp can also be accessed !link', array(
          '@idp' => variable_get('saml_sp_drupal_login__idp', ''),
          '!link' => l(t('here'), saml_sp__metadata_url()),
        )),
        '#disabled' => TRUE,
        '#rows' => 20,
        '#default_value' => $metadata,
      );
    }
    else {
      $form['metadata']['none'] = array(
        '#markup' => $no_metadata,
      );
    }

    /*
        if ( module_exists('devel')) {
          $form['saml_sp__debug'] = array(
            '#type'             => 'checkbox',
            '#title'            => t('Debug'),
            '#description'      => t('Works with Devel module to display SAML requests and Responses for review.'),
            //'#default_value'    => variable_get('saml_sp__debug', FALSE),
          );
        }/**/
    return parent::buildForm($form, $form_state);
  }

}

Classes

Namesort descending Description
SamlSpConfigSPForm