Allow adding and removing values to/from multi-valued attributes
This commit is contained in:
parent
ee51f74a8c
commit
fa9db29064
@ -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}
|
||||
*/
|
||||
|
@ -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
|
||||
-----
|
||||
|
||||
|
@ -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'));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user