PHP code example of gozoro / ldap

1. Go to this page and download the library: Download gozoro/ldap library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gozoro / ldap example snippets



$config = [
	'username'   => 'admin',
	'password'   => '12345',
	'hosts'      => ['ldap1.example.net', 'ldap2.example.net'],
	'domainName' => 'example.net',
];


$ldap = new \gozoro\ldap\Ldap($config);

$user = $ldap->findUser('john');

print $user->getPrincipalName(); // [email protected]
print $user->getDisplayName();   // John Smith
print $user->getLastLogonTime(); // 2020-07-12 14:23:17
print $user->getObjectGuid();    // 1ba5b8ff-b80b-40d4-ae45-7418f8eedd6a
print_r($user->getGroupNames()); // Array(0=>'admins', 'users')

$userPassword = 'qwerty';

if($user->validatePassword($userPassword))
{
	print 'password: OK';
}

foreach($user->getGroups() as $userGroup)
{
	print $userGroup->getName();
	print $userGroup->getObjectGUID();
}




$config = [
	'hosts'      => ['ldap1.example.net', 'ldap2.example.net'],
	'domainName' => 'example.net',
	'protocolVersion' => 3,

	'SASL' => 'GSSAPI',

	'beforeConnect' => function($ldap){
		$cmd = "kinit -k -t /etc/keytabs/my.access.keytab ldap/[email protected] 2>&1";
		exec($cmd, $output, $result);
		if ($result !== 0) {
			throw new \Exception("kinit failed: " . implode("\n", $output));
		}
	},

	'afterClose' => function(){
		$cmd = "kdestroy 2>&1";
		exec($cmd, $output, $result);
		if ($result !== 0) {
			throw new \Exception("kdestroy failed: " . implode("\n", $output));
		}
	},
];

// Authentication with SASL mechanism GSSAPI (Kerberos v5)
$ldap = new \gozoro\ldap\Ldap($config);

// Validate users's password with SASL mechanism DIGEST-MD5
$user = $ldap->validatePassword('john', 'qwerty', 'DIGEST-MD5');