Skip to content

Core\Ldap\Entry::AccountType

Says what kind of object an Active Directory entry is.

Core\Ldap\Entry::AccountType is declared in the class Core\Ldap\Entry. The method accountType returns it.

Active Directory keeps people, computers and groups in the same tree. Each of them has an attribute, sAMAccountType, that says which kind it is. Core\Ldap\Entry::accountType() reads that attribute and returns one of these cases. User is a user account, Machine is a computer, and Group and Alias are groups. The result is null when the entry has no such attribute, for example an organizational unit.

Good to know: the search must select sAMAccountType, or accountType() returns null.

Examples

The examples below run with this configuration file.

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

A person or a group

<?nvs
// Shows how to tell people, computers and groups apart in Active Directory.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
class Kind
{
// `accountType()` returns a `Core\Ldap\Entry::AccountType` case, or `null` when the entry has none.
public static function of(Core\Ldap\Entry $entry): string
{
var $type = $entry->accountType();
if ($type == Core\Ldap\Entry::AccountType::User) {
return 'a person';
}
if ($type == Core\Ldap\Entry::AccountType::Machine) {
return 'a computer';
}
if ($type == Core\Ldap\Entry::AccountType::Group || $type == Core\Ldap\Entry::AccountType::Alias) {
return 'a group';
}
return 'something else';
}
}
// This prints `Anna Berg: a person` and `Domain Users: a group`.
array<string> $names = ['CN=Anna Berg,OU=Company,DC=example,DC=test', 'CN=Domain Users,CN=Users,DC=example,DC=test'];
foreach ($names as string $name) {
var $entry = $corp->read($name, { select: ['cn', 'sAMAccountType'] });
if ($entry != null) {
echo $entry->string('cn') ?? '', ': ', Kind::of($entry), "\n";
}
}
Output
Anna Berg: a person
Domain Users: a group