[Ldap] Entry move support

This commit is contained in:
Kyle Evans 2018-12-03 20:31:09 -06:00 committed by Fabien Potencier
parent bff9e68bb4
commit 32743c850f
4 changed files with 50 additions and 0 deletions

View File

@ -21,6 +21,8 @@ use Symfony\Component\Ldap\Exception\NotBoundException;
* @author Charles Sarrazin <charles@sarraz.in>
* @author Bob van de Vijver <bobvandevijver@hotmail.com>
* @author Kevin Schuurmans <kevin.schuurmans@freshheads.com>
*
* @method void move(Entry $entry, string $newParent) Moves an entry on the Ldap server
*/
interface EntryManagerInterface
{

View File

@ -110,6 +110,22 @@ class EntryManager implements EntryManagerInterface
}
}
/**
* 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.
*/
@ -139,4 +155,13 @@ class EntryManager implements EntryManagerInterface
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()));
}
return $matches[1];
}
}

View File

@ -1,6 +1,11 @@
CHANGELOG
=========
4.3.0
-----
* added `EntryManager::move`, not implementing it is deprecated
4.2.0
-----

View File

@ -341,4 +341,22 @@ class LdapManagerTest extends LdapTestCase
$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());
}
}