Skip to content

Commit 90341e0

Browse files
committed
LDAP: Review and testing of mulitple-display-name attr support
Review of #5295 Added test to cover functionality. Moved splitting from config to service.
1 parent 87242ce commit 90341e0

File tree

3 files changed

+45
-20
lines changed

3 files changed

+45
-20
lines changed

app/Access/LdapService.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,23 @@ private function getUserWithAttributes(string $userName, array $attributes): ?ar
7272
}
7373

7474
/**
75-
* Calculate the display name.
75+
* Build the user display name from the (potentially multiple) attributes defined by the configuration.
7676
*/
77-
protected function getUserDisplayName(array $displayNameAttr, array $userDetails, string $defaultValue): string
77+
protected function getUserDisplayName(array $userDetails, array $displayNameAttrs, string $defaultValue): string
7878
{
79-
$displayName = [];
80-
foreach ($displayNameAttr as $dnAttr) {
79+
$displayNameParts = [];
80+
foreach ($displayNameAttrs as $dnAttr) {
8181
$dnComponent = $this->getUserResponseProperty($userDetails, $dnAttr, null);
82-
if ($dnComponent !== null) {
83-
$displayName[] = $dnComponent;
82+
if ($dnComponent) {
83+
$displayNameParts[] = $dnComponent;
8484
}
8585
}
8686

87-
if (count($displayName) == 0) {
88-
$displayName = $defaultValue;
89-
} else {
90-
$displayName = implode(' ', $displayName);
87+
if (empty($displayNameParts)) {
88+
return $defaultValue;
9189
}
9290

93-
return $displayName;
91+
return implode(' ', $displayNameParts);
9492
}
9593

9694
/**
@@ -103,12 +101,12 @@ public function getUserDetails(string $userName): ?array
103101
{
104102
$idAttr = $this->config['id_attribute'];
105103
$emailAttr = $this->config['email_attribute'];
106-
$displayNameAttr = $this->config['display_name_attribute'];
104+
$displayNameAttrs = explode('|', $this->config['display_name_attribute']);
107105
$thumbnailAttr = $this->config['thumbnail_attribute'];
108106

109-
$user = $this->getUserWithAttributes($userName, array_filter(array_merge($displayNameAttr, [
110-
'cn', 'dn', $idAttr, $emailAttr, $thumbnailAttr,
111-
])));
107+
$user = $this->getUserWithAttributes($userName, array_filter([
108+
'cn', 'dn', $idAttr, $emailAttr, ...$displayNameAttrs, $thumbnailAttr,
109+
]));
112110

113111
if (is_null($user)) {
114112
return null;
@@ -117,7 +115,7 @@ public function getUserDetails(string $userName): ?array
117115
$userCn = $this->getUserResponseProperty($user, 'cn', null);
118116
$formatted = [
119117
'uid' => $this->getUserResponseProperty($user, $idAttr, $user['dn']),
120-
'name' => $this->getUserDisplayName($displayNameAttr, $user, $userCn),
118+
'name' => $this->getUserDisplayName($user, $displayNameAttrs, $userCn),
121119
'dn' => $user['dn'],
122120
'email' => $this->getUserResponseProperty($user, $emailAttr, null),
123121
'avatar' => $thumbnailAttr ? $this->getUserResponseProperty($user, $thumbnailAttr, null) : null,

app/Config/services.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
'version' => env('LDAP_VERSION', false),
128128
'id_attribute' => env('LDAP_ID_ATTRIBUTE', 'uid'),
129129
'email_attribute' => env('LDAP_EMAIL_ATTRIBUTE', 'mail'),
130-
'display_name_attribute' => explode('|', env('LDAP_DISPLAY_NAME_ATTRIBUTE', 'cn')),
130+
'display_name_attribute' => env('LDAP_DISPLAY_NAME_ATTRIBUTE', 'cn'),
131131
'follow_referrals' => env('LDAP_FOLLOW_REFERRALS', false),
132132
'user_to_groups' => env('LDAP_USER_TO_GROUPS', false),
133133
'group_attribute' => env('LDAP_GROUP_ATTRIBUTE', 'memberOf'),

tests/Auth/LdapTest.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected function setUp(): void
2929
'auth.defaults.guard' => 'ldap',
3030
'services.ldap.base_dn' => 'dc=ldap,dc=local',
3131
'services.ldap.email_attribute' => 'mail',
32-
'services.ldap.display_name_attribute' => ['cn'],
32+
'services.ldap.display_name_attribute' => 'cn',
3333
'services.ldap.id_attribute' => 'uid',
3434
'services.ldap.user_to_groups' => false,
3535
'services.ldap.version' => '3',
@@ -581,7 +581,7 @@ public function test_login_group_mapping_does_not_conflict_with_default_role()
581581
public function test_login_uses_specified_display_name_attribute()
582582
{
583583
app('config')->set([
584-
'services.ldap.display_name_attribute' => ['displayName'],
584+
'services.ldap.display_name_attribute' => 'displayName',
585585
]);
586586

587587
$this->commonLdapMocks(1, 1, 2, 4, 2);
@@ -603,10 +603,37 @@ 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([
609-
'services.ldap.display_name_attribute' => ['displayName'],
636+
'services.ldap.display_name_attribute' => 'displayName',
610637
]);
611638

612639
$this->commonLdapMocks(1, 1, 2, 4, 2);

0 commit comments

Comments
 (0)