You are here

public function LdapServerTest::bind in Lightweight Directory Access Protocol (LDAP) 7.2

Same name and namespace in other branches
  1. 8.2 ldap_test/LdapServerTest.class.php \LdapServerTest::bind()
  2. 7 ldap_servers/tests/LdapServerTest.class.php \LdapServerTest::bind()

Bind (authenticate) against an active LDAP database.

Parameters

$userdn: The DN to bind against. If NULL, we use $this->binddn

$pass: The password search base. If NULL, we use $this->bindpw

Return value

Result of bind; TRUE if successful, FALSE otherwise.

Overrides LdapServer::bind

File

ldap_test/LdapServerTest.class.php, line 92
Simpletest ldapServer class for testing without an actual ldap server.

Class

LdapServerTest

Code

public function bind($userdn = NULL, $pass = NULL, $anon_bind = FALSE) {
  $userdn = $userdn != NULL ? $userdn : $this->binddn;
  $pass = $pass != NULL ? $pass : $this->bindpw;
  if (!isset($this->entries[$userdn])) {

    // 0x20 or 32.
    $ldap_errno = LDAP_NO_SUCH_OBJECT;
    if (function_exists('ldap_err2str')) {
      $ldap_error = ldap_err2str($ldap_errno);
    }
    else {
      $ldap_error = "Failed to find {$userdn} in LdapServerTest.class.php";
    }
  }
  elseif (isset($this->entries[$userdn]['password'][0]) && $this->entries[$userdn]['password'][0] == $pass && $pass) {
    return LDAP_SUCCESS;
  }
  else {
    if (!$pass) {
      debug("Simpletest failure for {$userdn}.  No password submitted");
    }
    if (!isset($this->entries[$userdn]['password'][0])) {
      debug("Simpletest failure for {$userdn}.  No password in entry to test for bind");
      debug($this->entries[$userdn]);
    }
    $ldap_errno = LDAP_INVALID_CREDENTIALS;
    if (function_exists('ldap_err2str')) {
      $ldap_error = ldap_err2str($ldap_errno);
    }
    else {
      $ldap_error = "Credentials for {$userdn} failed in LdapServerTest.class.php";
    }
  }
  $watchdog_tokens = [
    '%user' => $userdn,
    '%errno' => $ldap_errno,
    '%error' => $ldap_error,
  ];
  watchdog('ldap_servers', "LDAP bind failure for user %user. Error %errno: %error", $watchdog_tokens);
  return $ldap_errno;
}