Skip to content

Commit 58e9bd4

Browse files
author
tchapi
committed
chore
1 parent 5e8392b commit 58e9bd4

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/Services/LDAPAuth.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,30 @@ protected function ldapOpen($username, $password)
172172
}
173173

174174
if ($success && $this->autoCreate) {
175-
$user = $this->doctrine->getRepository(User::class)->findOneBy(['username' => $username]);
175+
// First, we'll be asking the LDAP server to give us the user name back, in case the LDAP server is case-insensitive
176+
// See https://github.com/tchapi/davis/issues/167
177+
$realUsername = $username;
178+
179+
try {
180+
$search_results = ldap_read($ldap, $dn, '(objectclass=*)', ['uid']);
181+
} catch (\Exception $e) {
182+
// Probably a "No such object" error, ignore and use available credentials (username)
183+
}
184+
185+
if (false !== $search_results) {
186+
$entry = ldap_get_entries($ldap, $search_results);
187+
188+
if (false !== $entry && !empty($entry[0]['uid'])) {
189+
$realUsername = $entry[0]['uid'][0];
190+
}
191+
}
192+
193+
$user = $this->doctrine->getRepository(User::class)->findOneBy(['username' => $realUsername]);
176194

177195
if (!$user) {
178196
// Default fallback values
179-
$displayName = $username;
180-
$email = $username;
197+
$displayName = $realUsername;
198+
$email = $realUsername;
181199

182200
// Try to extract display name and email for this user.
183201
// NB: We suppose display name is `cn` (email is configurable, generally `mail`)
@@ -201,7 +219,7 @@ protected function ldapOpen($username, $password)
201219
}
202220
}
203221

204-
$this->utils->createPasswordlessUserWithDefaultObjects($username, $displayName, $email);
222+
$this->utils->createPasswordlessUserWithDefaultObjects($realUsername, $displayName, $email);
205223

206224
$em = $this->doctrine->getManager();
207225

0 commit comments

Comments
 (0)