From 0346c438491709a5720464175622a174104b0336 Mon Sep 17 00:00:00 2001 From: Matthieu Leboeuf Date: Thu, 24 Oct 2024 22:58:40 +0200 Subject: [PATCH] Add support of first_name and last_name ldap variables to override display_name --- app/Access/LdapService.php | 15 +++++++++++++-- app/Config/services.php | 2 ++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/app/Access/LdapService.php b/app/Access/LdapService.php index 365cb1db015..7e65035c8e7 100644 --- a/app/Access/LdapService.php +++ b/app/Access/LdapService.php @@ -82,10 +82,12 @@ public function getUserDetails(string $userName): ?array $idAttr = $this->config['id_attribute']; $emailAttr = $this->config['email_attribute']; $displayNameAttr = $this->config['display_name_attribute']; + $firstNameAttr = $this->config['first_name_attribute']; + $lastNameAttr = $this->config['last_name_attribute']; $thumbnailAttr = $this->config['thumbnail_attribute']; $user = $this->getUserWithAttributes($userName, array_filter([ - 'cn', 'dn', $idAttr, $emailAttr, $displayNameAttr, $thumbnailAttr, + 'cn', 'dn', $idAttr, $emailAttr, $displayNameAttr, $firstNameAttr, $lastNameAttr, $thumbnailAttr ])); if (is_null($user)) { @@ -93,9 +95,18 @@ public function getUserDetails(string $userName): ?array } $userCn = $this->getUserResponseProperty($user, 'cn', null); + + $name = $this->getUserResponseProperty($user, $displayNameAttr, $userCn); + + if ($firstNameAttr and $lastNameAttr) { + $firstName = $this->getUserResponseProperty($user, $firstNameAttr, null); + $lastName = $this->getUserResponseProperty($user, $lastNameAttr, null); + $name = $firstName . ' ' . $lastName; + } + $formatted = [ 'uid' => $this->getUserResponseProperty($user, $idAttr, $user['dn']), - 'name' => $this->getUserResponseProperty($user, $displayNameAttr, $userCn), + 'name' => $name, 'dn' => $user['dn'], 'email' => $this->getUserResponseProperty($user, $emailAttr, null), 'avatar' => $thumbnailAttr ? $this->getUserResponseProperty($user, $thumbnailAttr, null) : null, diff --git a/app/Config/services.php b/app/Config/services.php index d7345823150..e7d911a0ab0 100644 --- a/app/Config/services.php +++ b/app/Config/services.php @@ -128,6 +128,8 @@ 'id_attribute' => env('LDAP_ID_ATTRIBUTE', 'uid'), 'email_attribute' => env('LDAP_EMAIL_ATTRIBUTE', 'mail'), 'display_name_attribute' => env('LDAP_DISPLAY_NAME_ATTRIBUTE', 'cn'), + 'first_name_attribute' => env('LDAP_FIRST_NAME_ATTRIBUTE', null), + 'last_name_attribute' => env('LDAP_LAST_NAME_ATTRIBUTE', null), 'follow_referrals' => env('LDAP_FOLLOW_REFERRALS', false), 'user_to_groups' => env('LDAP_USER_TO_GROUPS', false), 'group_attribute' => env('LDAP_GROUP_ATTRIBUTE', 'memberOf'),