Skip to content

Commit a4b2bf8

Browse files
committed
feature #39037 [Ldap] Ldap Entry case-sensitive attribute key option (karlshea)
This PR was merged into the 5.3-dev branch. Discussion ---------- [Ldap] Ldap Entry case-sensitive attribute key option | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | N/A | License | MIT | Doc PR | N/A See PR #36432 Commits ------- d3b9440 [Ldap] Ldap Entry case-sensitive attribute key option
2 parents 4346ef4 + d3b9440 commit a4b2bf8

File tree

3 files changed

+94
-7
lines changed

3 files changed

+94
-7
lines changed

src/Symfony/Component/Ldap/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.3.0
5+
-----
6+
7+
* Added caseSensitive option for attribute keys in the Entry class.
8+
49
5.1.0
510
-----
611

src/Symfony/Component/Ldap/Entry.php

+47-7
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,23 @@
1313

1414
/**
1515
* @author Charles Sarrazin <charles@sarraz.in>
16+
* @author Karl Shea <karl@karlshea.com>
1617
*/
1718
class Entry
1819
{
1920
private $dn;
2021
private $attributes;
22+
private $lowerMap;
2123

2224
public function __construct(string $dn, array $attributes = [])
2325
{
2426
$this->dn = $dn;
25-
$this->attributes = $attributes;
27+
$this->attributes = [];
28+
$this->lowerMap = [];
29+
30+
foreach ($attributes as $key => $attribute) {
31+
$this->setAttribute($key, $attribute);
32+
}
2633
}
2734

2835
/**
@@ -38,13 +45,21 @@ public function getDn()
3845
/**
3946
* Returns whether an attribute exists.
4047
*
41-
* @param string $name The name of the attribute
48+
* @param string $name The name of the attribute
49+
* @param bool $caseSensitive Whether the check should be case-sensitive
4250
*
4351
* @return bool
4452
*/
45-
public function hasAttribute(string $name)
53+
public function hasAttribute(string $name/* , bool $caseSensitive = true */)
4654
{
47-
return isset($this->attributes[$name]);
55+
$caseSensitive = 2 > \func_num_args() || true === func_get_arg(1);
56+
$attributeKey = $this->getAttributeKey($name, $caseSensitive);
57+
58+
if (null === $attributeKey) {
59+
return false;
60+
}
61+
62+
return isset($this->attributes[$attributeKey]);
4863
}
4964

5065
/**
@@ -53,13 +68,21 @@ public function hasAttribute(string $name)
5368
* As LDAP can return multiple values for a single attribute,
5469
* this value is returned as an array.
5570
*
56-
* @param string $name The name of the attribute
71+
* @param string $name The name of the attribute
72+
* @param bool $caseSensitive Whether the attribute name is case-sensitive
5773
*
5874
* @return array|null
5975
*/
60-
public function getAttribute(string $name)
76+
public function getAttribute(string $name/* , bool $caseSensitive = true */)
6177
{
62-
return isset($this->attributes[$name]) ? $this->attributes[$name] : null;
78+
$caseSensitive = 2 > \func_num_args() || true === func_get_arg(1);
79+
$attributeKey = $this->getAttributeKey($name, $caseSensitive);
80+
81+
if (null === $attributeKey) {
82+
return null;
83+
}
84+
85+
return $this->attributes[$attributeKey] ?? null;
6386
}
6487

6588
/**
@@ -78,6 +101,7 @@ public function getAttributes()
78101
public function setAttribute(string $name, array $value)
79102
{
80103
$this->attributes[$name] = $value;
104+
$this->lowerMap[strtolower($name)] = $name;
81105
}
82106

83107
/**
@@ -86,5 +110,21 @@ public function setAttribute(string $name, array $value)
86110
public function removeAttribute(string $name)
87111
{
88112
unset($this->attributes[$name]);
113+
unset($this->lowerMap[strtolower($name)]);
114+
}
115+
116+
/**
117+
* Get the attribute key.
118+
*
119+
* @param string $name The attribute name
120+
* @param bool $caseSensitive Whether the attribute name is case-sensitive
121+
*/
122+
private function getAttributeKey(string $name, bool $caseSensitive = true): ?string
123+
{
124+
if ($caseSensitive) {
125+
return $name;
126+
}
127+
128+
return $this->lowerMap[strtolower($name)] ?? null;
89129
}
90130
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Ldap\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Ldap\Entry;
16+
17+
class EntryTest extends TestCase
18+
{
19+
public function testCaseSensitiveAttributeAccessors()
20+
{
21+
$mail = 'fabpot@symfony.com';
22+
$givenName = 'Fabien Potencier';
23+
24+
$entry = new Entry('cn=fabpot,dc=symfony,dc=com', [
25+
'mail' => [$mail],
26+
'givenName' => [$givenName],
27+
]);
28+
29+
$this->assertFalse($entry->hasAttribute('givenname'));
30+
$this->assertTrue($entry->hasAttribute('givenname', false));
31+
32+
$this->assertNull($entry->getAttribute('givenname'));
33+
$this->assertSame($givenName, $entry->getAttribute('givenname', false)[0]);
34+
35+
$firstName = 'Fabien';
36+
37+
$entry->setAttribute('firstName', [$firstName]);
38+
$this->assertSame($firstName, $entry->getAttribute('firstname', false)[0]);
39+
$entry->removeAttribute('firstName');
40+
$this->assertFalse($entry->hasAttribute('firstname', false));
41+
}
42+
}

0 commit comments

Comments
 (0)