Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit 5adb684

Browse files
committed
Merge branch 'feature/73' into develop
Closes #73 Closes #68 Closes #67
2 parents 62b8764 + 8cc049f commit 5adb684

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file, in reverse
88

99
- [#64](https://github.com/zendframework/zend-ldap/pull/64) Adds support for SASL-Bind - Thanks to @mbaynton
1010
- [#66](https://github.com/zendframework/zend-ldap/pull/66) Adds support for automatic reconnection - Thanks to @mbaynton
11+
- [#73](https://github.com/zendframework/zend-ldap/pull/73) Adds support for modifying attributes - Thanks to @glimac and @mbaynton
1112

1213
### Deprecated
1314

src/Ldap.php

+39
Original file line numberDiff line numberDiff line change
@@ -1538,6 +1538,45 @@ public function addAttributes($dn, array $attributes, $allowEmptyAttributes = fa
15381538
return $this;
15391539
}
15401540

1541+
/**
1542+
* Update one or more attributes to the specified dn
1543+
*
1544+
* @param string|Dn $dn
1545+
* @param array $attributes
1546+
* @param bool $allowEmptyAttributes
1547+
* @return Ldap Provides a fluid interface
1548+
* @throws LdapException
1549+
*/
1550+
public function updateAttributes($dn, array $attributes, $allowEmptyAttributes = false)
1551+
{
1552+
// Safety-flap: Check whether there are empty arrays that would cause
1553+
// complete removal of entries without the emptyAll flag.
1554+
if ($allowEmptyAttributes !== true) {
1555+
foreach ($attributes as $key => $value) {
1556+
if (empty($value)) {
1557+
unset($attributes[$key]);
1558+
}
1559+
}
1560+
}
1561+
1562+
if ($dn instanceof Dn) {
1563+
$dn = $dn->toString();
1564+
}
1565+
1566+
do {
1567+
ErrorHandler::start(E_WARNING);
1568+
$entryUpdated = ldap_mod_replace($this->resource, $dn, $attributes);
1569+
ErrorHandler::stop();
1570+
} while ($entryUpdated === false && $this->shouldReconnect($this->resource));
1571+
1572+
1573+
if ($entryUpdated === false) {
1574+
throw new Exception\LdapException($this, 'updating attribute: ' . $dn);
1575+
}
1576+
1577+
return $this;
1578+
}
1579+
15411580
/**
15421581
* Delete single attributes from a LDAP-Node
15431582
*

test/OfflineTest.php

+36
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,40 @@ public function testAddingAttributesFails()
225225
$ldap = new \Zend\Ldap\Ldap();
226226
$ldap->addAttributes('foo', ['bar']);
227227
}
228+
229+
/**
230+
* @dataProvider removingAttributesProvider
231+
*/
232+
public function testUpdatingAttributes(
233+
$dn,
234+
$attributes,
235+
$allowEmptyAttributes,
236+
$expectedDn,
237+
$expectedAttributesToRemove
238+
) {
239+
$ldap_mod_upd = $this->getFunctionMock('Zend\\Ldap', "ldap_mod_replace");
240+
$ldap_mod_upd->expects($this->once())
241+
->with(
242+
$this->isNull(),
243+
$this->equalTo($expectedDn),
244+
$this->equalTo($expectedAttributesToRemove)
245+
)
246+
->willReturn(true);
247+
248+
$ldap = new \Zend\Ldap\Ldap();
249+
$this->assertSame($ldap, $ldap->updateAttributes($dn, $attributes, $allowEmptyAttributes));
250+
}
251+
252+
/**
253+
* @expectedException \Zend\Ldap\Exception\LdapException
254+
*/
255+
public function testUpdatingAttributesFails()
256+
{
257+
$ldap_mod_upd = $this->getFunctionMock('Zend\\Ldap', 'ldap_mod_replace');
258+
$ldap_mod_upd->expects($this->once())
259+
->willReturn(false);
260+
261+
$ldap = new \Zend\Ldap\Ldap();
262+
$ldap->updateAttributes('foo', ['bar']);
263+
}
228264
}

0 commit comments

Comments
 (0)