Core\Ldap\LdapError::Kind
Says why a directory operation failed.
Core\Ldap\LdapError::Kind is declared in the class Core\Ldap\LdapError. Its property kind has this type.
When a directory operation fails, the Core\Ldap\LdapError your program catches has a kind property. It is one of these cases. InvalidCredentials means the login or the password is wrong. NoSuchObject means the entry does not exist. Unavailable and Timeout mean the server did not answer in time, so a later try may work. Active Directory gives a separate case for each reason a login fails, such as AccountDisabled, AccountLocked and PasswordExpired.
Compare kind with a case to choose what your program does next.
Good to know: a user that does not exist and a wrong password are both InvalidCredentials. Nobody can use a login form to find out which users exist.
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"]A message for each reason
<?nvs// Shows how to turn the reason for a failed directory operation into a message for a person.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
class Reason{ // `kind` of the error is a `Core\Ldap\LdapError::Kind` case. public static function of(Core\Ldap\LdapError $error): string { if ($error->kind == Core\Ldap\LdapError::Kind::InvalidCredentials) { return 'wrong login or password'; } if ($error->kind == Core\Ldap\LdapError::Kind::NoSuchObject) { return 'that entry does not exist'; } if ($error->kind == Core\Ldap\LdapError::Kind::Unavailable) { return 'the directory is not available, try again later'; } return 'the directory rejected the request'; }}
// This user does not exist. This prints `wrong login or password`.try { $corp->authenticate('CN=Nobody,OU=Company,DC=example,DC=test', 'Some-password-1');} catch (Core\Ldap\LdapError $error) { echo Reason::of($error), "\n";}
// This unit does not exist. This prints `that entry does not exist`.try { foreach ($corp->search(Core\Ldap\Filter::present('cn'), { base: 'OU=Nowhere,DC=example,DC=test' }) as Core\Ldap\Entry $entry) { }} catch (Core\Ldap\LdapError $error) { echo Reason::of($error), "\n";}wrong login or passwordthat entry does not exist