Skip to content

Core\Ldap\Entry::groupType

$entry->groupType(): ?Core\Ldap\Ad\GroupType

Returns the type of an Active Directory group as a Core\Ldap\Ad\GroupType. Each flag is a function that returns a bool, such as isSecurityGroup() or isGlobal().

Active Directory stores the type as one number in groupType. Select that attribute in the search. isSecurityGroup() says if the group can give its members access to something. A group where it is false is a distribution group, which is only used for email. isGlobal(), isDomainLocal() and isUniversal() give the scope of the group.

If the entry has no groupType, the result is null. A user has none. If the value is not a number, groupType throws a LogicError that names the attribute.

Good to know: the security flag is the highest bit of the number, so bits() returns a negative number for a security group.

Return value

Returns ?Core\Ldap\Ad\GroupType. The group type, or null when the entry has no groupType, such as a user.

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.

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"]

Security or mail group

<?nvs
// Shows how to tell a security group from a group that is only for email.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
array<string> $dns = [
'CN=Employees,OU=Company,DC=example,DC=test',
'CN=Newsletter,OU=Company,DC=example,DC=test',
];
foreach ($dns as string $dn) {
var $group = $corp->read($dn, { select: ['groupType'] });
var $type = $group?->groupType();
if ($group == null || $type == null) {
continue;
}
// Only a security group can give its members access to something.
// This prints `Employees: security` and `Newsletter: email only`.
echo $group->dn()->rdnValue(), ': ', $type->isSecurityGroup() ? 'security' : 'email only', "\n";
}
Output
Employees: security
Newsletter: email only

The scope of a group

<?nvs
// Shows how to read the scope of a group: global, domain local or universal.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
var $group = $corp->read('CN=Managers,OU=Company,DC=example,DC=test', { select: ['groupType'] });
var $type = $group?->groupType();
if ($type != null) {
// A global group has members from its own domain only.
// This prints `global`.
string $scope = 'unknown';
if ($type->isGlobal()) {
$scope = 'global';
} elseif ($type->isDomainLocal()) {
$scope = 'domain local';
} elseif ($type->isUniversal()) {
$scope = 'universal';
}
echo $scope, "\n";
// `bits` returns the number Active Directory stored. The security flag is the
// highest bit, so the number is negative. This prints `-2147483646`.
echo $type->bits(), "\n";
}
Output
global
-2147483646

A person has no group type

<?nvs
// Shows that `groupType` returns `null` for an entry that is not a group.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
var $person = $corp->read('CN=Anna Berg,OU=Company,DC=example,DC=test', { select: ['groupType'] });
if ($person != null) {
// A user has no `groupType`. This prints `not a group`.
echo $person->groupType() == null ? 'not a group' : 'a group', "\n";
}
Output
not a group