feature #20390 [Ldap] added Ldap entry rename for ExtLdap adapter (fruitwasp)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[Ldap] added Ldap entry rename for ExtLdap adapter

| Q             | A
| ------------- | ---
| Branch?       | "master"
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | yes
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Commits
-------

2d8eeb200f [LDAP] implemented LDAP entry rename for ExtLdap adapter
This commit is contained in:
Fabien Potencier 2017-01-17 21:50:56 -08:00
commit cf7d2f2c57
4 changed files with 86 additions and 1 deletions

View File

@ -390,3 +390,8 @@ Yaml
the `!php/object` tag.
* Duplicate mapping keys lead to a `ParseException`.
Ldap
--------------
* The `RenameEntryInterface` has been deprecated, and merged with `EntryManagerInterface`

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\Ldap\Adapter\ExtLdap;
use Symfony\Component\Ldap\Adapter\EntryManagerInterface;
use Symfony\Component\Ldap\Adapter\RenameEntryInterface;
use Symfony\Component\Ldap\Entry;
use Symfony\Component\Ldap\Exception\LdapException;
use Symfony\Component\Ldap\Exception\NotBoundException;
@ -20,7 +21,7 @@ use Symfony\Component\Ldap\Exception\NotBoundException;
* @author Charles Sarrazin <charles@sarraz.in>
* @author Bob van de Vijver <bobvandevijver@hotmail.com>
*/
class EntryManager implements EntryManagerInterface
class EntryManager implements EntryManagerInterface, RenameEntryInterface
{
private $connection;
@ -67,6 +68,18 @@ class EntryManager implements EntryManagerInterface
}
}
/**
* {@inheritdoc}
*/
public function rename(Entry $entry, $newRdn, $removeOldRdn = true)
{
$con = $this->getConnectionResource();
if (!@ldap_rename($con, $entry->getDn(), $newRdn, null, $removeOldRdn)) {
throw new LdapException(sprintf('Could not rename entry "%s" to "%s": %s', $entry->getDn(), $newRdn, ldap_error($con)));
}
}
/**
* Get the connection resource, but first check if the connection is bound.
*/

View File

@ -0,0 +1,22 @@
<?php
namespace Symfony\Component\Ldap\Adapter;
use Symfony\Component\Ldap\Entry;
/**
* @deprecated This interface will be deprecated in 4.0, and merged with `EntryManagerInterface`
*
* @author Kevin Schuurmans <kevin.schuurmans@freshheads.com>
*/
interface RenameEntryInterface
{
/**
* Renames an entry on the Ldap server.
*
* @param Entry $entry
* @param string $newRdn
* @param bool $removeOldRdn
*/
public function rename(Entry $entry, $newRdn, $removeOldRdn = true);
}

View File

@ -147,4 +147,49 @@ class LdapManagerTest extends LdapTestCase
return $results;
}
/**
* @group functional
*/
public function testLdapRename()
{
$result = $this->executeSearchQuery(1);
$entry = $result[0];
$entryManager = $this->adapter->getEntryManager();
$entryManager->rename($entry, 'cn=Kevin');
$result = $this->executeSearchQuery(1);
$renamedEntry = $result[0];
$this->assertEquals($renamedEntry->getAttribute('cn')[0], 'Kevin');
$oldRdn = $entry->getAttribute('cn')[0];
$entryManager->rename($renamedEntry, 'cn='.$oldRdn);
$this->executeSearchQuery(1);
}
/**
* @group functional
*/
public function testLdapRenameWithoutRemovingOldRdn()
{
$result = $this->executeSearchQuery(1);
$entry = $result[0];
$entryManager = $this->adapter->getEntryManager();
$entryManager->rename($entry, 'cn=Kevin', false);
$result = $this->executeSearchQuery(1);
$newEntry = $result[0];
$originalCN = $entry->getAttribute('cn')[0];
$this->assertContains($originalCN, $newEntry->getAttribute('cn'));
$entryManager->rename($newEntry, 'cn='.$originalCN);
$this->executeSearchQuery(1);
}
}