Allow adding and removing values to/from multi-valued attributes

This commit is contained in:
jean-gui 2017-03-03 17:30:27 +01:00 committed by Jean-Guilhem Rouel
parent ee51f74a8c
commit fa9db29064
3 changed files with 82 additions and 0 deletions

View File

@ -67,6 +67,36 @@ class EntryManager implements EntryManagerInterface
}
}
/**
* Adds values to an entry's multi-valued attribute from the LDAP server.
*
* @throws NotBoundException
* @throws LdapException
*/
public function addAttributeValues(Entry $entry, string $attribute, array $values)
{
$con = $this->getConnectionResource();
if (!@ldap_mod_add($con, $entry->getDn(), array($attribute => $values))) {
throw new LdapException(sprintf('Could not add values to entry "%s", attribute %s: %s.', $entry->getDn(), $attribute, ldap_error($con)));
}
}
/**
* Removes values from an entry's multi-valued attribute from the LDAP server.
*
* @throws NotBoundException
* @throws LdapException
*/
public function removeAttributeValues(Entry $entry, string $attribute, array $values)
{
$con = $this->getConnectionResource();
if (!@ldap_mod_del($con, $entry->getDn(), array($attribute => $values))) {
throw new LdapException(sprintf('Could not remove values from entry "%s", attribute %s: %s.', $entry->getDn(), $attribute, ldap_error($con)));
}
}
/**
* {@inheritdoc}
*/

View File

@ -1,6 +1,12 @@
CHANGELOG
=========
4.1.0
-----
* Added support for adding values to multi-valued attributes
* Added support for removing values from multi-valued attributes
4.0.0
-----

View File

@ -192,4 +192,50 @@ class LdapManagerTest extends LdapTestCase
$this->executeSearchQuery(1);
}
public function testLdapAddRemoveAttributeValues()
{
$entryManager = $this->adapter->getEntryManager();
$result = $this->executeSearchQuery(1);
$entry = $result[0];
$entryManager->addAttributeValues($entry, 'mail', array('fabpot@example.org', 'fabpot2@example.org'));
$result = $this->executeSearchQuery(1);
$newEntry = $result[0];
$this->assertCount(4, $newEntry->getAttribute('mail'));
$entryManager->removeAttributeValues($newEntry, 'mail', array('fabpot@example.org', 'fabpot2@example.org'));
$result = $this->executeSearchQuery(1);
$newNewEntry = $result[0];
$this->assertCount(2, $newNewEntry->getAttribute('mail'));
}
public function testLdapRemoveAttributeValuesError()
{
$entryManager = $this->adapter->getEntryManager();
$result = $this->executeSearchQuery(1);
$entry = $result[0];
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(LdapException::class);
$entryManager->removeAttributeValues($entry, 'mail', array('fabpot@example.org'));
}
public function testLdapAddAttributeValuesError()
{
$entryManager = $this->adapter->getEntryManager();
$result = $this->executeSearchQuery(1);
$entry = $result[0];
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(LdapException::class);
$entryManager->addAttributeValues($entry, 'mail', $entry->getAttribute('mail'));
}
}