1 <?php namespace BookStack\Services;
4 use BookStack\Exceptions\LdapException;
9 public function getUserDetails($userName)
12 if(!function_exists('ldap_connect')) {
13 throw new LdapException('LDAP PHP extension not installed');
17 $ldapServer = explode(':', config('services.ldap.server'));
18 $ldapConnection = ldap_connect($ldapServer[0], count($ldapServer) > 1 ? $ldapServer[1] : 389);
20 if ($ldapConnection === false) {
21 throw new LdapException('Cannot connect to ldap server, Initial connection failed');
26 ldap_set_option($ldapConnection, LDAP_OPT_PROTOCOL_VERSION, 3); // TODO - make configurable
28 $ldapDn = config('services.ldap.dn');
29 $ldapPass = config('services.ldap.pass');
30 $isAnonymous = ($ldapDn === false || $ldapPass === false);
32 $ldapBind = ldap_bind($ldapConnection);
34 $ldapBind = ldap_bind($ldapConnection, $ldapDn, $ldapPass);
37 if (!$ldapBind) throw new LdapException('LDAP access failed using ' . $isAnonymous ? ' anonymous bind.' : ' given dn & pass details');
40 $userFilter = $this->buildFilter(config('services.ldap.user_filter'), ['user' => $userName]);
42 $baseDn = config('services.ldap.base_dn');
43 $ldapSearch = ldap_search($ldapConnection, $baseDn, $userFilter);
44 $users = ldap_get_entries($ldapConnection, $ldapSearch);
50 private function buildFilter($filterString, $attrs)
53 foreach ($attrs as $key => $attrText) {
54 $newKey = '${'.$key.'}';
55 $newAttrs[$newKey] = $attrText;
57 return strtr($filterString, $newAttrs);