Initial commit of LDAP plugin

This commit is contained in:
Andy Miller
2018-05-07 05:40:25 -06:00
parent cef5268e09
commit d4d885a874
90 changed files with 7961 additions and 1 deletions

View File

@@ -0,0 +1,114 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Ldap\Tests;
use Symfony\Component\Ldap\Adapter\ExtLdap\Adapter;
use Symfony\Component\Ldap\Adapter\ExtLdap\Collection;
use Symfony\Component\Ldap\Adapter\ExtLdap\Query;
use Symfony\Component\Ldap\Entry;
use Symfony\Component\Ldap\Exception\NotBoundException;
use Symfony\Component\Ldap\LdapInterface;
/**
* @requires extension ldap
*/
class AdapterTest extends LdapTestCase
{
public function testLdapEscape()
{
$ldap = new Adapter();
$this->assertEquals('\20foo\3dbar\0d(baz)*\20', $ldap->escape(" foo=bar\r(baz)* ", null, LdapInterface::ESCAPE_DN));
}
/**
* @group functional
*/
public function testLdapQuery()
{
$ldap = new Adapter($this->getLdapConfig());
$ldap->getConnection()->bind('cn=admin,dc=symfony,dc=com', 'symfony');
$query = $ldap->createQuery('dc=symfony,dc=com', '(&(objectclass=person)(ou=Maintainers))', array());
$result = $query->execute();
$this->assertInstanceOf(Collection::class, $result);
$this->assertCount(1, $result);
$entry = $result[0];
$this->assertInstanceOf(Entry::class, $entry);
$this->assertEquals(array('Fabien Potencier'), $entry->getAttribute('cn'));
$this->assertEquals(array('fabpot@symfony.com', 'fabien@potencier.com'), $entry->getAttribute('mail'));
}
/**
* @group functional
*/
public function testLdapQueryIterator()
{
$ldap = new Adapter($this->getLdapConfig());
$ldap->getConnection()->bind('cn=admin,dc=symfony,dc=com', 'symfony');
$query = $ldap->createQuery('dc=symfony,dc=com', '(&(objectclass=person)(ou=Maintainers))', array());
$result = $query->execute();
$iterator = $result->getIterator();
$iterator->rewind();
$entry = $iterator->current();
$this->assertInstanceOf(Entry::class, $entry);
$this->assertEquals(array('Fabien Potencier'), $entry->getAttribute('cn'));
$this->assertEquals(array('fabpot@symfony.com', 'fabien@potencier.com'), $entry->getAttribute('mail'));
}
/**
* @group functional
*/
public function testLdapQueryWithoutBind()
{
$ldap = new Adapter($this->getLdapConfig());
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(NotBoundException::class);
$query = $ldap->createQuery('dc=symfony,dc=com', '(&(objectclass=person)(ou=Maintainers))', array());
$query->execute();
}
public function testLdapQueryScopeBase()
{
$ldap = new Adapter($this->getLdapConfig());
$ldap->getConnection()->bind('cn=admin,dc=symfony,dc=com', 'symfony');
$query = $ldap->createQuery('cn=Fabien Potencier,dc=symfony,dc=com', '(objectclass=*)', array(
'scope' => Query::SCOPE_BASE,
));
$result = $query->execute();
$entry = $result[0];
$this->assertEquals($result->count(), 1);
$this->assertEquals(array('Fabien Potencier'), $entry->getAttribute('cn'));
}
public function testLdapQueryScopeOneLevel()
{
$ldap = new Adapter($this->getLdapConfig());
$ldap->getConnection()->bind('cn=admin,dc=symfony,dc=com', 'symfony');
$one_level_result = $ldap->createQuery('ou=Components,dc=symfony,dc=com', '(objectclass=*)', array(
'scope' => Query::SCOPE_ONE,
))->execute();
$subtree_count = $ldap->createQuery('ou=Components,dc=symfony,dc=com', '(objectclass=*)')->execute()->count();
$this->assertNotEquals($one_level_result->count(), $subtree_count);
$this->assertEquals($one_level_result->count(), 1);
$this->assertEquals($one_level_result[0]->getAttribute('ou'), array('Ldap'));
}
}

View File

@@ -0,0 +1,195 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Ldap\Tests;
use Symfony\Component\Ldap\Adapter\ExtLdap\Adapter;
use Symfony\Component\Ldap\Adapter\ExtLdap\Collection;
use Symfony\Component\Ldap\Entry;
use Symfony\Component\Ldap\Exception\LdapException;
use Symfony\Component\Ldap\Exception\NotBoundException;
/**
* @requires extension ldap
*/
class LdapManagerTest extends LdapTestCase
{
/** @var Adapter */
private $adapter;
protected function setUp()
{
$this->adapter = new Adapter($this->getLdapConfig());
$this->adapter->getConnection()->bind('cn=admin,dc=symfony,dc=com', 'symfony');
}
/**
* @group functional
*/
public function testLdapAddAndRemove()
{
$this->executeSearchQuery(1);
$entry = new Entry('cn=Charles Sarrazin,dc=symfony,dc=com', array(
'sn' => array('csarrazi'),
'objectclass' => array(
'inetOrgPerson',
),
));
$em = $this->adapter->getEntryManager();
$em->add($entry);
$this->executeSearchQuery(2);
$em->remove($entry);
$this->executeSearchQuery(1);
}
/**
* @group functional
*/
public function testLdapAddInvalidEntry()
{
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(LdapException::class);
$this->executeSearchQuery(1);
// The entry is missing a subject name
$entry = new Entry('cn=Charles Sarrazin,dc=symfony,dc=com', array(
'objectclass' => array(
'inetOrgPerson',
),
));
$em = $this->adapter->getEntryManager();
$em->add($entry);
}
/**
* @group functional
*/
public function testLdapUpdate()
{
$result = $this->executeSearchQuery(1);
$entry = $result[0];
$this->assertNull($entry->getAttribute('email'));
$em = $this->adapter->getEntryManager();
$em->update($entry);
$result = $this->executeSearchQuery(1);
$entry = $result[0];
$this->assertNull($entry->getAttribute('email'));
$entry->removeAttribute('email');
$em->update($entry);
$result = $this->executeSearchQuery(1);
$entry = $result[0];
$this->assertNull($entry->getAttribute('email'));
}
/**
* @group functional
*/
public function testLdapUnboundAdd()
{
$this->adapter = new Adapter($this->getLdapConfig());
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(NotBoundException::class);
$em = $this->adapter->getEntryManager();
$em->add(new Entry(''));
}
/**
* @group functional
*/
public function testLdapUnboundRemove()
{
$this->adapter = new Adapter($this->getLdapConfig());
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(NotBoundException::class);
$em = $this->adapter->getEntryManager();
$em->remove(new Entry(''));
}
/**
* @group functional
*/
public function testLdapUnboundUpdate()
{
$this->adapter = new Adapter($this->getLdapConfig());
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(NotBoundException::class);
$em = $this->adapter->getEntryManager();
$em->update(new Entry(''));
}
/**
* @return Collection|Entry[]
*/
private function executeSearchQuery($expectedResults = 1)
{
$results = $this
->adapter
->createQuery('dc=symfony,dc=com', '(objectclass=person)')
->execute()
;
$this->assertCount($expectedResults, $results);
return $results;
}
/**
* @group functional
*/
public function testLdapRename()
{
$result = $this->executeSearchQuery(1);
$entry = $result[0];
$entryManager = $this->adapter->getEntryManager();
$entryManager->rename($entry, 'cn=Kevin');
$result = $this->executeSearchQuery(1);
$renamedEntry = $result[0];
$this->assertEquals($renamedEntry->getAttribute('cn')[0], 'Kevin');
$oldRdn = $entry->getAttribute('cn')[0];
$entryManager->rename($renamedEntry, 'cn='.$oldRdn);
$this->executeSearchQuery(1);
}
/**
* @group functional
*/
public function testLdapRenameWithoutRemovingOldRdn()
{
$result = $this->executeSearchQuery(1);
$entry = $result[0];
$entryManager = $this->adapter->getEntryManager();
$entryManager->rename($entry, 'cn=Kevin', false);
$result = $this->executeSearchQuery(1);
$newEntry = $result[0];
$originalCN = $entry->getAttribute('cn')[0];
$this->assertContains($originalCN, $newEntry->getAttribute('cn'));
$entryManager->rename($newEntry, 'cn='.$originalCN);
$this->executeSearchQuery(1);
}
}

View File

@@ -0,0 +1,17 @@
# See slapd.conf(5) for details on configuration options.
include /etc/ldap/schema/core.schema
include /etc/ldap/schema/cosine.schema
include /etc/ldap/schema/inetorgperson.schema
include /etc/ldap/schema/nis.schema
pidfile /tmp/slapd/slapd.pid
argsfile /tmp/slapd/slapd.args
modulepath /usr/lib/openldap
database ldif
directory /tmp/slapd
suffix "dc=symfony,dc=com"
rootdn "cn=admin,dc=symfony,dc=com"
rootpw {SSHA}btWUi971ytYpVMbZLkaQ2A6ETh3VA0lL

View File

@@ -0,0 +1,4 @@
dn: dc=symfony,dc=com
objectClass: dcObject
objectClass: organizationalUnit
ou: Organization

View File

@@ -0,0 +1,26 @@
dn: cn=Fabien Potencier,dc=symfony,dc=com
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: person
objectClass: top
cn: Fabien Potencier
sn: fabpot
mail: fabpot@symfony.com
mail: fabien@potencier.com
ou: People
ou: Maintainers
ou: Founder
givenName: Fabien Potencier
description: Founder and project lead @Symfony
dn: ou=Components,dc=symfony,dc=com
objectclass: organizationalunit
ou: Components
dn: ou=Ldap,ou=Components,dc=symfony,dc=com
objectclass: organizationalunit
ou: Ldap
dn: ou=Ldap scoping,ou=Ldap,ou=Components,dc=symfony,dc=com
objectclass: organizationalunit
ou: Ldap scoping

View File

@@ -0,0 +1,229 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Ldap\Tests;
use Symfony\Component\Ldap\Adapter\CollectionInterface;
use Symfony\Component\Ldap\Adapter\QueryInterface;
use Symfony\Component\Ldap\Entry;
use Symfony\Component\Ldap\LdapClient;
use Symfony\Component\Ldap\LdapInterface;
/**
* @group legacy
*/
class LdapClientTest extends LdapTestCase
{
/** @var LdapClient */
private $client;
/** @var \PHPUnit_Framework_MockObject_MockObject */
private $ldap;
protected function setUp()
{
$this->ldap = $this->getMockBuilder(LdapInterface::class)->getMock();
$this->client = new LdapClient(null, 389, 3, false, false, false, $this->ldap);
}
public function testLdapBind()
{
$this->ldap
->expects($this->once())
->method('bind')
->with('foo', 'bar')
;
$this->client->bind('foo', 'bar');
}
public function testLdapEscape()
{
$this->ldap
->expects($this->once())
->method('escape')
->with('foo', 'bar', 'baz')
;
$this->client->escape('foo', 'bar', 'baz');
}
public function testLdapQuery()
{
$this->ldap
->expects($this->once())
->method('query')
->with('foo', 'bar', array('baz'))
;
$this->client->query('foo', 'bar', array('baz'));
}
public function testLdapFind()
{
$collection = $this->getMockBuilder(CollectionInterface::class)->getMock();
$collection
->expects($this->once())
->method('getIterator')
->will($this->returnValue(new \ArrayIterator(array(
new Entry('cn=qux,dc=foo,dc=com', array(
'cn' => array('qux'),
'dc' => array('com', 'foo'),
'givenName' => array('Qux'),
)),
new Entry('cn=baz,dc=foo,dc=com', array(
'cn' => array('baz'),
'dc' => array('com', 'foo'),
'givenName' => array('Baz'),
)),
))))
;
$query = $this->getMockBuilder(QueryInterface::class)->getMock();
$query
->expects($this->once())
->method('execute')
->will($this->returnValue($collection))
;
$this->ldap
->expects($this->once())
->method('query')
->with('dc=foo,dc=com', 'bar', array('filter' => 'baz'))
->willReturn($query)
;
$expected = array(
'count' => 2,
0 => array(
'count' => 3,
0 => 'cn',
'cn' => array(
'count' => 1,
0 => 'qux',
),
1 => 'dc',
'dc' => array(
'count' => 2,
0 => 'com',
1 => 'foo',
),
2 => 'givenname',
'givenname' => array(
'count' => 1,
0 => 'Qux',
),
'dn' => 'cn=qux,dc=foo,dc=com',
),
1 => array(
'count' => 3,
0 => 'cn',
'cn' => array(
'count' => 1,
0 => 'baz',
),
1 => 'dc',
'dc' => array(
'count' => 2,
0 => 'com',
1 => 'foo',
),
2 => 'givenname',
'givenname' => array(
'count' => 1,
0 => 'Baz',
),
'dn' => 'cn=baz,dc=foo,dc=com',
),
);
$this->assertEquals($expected, $this->client->find('dc=foo,dc=com', 'bar', 'baz'));
}
/**
* @dataProvider provideConfig
*/
public function testLdapClientConfig($args, $expected)
{
$reflObj = new \ReflectionObject($this->client);
$reflMethod = $reflObj->getMethod('normalizeConfig');
$reflMethod->setAccessible(true);
array_unshift($args, $this->client);
$this->assertEquals($expected, call_user_func_array(array($reflMethod, 'invoke'), $args));
}
/**
* @group functional
* @requires extension ldap
*/
public function testLdapClientFunctional()
{
$config = $this->getLdapConfig();
$ldap = new LdapClient($config['host'], $config['port']);
$ldap->bind('cn=admin,dc=symfony,dc=com', 'symfony');
$result = $ldap->find('dc=symfony,dc=com', '(&(objectclass=person)(ou=Maintainers))');
$con = @ldap_connect($config['host'], $config['port']);
@ldap_bind($con, 'cn=admin,dc=symfony,dc=com', 'symfony');
$search = @ldap_search($con, 'dc=symfony,dc=com', '(&(objectclass=person)(ou=Maintainers))', array('*'));
$expected = @ldap_get_entries($con, $search);
$this->assertSame($expected, $result);
}
public function provideConfig()
{
return array(
array(
array('localhost', 389, 3, true, false, false),
array(
'host' => 'localhost',
'port' => 389,
'encryption' => 'ssl',
'options' => array(
'protocol_version' => 3,
'referrals' => false,
),
),
),
array(
array('localhost', 389, 3, false, true, false),
array(
'host' => 'localhost',
'port' => 389,
'encryption' => 'tls',
'options' => array(
'protocol_version' => 3,
'referrals' => false,
),
),
),
array(
array('localhost', 389, 3, false, false, false),
array(
'host' => 'localhost',
'port' => 389,
'encryption' => 'none',
'options' => array(
'protocol_version' => 3,
'referrals' => false,
),
),
),
array(
array('localhost', 389, 3, false, false, false),
array(
'host' => 'localhost',
'port' => 389,
'encryption' => 'none',
'options' => array(
'protocol_version' => 3,
'referrals' => false,
),
),
),
);
}
}

84
vendor/symfony/ldap/Tests/LdapTest.php vendored Normal file
View File

@@ -0,0 +1,84 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Ldap\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Ldap\Adapter\AdapterInterface;
use Symfony\Component\Ldap\Adapter\ConnectionInterface;
use Symfony\Component\Ldap\Exception\DriverNotFoundException;
use Symfony\Component\Ldap\Ldap;
class LdapTest extends TestCase
{
/** @var \PHPUnit_Framework_MockObject_MockObject */
private $adapter;
/** @var Ldap */
private $ldap;
protected function setUp()
{
$this->adapter = $this->getMockBuilder(AdapterInterface::class)->getMock();
$this->ldap = new Ldap($this->adapter);
}
public function testLdapBind()
{
$connection = $this->getMockBuilder(ConnectionInterface::class)->getMock();
$connection
->expects($this->once())
->method('bind')
->with('foo', 'bar')
;
$this->adapter
->expects($this->once())
->method('getConnection')
->will($this->returnValue($connection))
;
$this->ldap->bind('foo', 'bar');
}
public function testLdapEscape()
{
$this->adapter
->expects($this->once())
->method('escape')
->with('foo', 'bar', 'baz')
;
$this->ldap->escape('foo', 'bar', 'baz');
}
public function testLdapQuery()
{
$this->adapter
->expects($this->once())
->method('createQuery')
->with('foo', 'bar', array('baz'))
;
$this->ldap->query('foo', 'bar', array('baz'));
}
/**
* @requires extension ldap
*/
public function testLdapCreate()
{
$ldap = Ldap::create('ext_ldap');
$this->assertInstanceOf(Ldap::class, $ldap);
}
public function testCreateWithInvalidAdapterName()
{
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(DriverNotFoundException::class);
Ldap::create('foo');
}
}

View File

@@ -0,0 +1,16 @@
<?php
namespace Symfony\Component\Ldap\Tests;
use PHPUnit\Framework\TestCase;
class LdapTestCase extends TestCase
{
protected function getLdapConfig()
{
return array(
'host' => getenv('LDAP_HOST'),
'port' => getenv('LDAP_PORT'),
);
}
}