$this->ldap->setOption(null, LDAP_OPT_X_TLS_REQUIRE_CERT, LDAP_OPT_X_TLS_NEVER);
}
- $serverDetails = $this->parseServerString($this->config['server']);
- $ldapConnection = $this->ldap->connect($serverDetails['host'], $serverDetails['port']);
+ $serverDetails = $this->parseEnvironmentServer($this->config['server']);
+ $this->ldapConnection = $this->prepareServerConnection($serverDetails);
- if ($ldapConnection === false) {
- throw new LdapException(trans('errors.ldap_cannot_connect'));
- }
-
- // Set any required options
- if ($this->config['version']) {
- $this->ldap->setVersion($ldapConnection, $this->config['version']);
- }
+ return $this->ldapConnection;
+ }
- // Start and verify TLS if it's enabled
- if ($this->config['start_tls']) {
- $started = $this->ldap->startTls($ldapConnection);
- if (!$started) {
- throw new LdapException('Could not start TLS connection');
+ /**
+ * Processes an array of received servers and returns the first working connection.
+ *
+ * @param array $serverDetails
+ * @return resource
+ * @throws LdapException
+ */
+ protected function prepareServerConnection(array $serverDetails)
+ {
+ $lastException = null;
+ foreach ($serverDetails as $server) {
+ try {
+ $ldapConnection = $this->ldap->connect($server['host'], $server['port']);
+
+ if (!$ldapConnection) {
+ throw new LdapException(trans('errors.ldap_cannot_connect'));
+ }
+
+ // Set any required options
+ if ($this->config['version']) {
+ $this->ldap->setVersion($ldapConnection, $this->config['version']);
+ }
+
+ // Start and verify TLS if it's enabled
+ if ($this->config['start_tls']) {
+ $started = $this->ldap->startTls($ldapConnection);
+ if (!$started) {
+ throw new LdapException('Could not start TLS connection');
+ }
+ }
+
+ return $ldapConnection;
+ } catch (LdapException $exception) {
+ $lastException = $exception;
}
}
- $this->ldapConnection = $ldapConnection;
+ throw $lastException;
+ }
- return $this->ldapConnection;
+ /**
+ * Parse environment variable with LDAP server and returns an array of recognized servers.
+ * If you need to use multiple addresses, separate them with a semicolon.
+ * Ex: 'ldap.example.com:8069;ldaps://ldap.example.com'
+ */
+ protected function parseEnvironmentServer(string $environmentServer): array
+ {
+ $explodedEnvironmentServer = explode(';', $environmentServer);
+ $result_servers = [];
+
+ foreach ($explodedEnvironmentServer as $serverString) {
+ $result_servers[] = $this->parseServerString($serverString);
+ }
+
+ return $result_servers;
}
/**