Skip to content

Commit

Permalink
Bugfix: reinstate the possibility to return ALL attributes (#40)
Browse files Browse the repository at this point in the history
* Bugfix: reinstate the possibility to return ALL attributes
* Fix & add unit tests

Closes #5
  • Loading branch information
tvdijen committed Mar 1, 2023
1 parent 274d695 commit 836da78
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/Auth/Source/Ldap.php
Expand Up @@ -179,7 +179,12 @@ public function getAttributes(string $username): array
*/
private function processAttributes(Entry $entry): array
{
$attributes = $this->ldapConfig->getOptionalValue('attributes', []);
$attributes = $this->ldapConfig->getOptionalValue(
'attributes',
// If specifically set to NULL return all attributes, if not set at all return nothing (safe default)
$this->ldapConfig->hasValue('attributes') ? null: [],
);

if ($attributes === null) {
$result = $entry->getAttributes();
} else {
Expand Down
22 changes: 17 additions & 5 deletions tests/src/Auth/Source/LdapTest.php
Expand Up @@ -47,7 +47,7 @@ public function __construct(ConnectorInterface $connector)
parent::__construct(
['AuthId' => 'ldap_login'],
[
'attributes' => null,
'attributes' => ['test1', 'test2', 'test3'],
'search.base' => ['DC=example,DC=com'],
'search.username' => 'readonly',
'dnpattern' => '%username%@example.com'
Expand All @@ -62,7 +62,7 @@ public function testLogin(): void
{
$source = $this->buildSourceMock();
$source->connector->method('search')->willReturn(
new Entry('test', ['test' => ['test']])
new Entry('test', ['test1' => ['testval1']])
);

// This forces the login flow through the ECP processing
Expand All @@ -73,18 +73,30 @@ public function testLogin(): void
$_SERVER['PHP_AUTH_PW'] = 'test';
$source->authenticate($ary);

$this->assertEquals(['test' => ['test']], $ary['Attributes']);
$this->assertEquals(['test1' => ['testval1']], $ary['Attributes']);
}


public function testGetAttributes(): void
{
$source = $this->buildSourceMock();
$source->connector->method('search')->willReturn(
new Entry('test', ['test' => ['test']])
new Entry('test', ['test2' => ['testval2']])
);

$result = $source->getAttributes('test');
$this->assertEquals(['test' => ['test']], $result);
$this->assertEquals(['test2' => ['testval2']], $result);
}


public function testGetAttributesWithAttributesSetToNull(): void
{
$source = $this->buildSourceMock();
$source->connector->method('search')->willReturn(
new Entry('test', ['test1' => ['testval1'], 'test2' => ['testval2'], 'test3' => ['testval3']])
);

$result = $source->getAttributes('test');
$this->assertEquals(['test1' => ['testval1'], 'test2' => ['testval2'], 'test3' => ['testval3']], $result);
}
}

0 comments on commit 836da78

Please sign in to comment.