Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: Ability to merge firstname and lastname when fullname is not the… #12

Open
wants to merge 7 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
9 changes: 8 additions & 1 deletion app/Access/LdapService.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,24 @@ 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)[0];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brijm-improwised it should be 1. Also, you should check length before accessing index 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)) {
return null;
}

$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),
Expand Down
42 changes: 42 additions & 0 deletions tests/Auth/LdapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]]);
Expand Down Expand Up @@ -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]);
Expand Down
Loading