You are here

public function ServerTestForm::buildForm in Lightweight Directory Access Protocol (LDAP) 8.4

Same name and namespace in other branches
  1. 8.3 ldap_servers/src/Form/ServerTestForm.php \Drupal\ldap_servers\Form\ServerTestForm::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 EntityForm::buildForm

File

ldap_servers/src/Form/ServerTestForm.php, line 140

Class

ServerTestForm
Use Drupal\Core\Form\FormBase;.

Namespace

Drupal\ldap_servers\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $ldap_server = NULL) : array {
  if ($ldap_server) {
    $this->ldapServer = $ldap_server;
  }
  $form['#title'] = $this
    ->t('Test LDAP Server Configuration: @server', [
    '@server' => $this->ldapServer
      ->label(),
  ]);
  $form['#prefix'] = $this
    ->t('<h3>Send test queries</h3><p>Enter identifiers here to query LDAP directly based on your server configuration. The only data this function will modify is the test LDAP group, which will be deleted and added</p>');
  if (!$this->moduleHandler
    ->moduleExists('ldap_user')) {
    $form['error'] = [
      '#markup' => '<h3>' . $this
        ->t('This form requires ldap_user to function correctly, please enable it.') . '</h3>',
    ];
    return $form;
  }
  $properties = [];
  $settings = [
    '#theme' => 'item_list',
    '#items' => $properties,
    '#list_type' => 'ul',
  ];
  $form['server_variables'] = [
    '#markup' => $this->renderer
      ->render($settings),
  ];
  $form['id'] = [
    '#type' => 'hidden',
    '#title' => $this
      ->t('Machine name for this server'),
    '#default_value' => $this->ldapServer
      ->id(),
  ];
  $userCredentialsRequired = FALSE;
  if (in_array($this->ldapServer
    ->get('bind_method'), [
    'anon_user',
    'user',
  ], TRUE)) {
    $userCredentialsRequired = TRUE;
  }
  $form['testing_drupal_username'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Authentication name'),
    '#default_value' => $this->ldapServer
      ->get('testing_drupal_username'),
    '#size' => 30,
    '#maxlength' => 255,
    '#required' => $userCredentialsRequired,
    '#description' => $this
      ->t("This is usually the equivalent of the Drupal username. The user need not exist in Drupal and testing will not affect the user's LDAP or Drupal account."),
  ];
  if ($userCredentialsRequired) {
    $form['testing_drupal_userpw'] = [
      '#type' => 'password',
      '#title' => $this
        ->t('Testing Drupal User Password'),
      '#size' => 30,
      '#maxlength' => 255,
      '#required' => TRUE,
      '#description' => $this
        ->t('Credentials required for testing with user binding.'),
    ];
  }
  $form['testing_drupal_user_dn'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Testing Drupal DN'),
    '#default_value' => $this->ldapServer
      ->getTestingDrupalUserDn(),
    // @todo Add this field back in. The logic for it is missing completely.
    '#access' => FALSE,
    '#size' => 120,
    '#maxlength' => 255,
    '#description' => $this
      ->t("The user is not required to exist in Drupal and testing will not affect the user's LDAP or Drupal account."),
  ];
  $form['grp_test_grp_dn'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Group DN'),
    '#default_value' => $this->ldapServer
      ->get('grp_test_grp_dn'),
    '#size' => 120,
    '#description' => $this
      ->t('Optionally add a group to received information about it.'),
    '#maxlength' => 255,
  ];
  $form['grp_test_grp_dn_writeable'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Group DN (write testing)'),
    '#default_value' => $this->ldapServer
      ->get('grp_test_grp_dn_writeable'),
    '#size' => 120,
    '#maxlength' => 255,
    '#description' => $this
      ->t("<strong>Warning: Testing writable groups means that existing groups can be deleted, created or have members added to it!</strong><br>Note that this test assumes that your group definition follows a pattern such as objectClass:['YOUR_CATEGORY','top']. If your directory differs, this might give false negatives."),
  ];
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => 'Test',
    '#weight' => 100,
  ];
  if ($form_state
    ->get([
    'ldap_server_test_data',
  ])) {
    $test_data = $form_state
      ->get([
      'ldap_server_test_data',
    ]);
    $form['#suffix'] = '';
    $titles = [
      'basic' => 'Test Results',
      'group1' => 'Group Create, Delete, Add Member, Remove Member Tests',
      'group2' => 'User Group Membership Functions Test',
      'group_direct' => 'Direct queries for the group',
      'tokens' => 'User Token Samples',
      'groupfromDN' => 'Groups Derived From User DN',
    ];
    foreach ($test_data['results_tables'] as $table_name => $table_data) {
      $settings = [
        '#theme' => 'table',
        '#header' => $table_name === 'basic' ? [
          'Test',
        ] : [
          'Test',
          'Result',
        ],
        '#rows' => $table_data,
      ];
      $form['#suffix'] .= '<h2>' . $titles[$table_name] . '</h2>' . $this->renderer
        ->render($settings);
    }
    if (isset($test_data['username'], $test_data['ldap_user'])) {
      $rows = $this
        ->computeUserData($test_data['ldap_user']);
      $settings = [
        '#theme' => 'table',
        '#header' => [
          'Attribute Name',
          'Instance',
          'Value',
          'Token',
        ],
        '#rows' => $rows,
      ];
      $form['#suffix'] .= sprintf('<div class="content"><h2>%s</h2>%s</div>', $this
        ->t('LDAP Entry for %username (dn: %dn)', [
        '%dn' => $test_data['ldap_user']
          ->getDn(),
        '%username' => $test_data['username'],
      ]), $this->renderer
        ->render($settings));
    }
    if (!empty($test_data['username'])) {
      $users = $this->entityTypeManager
        ->getStorage('user')
        ->loadByProperties([
        'name' => $test_data['username'],
      ]);
      $users ? reset($users) : FALSE;
      if ($users) {
        $form['#suffix'] .= sprintf('<h3>%s</h3>, <pre>%s</pre>', $this
          ->t('Corresponding Drupal user object for @user:', [
          '@user' => $test_data['username'],
        ]), json_encode(reset($users)
          ->toArray(), JSON_PRETTY_PRINT));
        if (isset($test_data['group_entry'][0])) {
          $form['#suffix'] .= sprintf('<h3>%s</h3>, <pre>%s</pre>', $this
            ->t('Corresponding test group LDAP entry:'), json_encode($test_data['group_entry'][0], JSON_PRETTY_PRINT));
        }
      }
    }
  }
  return $form;
}