Core\Ldap\Entry::accountType
$entry->accountType(): ?Core\Ldap\Entry::AccountTypeReturns what kind of account an Active Directory entry is, as a Core\Ldap\Entry::AccountType. For example, a person is AccountType::User, a computer is AccountType::Machine and a security group is AccountType::Group.
Active Directory stores the kind as a number in sAMAccountType. Select that attribute in the search. accountType returns the case of the enum for that number, so you can use it in a match.
If the entry has no sAMAccountType, the result is null. An organizational unit has none. If the value is not a number that Active Directory uses for this attribute, accountType throws a LogicError that names the attribute.
Return value
Returns ?Core\Ldap\Entry::AccountType. The account type, or null when the entry has no sAMAccountType.
Errors
LogicErrorThe attribute has more than one value, or its value is not in the form this function reads. The message names the attribute.
Examples
The examples below run with this configuration file.
# 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"]What kind of entry
<?nvs// Shows how to check what kind of account an entry is: a person, a group or a computer.// requires: samba-ad
class Kind{ public static function of(Core\Ldap\Entry::AccountType $type): string { return match ($type) { Core\Ldap\Entry::AccountType::User => 'a person', Core\Ldap\Entry::AccountType::Machine => 'a computer', Core\Ldap\Entry::AccountType::Group => 'a security group', Core\Ldap\Entry::AccountType::NonSecurityGroup => 'an email group', default => 'something else', }; }}
var $corp = Core\Ldap::connect('corp');array<string> $dns = [ 'CN=Anna Berg,OU=Company,DC=example,DC=test', 'CN=Managers,OU=Company,DC=example,DC=test', 'CN=Newsletter,OU=Company,DC=example,DC=test',];
foreach ($dns as string $dn) { var $entry = $corp->read($dn, { select: ['sAMAccountType'] }); var $type = $entry?->accountType(); if ($entry == null || $type == null) { continue; } // This prints `Anna Berg: a person`, `Managers: a security group` // and `Newsletter: an email group`. echo $entry->dn()->rdnValue(), ': ', Kind::of($type), "\n";}Anna Berg: a personManagers: a security groupNewsletter: an email groupCount people in a search
<?nvs// Shows how to count the people among the entries of a search.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');var $found = $corp->search( Core\Ldap\Filter::present('sAMAccountType'), { base: 'OU=Company,DC=example,DC=test', select: ['sAMAccountType'] },);
// `OU=Company` has five users and three groups.// This prints `5 people, 3 other entries`.uint $people = 0;uint $other = 0;foreach ($found as Core\Ldap\Entry $entry) { if ($entry->accountType() == Core\Ldap\Entry::AccountType::User) { $people = $people + 1; } else { $other = $other + 1; }}echo $people, ' people, ', $other, " other entries\n";5 people, 3 other entriesAn entry without an account type
<?nvs// Shows that `accountType` returns `null` for an entry that is not an account, such as an organizational unit.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');var $unit = $corp->read('OU=Company,DC=example,DC=test', { select: ['sAMAccountType'] });
if ($unit != null) { // An organizational unit has no `sAMAccountType`. This prints `not an account`. echo $unit->accountType() == null ? 'not an account' : 'an account', "\n";}not an account