Skip to content

Commit 72d9ffd

Browse files
Added support for concatenating multiple LDAP attributes in displayName
1 parent e4ca3bf commit 72d9ffd

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

app/Access/LdapService.php

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

74+
/**
75+
* Calculate the display name.
76+
*/
77+
protected function getUserDisplayName(array $displayNameAttr, array $userDetails, string $defaultValue): string
78+
{
79+
$displayName = [];
80+
foreach ($displayNameAttr as $dnAttr) {
81+
$dnComponent = $this->getUserResponseProperty($userDetails, $dnAttr, null);
82+
if ($dnComponent !== null) {
83+
$displayName[] = $dnComponent;
84+
}
85+
}
86+
87+
if (count($displayName) == 0) {
88+
$displayName = $defaultValue;
89+
} else {
90+
$displayName = implode(' ', $displayName);
91+
}
92+
93+
return $displayName;
94+
}
95+
7496
/**
7597
* Get the details of a user from LDAP using the given username.
7698
* User found via configurable user filter.
@@ -84,9 +106,9 @@ public function getUserDetails(string $userName): ?array
84106
$displayNameAttr = $this->config['display_name_attribute'];
85107
$thumbnailAttr = $this->config['thumbnail_attribute'];
86108

87-
$user = $this->getUserWithAttributes($userName, array_filter([
88-
'cn', 'dn', $idAttr, $emailAttr, $displayNameAttr, $thumbnailAttr,
89-
]));
109+
$user = $this->getUserWithAttributes($userName, array_filter(array_merge($displayNameAttr, [
110+
'cn', 'dn', $idAttr, $emailAttr, $thumbnailAttr,
111+
])));
90112

91113
if (is_null($user)) {
92114
return null;
@@ -95,7 +117,7 @@ public function getUserDetails(string $userName): ?array
95117
$userCn = $this->getUserResponseProperty($user, 'cn', null);
96118
$formatted = [
97119
'uid' => $this->getUserResponseProperty($user, $idAttr, $user['dn']),
98-
'name' => $this->getUserResponseProperty($user, $displayNameAttr, $userCn),
120+
'name' => $this->getUserDisplayName($displayNameAttr, $user, $userCn),
99121
'dn' => $user['dn'],
100122
'email' => $this->getUserResponseProperty($user, $emailAttr, null),
101123
'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' => env('LDAP_DISPLAY_NAME_ATTRIBUTE', 'cn'),
130+
'display_name_attribute' => explode('|', 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'),

0 commit comments

Comments
 (0)