diff --git a/app/Access/LdapService.php b/app/Access/LdapService.php index 365cb1db015..1d64ce3fd57 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']; + $firstName = explode(',', $displayNameAttr)[0]; + $lastName = explode(',', $displayNameAttr)[1]; $thumbnailAttr = $this->config['thumbnail_attribute']; $user = $this->getUserWithAttributes($userName, array_filter([ - 'cn', 'dn', $idAttr, $emailAttr, $displayNameAttr, $thumbnailAttr, + 'cn', 'dn', $idAttr, $emailAttr, $firstName,$lastName, $thumbnailAttr, ])); if (is_null($user)) { @@ -93,6 +95,11 @@ public function getUserDetails(string $userName): ?array } $userCn = $this->getUserResponseProperty($user, 'cn', null); + + if ($userCn === null) { + $userCn = $this->getUserResponseProperty($user, $firstName, null) . ' ' . $this->getUserResponseProperty($user, $lastName, null); + } + $formatted = [ 'uid' => $this->getUserResponseProperty($user, $idAttr, $user['dn']), 'name' => $this->getUserResponseProperty($user, $displayNameAttr, $userCn), diff --git a/tests/Auth/LdapTest.php b/tests/Auth/LdapTest.php index ef95bc2e8f4..7b396263233 100644 --- a/tests/Auth/LdapTest.php +++ b/tests/Auth/LdapTest.php @@ -590,6 +590,8 @@ public function test_login_uses_specified_display_name_attribute() ->andReturn(['count' => 1, 0 => [ 'uid' => [$this->mockUser->name], 'cn' => [$this->mockUser->name], + 'givenName' => [explode(" ", $this->mockUser)[0]], + 'sn' => [explode(" ", $this->mockUser)[1]], 'dn' => 'dc=test' . config('services.ldap.base_dn'), 'displayname' => 'displayNameAttribute', ]]); @@ -632,6 +634,46 @@ public function test_login_uses_default_display_name_attribute_if_specified_not_ ]); } + public function test_login_uses_givenName_and_sn_merge_if_display_name_is_not_present() + { + app('config')->set([ + 'services.ldap.display_name_attribute' => 'displayName', + ]); + + $this->mockUser->firstName = explode(" ", $this->mockUser->name)[0]; + $this->mockUser->lastName = explode(" ", $this->mockUser->name)[1]; + + $this->commonLdapMocks(1, 1, 2, 4, 2); + $this->mockLdap->shouldReceive('searchAndGetEntries')->times(2) + ->with($this->resourceId, config('services.ldap.base_dn'), \Mockery::type('string'), \Mockery::type('array')) + ->andReturn(['count' => 1, 0 => [ + 'uid' => [$this->mockUser->name], + 'cn' => [$this->mockUser->name], + 'givenName' => [$this->mockUser->firstName], + 'sn' => [$this->mockUser->lastName], + 'dn' => 'dc=test' . config('services.ldap.base_dn'), + ]]); + + $this->mockUserLogin()->assertRedirect('/login'); + $this->get('/login')->assertSee('Please enter an email to use for this account.'); + + $resp = $this->mockUserLogin($this->mockUser->email); + $resp->assertRedirect('/'); + + $expectedDisplayName = $this->mockUser->firstName . ' ' . $this->mockUser->lastName; + + $this->get('/')->assertSee($expectedDisplayName); + + $this->assertDatabaseHas('users', [ + 'email' => $this->mockUser->email, + 'email_confirmed' => false, + 'external_auth_id' => $this->mockUser->name, + 'name' => $this->mockUser->name, + ]); + } + + + protected function checkLdapReceivesCorrectDetails($serverString, $expectedHostString): void { app('config')->set(['services.ldap.server' => $serverString]);