Skip to content

Core\Ldap\Connection::rename

$connection->rename(
    Core\Ldap\Dn|string $from,
    Core\Ldap\Dn|string $to,
): void

Gives an entry in the directory a new DN. It returns nothing.

The new DN can have the same parent as the old one. Then the entry only gets a new name, such as a unit called Sales Team that becomes Sales. The new DN can also have another parent. Then the entry moves there, such as a person who changes to another team.

The entry keeps everything else. Its attributes and its GUID stay the same, and so does the login name of a user account. Entries below a unit move with it.

The connection must log in as an account that may change the entry. When another entry already has the new DN, rename throws Core\Ldap\LdapError with $kind set to AlreadyExists, and nothing changes.

Good to know: a program that saved the old DN cannot find the entry with it any more. Save the GUID instead, because it never changes.

Parameters

  • $fromCore\Ldap\Dn|stringThe entry's DN now, as a Dn or as text. Text cannot be tainted.
  • $toCore\Ldap\Dn|stringThe entry's new DN, such as CN=Shop,OU=Groups,DC=example,DC=test. Text cannot be tainted.

Return value

Returns void. Nothing.

Errors

  • LogicErrorfrom or to is text that is not a DN, or the connection is closed.
  • Core\Ldap\LdapErrorThe server returned an error, such as NoSuchObject or AlreadyExists.

Examples

The examples below run with this configuration file.

nvs.toml
# These examples use the LDAP directory server on this computer at port 16636.
# They log in as the administrator, because they change accounts in the directory.
# `tls_ca_file` is the certificate that the server's certificate is checked against.
[ldap.corp-accounts]
url = "ldaps://127.0.0.1:16636"
base = "DC=example,DC=test"
user = "CN=Administrator,CN=Users,DC=example,DC=test"
password = "Novis-test-1"
tls_ca_file = "../../../../../tests/db/ca.crt"
timeout = "5s"
# The program may open the `corp-accounts` directory.
[capabilities.ldap]
connect = ["corp-accounts"]

Give a unit a new name

<?nvs
// Shows how to give an organizational unit a new name.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp-accounts');
string $old = 'OU=Sales Team 1,DC=example,DC=test';
string $new = 'OU=Sales 1,DC=example,DC=test';
// A run that stopped early can leave the unit behind, so the program deletes it first.
array<string> $leftovers = [$old, $new];
foreach ($leftovers as string $left) {
try {
$corp->delete($left);
} catch (Core\Ldap\LdapError $missing) {
}
}
$corp->add($old, ['objectClass' => 'organizationalUnit']);
// The new DN has the same parent, so the unit only gets a new name.
$corp->rename($old, $new);
// This prints `old name: not found` and `new name: Sales 1`.
echo 'old name: ', $corp->read($old) == null ? 'not found' : 'found', "\n";
echo 'new name: ', $corp->read($new, { select: ['ou'] })?->string('ou') ?? 'not found', "\n";
$corp->delete($new);
Output
old name: not found
new name: Sales 1

Move a person to another team

<?nvs
// Shows how to move a user account to another unit when a person changes teams.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp-accounts');
string $sales = 'OU=Sales 2,DC=example,DC=test';
string $support = 'OU=Support 2,DC=example,DC=test';
// A run that stopped early can leave the accounts behind, so the program deletes them first.
array<string> $leftovers = ['CN=Nora Haas,' . $sales, 'CN=Nora Haas,' . $support, $sales, $support];
foreach ($leftovers as string $left) {
try {
$corp->delete($left);
} catch (Core\Ldap\LdapError $missing) {
}
}
$corp->add($sales, ['objectClass' => 'organizationalUnit']);
$corp->add($support, ['objectClass' => 'organizationalUnit']);
$corp->add('CN=Nora Haas,' . $sales, ['objectClass' => 'user', 'sAMAccountName' => 'nora.haas']);
// The new DN has another parent, so the account moves. Its login name stays the same.
$corp->rename('CN=Nora Haas,' . $sales, 'CN=Nora Haas,' . $support);
// This prints the new DN and `nora.haas`.
var $moved = $corp->read('CN=Nora Haas,' . $support, { select: ['sAMAccountName'] });
if ($moved != null) {
echo $moved->dn()->toString(), "\n";
echo $moved->string('sAMAccountName') ?? '', "\n";
}
$corp->delete('CN=Nora Haas,' . $support);
$corp->delete($sales);
$corp->delete($support);
Output
CN=Nora Haas,OU=Support 2,DC=example,DC=test
nora.haas

A name that is already taken

<?nvs
// Shows what happens when the new DN is already used by another entry.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp-accounts');
string $shop = 'OU=Shop 3,DC=example,DC=test';
string $blog = 'OU=Blog 3,DC=example,DC=test';
// A run that stopped early can leave the units behind, so the program deletes them first.
array<string> $leftovers = [$shop, $blog];
foreach ($leftovers as string $left) {
try {
$corp->delete($left);
} catch (Core\Ldap\LdapError $missing) {
}
}
$corp->add($shop, ['objectClass' => 'organizationalUnit']);
$corp->add($blog, ['objectClass' => 'organizationalUnit']);
// `rename` throws an error and changes nothing. This prints `the name is taken`.
try {
$corp->rename($shop, $blog);
} catch (Core\Ldap\LdapError $error) {
if ($error->kind == Core\Ldap\LdapError::Kind::AlreadyExists) {
echo 'the name is taken', "\n";
}
}
// Both units are still there. This prints `Shop 3` and `Blog 3`.
echo $corp->read($shop, { select: ['ou'] })?->string('ou') ?? 'missing', "\n";
echo $corp->read($blog, { select: ['ou'] })?->string('ou') ?? 'missing', "\n";
$corp->delete($shop);
$corp->delete($blog);
Output
the name is taken
Shop 3
Blog 3