Skip to content

[Ldap] Ldap Entry case-sensitive attribute key option #39037

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

Merged
merged 1 commit into from
Nov 27, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions src/Symfony/Component/Ldap/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.3.0
-----

* Added caseSensitive option for attribute keys in the Entry class.

5.1.0
-----

Expand Down
54 changes: 47 additions & 7 deletions src/Symfony/Component/Ldap/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,23 @@

/**
* @author Charles Sarrazin <charles@sarraz.in>
* @author Karl Shea <karl@karlshea.com>
*/
class Entry
{
private $dn;
private $attributes;
private $lowerMap;

public function __construct(string $dn, array $attributes = [])
{
$this->dn = $dn;
$this->attributes = $attributes;
$this->attributes = [];
$this->lowerMap = [];

foreach ($attributes as $key => $attribute) {
$this->setAttribute($key, $attribute);
}
}

/**
Expand All @@ -38,13 +45,21 @@ public function getDn()
/**
* Returns whether an attribute exists.
*
* @param string $name The name of the attribute
* @param string $name The name of the attribute
* @param bool $caseSensitive Whether the check should be case-sensitive
*
* @return bool
*/
public function hasAttribute(string $name)
public function hasAttribute(string $name/* , bool $caseSensitive = true */)
{
return isset($this->attributes[$name]);
$caseSensitive = 2 > \func_num_args() || true === func_get_arg(1);
$attributeKey = $this->getAttributeKey($name, $caseSensitive);

if (null === $attributeKey) {
return false;
}

return isset($this->attributes[$attributeKey]);
}

/**
Expand All @@ -53,13 +68,21 @@ public function hasAttribute(string $name)
* As LDAP can return multiple values for a single attribute,
* this value is returned as an array.
*
* @param string $name The name of the attribute
* @param string $name The name of the attribute
* @param bool $caseSensitive Whether the attribute name is case-sensitive
*
* @return array|null
*/
public function getAttribute(string $name)
public function getAttribute(string $name/* , bool $caseSensitive = true */)
{
return isset($this->attributes[$name]) ? $this->attributes[$name] : null;
$caseSensitive = 2 > \func_num_args() || true === func_get_arg(1);
$attributeKey = $this->getAttributeKey($name, $caseSensitive);

if (null === $attributeKey) {
return null;
}

return $this->attributes[$attributeKey] ?? null;
}

/**
Expand All @@ -78,6 +101,7 @@ public function getAttributes()
public function setAttribute(string $name, array $value)
{
$this->attributes[$name] = $value;
$this->lowerMap[strtolower($name)] = $name;
}

/**
Expand All @@ -86,5 +110,21 @@ public function setAttribute(string $name, array $value)
public function removeAttribute(string $name)
{
unset($this->attributes[$name]);
unset($this->lowerMap[strtolower($name)]);
}

/**
* Get the attribute key.
*
* @param string $name The attribute name
* @param bool $caseSensitive Whether the attribute name is case-sensitive
*/
private function getAttributeKey(string $name, bool $caseSensitive = true): ?string
{
if ($caseSensitive) {
return $name;
}

return $this->lowerMap[strtolower($name)] ?? null;
}
}
42 changes: 42 additions & 0 deletions src/Symfony/Component/Ldap/Tests/EntryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Ldap\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Ldap\Entry;

class EntryTest extends TestCase
{
public function testCaseSensitiveAttributeAccessors()
{
$mail = 'fabpot@symfony.com';
$givenName = 'Fabien Potencier';

$entry = new Entry('cn=fabpot,dc=symfony,dc=com', [
'mail' => [$mail],
'givenName' => [$givenName],
]);

$this->assertFalse($entry->hasAttribute('givenname'));
$this->assertTrue($entry->hasAttribute('givenname', false));

$this->assertNull($entry->getAttribute('givenname'));
$this->assertSame($givenName, $entry->getAttribute('givenname', false)[0]);

$firstName = 'Fabien';

$entry->setAttribute('firstName', [$firstName]);
$this->assertSame($firstName, $entry->getAttribute('firstname', false)[0]);
$entry->removeAttribute('firstName');
$this->assertFalse($entry->hasAttribute('firstname', false));
}
}