You are here

public function SimpleLdapUserSettingsForm::buildForm in Simple LDAP 8

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

modules/simple_ldap_user/src/Form/SimpleLdapUserSettingsForm.php, line 55

Class

SimpleLdapUserSettingsForm

Namespace

Drupal\simple_ldap_user\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('simple_ldap.user');

  // Only display the form if we have a successful binding.
  if ($this->server
    ->bind()) {
    $this->schema = \Drupal::service('simple_ldap.schema');
    $object_classes = $this
      ->getObjectClassOptions();
    $schema_defaults = $this->schema
      ->getDefaultAttributeSettings();
    $selected_object_classes = $config
      ->get('object_class') ? $config
      ->get('object_class') : $schema_defaults['object_class'];

    // If this is Active Directory, we lock the main attribute options.
    $readonly = $this->server
      ->getServerType() == 'Active Directory' ? TRUE : FALSE;
    if ($readonly) {
      $this
        ->messenger()
        ->addWarning($this
        ->t('Your server is Active Directory, so some settings have been disabled.'));
    }

    // If there is user input via an ajax callback, set it here
    $selected_object_classes = $form_state
      ->getValue('object_class') ? $form_state
      ->getValue('object_class') : $selected_object_classes;
    $form['users'] = array(
      '#type' => 'fieldset',
      '#title' => $this
        ->t('LDAP Users'),
      '#open' => TRUE,
    );
    $form['users']['basedn'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Base DN'),
      '#description' => $this
        ->t('The Base DN that will be searched for user accounts. Ex: dc=example,dc=com'),
      '#required' => TRUE,
      '#default_value' => $config
        ->get('basedn'),
    );
    $form['users']['user_scope'] = array(
      '#type' => 'radios',
      '#title' => $this
        ->t('Search scope'),
      '#options' => array(
        'sub' => $this
          ->t('Subtree') . ' - ' . t('Search the base DN and all of its children for user accounts.'),
        'one' => $this
          ->t('One-level') . ' - ' . t('Do not include children of the base DN while searching for user accounts.'),
      ),
      '#required' => TRUE,
      '#default_value' => $config
        ->get('user_scope'),
    );
    $form['users']['object_class'] = array(
      '#type' => 'select',
      '#title' => $this
        ->t('User ObjectClass'),
      '#options' => $object_classes,
      '#default_value' => $selected_object_classes,
      '#required' => TRUE,
      '#multiple' => TRUE,
      '#size' => 10,
      '#description' => $this
        ->t('Which LDAP objectClass should be used when searching for a user. This also determines which attributes you have available to map below.'),
      '#ajax' => array(
        'callback' => array(
          $this,
          'populateObjectClassAttributes',
        ),
        'wrapper' => 'user-attribute-mapping-wrapper',
      ),
      '#disabled' => $readonly,
    );
    $attributes = $this
      ->getAttributeOptions($selected_object_classes);
    $form['users']['attribute_mapping'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'id' => 'user-attribute-mapping-wrapper',
      ),
    );

    // @TODO Use a Schema object to pull all attributes as options, and use select forms for these attributes
    $form['users']['attribute_mapping']['name_attribute'] = array(
      '#type' => 'select',
      '#title' => t('Username attribute'),
      '#options' => $attributes,
      '#required' => TRUE,
      '#description' => $this
        ->t('Which LDAP attribute should be mapped to a Drupal username. This is commonly "cn" or "uid".'),
      '#default_value' => $config
        ->get('name_attribute') ? $config
        ->get('name_attribute') : $schema_defaults['name_attribute'],
      '#disabled' => $readonly,
    );
    $form['users']['attribute_mapping']['mail_attribute'] = array(
      '#type' => 'select',
      '#title' => t('Mail attribute'),
      '#options' => $attributes,
      '#required' => TRUE,
      '#description' => $this
        ->t('Which LDAP attribute should be mapped to a Drupal email. This is commonly "mail".'),
      '#default_value' => $config
        ->get('mail_attribute') ? $config
        ->get('mail_attribute') : $schema_defaults['mail_attribute'],
      '#disabled' => $readonly,
    );
    $frequencies = [
      86400 => t('Every day'),
      21600 => t('Every 6 hours'),
      3600 => t('Every hour'),
      0 => t('Every cron run'),
    ];
    $form['users']['cron_frequency'] = array(
      '#type' => 'select',
      '#title' => t('Cron frequency'),
      '#options' => $frequencies,
      '#required' => TRUE,
      '#description' => $this
        ->t('How frequently should active/blocked users be checked against the LDAP server? This value is only checked when cron runs, so the frequency is constrained by how frequently cron runs.'),
      '#default_value' => $config
        ->get('cron_frequency') ? $config
        ->get('cron_frequency') : 0,
      '#disabled' => $readonly,
    );
  }
  else {
    $this
      ->messenger()
      ->addWarning($this
      ->t('There is a problem with your LDAP Server connection. As a result, this form has been disabled. Please <a href="@url">check your settings</a>.', array(
      '@url' => Url::fromRoute('simple_ldap.server')
        ->toString(),
    )));
  }
  return parent::buildForm($form, $form_state);
}