You are here

private function ServerTestForm::testConnection in Lightweight Directory Access Protocol (LDAP) 8.3

Same name and namespace in other branches
  1. 8.4 ldap_servers/src/Form/ServerTestForm.php \Drupal\ldap_servers\Form\ServerTestForm::testConnection()

Test the connection.

Parameters

array $values: Input data.

1 call to ServerTestForm::testConnection()
ServerTestForm::submitForm in ldap_servers/src/Form/ServerTestForm.php
This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state…

File

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

Class

ServerTestForm
Use Drupal\Core\Form\FormBase;.

Namespace

Drupal\ldap_servers\Form

Code

private function testConnection(array $values) {
  if ($this->ldapServer
    ->connect() != Server::LDAP_SUCCESS) {
    $this->resultsTables['basic'][] = [
      'class' => 'color-error',
      'data' => [
        $this
          ->t('Failed to connect to LDAP server: @error', [
          '@error' => $this->ldapServer
            ->formattedError($this->ldapServer
            ->ldapErrorNumber()),
        ]),
      ],
    ];
    $this->exception = TRUE;
    return;
  }
  if ($this->ldapServer
    ->get('bind_method') == 'service_account') {
    $this->resultsTables['basic'][] = [
      $this
        ->t('Binding with DN for non-anonymous search (%bind_dn).', [
        '%bind_dn' => $this->ldapServer
          ->get('binddn'),
      ]),
    ];
    $this
      ->testBinding();
  }
  else {
    $this->resultsTables['basic'][] = [
      $this
        ->t('Binding with null DN for anonymous search.'),
    ];
    $this
      ->testBinding();
  }
  if ($this->ldapServer
    ->get('bind_method') == 'anon_user' || $this->ldapServer
    ->get('bind_method') == 'user') {
    CredentialsStorage::storeUserDn($values['testing_drupal_username']);
    CredentialsStorage::storeUserPassword($values['testing_drupal_userpw']);
    $this->resultsTables['basic'][] = [
      $this
        ->t('Binding with user credentials (%bind_dn).', [
        '%bind_dn' => $values['testing_drupal_username'],
      ]),
    ];
    $this
      ->testBinding();
    $ldap_user = $this
      ->testUserMapping($values['testing_drupal_username']);
    if (!$this->exception) {
      $mapping[] = "dn = " . $ldap_user['dn'];
      foreach ($ldap_user['attr'] as $key => $value) {
        if (is_array($value)) {
          $mapping[] = "{$key} = " . $this
            ->binaryCheck($value[0]);
        }
      }
      $item_list = [
        '#list_type' => 'ul',
        '#theme' => 'item_list',
        '#items' => $mapping,
        '#title' => $this
          ->t('Attributes available to anonymous search', [
          '%bind_dn' => $this->ldapServer
            ->get('binddn'),
        ]),
      ];
      $this->resultsTables['basic'][] = [
        render($item_list),
      ];
    }
    $this->resultsTables['basic'][] = [
      $this
        ->t('Binding with DN (%bind_dn), using supplied password.', [
        '%bind_dn' => $ldap_user['dn'],
      ]),
    ];
    $this
      ->testBinding();
  }
}