diff --git a/src/Symfony/Component/Ldap/Entry.php b/src/Symfony/Component/Ldap/Entry.php index da3b8bb74ef41..1a156e5f2280c 100644 --- a/src/Symfony/Component/Ldap/Entry.php +++ b/src/Symfony/Component/Ldap/Entry.php @@ -13,16 +13,23 @@ /** * @author Charles Sarrazin + * @author Karl Shea */ 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); + } } /** @@ -38,13 +45,20 @@ 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, $caseSensitive = true) { - return isset($this->attributes[$name]); + $attributeKey = $this->getAttributeKey($name, $caseSensitive); + + if (null === $attributeKey) { + return false; + } + + return isset($this->attributes[$attributeKey]); } /** @@ -53,13 +67,20 @@ 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, $caseSensitive = true) { - return isset($this->attributes[$name]) ? $this->attributes[$name] : null; + $attributeKey = $this->getAttributeKey($name, $caseSensitive); + + if (null === $attributeKey) { + return null; + } + + return $this->attributes[$attributeKey] ?? null; } /** @@ -78,6 +99,7 @@ public function getAttributes() public function setAttribute(string $name, array $value) { $this->attributes[$name] = $value; + $this->lowerMap[strtolower($name)] = $name; } /** @@ -86,5 +108,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; } } diff --git a/src/Symfony/Component/Ldap/Tests/EntryTest.php b/src/Symfony/Component/Ldap/Tests/EntryTest.php new file mode 100644 index 0000000000000..7185db1040281 --- /dev/null +++ b/src/Symfony/Component/Ldap/Tests/EntryTest.php @@ -0,0 +1,42 @@ + + * + * 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)); + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy