Core\Ldap\Entry::isDeleted
$entry->isDeleted(): boolChecks whether an entry is a deleted entry. It returns true or false.
Active Directory does not remove a deleted account at once. It keeps the entry for some time, with most of its attributes removed. The SID and the GUID stay. A normal search does not find these entries. A search with showDeleted: true finds them too, and isDeleted() tells them apart from the active ones.
The account that searches needs the right to read deleted entries. An administrator has it. Without that right, the server does not send the isDeleted attribute, so isDeleted() returns false.
Good to know: a deleted entry has a new DN under CN=Deleted Objects. To find a deleted account again, search by a value that stays, such as its SID.
Return value
Returns bool. true when the entry's isDeleted attribute is TRUE, and false otherwise.
Examples
The examples below run with this configuration file.
# 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"]Check whether an account was deleted
<?nvs// Shows how to check whether a user account was deleted, with the SID a program saved earlier.// requires: samba-ad
var $corp = Core\Ldap::connect('corp-accounts');string $unit = 'OU=Former Staff 1,DC=example,DC=test';string $user = 'CN=Lena Vogt,' . $unit;
// A run that stopped early can leave the account behind, so the program deletes it first.array<string> $leftovers = [$user, $unit];foreach ($leftovers as string $left) { try { $corp->delete($left); } catch (Core\Ldap\LdapError $missing) { }}$corp->add($unit, ['objectClass' => 'organizationalUnit']);$corp->add($user, ['objectClass' => 'user', 'sAMAccountName' => 'lena.vogt']);
// The SID of an account never changes, so a program can save it and use it later.var $found = $corp->read($user, { select: ['objectSid'] });string $sid = $found?->sid('objectSid')?->toString() ?? '';$corp->delete($user);
// `showDeleted: true` also finds deleted accounts. This prints `deleted: yes`.var $bySid = Core\Ldap\Filter::equals('objectSid', $sid);foreach ($corp->search($bySid, { showDeleted: true }) as Core\Ldap\Entry $account) { echo 'deleted: ', $account->isDeleted() ? 'yes' : 'no', "\n";}
$corp->delete($unit);deleted: yesA normal search skips deleted accounts
<?nvs// Shows that a normal search does not find a deleted account, and `showDeleted: true` does.// requires: samba-ad
var $corp = Core\Ldap::connect('corp-accounts');string $unit = 'OU=Former Staff 2,DC=example,DC=test';string $user = 'CN=Jonas Weber,' . $unit;
// A run that stopped early can leave the account behind, so the program deletes it first.array<string> $leftovers = [$user, $unit];foreach ($leftovers as string $left) { try { $corp->delete($left); } catch (Core\Ldap\LdapError $missing) { }}$corp->add($unit, ['objectClass' => 'organizationalUnit']);$corp->add($user, ['objectClass' => 'user', 'sAMAccountName' => 'jonas.weber']);var $found = $corp->read($user, { select: ['objectSid'] });string $sid = $found?->sid('objectSid')?->toString() ?? '';$corp->delete($user);$corp->delete($unit);
var $bySid = Core\Ldap\Filter::equals('objectSid', $sid);
// This prints `normal search: 0`.int $normal = 0;foreach ($corp->search($bySid) as Core\Ldap\Entry $account) { $normal = $normal + 1;}echo 'normal search: ', $normal, "\n";
// This prints `with showDeleted: 1, deleted: 1`.int $all = 0;int $deleted = 0;foreach ($corp->search($bySid, { showDeleted: true }) as Core\Ldap\Entry $account) { $all = $all + 1; if ($account->isDeleted()) { $deleted = $deleted + 1; }}echo 'with showDeleted: ', $all, ', deleted: ', $deleted, "\n";normal search: 0with showDeleted: 1, deleted: 1Tell active and deleted accounts apart
<?nvs// Shows how a program checks a saved list of accounts and finds the ones that were deleted.// requires: samba-ad
var $corp = Core\Ldap::connect('corp-accounts');string $unit = 'OU=Former Staff 3,DC=example,DC=test';array<string> $people = ['Mia Klein', 'Paul Brandt'];
// A run that stopped early can leave the accounts behind, so the program deletes them first.array<string> $leftovers = ['CN=Mia Klein,' . $unit, 'CN=Paul Brandt,' . $unit, $unit];foreach ($leftovers as string $left) { try { $corp->delete($left); } catch (Core\Ldap\LdapError $missing) { }}$corp->add($unit, ['objectClass' => 'organizationalUnit']);
// The program saves the SID of each account, with the person's name.array<string> $saved = [];foreach ($people as string $person) { string $user = 'CN=' . $person . ',' . $unit; string $login = Core\Str::lower(Core\Str::replace($person, ' ', '.')); $corp->add($user, ['objectClass' => 'user', 'sAMAccountName' => $login]); var $found = $corp->read($user, { select: ['objectSid'] }); $saved[$found?->sid('objectSid')?->toString() ?? ''] = $person;}
// Later, Paul Brandt's account is deleted.$corp->delete('CN=Paul Brandt,' . $unit);
array<Core\Ldap\Filter> $filters = [];foreach ($saved as string $sid => string $person) { $filters[] = Core\Ldap\Filter::equals('objectSid', $sid);}
// A search returns entries in no fixed order, so the lines are sorted.// This prints `Mia Klein: active` and `Paul Brandt: deleted`.array<string> $lines = [];foreach ($corp->search(Core\Ldap\Filter::any(...$filters), { select: ['objectSid'], showDeleted: true }) as Core\Ldap\Entry $account) { string $name = $saved[$account->sid('objectSid')?->toString() ?? ''] ?? 'unknown'; $lines[] = $name . ': ' . ($account->isDeleted() ? 'deleted' : 'active');}foreach (Core\Arr::sort($lines) as string $line) { echo $line, "\n";}
$corp->delete('CN=Mia Klein,' . $unit);$corp->delete($unit);Mia Klein: activePaul Brandt: deleted