Skip to content

Commit 80f258c

Browse files
committed
Merge branch 'fix-ldap-display-name' into development
2 parents 3298374 + 90341e0 commit 80f258c

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

app/Access/LdapService.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,26 @@ private function getUserWithAttributes(string $userName, array $attributes): ?ar
7171
return $users[0];
7272
}
7373

74+
/**
75+
* Build the user display name from the (potentially multiple) attributes defined by the configuration.
76+
*/
77+
protected function getUserDisplayName(array $userDetails, array $displayNameAttrs, string $defaultValue): string
78+
{
79+
$displayNameParts = [];
80+
foreach ($displayNameAttrs as $dnAttr) {
81+
$dnComponent = $this->getUserResponseProperty($userDetails, $dnAttr, null);
82+
if ($dnComponent) {
83+
$displayNameParts[] = $dnComponent;
84+
}
85+
}
86+
87+
if (empty($displayNameParts)) {
88+
return $defaultValue;
89+
}
90+
91+
return implode(' ', $displayNameParts);
92+
}
93+
7494
/**
7595
* Get the details of a user from LDAP using the given username.
7696
* User found via configurable user filter.
@@ -81,11 +101,11 @@ public function getUserDetails(string $userName): ?array
81101
{
82102
$idAttr = $this->config['id_attribute'];
83103
$emailAttr = $this->config['email_attribute'];
84-
$displayNameAttr = $this->config['display_name_attribute'];
104+
$displayNameAttrs = explode('|', $this->config['display_name_attribute']);
85105
$thumbnailAttr = $this->config['thumbnail_attribute'];
86106

87107
$user = $this->getUserWithAttributes($userName, array_filter([
88-
'cn', 'dn', $idAttr, $emailAttr, $displayNameAttr, $thumbnailAttr,
108+
'cn', 'dn', $idAttr, $emailAttr, ...$displayNameAttrs, $thumbnailAttr,
89109
]));
90110

91111
if (is_null($user)) {
@@ -95,7 +115,7 @@ public function getUserDetails(string $userName): ?array
95115
$userCn = $this->getUserResponseProperty($user, 'cn', null);
96116
$formatted = [
97117
'uid' => $this->getUserResponseProperty($user, $idAttr, $user['dn']),
98-
'name' => $this->getUserResponseProperty($user, $displayNameAttr, $userCn),
118+
'name' => $this->getUserDisplayName($user, $displayNameAttrs, $userCn),
99119
'dn' => $user['dn'],
100120
'email' => $this->getUserResponseProperty($user, $emailAttr, null),
101121
'avatar' => $thumbnailAttr ? $this->getUserResponseProperty($user, $thumbnailAttr, null) : null,

tests/Auth/LdapTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,33 @@ public function test_login_uses_specified_display_name_attribute()
603603
$this->assertDatabaseHas('users', ['email' => $this->mockUser->email, 'email_confirmed' => false, 'external_auth_id' => $this->mockUser->name, 'name' => 'displayNameAttribute']);
604604
}
605605

606+
public function test_login_uses_multiple_display_properties_if_defined()
607+
{
608+
app('config')->set([
609+
'services.ldap.display_name_attribute' => 'firstname|middlename|noname|lastname',
610+
]);
611+
612+
$this->commonLdapMocks(1, 1, 1, 2, 1);
613+
$this->mockLdap->shouldReceive('searchAndGetEntries')->times(1)
614+
->with($this->resourceId, config('services.ldap.base_dn'), \Mockery::type('string'), \Mockery::type('array'))
615+
->andReturn(['count' => 1, 0 => [
616+
'uid' => [$this->mockUser->name],
617+
'cn' => [$this->mockUser->name],
618+
'dn' => 'dc=test' . config('services.ldap.base_dn'),
619+
'firstname' => ['Barry'],
620+
'middlename' => ['Elliott'],
621+
'lastname' => ['Chuckle'],
622+
'mail' => [$this->mockUser->email],
623+
]]);
624+
625+
$this->mockUserLogin();
626+
627+
$this->assertDatabaseHas('users', [
628+
'email' => $this->mockUser->email,
629+
'name' => 'Barry Elliott Chuckle',
630+
]);
631+
}
632+
606633
public function test_login_uses_default_display_name_attribute_if_specified_not_present()
607634
{
608635
app('config')->set([

0 commit comments

Comments
 (0)