Skip to content

Core\Ldap\Connection::hasValue

$connection->hasValue(
    Core\Ldap\Dn|string $dn,
    string $attribute,
    mixed $value,
): bool

Checks whether an entry in the directory has a value. It returns true or false.

You give the entry, the name of an attribute and one value. The server checks the value and sends back only the answer. The program does not read the entry, so this is a quick way to test one fact, such as "is this person in the Sales department".

The server compares the value by the rules of the attribute. For most text attributes, upper and lower case are the same, so "sales" matches "Sales".

The value can be a string, an int or a Core\Ldap\Dn. A Dn is useful for an attribute that contains the names of other entries, such as the member list of a group.

Good to know: an entry that does not exist throws Core\Ldap\LdapError. A list or null as the value throws LogicError, because hasValue checks exactly one value.

Parameters

  • $dnCore\Ldap\Dn|stringThe entry's DN, as a Dn or as text. Text cannot be tainted. Build a DN from user input with Dn::of or child.
  • $attributestringThe attribute's name, such as department. It cannot be tainted.
  • $valuemixedOne value, in any form a write accepts, such as a string, an int or a Dn.

Return value

Returns bool. true when the entry has the value, and false when it does not.

Errors

  • LogicErrorattribute is not an attribute name, value is null, a list or has no LDAP form, or the connection is closed.
  • Core\Ldap\LdapErrorThe server returned an error. $kind is NoSuchObject when there is no entry with this DN.

Changelog

VersionChange
0.0.6Core\Ldap\Connection->hasValue() checks whether an entry has a value, and searchChanges() reads the entries that changed. Core\Ldap\Filter::approximately builds an approximate match. The old names were compare, changes and approx.

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

Is this person in sales

<?nvs
// Shows how to check whether a person works in the Sales department.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
array<string> $people = ['Anna Berg', 'Ben Kohl', 'Carla Diaz', 'Dora Lind'];
// Anna Berg and Carla Diaz work in Sales, so their lines say `yes`.
foreach ($people as string $name) {
string $dn = 'CN=' . $name . ',OU=Company,DC=example,DC=test';
bool $inSales = $corp->hasValue($dn, 'department', 'Sales');
echo $name, ': ', $inSales ? 'yes' : 'no', "\n";
}
Output
Anna Berg: yes
Ben Kohl: no
Carla Diaz: yes
Dora Lind: no

Upper and lower case are the same

<?nvs
// Shows that the server ignores upper and lower case when it compares a text value.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
string $dn = 'CN=Ben Kohl,OU=Company,DC=example,DC=test';
// Ben Kohl's department is `Warehouse`. These three lines print `true`.
echo $corp->hasValue($dn, 'department', 'Warehouse') ? 'true' : 'false', "\n";
echo $corp->hasValue($dn, 'department', 'warehouse') ? 'true' : 'false', "\n";
echo $corp->hasValue($dn, 'mail', 'BEN.KOHL@EXAMPLE.TEST') ? 'true' : 'false', "\n";
// A different word is a different value, so this prints `false`.
echo $corp->hasValue($dn, 'department', 'Sales') ? 'true' : 'false', "\n";
Output
true
true
true
false

Is this person a member of a group

<?nvs
// Shows how to check whether a person is a direct member of a group.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
string $managers = 'CN=Managers,OU=Company,DC=example,DC=test';
// The `member` attribute of a group contains the names of its members.
// The value is a `Core\Ldap\Dn`, so the server compares it as a name.
var $anna = Core\Ldap\Dn::parse('CN=Anna Berg,OU=Company,DC=example,DC=test');
var $ben = Core\Ldap\Dn::parse('CN=Ben Kohl,OU=Company,DC=example,DC=test');
// Only Anna Berg is a manager. This prints `true` and then `false`.
echo $corp->hasValue($managers, 'member', $anna) ? 'true' : 'false', "\n";
echo $corp->hasValue($managers, 'member', $ben) ? 'true' : 'false', "\n";
Output
true
false