Skip to content

Commit 35b45a2

Browse files
committed
LDAP: Fixed php type error when no cn provided for user
Changes default fallback for name to first DN part, otherwise the whole DN, rather than leave as null which was causing a type error. For #5443
1 parent 3b4d343 commit 35b45a2

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

app/Access/LdapService.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,14 @@ public function getUserDetails(string $userName): ?array
112112
return null;
113113
}
114114

115-
$userCn = $this->getUserResponseProperty($user, 'cn', null);
115+
$nameDefault = $this->getUserResponseProperty($user, 'cn', null);
116+
if (is_null($nameDefault)) {
117+
$nameDefault = ldap_explode_dn($user['dn'], 1)[0] ?? $user['dn'];
118+
}
119+
116120
$formatted = [
117121
'uid' => $this->getUserResponseProperty($user, $idAttr, $user['dn']),
118-
'name' => $this->getUserDisplayName($user, $displayNameAttrs, $userCn),
122+
'name' => $this->getUserDisplayName($user, $displayNameAttrs, $nameDefault),
119123
'dn' => $user['dn'],
120124
'email' => $this->getUserResponseProperty($user, $emailAttr, null),
121125
'avatar' => $thumbnailAttr ? $this->getUserResponseProperty($user, $thumbnailAttr, null) : null,

tests/Auth/LdapTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,26 @@ public function test_login_works_when_no_uid_provided_by_ldap_server()
166166
$this->assertDatabaseHas('users', ['email' => $this->mockUser->email, 'email_confirmed' => false, 'external_auth_id' => $ldapDn]);
167167
}
168168

169+
public function test_login_works_when_ldap_server_does_not_provide_a_cn_value()
170+
{
171+
$ldapDn = 'cn=test-user,dc=test' . config('services.ldap.base_dn');
172+
173+
$this->commonLdapMocks(1, 1, 1, 2, 1);
174+
$this->mockLdap->shouldReceive('searchAndGetEntries')->times(1)
175+
->with($this->resourceId, config('services.ldap.base_dn'), \Mockery::type('string'), \Mockery::type('array'))
176+
->andReturn(['count' => 1, 0 => [
177+
'dn' => $ldapDn,
178+
'mail' => [$this->mockUser->email],
179+
]]);
180+
181+
$resp = $this->mockUserLogin();
182+
$resp->assertRedirect('/');
183+
$this->assertDatabaseHas('users', [
184+
'name' => 'test-user',
185+
'email' => $this->mockUser->email,
186+
]);
187+
}
188+
169189
public function test_a_custom_uid_attribute_can_be_specified_and_is_used_properly()
170190
{
171191
config()->set(['services.ldap.id_attribute' => 'my_custom_id']);

0 commit comments

Comments
 (0)