8000 Allow adding and removing values to/from multi-valued attributes · symfony/symfony@c530782 · GitHub
[go: up one dir, main page]

Skip to content
Sign in

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c530782

Browse files
committed
Allow adding and removing values to/from multi-valued attributes
1 parent 119087a commit c530782

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,47 @@ public function remove(Entry $entry)
6868
}
6969
}
7070

71+
/**
72+
* Adds values to an entry's multi-valued attribute from the Ldap server.
73+
*
74+
* @param Entry $entry
75+
* @param $attribute
76+
* @param $values
77+
*
78+
* @throws NotBoundException
79+
* @throws LdapException
80+
*/
81+
public function addValues(Entry $entry, $attribute, $values)
82+
{
83+
$con = $this->getConnectionResource();
84+
85+
if (!@ldap_mod_add($con, $entry->getDn(), array($attribute => $values))) {
86+
throw new LdapException(sprintf('Could not add values to entry "%s", attribute %s: %s', $entry->getDn(),
87+
$attribute, ldap_error($con)));
88+
}
89+
}
90+
91+
/**
92+
* Removes values from an entry's multi-valued attribute from the Ldap server.
93+
*
94+
* @param Entry $entry
95+
* @param $attribute
96+
* @param $values
97+
*
98+
* @throws NotBoundException
99+
* @throws LdapException
100+
*/
101+
public function removeValues(Entry $entry, $attribute, $values)
102+
{
103+
$con = $this->getConnectionResource();
104+
105+
if (!@ldap_mod_del($con, $entry->getDn(), array($attribute => $values))) {
106+
throw new LdapException(sprintf('Could not remove values from entry "%s", attribute %s: %s',
107+
$entry->getDn(),
108+
$attribute, ldap_error($con)));
109+
}
110+
}
111+
71112
/**
72113
* {@inheritdoc}
73114
*/

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

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

193193
$this->executeSearchQuery(1);
194194
}
195+
196+
public function testLdapAddRemoveValues()
197+
{
198+
$entryManager = $this->adapter->getEntryManager();
199+
200+
$result = $this->executeSearchQuery(1);
201+
$entry = $result[0];
202+
203+
$entryManager->addValues($entry, 'mail', array('fabpot@example.org', 'fabpot2@example.org'));
204+
205+
$result = $this->executeSearchQuery(1);
206+
$newEntry = $result[0];
207+
208+
$this->assertCount(4, $newEntry->getAttribute('mail'));
209+
210+
$entryManager->removeValues($newEntry, 'mail', array('fabpot@example.org', 'fabpot2@example.org'));
211+
212+
$result = $this->executeSearchQuery(1);
213+
$newNewEntry = $result[0];
214+
215+
$this->assertCount(2, $newNewEntry->getAttribute('mail'));
216+
}
217+
218+
public function testLdapRemoveValuesError()
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->removeValues($entry, 'mail', array('fabpot@example.org'));
228+
}
229+
230+
public function testLdapAddValuesError()
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->addValues($entry, 'mail', $entry->getAttribute('mail'));
240+
}
241+
195242
}

0 commit comments

Comments
 (0)
0