Core\Ldap\Entry::sid
$entry->sid(string $name): ?Core\Ldap\Sid$entry->sid( string $name, ): ?Core\Ldap\Sid
Returns the one value of an attribute as a Core\Ldap\Sid, such as an entry's objectSid.
A SID (security identifier) is the id Windows uses for a user, a group or a computer. The directory sends it as bytes. sid reads those bytes and returns a Core\Ldap\Sid. With it you can print the SID as text, such as S-1-5-21-…-1107, read its last number with rid, or find its domain with domain.
If the entry has no value for the attribute, the result is null. For an attribute with several values, sid throws a LogicError, and sids returns every value. sid also throws a LogicError when the value is not a SID, and the message names the attribute.
Parameters
$namestringThe attribute's name, such asmail. Upper and lower case are the same.
Return value
Returns ?Core\Ldap\Sid. The SID, or null when the entry has no value for the attribute.
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"]Read the rid of an account
<?nvs// Shows how to read the SID of an account and its last number.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');var $admin = $corp->read('CN=Administrator,CN=Users,DC=example,DC=test', { select: ['objectSid'] });
if ($admin != null) { // The first part of a SID is different in every domain. The last number, the RID, // is always 500 for the account `Administrator`. This prints `500`. echo $admin->sid('objectSid')?->rid() ?? 0, "\n";}500Check the domain of a user
<?nvs// Shows how to check that a user belongs to this domain.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');var $domain = $corp->read('DC=example,DC=test', { select: ['objectSid'] });var $person = $corp->read('CN=Anna Berg,OU=Company,DC=example,DC=test', { select: ['objectSid'] });
if ($domain != null && $person != null) { // `domain` returns the SID without its last number. For a user, that is the SID of the domain. string $ofDomain = $domain->sid('objectSid')?->toString() ?? 'none'; string $ofPerson = $person->sid('objectSid')?->domain()?->toString() ?? 'none';
// This prints `Anna Berg is in this domain.` if ($ofDomain == $ofPerson) { echo "Anna Berg is in this domain.\n"; }}Anna Berg is in this domain.A value that is not a sid
<?nvs// Shows what `sid` does with a value that is missing, and with a value that is not a SID.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');var $person = $corp->read('CN=Anna Berg,OU=Company,DC=example,DC=test', { select: ['mail'] });
if ($person != null) { // The read did not select `objectSid`, so `sid` returns `null`. This prints `no SID`. echo $person->sid('objectSid')?->toString() ?? 'no SID', "\n";
// `mail` is text. `sid` throws a `LogicError` that names the attribute. try { echo $person->sid('mail')?->toString() ?? 'no SID', "\n"; } catch (LogicError $error) { echo $error->message, "\n"; }}no SIDCore\Ldap\Entry::sid: a value of `mail` is not a SID