8000 feature #29448 [Ldap] Entry move support (kevans91) · symfony/symfony@22234e3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 22234e3

Browse files
committed
feature #29448 [Ldap] Entry move support (kevans91)
This PR was squashed before being merged into the 4.3-dev branch (closes #29448). Discussion ---------- [Ldap] Entry move support | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | no (see [1]) | Fixed tickets | N/A | License | MIT | Doc PR | N/A (see [2]) Add Move support to the ldap EntryManagerInterface and the ExtLdap adapter. This is used to re-parent an entry to another part of the directory. The interface to do so need not be complicated, requiring only the entry to be moved and the parent DN to be moved to. Underlying implementations may require a 'newrdn' attribute -- this is generally the RDN w.r.t. the immediate parent of the entry, which is easily parsed. I've attempted to implement it as the rename functionality was originally implemented: adding an interface to be deprecated effective immediately, presumably to allow it to be backported without breaking existing interfaces, and then implementing this interface in the ExtLdap adapter. [1] I do not have the capacity to run the ldap tests for this locally due to current $work situation; I have no reason to believe this test will fail as written, though. This functionality has been used as currently implemented (against Windows ADS) for some time in my production environment, so it has been functionally tested otherwise. [2] No doc PR has been created for this feature addition, since it's a minor addition. The LDAP documentation should likely be amended to include rename functionality as well as this. Commits ------- 32743c8 [Ldap] Entry move support
2 parents c9b8ca6 + 32743c8 commit 22234e3

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

src/Symfony/Component/Ldap/Adapter/EntryManagerInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* @author Charles Sarrazin <charles@sarraz.in>
2222
* @author Bob van de Vijver <bobvandevijver@hotmail.com>
2323
* @author Kevin Schuurmans <kevin.schuurmans@freshheads.com>
24+
*
25+
* @method void move(Entry $entry, string $newParent) Moves an entry on the Ldap server
2426
*/
2527
interface EntryManagerInterface
2628
{

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,22 @@ public function rename(Entry $entry, $newRdn, $removeOldRdn = true)
110110
}
111111
}
112112

113+
/**
114+
* Moves an entry on the Ldap server.
115+
*
116+
* @throws NotBoundException if the connection has not been previously bound.
117+
* @throws LdapException if an error is thrown during the rename operation.
118+
*/
119+
public function move(Entry $entry, string $newParent)
120+
{
121+
$con = $this->getConnectionResource();
122+
$rdn = $this->parseRdnFromEntry($entry);
123+
// deleteOldRdn does not matter here, since the Rdn will not be changing in the move.
124+
if (!@ldap_rename($con, $entry->getDn(), $rdn, $newParent, true)) {
125+
throw new LdapException(sprintf('Could not move entry "%s" to "%s": %s.', $entry->getDn(), $newParent, ldap_error($con)));
126+
}
127+
}
128+
113129
/**
114130
* Get the connection resource, but first check if the connection is bound.
115131
*/
@@ -139,4 +155,13 @@ public function applyOperations(string $dn, iterable $operations): void
139155
throw new UpdateOperationException(sprintf('Error executing UpdateOperation on "%s": "%s".', $dn, ldap_error($this->getConnectionResource())));
140156
}
141157
}
158+
159+
private function parseRdnFromEntry(Entry $entry)
160+
{
161+
if (!preg_match('/^([^,]+),/', $entry->getDn(), $matches)) {
162+
throw new LdapException(sprintf('Entry "%s" malformed, could not parse RDN', $entry->getDn()));
163+
}
164+
165+
return $matches[1];
166+
}
142167
}

src/Symfony/Component/Ldap/CHANGELOG.md

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

4+
4.3.0
5+
-----
6+
7+
* added `EntryManager::move`, not implementing it is deprecated
8+
49
4.2.0
510
-----
611

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,4 +341,22 @@ public function testUpdateOperationsThrowsExceptionWhenAddedDuplicatedValue()
341341

342342
$entryManager->applyOperations($entry->getDn(), $duplicateIterator);
343343
}
344+
345+
/**
346+
* @group functional
347+
*/
348+
public function testLdapMove()
349+
{
350+
$result = $this->executeSearchQuery(1);
351+
352+
$entry = $result[0];
353+
$this->assertNotContains('ou=Ldap', $entry->getDn());
354+
355+
$entryManager = $this->adapter->getEntryManager();
356+
$entryManager->move($entry, 'ou=Ldap,ou=Components,dc=symfony,dc=com');
357+
358+
$result = $this->executeSearchQuery(1);
359+
$movedEntry = $result[0];
360+
$this->assertContains('ou=Ldap', $movedEntry->getDn());
361+
}
344362
}

0 commit comments

Comments
 (0)
0