Skip to content

Code examples

Rich2k edited this page Apr 18, 2013 · 1 revision

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.

Connecting to Active Directory

require_once(dirname(__FILE__) . '/adLDAP.php');
$adldap = new adLDAP();

Authenticating a user

See user functions

$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";
}

Adding a user to a group

See group functions

$result = $adldap->group()->addUser("Group Name","username");

Create a group

See group functions

$attributes = array(
  "group_name"=>"Test Group",
  "description"=>"Just Testing",
  "container"=>array("Groups","A Container"),
);
$result = $adldap->group()->create($attributes);

Retrieve information about a group

See group functions

$result = $adldap->group()->info("Group Name");

Alternatively you can use the groupCollection

$result = $adldap->group()->infoCollection("Group Name", array('*'));
echo $result->description;

Create a user account

See user functions

$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();   
}

Group membership of a user

See user functions

$result = $adldap->user()->groups("username");

Information about a user

See user functions

$result = $adldap->user()->info("username");

Alternatively you can use the userCollection

$result = $adldap->user()->infoCollection("username", array("*"));
echo $result->displayName;
echo $result->mail;

See if a user is in a group

See user functions

$result = $adldap->user()->inGroup("username","Group Name");

Modify a user account

See user functions

This example will set "user must change password at next logon"

$attributes = array(
  "change_password"=>1,
);
$result = $adldap->user()->modify("username",$attributes);

Change the password of a user

See user functions

try {
  $result = $adldap->user()->password("username","Password123");
  /* Do Something */
}
catch (adLDAPException $e) {
  echo $e; exit();   
}

Disable a user

See user functions

$result = $adldap->user()->disable("username");

Create a Microsoft Exchange account

See exchange functions

$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'
);

Adding an email address to an existing Exchange mailbox

See exchange functions

$add = $adldap->exchange()->addAddress('Username', '[email protected]', false);

Disabling Exchange Recipient Policy on a user

See exchange functions

$modify = $adldap->user()->modify('Username', array('exchange_policyexclude'=>'{26491CFC-9E50-4857-861B-0CB8DF22B5D7}'));

Active Directory folder structure

See folder functions

$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'