1. Go to this page and download the library: Download newtondasilva/ldaptools 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/ */
newtondasilva / ldaptools example snippets
use LdapTools\Configuration;
use LdapTools\LdapManager;
$config = (new Configuration())->load('/path/to/ldap/config.yml');
$ldap = new LdapManager($config);
use LdapTools\Object\LdapObjectType;
// Get an instance of the query...
$query = $ldap->buildLdapQuery();
// Returns a LdapObjectCollection of all users whose first name
// starts with 'Foo' and last name is 'Bar' or 'Smith'.
// The result set will also be ordered by state name (ascending).
$users = $query->fromUsers()
->where($query->filter()->startsWith('firstName', 'Foo'))
->orWhere(['lastName' => 'Bar'])
->orWhere(['lastName' => 'Smith'])
->orderBy('state')
->getLdapQuery()
->getResult();
echo "Found ".$users->count()." user(s).";
foreach ($users as $user) {
echo "User: ".$user->getUsername();
}
// Get all OUs and Containers at the base of the domain, ordered by name.
$results = $ldap->buildLdapQuery()
->from(LdapObjectType::OU)
->from(LdapObjectType::CONTAINER)
->orderBy('name')
->setScopeOneLevel()
->getLdapQuery()
->getResult();
// Get a single LDAP object and select some specific attributes...
$user = $ldap->buildLdapQuery()
->select(['upn', 'guid', 'sid', 'passwordLastSet'])
->fromUsers()
->where(['username' => 'chad'])
->getLdapQuery()
->getSingleResult();
// Get a single attribute value from a LDAP object...
$guid = $ldap->buildLdapQuery()
->select('guid')
->fromUsers()
->where(['username' => 'chad'])
->getLdapQuery()
->getSingleScalarResult();
// It also supports the concepts of repositories...
$userRepository = $ldap->getRepository('user');
// Find all users whose last name equals Smith.
$users = $userRepository->findByLastName('Smith');
// Get the first user whose username equals 'jsmith'. Returns a `LdapObject`.
$user = $userRepository->findOneByUsername('jsmith');
echo "First name ".$user->getFirstName()." and last name ".$user->getLastName();
$user = $ldap->buildLdapQuery()
->select(['title', 'mobilePhone', 'disabled'])
->fromUsers()
->where(['username' => 'jsmith'])
->getLdapQuery()
->getSingleResult();
// Make some modifications to the user account.
// All these changes are tracked so it knows how to modify the object.
$user->setTitle('CEO');
if ($user->hasMobilePhone()) {
$user->resetMobilePhone();
}
// Set a field by a property instead...
if ($user->disabled) {
$user->disabled = false;
}
// Add a value to an attribute...
$user->addOtherIpPhones('#001-5555');
// Add a few values at one time...
$user->addOtherIpPhones('#001-4444', '#001-3333', '#001-2222');
// Now actually save the changes back to LDAP...
try {
$ldap->persist($user);
} catch (\Exception $e) {
echo "Error updating user! ".$e->getMessage();
}
// Decide they no longer work here and should be deleted?
$user = $userRepository->findOneByUsername('jsmith');
try {
$ldap->delete($user);
} catch (\Exception $e) {
echo "Error deleting user! ".$e->getMessage();
}
$ldapObject = $ldap->createLdapObject();
// Creating a user account (enabled by default)
$ldapObject->createUser()
->in('cn=Users,dc=example,dc=local')
->with(['username' => 'jsmith', 'password' => '12345'])
->execute();
// Create a typical AD global security group...
$ldapObject->createGroup()
->in('dc=example,dc=local')
->with(['name' => 'Generic Security Group'])
->execute();
// Creates a contact user...
$ldapObject->createContact()
->in('dc=example,dc=local')
->with(['name' => 'Some Guy', 'emailAddress' => '[email protected]'])
->execute();
// Creates a computer object...
$ldapObject->createComputer()
->in('dc=example,dc=local')
->with(['name' => 'MYWOKRSTATION'])
->execute();
// Creates an OU object...
$ldapObject->createOU()
->in('dc=example,dc=local')
->with(['name' => 'Employees'])
->execute();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.