Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions app/Access/LdapService.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,28 @@ private function getUserWithAttributes(string $userName, array $attributes): ?ar
return $users[0];
}

/**
* Calculate the display name.
*/
protected function getUserDisplayName(array $displayNameAttr, array $userDetails, string $defaultValue): string
{
$displayName = [];
foreach ($displayNameAttr as $dnAttr) {
$dnComponent = $this->getUserResponseProperty($userDetails, $dnAttr, null);
if ($dnComponent !== null) {
$displayName[] = $dnComponent;
}
}

if (count($displayName) == 0) {
$displayName = $defaultValue;
} else {
$displayName = implode(' ', $displayName);
}

return $displayName;
}

/**
* Get the details of a user from LDAP using the given username.
* User found via configurable user filter.
Expand All @@ -84,9 +106,9 @@ public function getUserDetails(string $userName): ?array
$displayNameAttr = $this->config['display_name_attribute'];
$thumbnailAttr = $this->config['thumbnail_attribute'];

$user = $this->getUserWithAttributes($userName, array_filter([
'cn', 'dn', $idAttr, $emailAttr, $displayNameAttr, $thumbnailAttr,
]));
$user = $this->getUserWithAttributes($userName, array_filter(array_merge($displayNameAttr, [
'cn', 'dn', $idAttr, $emailAttr, $thumbnailAttr,
])));

if (is_null($user)) {
return null;
Expand All @@ -95,7 +117,7 @@ public function getUserDetails(string $userName): ?array
$userCn = $this->getUserResponseProperty($user, 'cn', null);
$formatted = [
'uid' => $this->getUserResponseProperty($user, $idAttr, $user['dn']),
'name' => $this->getUserResponseProperty($user, $displayNameAttr, $userCn),
'name' => $this->getUserDisplayName($displayNameAttr, $user, $userCn),
'dn' => $user['dn'],
'email' => $this->getUserResponseProperty($user, $emailAttr, null),
'avatar' => $thumbnailAttr ? $this->getUserResponseProperty($user, $thumbnailAttr, null) : null,
Expand Down
2 changes: 1 addition & 1 deletion app/Config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
'version' => env('LDAP_VERSION', false),
'id_attribute' => env('LDAP_ID_ATTRIBUTE', 'uid'),
'email_attribute' => env('LDAP_EMAIL_ATTRIBUTE', 'mail'),
'display_name_attribute' => env('LDAP_DISPLAY_NAME_ATTRIBUTE', 'cn'),
'display_name_attribute' => explode('|', env('LDAP_DISPLAY_NAME_ATTRIBUTE', 'cn')),
'follow_referrals' => env('LDAP_FOLLOW_REFERRALS', false),
'user_to_groups' => env('LDAP_USER_TO_GROUPS', false),
'group_attribute' => env('LDAP_GROUP_ATTRIBUTE', 'memberOf'),
Expand Down
6 changes: 3 additions & 3 deletions tests/Auth/LdapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected function setUp(): void
'auth.defaults.guard' => 'ldap',
'services.ldap.base_dn' => 'dc=ldap,dc=local',
'services.ldap.email_attribute' => 'mail',
'services.ldap.display_name_attribute' => 'cn',
'services.ldap.display_name_attribute' => ['cn'],
'services.ldap.id_attribute' => 'uid',
'services.ldap.user_to_groups' => false,
'services.ldap.version' => '3',
Expand Down Expand Up @@ -581,7 +581,7 @@ public function test_login_group_mapping_does_not_conflict_with_default_role()
public function test_login_uses_specified_display_name_attribute()
{
app('config')->set([
'services.ldap.display_name_attribute' => 'displayName',
'services.ldap.display_name_attribute' => ['displayName'],
]);

$this->commonLdapMocks(1, 1, 2, 4, 2);
Expand All @@ -606,7 +606,7 @@ public function test_login_uses_specified_display_name_attribute()
public function test_login_uses_default_display_name_attribute_if_specified_not_present()
{
app('config')->set([
'services.ldap.display_name_attribute' => 'displayName',
'services.ldap.display_name_attribute' => ['displayName'],
]);

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