src/Authentication/Handlers/HandlerLDAP.php
public function connect($username="", $password="") {
		// make sure the ldap extension has been loaded
		if(!extension_loaded('ldap')) {
			throw new LdapExtensionNotLoadedException();
		}

		$params = array(
		    'hostname'  => $this->host,
		    'base_dn' => $this->basedn,
		    'options' => [
		    	ConnectionInterface::OPT_PROTOCOL_VERSION => $this->version,
		    ],
		);

		// if there is an overlay, use that as the base DN instead
		if(!empty($this->overlay_dn)) {
			$params['base_dn'] = $this->overlay_dn;
		}

		$this->ldap = new Manager($params, new Driver());

		// connect to the server and bind with the credentials
		try
		{
			$this->ldap->connect();

			// if override parameters have been specified then use those
			// for the binding operation
			if(!empty($username)) {
				foreach($this->basedn_array as $basedn) {
					try
					{
						// bind by uid; append the overlay DN if one has been specified
						$selectedUsername = $this->search_username . "=" .
							$username;
						if(!empty($basedn)) {
							$selectedUsername .= "," . $basedn;
						}

						$selectedPassword = "";

						// do we allow empty passwords for bind attempts?
						if(empty($password)) {
							if($this->allowNoPass) {
								// yes so use the constructor-provided DN and password
								$selectedUsername = $this->dn;
								$selectedPassword = $this->password;
							}
						}
						else
						{
							// password provided so use what we were given
							$selectedPassword = $password;
						}

						// append the overlay DN if it exists
						if(!empty($this->overlay_dn)) {
							$selectedUsername .= "," . $this->overlay_dn;
						}

						// now perform the bind
						$this->bind($selectedUsername, $selectedPassword);

						// if we get here without hitting an exception, the bind
						// was successful
						return true;
					}
					catch(BindException $e) {
						// just because we hit a bind exception it doesn't mean
						// that there was an error; this could have been the
						// result of bad credentials so we will continue with
						// the next element in the array
					}
				}
			}
			else
			{
				// use the admin bind credentials
				$dn = $this->dn;
				if(!empty($this->overlay_dn)) {
					$dn .= "," . $this->overlay_dn;
				}
				$this->bind($dn, $this->password);
			}

			// if it hits this return then the connection was successful and
			// the binding was also successful
			return true;
		}
		catch(BindException $be)
		{
			// could not bind with the provided credentials (admin bind)
			return false;
		}
		catch(Exception $e)
		{
			throw $e;
		}

		// could not bind with the provided credentials (regular user bind) or
		// something else went wrong
		return false;
	}
public function connect($username="", $password="") {
		// make sure the ldap extension has been loaded
		if(!extension_loaded('ldap')) {
			throw new LdapExtensionNotLoadedException();
		}

		$params = array(
		    'hostname'  => $this->host,
		    'base_dn' => $this->basedn,
		    'options' => [
		    	ConnectionInterface::OPT_PROTOCOL_VERSION => $this->version,
		    ],
		);

		// if there is an overlay, use that as the base DN instead
		if(!empty($this->overlay_dn)) {
			$params['base_dn'] = $this->overlay_dn;
		}

		$this->ldap = new Manager($params, new Driver());

		// connect to the server and bind with the credentials
		try
		{
			$this->ldap->connect();

			// if override parameters have been specified then use those
			// for the binding operation
			if(!empty($username)) {
				foreach($this->basedn_array as $basedn) {
					try
					{
						// bind by uid; append the overlay DN if one has been specified
						$selectedUsername = $this->search_username . "=" .
							$username;
						if(!empty($basedn)) {
							$selectedUsername .= "," . $basedn;
						}

						$selectedPassword = "";

						// do we allow empty passwords for bind attempts?
						if(empty($password)) {
							if($this->allowNoPass) {
								// yes so use the constructor-provided DN and password
								$selectedUsername = $this->dn;
								$selectedPassword = $this->password;
							}
						}
						else
						{
							// password provided so use what we were given
							$selectedPassword = $password;
						}

						// append the overlay DN if it exists
						if(!empty($this->overlay_dn)) {
							$selectedUsername .= "," . $this->overlay_dn;
						}

						// now perform the bind
						$this->bind($selectedUsername, $selectedPassword);

						// if we get here without hitting an exception, the bind
						// was successful
						return true;
					}
					catch(BindException $e) {
						// just because we hit a bind exception it doesn't mean
						// that there was an error; this could have been the
						// result of bad credentials so we will continue with
						// the next element in the array
					}
				}
			}
			else
			{
				// use the admin bind credentials
				$dn = $this->dn;
				if(!empty($this->overlay_dn)) {
					$dn .= "," . $this->overlay_dn;
				}
				$this->bind($dn, $this->password);
			}

			// if it hits this return then the connection was successful and
			// the binding was also successful
			return true;
		}
		catch(BindException $be)
		{
			// could not bind with the provided credentials (admin bind)
			return false;
		}
		catch(Exception $e)
		{
			throw $e;
		}

		// could not bind with the provided credentials (regular user bind) or
		// something else went wrong
		return false;
	}
public function connect ( $ username = "" , $ password = "" ) { // make sure the ldap extension has been loaded if ( ! extension_loaded ( 'ldap' ) ) { throw new LdapExtensionNotLoadedException ( ) ; } $ params = array ( 'hostname' => $ this -> host , 'base_dn' => $ this -> basedn , 'options' => [ ConnectionInterface :: OPT_PROTOCOL_VERSION => $ this -> version , ] , ) ; // if there is an overlay, use that as the base DN instead if ( ! empty ( $ this -> overlay_dn ) ) { $ params [ 'base_dn' ] = $ this -> overlay_dn ; } $ this -> ldap = new Manager ( $ params , new Driver ( ) ) ; // connect to the server and bind with the credentials try { $ this -> ldap -> connect ( ) ; // if override parameters have been specified then use those // for the binding operation if ( ! empty ( $ username ) ) { foreach ( $ this -> basedn_array as $ basedn ) { try { // bind by uid; append the overlay DN if one has been specified $ selectedUsername = $ this -> search_username . "=" . $ username ; if ( ! empty ( $ basedn ) ) { $ selectedUsername .= "," . $ basedn ; } $ selectedPassword = "" ; // do we allow empty passwords for bind attempts? if ( empty ( $ password ) ) { if ( $ this -> allowNoPass ) { // yes so use the constructor-provided DN and password $ selectedUsername = $ this -> dn ; $ selectedPassword = $ this -> password ; } } else { // password provided so use what we were given $ selectedPassword = $ password ; } // append the overlay DN if it exists if ( ! empty ( $ this -> overlay_dn ) ) { $ selectedUsername .= "," . $ this -> overlay_dn ; } // now perform the bind $ this -> bind ( $ selectedUsername , $ selectedPassword ) ; // if we get here without hitting an exception, the bind // was successful return true ; } catch ( BindException $ e ) { // just because we hit a bind exception it doesn't mean // that there was an error; this could have been the // result of bad credentials so we will continue with // the next element in the array } } } else { // use the admin bind credentials $ dn = $ this -> dn ; if ( ! empty ( $ this -> overlay_dn ) ) { $ dn .= "," . $ this -> overlay_dn ; } $ this -> bind ( $ dn , $ this -> password ) ; } // if it hits this return then the connection was successful and // the binding was also successful return true ; } catch ( BindException $ be ) { // could not bind with the provided credentials (admin bind) return false ; } catch ( Exception $ e ) { throw $ e ; } // could not bind with the provided credentials (regular user bind) or // something else went wrong return false ; }
Connects and binds to the LDAP server. An optional username and password
can be supplied to override the default credentials. Returns whether the
connection and binding was successful.

@param string $username The override username to use
@param string $password The override password to use

@throws LdapExtensionNotLoadedException If the LDAP extension has not
been installed and loaded
@throws Exception If the LDAP connection fails
@return boolean
Connects and binds to the LDAP server . An optional username and password can be supplied to override the default credentials . Returns whether the connection and binding was successful .