PHP code example of codecrafting-io / adoldap
1. Go to this page and download the library: Download codecrafting-io/adoldap 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/ */
codecrafting-io / adoldap example snippets
try {
$ad = new AdoLDAP();
dump($ad->info());
} catch(\Exception $e) {
dump($e);
}
try {
//Minimal recommended configuration
$config = [
'host' => 'server01.mydomain.com', //use a primaryDomainController
'baseDn' => 'DC=MYDOMAIN,DC=COM',
];
$ad = new AdoLDAP($config); //auto connected
} catch(\AdoLDAPException $e) {
dump($e);
}
try {
$config = [
'host' => 'server01.mydomain.com', //use a primaryDomainController
'baseDn' => 'DC=MYDOMAIN,DC=COM',
];
$ad = new AdoLDAP($config);
$ad->search()->query("<LDAP://server01.mydomain.com/DC=MYDOMAIN,DC=COM>;(&(objectCategory=user)(sAMAccountName=jdoe));sAMAccountName,name");
} catch(\AdoLDAPException $e) {
dump($e);
}
try {
$config = [
'host' => 'server01.mydomain.com', //use a primaryDomainController
'baseDn' => 'DC=MYDOMAIN,DC=COM',
];
$ad = new AdoLDAP($config);
$ad->search()->whereEquals('objectCategory', 'user')->findBy('sAMAccountName', 'jdoe');
} catch(\AdoLDAPException $e) {
dump($e);
}
$ad->search()->newQuery()->whereEquals('objectCategory', 'user')->findBy('sAMAccountName', 'jdoe');
//OR
$ad->search()->whereEquals('objectCategory', 'user')->findBy('sAMAccountName', 'jdoe');
$ad->search()->users();
//EQUALS
$ad->search()->whereEquals('objectCategory', 'user');
$ad->search()->user('jdoe', ['name']);
//EQUALS
$ad->search()->whereEquals('objectCategory', 'user')->firstBy('sAMAccountName', 'jdoe', ['name']);
$ad->search()
->select(['name'])
->from('DC=MYDOMAIN,DC=COM')
->whereEquals('objectCategory', 'user')
->orWhere('objectCategory', 'computer')
->get();
try {
$config = [
'host' => 'server01.mydomain.com',
'baseDn' => 'DC=MYDOMAIN,DC=COM',
'dialect' => SQLDialect::class
];
$ad = new AdoLDAP($config);
} catch(\AdoLDAPException $e) {
dump($e);
}
$users = $ad->search()->users()
->select(['sAMAccountName', 'name', 'thumbnailPhoto', 'mail'])
->whereMemberOf('CN=AWESOME GROUP,DC=MYDOMAIN,DC=COM')->get();
foreach($users as $user) {
echo $user->sAMAccountName . '<br>';
echo $user->name . '<br>';
echo $user->thumbnailPhoto . '<br>';
echo $user->mail . '<br>';
}
const COLUMN_MAP = [
'objectclass' => 'objectclass',
'dn' => 'distinguishedname',
'account' => 'samaccountname',
'firtname' => 'givenname',
'name' => 'name',
'workstations' => 'userworkstations',
'mail' => 'mail',
'jobtitle' => 'description',
'jobrole' => 'title',
'address' => [
'street',
'postalcode',
'st',
'l',
'co'
],
'mailboxes' => 'msexchdelegatelistbl',
'mobile' => 'mobile',
'phone' => 'telephoneNumber',
'department' => 'department',
'departmentcode' => 'extensionAttribute1',
'memberOf' => 'memberOf',
'company' => 'company',
'photo' => 'thumbnailphoto',
'passwordlastset' => 'pwdlastset',
'passworderrorcount' => 'badpwdcount',
'passworderrortime' => 'badpasswordtime',
'lastlogin' => 'lastlogontimestamp',
'lockouttime' => 'lockouttime',
'createdat' => 'whencreated',
'objectguid' => 'objectguid',
'objectsid' => 'objectsid'
];
$users = $ad->search()->users()
->select(['sAMAccountName', 'name', 'thumbnailPhoto', 'mail'])
->whereMemberOf('CN=AWESOME GROUP,DC=MYDOMAIN,DC=COM')->get();
foreach($users as $user) {
echo $user->getAccount();
//EQUALS
echo $user->sAMAccountName;
}
$users = $ad->search()->users()
->select(['sAMAccountName', 'name', 'thumbnailPhoto'])
->whereMemberOf('CN=AWESOME GROUP,DC=MYDOMAIN,DC=COM')->get();
foreach($users as $user) {
echo $user->getAccount() . '<br>';
echo $user->getName() . '<br>';
echo $user->getHtmlPhoto(['class' => 'profile-picture']) . '<br>';
}
$attributes = SearchFactory::translateAttributes(User::COLUMN_MAP, ['accountName', 'name', 'photo', 'mailboxes']);
$ad->search()->users()
->select($attributes)
->whereMemberOf('CN=AWESOME GROUP,DC=MYDOMAIN,DC=COM')->get();
}
$ad->search()->user('jdoe', ['accountName', 'name', 'photo', 'mailboxes']);
//EQUALS
$ad->search()->user('jdoe', ['sAMAccountName', 'name', 'thumbnailPhoto', 'msExchDelegateListBl'], false);
$ad->search()->user('jdoe');
//EQUALS
$ad->search()->user('jdoe', User::getDefaultAttributes(), false);
$dn = new DistinguishedName('CN=AWESOME GROUP,DC=MYDOMAIN,DC=COM');
echo $dn->getName() . '<br>'; //Container Name - AWESOME GROUP
echo $dn->getPath() //Whole Path - CN=AWESOME GROUP,DC=MYDOMAIN,DC=COM
echo User::objectClass()->getMostRelevant() //outputs user
$user = $ad->search()->user('jdoe', ['accountName', 'address']);
//EQUALS TO: $ad->search()->user('jdoe', ['sAMAcountName', 'street', 'postalCode', 'st', 'l', 'co']);
echo $user->getAddress()->getCountry();
$computer = $ad->search()->computer('MACHINE01');
echo $computer->getOS()->getName();
$computer->compareTo($ad->search()->computer('MACHEINE02')) //outputs -1, 0, 1;
//Get 10 users offseting 1 page. Returns a array of User objects
$users = $ad->search()->users()
->select(User::getDefaultAttributes())
->whereMemberOf('CN=AWESOME GROUP,DC=MYDOMAIN,DC=COM')
->get()->getEntries(10, 1);
$user = $ad->search()->user('jdoe')->afterFetch(function($user) {
return [
'name' => $user->getName(),
'accountName' => $user->getAccount(),
'photo' => $user->getPhoto()
];
})->getEntries();