-
Notifications
You must be signed in to change notification settings - Fork 205
Code examples
Here are just a few examples of what you can do with adLDAP. Read the full Developers API Reference to see what other functions are available. There's so much more than just authenticating and getting group/user information!
These examples are specific to version 4.0 and above. For examples for 3.x please see here.
require_once(dirname(__FILE__) . '/adLDAP.php');
$adldap = new adLDAP();
$authUser = $adldap->user()->authenticate($username, $password);
if ($authUser == true) {
echo "User authenticated successfully";
}
else {
// getLastError is not needed, but may be helpful for finding out why:
echo $adldap->getLastError();
echo "User authentication unsuccessful";
}
$result = $adldap->group()->addUser("Group Name","username");
$attributes = array(
"group_name"=>"Test Group",
"description"=>"Just Testing",
"container"=>array("Groups","A Container"),
);
$result = $adldap->group()->create($attributes);
$result = $adldap->group()->info("Group Name");
Alternatively you can use the groupCollection
$result = $adldap->group()->infoCollection("Group Name", array('*'));
echo $result->description;
$attributes = array(
"username"=>"freds",
"logon_name"=>"[email protected]",
"firstname"=>"Fred",
"surname"=>"Smith",
"company"=>"My Company",
"department"=>"My Department",
"email"=>"[email protected]",
"container"=>array("Container Parent","Container Child"),
"enabled"=>1,
"password"=>"Password123",
);
try {
$result=$adldap->user()->create($attributes);
/* Do something */
}
catch (adLDAPException $e) {
echo $e; exit();
}
$result = $adldap->user()->groups("username");
$result = $adldap->user()->info("username");
Alternatively you can use the userCollection
$result = $adldap->user()->infoCollection("username", array("*"));
echo $result->displayName;
echo $result->mail;
$result = $adldap->user()->inGroup("username","Group Name");
This example will set "user must change password at next logon"
$attributes = array(
"change_password"=>1,
);
$result = $adldap->user()->modify("username",$attributes);
try {
$result = $adldap->user()->password("username","Password123");
/* Do Something */
}
catch (adLDAPException $e) {
echo $e; exit();
}
$result = $adldap->user()->disable("username");
$create = $adldap->exchange()->createAccount('Username',
array('Mailbox Store', 'Storage Group', 'InformationStore', 'MAILSERVER01',
'Servers', 'First Administrative Group', 'Administrative Group',
'Development', 'Microsoft Exchange', 'Services', 'Configuration'),
'AD User Name',
true,
'DC=someotherdomain,DC=local'
);
$add = $adldap->exchange()->addAddress('Username', '[email protected]', false);
$modify = $adldap->user()->modify('Username', array('exchange_policyexclude'=>'{26491CFC-9E50-4857-861B-0CB8DF22B5D7}'));
$folders = $adldap->folder()->listing(array('Users'), true);
This will return the folders 'Contacts', 'User Accounts' and all contacts and users contained within those two folders.
$folders = $adldap->folder()->listing(array('Users'), false);
This will return the folders 'Contacts' and 'User Accounts' only and any other records contained within the 'Users' level only
$folders = $adldap->folder()->listing(array('Users'), true, 'contact');
This will return only contacts within the 'Users' folder AND the folders 'Contacts', 'User Accounts'
$folders = $adldap->folder()->listing(array('Users'), false, 'user');
This will return only users within the 'Users' folder and NOT the folders 'Contacts', 'User Accounts'