Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2a4d024

Browse files
committed
feature #21856 [LDAP] Allow adding and removing values to/from multi-valued attributes (jean-gui)
This PR was merged into the 4.1-dev branch. Discussion ---------- [LDAP] Allow adding and removing values to/from multi-valued attributes | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT `EntryManagerInterface::update(Entry $entry)` is extremely slow in some specific cases such as adding or removing members to or from huge groupOfNames if you also enable the memberOf overlay in OpenLDAP. Disabling memberOf does make things a lot better, but it is still slow compared to inserting/removing only the elements you want. This PR adds two methods to Symfony\Component\Ldap\Adapter\ExtLdap\EntryManager taking advantage of ldap_mod_add and ldap_mod_del. I thought about using them directly in the update method, but since you need to know what values to add and remove, it would be necessary to retrieve the old values from LDAP. I'm also unsure whether these two methods should be in an interface. I think that adding them to EntryManagerInterface would break BC, but I could create another interface, similarly to RenameEntryInterface. Commits ------- fa9db29 Allow adding and removing values to/from multi-valued attributes
2 parents fe6aa64 + fa9db29 commit 2a4d024

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

src/Symfony/Component/Ldap/Adapter/ExtLdap/EntryManager.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,36 @@ public function remove(Entry $entry)
6767
}
6868
}
6969

70+
/**
71+
* Adds values to an entry's multi-valued attribute from the LDAP server.
72+
*
73+
* @throws NotBoundException
74+
* @throws LdapException
75+
*/
76+
public function addAttributeValues(Entry $entry, string $attribute, array $values)
77+
{
78+
$con = $this->getConnectionResource();
79+
80+
if (!@ldap_mod_add($con, $entry->getDn(), array($attribute => $values))) {
81+
throw new LdapException(sprintf('Could not add values to entry "%s", attribute %s: %s.', $entry->getDn(), $attribute, ldap_error($con)));
82+
}
83+
}
84+
85+
/**
86+
* Removes values from an entry's multi-valued attribute from the LDAP server.
87+
*
88+
* @throws NotBoundException
89+
* @throws LdapException
90+
*/
91+
public function removeAttributeValues(Entry $entry, string $attribute, array $values)
92+
{
93+
$con = $this->getConnectionResource();
94+
95+
if (!@ldap_mod_del($con, $entry->getDn(), array($attribute => $values))) {
96+
throw new LdapException(sprintf('Could not remove values from entry "%s", attribute %s: %s.', $entry->getDn(), $attribute, ldap_error($con)));
97+
}
98+
}
99+
70100
/**
71101
* {@inheritdoc}
72102
*/

src/Symfony/Component/Ldap/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
4.1.0
5+
-----
6+
7+
* Added support for adding values to multi-valued attributes
8+
* Added support for removing values from multi-valued attributes
9+
410
4.0.0
511
-----
612

src/Symfony/Component/Ldap/Tests/Adapter/ExtLdap/LdapManagerTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,50 @@ public function testLdapRenameWithoutRemovingOldRdn()
192192

193193
$this->executeSearchQuery(1);
194194
}
195+
196+
public function testLdapAddRemoveAttributeValues()
197+
{
198+
$entryManager = $this->adapter->getEntryManager();
199+
200+
$result = $this->executeSearchQuery(1);
201+
$entry = $result[0];
202+
203+
$entryManager->addAttributeValues($entry, 'mail', array('[email protected]', '[email protected]'));
204+
205+
$result = $this->executeSearchQuery(1);
206+
$newEntry = $result[0];
207+
208+
$this->assertCount(4, $newEntry->getAttribute('mail'));
209+
210+
$entryManager->removeAttributeValues($newEntry, 'mail', array('[email protected]', '[email protected]'));
211+
212+
$result = $this->executeSearchQuery(1);
213+
$newNewEntry = $result[0];
214+
215+
$this->assertCount(2, $newNewEntry->getAttribute('mail'));
216+
}
217+
218+
public function testLdapRemoveAttributeValuesError()
219+
{
220+
$entryManager = $this->adapter->getEntryManager();
221+
222+
$result = $this->executeSearchQuery(1);
223+
$entry = $result[0];
224+
225+
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(LdapException::class);
226+
227+
$entryManager->removeAttributeValues($entry, 'mail', array('[email protected]'));
228+
}
229+
230+
public function testLdapAddAttributeValuesError()
231+
{
232+
$entryManager = $this->adapter->getEntryManager();
233+
234+
$result = $this->executeSearchQuery(1);
235+
$entry = $result[0];
236+
237+
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(LdapException::class);
238+
239+
$entryManager->addAttributeValues($entry, 'mail', $entry->getAttribute('mail'));
240+
}
195241
}

0 commit comments

Comments
 (0)