Core\Ldap\Entry::sids
$entry->sids(string $name): ?array<Core\Ldap\Sid>$entry->sids( string $name, ): ?array<Core\Ldap\Sid>
Returns every value of an attribute as a Core\Ldap\Sid, such as a user's tokenGroups.
A SID (security identifier) is the id Windows uses for a user, a group or a computer. tokenGroups has the SID of every security group the user is in, also the groups the user is in through another group. The directory computes it only when you read one entry, not in a search.
sids returns the SIDs in the order the server sent them. That order is not fixed, so sort them when the order matters. If the entry has no value for the attribute, the result is null. If a value is not a SID, sids throws a LogicError that names the attribute.
sids also reads an attribute with only one value, and returns an array with one item.
Parameters
$namestringThe attribute's name, such asmail. Upper and lower case are the same.
Return value
Returns ?array<Core\Ldap\Sid>. The SIDs in the order the server sent them, or null when the entry has no value for the attribute.
Errors
LogicErrorA value is not a SID. 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"]List the groups of a user
<?nvs// Shows how to list the security groups of a user by their RID.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');var $person = $corp->read('CN=Anna Berg,OU=Company,DC=example,DC=test', { select: ['tokenGroups'] });
if ($person != null) { // The server sends the SIDs in no fixed order, so the RIDs are sorted. array<int> $rids = []; foreach ($person->sids('tokenGroups') ?? [] as Core\Ldap\Sid $group) { $rids[] = $group->rid(); }
// 513 is `Domain Users` and 545 is `Users`. The other two are `Employees` and `Managers`. // This prints 513, 545, 1104 and 1105, one on each line. foreach (Core\Arr::sort($rids) as int $rid) { echo $rid, "\n"; }}51354511041105Is this user a manager
<?nvs// Shows how to check whether a user is in a security group.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');var $managers = $corp->read('CN=Managers,OU=Company,DC=example,DC=test', { select: ['objectSid'] });string $managersSid = $managers?->sid('objectSid')?->toString() ?? 'none';
array<string> $dns = [ 'CN=Anna Berg,OU=Company,DC=example,DC=test', 'CN=Ben Kohl,OU=Company,DC=example,DC=test',];foreach ($dns as string $dn) { var $person = $corp->read($dn, { select: ['tokenGroups'] }); if ($person == null) { continue; } bool $isManager = false; foreach ($person->sids('tokenGroups') ?? [] as Core\Ldap\Sid $group) { if ($group->toString() == $managersSid) { $isManager = true; } } // This prints `Anna Berg: manager` and `Ben Kohl: not a manager`. echo $person->dn()->rdnValue(), ': ', $isManager ? 'manager' : 'not a manager', "\n";}Anna Berg: managerBen Kohl: not a managerValues that are not sids
<?nvs// Shows what `sids` does with one value, with no value, and with values that are not SIDs.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');var $person = $corp->read('CN=Carla Diaz,OU=Company,DC=example,DC=test', { select: ['objectSid', 'memberOf'] });
if ($person != null) { // `objectSid` has one value, so the array has one item. This prints `1`. echo Core\Arr::count($person->sids('objectSid') ?? []), "\n";
// The read did not select `tokenGroups`, so `sids` returns `null`. This prints `no value`. echo $person->sids('tokenGroups') == null ? 'no value' : 'has a value', "\n";
// `memberOf` is a list of DNs, not SIDs. `sids` throws a `LogicError` that names the attribute. try { echo Core\Arr::count($person->sids('memberOf') ?? []), "\n"; } catch (LogicError $error) { echo $error->message, "\n"; }}1no valueCore\Ldap\Entry::sids: a value of `memberOf` is not a SID