Dan Brown

Updated LDAP to allow protocol to be specified

As per details by fredericmohr in #236
...@@ -18,24 +18,6 @@ class Ldap ...@@ -18,24 +18,6 @@ class Ldap
18 */ 18 */
19 public function connect($hostName, $port) 19 public function connect($hostName, $port)
20 { 20 {
21 - /*
22 - * LDAPS is not working because even if port 363 is specified,
23 - * BookStack tries to open a LDAP connection on the LDAPS channel.
24 - * The if-clause below fixed this, although it would be better to
25 - * change the settings in .env from
26 - * LDAP_SERVER=servername:port
27 - * to
28 - * LDAP_SERVER=ldap://servername:389
29 - * LDAP_SERVER=ldaps://servername:363
30 - * in order to be compatible with non-standard setups. Currently,
31 - * specifying ldap:// or ldaps:// results in an error because BookStack
32 - * splits at ":" and takes the seconds chunk (in this case "//servername"
33 - * as the port value.
34 - */
35 - if ($port == 363)
36 - {
37 - $hostName = "ldaps://".$hostName;
38 - }
39 return ldap_connect($hostName, $port); 21 return ldap_connect($hostName, $port);
40 } 22 }
41 23
......
...@@ -112,9 +112,13 @@ class LdapService ...@@ -112,9 +112,13 @@ class LdapService
112 throw new LdapException(trans('errors.ldap_extension_not_installed')); 112 throw new LdapException(trans('errors.ldap_extension_not_installed'));
113 } 113 }
114 114
115 - // Get port from server string if specified. 115 + // Get port from server string and protocol if specified.
116 $ldapServer = explode(':', $this->config['server']); 116 $ldapServer = explode(':', $this->config['server']);
117 - $ldapConnection = $this->ldap->connect($ldapServer[0], count($ldapServer) > 1 ? $ldapServer[1] : 389); 117 + $hasProtocol = preg_match('/^ldaps{0,1}\:\/\//', $this->config['server']) === 1;
118 + if (!$hasProtocol) array_unshift($ldapServer, '');
119 + $hostName = $ldapServer[0] . ($hasProtocol?':':'') . $ldapServer[1];
120 + $defaultPort = $ldapServer[0] === 'ldaps' ? 636 : 389;
121 + $ldapConnection = $this->ldap->connect($hostName, count($ldapServer) > 2 ? intval($ldapServer[2]) : $defaultPort);
118 122
119 if ($ldapConnection === false) { 123 if ($ldapConnection === false) {
120 throw new LdapException(trans('errors.ldap_cannot_connect')); 124 throw new LdapException(trans('errors.ldap_cannot_connect'));
......