[Ldap] Ldap Entry case-sensitive attribute key option
This commit is contained in:
parent
2fa929133b
commit
d3b944046a
@ -1,6 +1,11 @@
|
|||||||
CHANGELOG
|
CHANGELOG
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
5.3.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* Added caseSensitive option for attribute keys in the Entry class.
|
||||||
|
|
||||||
5.1.0
|
5.1.0
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
@ -13,16 +13,23 @@ namespace Symfony\Component\Ldap;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Charles Sarrazin <charles@sarraz.in>
|
* @author Charles Sarrazin <charles@sarraz.in>
|
||||||
|
* @author Karl Shea <karl@karlshea.com>
|
||||||
*/
|
*/
|
||||||
class Entry
|
class Entry
|
||||||
{
|
{
|
||||||
private $dn;
|
private $dn;
|
||||||
private $attributes;
|
private $attributes;
|
||||||
|
private $lowerMap;
|
||||||
|
|
||||||
public function __construct(string $dn, array $attributes = [])
|
public function __construct(string $dn, array $attributes = [])
|
||||||
{
|
{
|
||||||
$this->dn = $dn;
|
$this->dn = $dn;
|
||||||
$this->attributes = $attributes;
|
$this->attributes = [];
|
||||||
|
$this->lowerMap = [];
|
||||||
|
|
||||||
|
foreach ($attributes as $key => $attribute) {
|
||||||
|
$this->setAttribute($key, $attribute);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -39,12 +46,20 @@ class Entry
|
|||||||
* Returns whether an attribute exists.
|
* 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
|
* @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]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -54,12 +69,20 @@ class Entry
|
|||||||
* this value is returned as an array.
|
* 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
|
* @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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -78,6 +101,7 @@ class Entry
|
|||||||
public function setAttribute(string $name, array $value)
|
public function setAttribute(string $name, array $value)
|
||||||
{
|
{
|
||||||
$this->attributes[$name] = $value;
|
$this->attributes[$name] = $value;
|
||||||
|
$this->lowerMap[strtolower($name)] = $name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -86,5 +110,21 @@ class Entry
|
|||||||
public function removeAttribute(string $name)
|
public function removeAttribute(string $name)
|
||||||
{
|
{
|
||||||
unset($this->attributes[$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
src/Symfony/Component/Ldap/Tests/EntryTest.php
Normal file
42
src/Symfony/Component/Ldap/Tests/EntryTest.php
Normal file
@ -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));
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user