Merge branch 'fix-ldap-display-name' into development
This commit is contained in:
commit
80f258c3c5
|
@ -71,6 +71,26 @@ class LdapService
|
||||||
return $users[0];
|
return $users[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build the user display name from the (potentially multiple) attributes defined by the configuration.
|
||||||
|
*/
|
||||||
|
protected function getUserDisplayName(array $userDetails, array $displayNameAttrs, string $defaultValue): string
|
||||||
|
{
|
||||||
|
$displayNameParts = [];
|
||||||
|
foreach ($displayNameAttrs as $dnAttr) {
|
||||||
|
$dnComponent = $this->getUserResponseProperty($userDetails, $dnAttr, null);
|
||||||
|
if ($dnComponent) {
|
||||||
|
$displayNameParts[] = $dnComponent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($displayNameParts)) {
|
||||||
|
return $defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
return implode(' ', $displayNameParts);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the details of a user from LDAP using the given username.
|
* Get the details of a user from LDAP using the given username.
|
||||||
* User found via configurable user filter.
|
* User found via configurable user filter.
|
||||||
|
@ -81,11 +101,11 @@ class LdapService
|
||||||
{
|
{
|
||||||
$idAttr = $this->config['id_attribute'];
|
$idAttr = $this->config['id_attribute'];
|
||||||
$emailAttr = $this->config['email_attribute'];
|
$emailAttr = $this->config['email_attribute'];
|
||||||
$displayNameAttr = $this->config['display_name_attribute'];
|
$displayNameAttrs = explode('|', $this->config['display_name_attribute']);
|
||||||
$thumbnailAttr = $this->config['thumbnail_attribute'];
|
$thumbnailAttr = $this->config['thumbnail_attribute'];
|
||||||
|
|
||||||
$user = $this->getUserWithAttributes($userName, array_filter([
|
$user = $this->getUserWithAttributes($userName, array_filter([
|
||||||
'cn', 'dn', $idAttr, $emailAttr, $displayNameAttr, $thumbnailAttr,
|
'cn', 'dn', $idAttr, $emailAttr, ...$displayNameAttrs, $thumbnailAttr,
|
||||||
]));
|
]));
|
||||||
|
|
||||||
if (is_null($user)) {
|
if (is_null($user)) {
|
||||||
|
@ -95,7 +115,7 @@ class LdapService
|
||||||
$userCn = $this->getUserResponseProperty($user, 'cn', null);
|
$userCn = $this->getUserResponseProperty($user, 'cn', null);
|
||||||
$formatted = [
|
$formatted = [
|
||||||
'uid' => $this->getUserResponseProperty($user, $idAttr, $user['dn']),
|
'uid' => $this->getUserResponseProperty($user, $idAttr, $user['dn']),
|
||||||
'name' => $this->getUserResponseProperty($user, $displayNameAttr, $userCn),
|
'name' => $this->getUserDisplayName($user, $displayNameAttrs, $userCn),
|
||||||
'dn' => $user['dn'],
|
'dn' => $user['dn'],
|
||||||
'email' => $this->getUserResponseProperty($user, $emailAttr, null),
|
'email' => $this->getUserResponseProperty($user, $emailAttr, null),
|
||||||
'avatar' => $thumbnailAttr ? $this->getUserResponseProperty($user, $thumbnailAttr, null) : null,
|
'avatar' => $thumbnailAttr ? $this->getUserResponseProperty($user, $thumbnailAttr, null) : null,
|
||||||
|
|
|
@ -603,6 +603,33 @@ class LdapTest extends TestCase
|
||||||
$this->assertDatabaseHas('users', ['email' => $this->mockUser->email, 'email_confirmed' => false, 'external_auth_id' => $this->mockUser->name, 'name' => 'displayNameAttribute']);
|
$this->assertDatabaseHas('users', ['email' => $this->mockUser->email, 'email_confirmed' => false, 'external_auth_id' => $this->mockUser->name, 'name' => 'displayNameAttribute']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_login_uses_multiple_display_properties_if_defined()
|
||||||
|
{
|
||||||
|
app('config')->set([
|
||||||
|
'services.ldap.display_name_attribute' => 'firstname|middlename|noname|lastname',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->commonLdapMocks(1, 1, 1, 2, 1);
|
||||||
|
$this->mockLdap->shouldReceive('searchAndGetEntries')->times(1)
|
||||||
|
->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],
|
||||||
|
'dn' => 'dc=test' . config('services.ldap.base_dn'),
|
||||||
|
'firstname' => ['Barry'],
|
||||||
|
'middlename' => ['Elliott'],
|
||||||
|
'lastname' => ['Chuckle'],
|
||||||
|
'mail' => [$this->mockUser->email],
|
||||||
|
]]);
|
||||||
|
|
||||||
|
$this->mockUserLogin();
|
||||||
|
|
||||||
|
$this->assertDatabaseHas('users', [
|
||||||
|
'email' => $this->mockUser->email,
|
||||||
|
'name' => 'Barry Elliott Chuckle',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
public function test_login_uses_default_display_name_attribute_if_specified_not_present()
|
public function test_login_uses_default_display_name_attribute_if_specified_not_present()
|
||||||
{
|
{
|
||||||
app('config')->set([
|
app('config')->set([
|
||||||
|
|
Loading…
Reference in New Issue