You are here

public function CasAttributesSettings::buildForm in CAS Attributes 2.x

Same name and namespace in other branches
  1. 8 src/Form/CasAttributesSettings.php \Drupal\cas_attributes\Form\CasAttributesSettings::buildForm()

Form constructor.

Parameters

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

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

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/CasAttributesSettings.php, line 62

Class

CasAttributesSettings
CAS Attributes settings form.

Namespace

Drupal\cas_attributes\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('cas_attributes.settings');
  $form['general'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('General Settings'),
    '#tree' => TRUE,
  ];
  $form['general']['sitewide_token_support'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Sitewide token support'),
    '#description' => $this
      ->t('When enabled, CAS attributes for the logged in user can be retrieved anywhere on your site using this token format: [cas:attribute:?], where ? is the attribute name in lowercase. <b>This is not required to use tokens for the user field mappings below.</b>'),
    '#default_value' => $config
      ->get('sitewide_token_support'),
  ];
  $form['general']['token_allowed_attributes'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Allowed Attributes'),
    '#description' => $this
      ->t('CAS attributes to create tokens for, one per line. If no attributes are specified, then they will all be available as tokens. Case does not matter.'),
    '#default_value' => implode("\n", $config
      ->get('token_allowed_attributes')),
    '#states' => [
      'visible' => [
        ':input[name="general[sitewide_token_support]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['field'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('User Field Mappings'),
    '#description' => $this
      ->t('Configure settings for mapping CAS attribute values to user fields during login/registration.'),
    '#tree' => TRUE,
    '#open' => TRUE,
  ];
  $form['field']['sync_frequency'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('When should field mappings be applied to the user?'),
    '#options' => [
      self::SYNC_FREQUENCY_NEVER => $this
        ->t('Never'),
      self::SYNC_FREQUENCY_INITIAL_REGISTRATION => $this
        ->t('Initial registration only (requires "Auto register users" <a href="@link">CAS setting</a> be enabled).', [
        '@link' => Url::fromRoute('cas.settings')
          ->toString(),
      ]),
      self::SYNC_FREQUENCY_EVERY_LOGIN => $this
        ->t('Every login'),
    ],
    '#default_value' => $config
      ->get('field.sync_frequency'),
  ];
  $form['field']['overwrite'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Overwrite existing field values'),
    '#description' => $this
      ->t('When checked, the field mappings below will always overwrite existing data on the user account.'),
    '#default_value' => $config
      ->get('field.overwrite'),
  ];
  $form['field']['mappings'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Fields'),
    '#description' => $this
      ->t('Optionally provide values for each user field below. To use a CAS attribute, insert a token in the format [cas:attribute:?], where ? is the attribute name in lowercase. <a href="@link">Browse available attribute tokens</a> for the currently logged in user. Note that attribute tokens will still work even if you have the "Sitewide token support" feature disabled (above).', [
      '@link' => Url::fromRoute('cas_attributes.available_attributes')
        ->toString(),
    ]),
    '#tree' => TRUE,
    '#open' => TRUE,
  ];
  $savedFieldMappings = $config
    ->get('field.mappings');
  $form['field']['mappings']['name'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Username'),
    '#description' => $this
      ->t('The CAS module defaults this to the username your CAS server provided. Any value placed here will overwrite what the CAS module provides.'),
    '#size' => 60,
    '#default_value' => isset($savedFieldMappings['name']) ? $savedFieldMappings['name'] : '',
  ];
  $form['field']['mappings']['mail'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('E-mail address'),
    '#description' => $this
      ->t('The <a href="@link">settings page for the main CAS module</a> defines the default value for e-mail. Any value placed here will overwrite what the CAS module provides.', [
      '@link' => Url::fromRoute('cas.settings')
        ->toString(),
    ]),
    '#size' => 60,
    '#default_value' => isset($savedFieldMappings['mail']) ? $savedFieldMappings['mail'] : '',
  ];
  foreach ($this->entityFieldManager
    ->getFieldDefinitions('user', 'user') as $name => $definition) {
    if (!empty($definition
      ->getTargetBundle())) {
      switch ($definition
        ->getType()) {
        case 'string':
        case 'list_string':
        case 'integer':
          $form['field']['mappings'][$name] = [
            '#type' => 'textfield',
            '#title' => $definition
              ->getLabel(),
            '#default_value' => isset($savedFieldMappings[$name]) ? $savedFieldMappings[$name] : '',
            '#size' => 60,
            '#description' => $this
              ->t('The account field with name %field_name.', [
              '%field_name' => $definition
                ->getName(),
            ]),
          ];
          break;
      }
    }
  }
  $form['role'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('User Role Mappings'),
    '#description' => $this
      ->t('Configure settings for assigning roles to users during login/registration based on CAS attribute values.'),
    '#tree' => TRUE,
    '#open' => TRUE,
  ];
  $form['role']['sync_frequency'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('When should role mappings be applied to the user?'),
    '#options' => [
      self::SYNC_FREQUENCY_NEVER => $this
        ->t('Never'),
      self::SYNC_FREQUENCY_INITIAL_REGISTRATION => $this
        ->t('Initial registration only (requires "Auto register users" <a href="@link">CAS setting</a> be enabled).', [
        '@link' => Url::fromRoute('cas.settings')
          ->toString(),
      ]),
      self::SYNC_FREQUENCY_EVERY_LOGIN => $this
        ->t('Every login'),
    ],
    '#default_value' => $config
      ->get('role.sync_frequency'),
  ];
  $form['role']['deny_login_no_match'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Deny login if no roles are mapped'),
    '#description' => $this
      ->t('If enabled, users will not be able to login via CAS unless at least one role is assigned based on the mappings below.'),
    '#default_value' => $config
      ->get('role.deny_login_no_match'),
  ];
  $form['role']['deny_registration_no_match'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Deny registration if no roles are mapped'),
    '#description' => $this
      ->t('If enabled, users will not be able to auto-register via CAS unless at least one role is assigned based on the mappings below.'),
    '#default_value' => $config
      ->get('role.deny_registration_no_match'),
  ];
  $form['role']['mappings'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('CAS Role Mappings'),
    '#description' => $this
      ->t("Each role mapping is a relationship between a role that is to be granted, an attribute name, an attribute value to match, and a method to use for comparison."),
    '#tree' => TRUE,
    '#open' => TRUE,
  ];
  $existingRoleMappings = $config
    ->get('role.mappings');
  $roles_options = user_role_names(TRUE);
  unset($roles_options[RoleInterface::AUTHENTICATED_ID]);

  // Add existing mappings to the form.
  foreach ($existingRoleMappings as $index => $condition) {
    $form['role']['mappings'][$index] = $this
      ->generateRoleMappingFormElements($roles_options, $condition);
    $form['role']['mappings'][$index]['delete'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Remove this mapping?'),
    ];
  }

  // Always add a empty row to allow adding a new mapping.
  $form['role']['mappings'][] = $this
    ->generateRoleMappingFormElements($roles_options);
  return parent::buildForm($form, $form_state);
}