You are here

public function LdapBridge::bind in Lightweight Directory Access Protocol (LDAP) 8.4

Bind (authenticate) against an active LDAP database.

Return value

bool Binding successful.

Overrides LdapBridgeInterface::bind

File

ldap_servers/src/LdapBridge.php, line 115

Class

LdapBridge
Ldap Bridge to symfony/ldap.

Namespace

Drupal\ldap_servers

Code

public function bind() : bool {
  if ($this->bindMethod === 'anon' || $this->bindMethod === 'anon_user' && !CredentialsStorage::validateCredentials()) {
    $userDn = NULL;
    $password = NULL;
  }
  else {

    // Default credentials form service account.
    $userDn = $this->bindDn;
    $password = $this->bindPw;

    // Runtime credentials for user binding and password checking.
    if (CredentialsStorage::validateCredentials()) {
      $userDn = CredentialsStorage::getUserDn();
      $password = CredentialsStorage::getPassword();
    }
    if (empty($password) || empty($userDn)) {
      $this->logger
        ->notice('LDAP bind failure due to missing credentials for user userdn=%userdn', [
        '%userdn' => $userDn,
      ]);
      return FALSE;
    }
  }
  try {
    $this->ldap
      ->bind($userDn, $password);
  } catch (ConnectionException $e) {
    $this->logger
      ->notice('LDAP connection failure: %message.', [
      '%message' => $e
        ->getMessage(),
    ]);
    return FALSE;
  } catch (LdapException $e) {
    $this->logger
      ->notice('LDAP bind failure: %message.', [
      '%message' => $e
        ->getMessage(),
    ]);
    return FALSE;
  }
  return TRUE;
}