Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Ldap] Entry move support #29448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Ldap] Entry move support
  • Loading branch information
Kyle Evans authored and fabpot committed Mar 31, 2019
commit 32743c850f901b77b81232b9239b2128d5c6f041
2 changes: 2 additions & 0 deletions src/Symfony/Component/Ldap/Adapter/EntryManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* @author Charles Sarrazin <[email protected]>
* @author Bob van de Vijver <[email protected]>
* @author Kevin Schuurmans <[email protected]>
*
* @method void move(Entry $entry, string $newParent) Moves an entry on the Ldap server
*/
interface EntryManagerInterface
{
Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Component/Ldap/Adapter/ExtLdap/EntryManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,22 @@ public function rename(Entry $entry, $newRdn, $removeOldRdn = true)
}
}

/**
* Moves an entry on the Ldap server.
*
* @throws NotBoundException if the connection has not been previously bound.
* @throws LdapException if an error is thrown during the rename operation.
*/
public function move(Entry $entry, string $newParent)
{
$con = $this->getConnectionResource();
$rdn = $this->parseRdnFromEntry($entry);
// deleteOldRdn does not matter here, since the Rdn will not be changing in the move.
if (!@ldap_rename($con, $entry->getDn(), $rdn, $newParent, true)) {
throw new LdapException(sprintf('Could not move entry "%s" to "%s": %s.', $entry->getDn(), $newParent, ldap_error($con)));
}
}

/**
* Get the connection resource, but first check if the connection is bound.
*/
Expand Down Expand Up @@ -139,4 +155,13 @@ public function applyOperations(string $dn, iterable $operations): void
throw new UpdateOperationException(sprintf('Error executing UpdateOperation on "%s": "%s".', $dn, ldap_error($this->getConnectionResource())));
}
}

private function parseRdnFromEntry(Entry $entry)
{
if (!preg_match('/^([^,]+),/', $entry->getDn(), $matches)) {
throw new LdapException(sprintf('Entry "%s" malformed, could not parse RDN', $entry->getDn()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing dot at the end of the exception message.

}

return $matches[1];
}
}
5 changes: 5 additions & 0 deletions src/Symfony/Component/Ldap/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.3.0
-----

* added `EntryManager::move`, not implementing it is deprecated

4.2.0
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,4 +341,22 @@ public function testUpdateOperationsThrowsExceptionWhenAddedDuplicatedValue()

$entryManager->applyOperations($entry->getDn(), $duplicateIterator);
}

/**
* @group functional
*/
public function testLdapMove()
{
$result = $this->executeSearchQuery(1);

$entry = $result[0];
$this->assertNotContains('ou=Ldap', $entry->getDn());

$entryManager = $this->adapter->getEntryManager();
$entryManager->move($entry, 'ou=Ldap,ou=Components,dc=symfony,dc=com');

$result = $this->executeSearchQuery(1);
$movedEntry = $result[0];
$this->assertContains('ou=Ldap', $movedEntry->getDn());
}
}