<?php require_once 'vendor/autoload.php'; use Symfony\Component\Ldap\Ldap; use Symfony\Component\Ldap\Adapter\ExtLdap\Adapter; $config = ['host' => 'localhost', 'port' => 389]; $dn = 'cn=admin,ou=admins,dc=openldap,dc=com'; $password = '******'; $adapter = new Adapter($config); $adapter->getConnection()->setOption('PROTOCOL_VERSION', 3); $ldap = new Ldap($adapter); $ldap->bind($dn, $password); $filter = 'objectClass=*'; $attributes = ['cn', 'sn', 'uid', 'description']; $query = $ldap->query($dn, 'objectClass=*', ['filter' => $attributes]); $collection = $query->execute(); var_dump('Good!', $collection->toArray());
/** * @Then /^The "(?P<objectclass>[^"]+)" with cn "(?P<cn>[^"]+)" should exist in Ldap$/ */ public function theObjectclassWithCnShouldExistInLdap($objectclass, $cn) { if (is_null($results = $this->client->query($this->rootDn, '(cn=' . $cn . ')')->execute())) { throw new \Exception('Unknow entry cn=' . $cn . ' in Ldap'); } $results = $results->toArray(); if ($results[0]->getAttribute('objectclass')[0] != $objectclass) { throw new \Exception('The entry cn=' . $cn . ' is not a ' . $objectclass . ' (' . $results[0]->getAttribute('objectclass')[0] . ')'); } }
public function testCreateWithInvalidAdapterName() { $this->setExpectedException(DriverNotFoundException::class); Ldap::create('foo'); }
/** * Construct an instance. * @staticvar int $instanceId * @param string $host The LDAP server host/ip * @param string $port The LDAP server port * @param string $dnString The distinguished name string containing '{username}' phrase, * which will be replaced with the username entered by user * @param integer $version The LDAP version (default = 3) */ public function __construct($host, $port, $dnString, $version = 3) { $this->ldapClient = Ldap::create('ext_ldap', array('host' => $host, 'port' => $port, 'version' => $version)); $this->dnString = $dnString; }