Permalink
Browse files

Merge branch '1.5.x'

  • Loading branch information...
1 parent f10e213 commit 97c63c27ed2931286c254e5270a5563400ed7278 @Maks3w committed Jun 18, 2012
Showing with 125 additions and 42 deletions.
  1. +12 −1 Ldap/LdapManager.php
  2. +5 −4 README.md
  3. +103 −37 Tests/Ldap/LdapManagerTest.php
  4. +5 −0 Tests/TestUser.php
View
@@ -92,7 +92,18 @@ protected function hydrate(LdapUserInterface $user, array $entry)
}
foreach ($this->params['attributes'] as $attr) {
- call_user_func(array($user, $attr['user_method']), $entry[$attr['ldap_attr']][0]);
+ $ldapValue = $entry[$attr['ldap_attr']];
+ $value = null;
+
+ if ((array_key_exists('count', $ldapValue) && $ldapValue['count'] == 1)
+ || !array_key_exists('count', $ldapValue)) {
+
+ $value = $ldapValue[0];
+ } else {
+ $value = array_slice($ldapValue, 1);
+ }
+
+ call_user_func(array($user, $attr['user_method']), $value);
}
$user->setDn($entry['dn']);
View
@@ -7,19 +7,20 @@ The bundle is mainly developed for unmanaged LDAP directories scenarios, so
you will able to retrieve users from LDAP and manage them using the [FOSUserBundle features](https://github.com/FriendsOfSymfony/FOSUserBundle/) (role management, guess users registration, etc) without the need to modify your corporate directory.
-Features included:
+**Features included:**
- Works with with [FOSUserBundle](https://github.com/FriendsOfSymfony/FOSUserBundle/)
- Customizable and extensible
- Unit tested
-**Note:** This bundle cannot work as standalone at this moment and requires an User Manager like `FOSUserBundle`
+**Note:** This bundle cannot work as standalone at this moment and requires an User Manager (For ex: `FOSUserBundle`)
**Versions and compatibilities:**
+
- [![Build Status](https://secure.travis-ci.org/Maks3w/FR3DLdapBundle.png?branch=1.5.x)](http://travis-ci.org/Maks3w/FR3DLdapBundle) `1.5.x` is compatible with Symfony 2.0.x and is recommended for stable projects.
- [![Build Status](https://secure.travis-ci.org/Maks3w/FR3DLdapBundle.png?branch=1.6.x)](http://travis-ci.org/Maks3w/FR3DLdapBundle) `1.6.x` is compatible with Symfony 2.1.x and has the same features than 1.5.x
- [![Build Status](https://secure.travis-ci.org/Maks3w/FR3DLdapBundle.png?branch=2.0.x)](http://travis-ci.org/Maks3w/FR3DLdapBundle) `2.0.x` is actually compatible with Symfony 2.0.x and have new features. This version is actually under development and is subject to changes.
-- [![Build Status](https://secure.travis-ci.org/Maks3w/FR3DLdapBundle.png?branch=master)](http://travis-ci.org/Maks3w/FR3DLdapBundle) `Master` is, at this moment, synced with 1.5.x but I encourage you fix your installation to the 1.5.x version branch unless you like the risk.
+- [![Build Status](https://secure.travis-ci.org/Maks3w/FR3DLdapBundle.png?branch=master)](http://travis-ci.org/Maks3w/FR3DLdapBundle) `Master` is, at this moment, synced with 1.5.x but I encourage fix your installation to the 1.5.x version branch unless you like the risk.
Documentation
-------------
@@ -44,7 +45,7 @@ This bundle is under the MIT license. See the complete license in the bundle:
Reporting an issue or a feature request
---------------------------------------
-Issues and feature requests are tracked in the [Github issue tracker](https://github.com/Maks3w/FR3DLdapBundle/issues).
+Issues and feature requests are tracked in the [GitHub issue tracker](https://github.com/Maks3w/FR3DLdapBundle/issues).
When reporting a bug, it may be a good idea to reproduce it in a basic project
built using the [Symfony Standard Edition](https://github.com/symfony/symfony-standard)
@@ -7,6 +7,9 @@
class LdapManagerTest extends \PHPUnit_Framework_TestCase
{
+ /** @var array */
+ protected $params;
+
/**
* @var \FR3D\LdapBundle\Driver\LdapConnectionInterface|\PHPUnit_Framework_MockObject_MockObject
*/
@@ -28,7 +31,7 @@ class LdapManagerTest extends \PHPUnit_Framework_TestCase
*/
protected function setUp()
{
- $params = array(
+ $this->params = array(
'baseDn' => 'ou=Groups,dc=example,dc=com',
'filter' => '(attr0=value0)',
'attributes' => array(
@@ -46,30 +49,20 @@ protected function setUp()
->method('createUser')
->will($this->returnValue(new TestUser()));
- $this->ldapManager = new LdapManager($this->connection, $this->userManager, $params);
+ $this->ldapManager = new LdapManager($this->connection, $this->userManager, $this->params);
}
/**
* @covers FR3D\LdapBundle\Ldap\LdapManager::__construct
*/
public function testConstruct()
{
- $params = array(
- 'baseDn' => 'ou=Groups,dc=example,dc=com',
- 'filter' => '(attr0=value0)',
- 'attributes' => array(
- array(
- 'ldap_attr' => 'uid',
- 'user_method' => 'setUsername',
- ),
- array(
- 'ldap_attr' => 'mail',
- 'user_method' => 'setEmail',
- ),
- ),
+ $this->params['attributes'][] = array(
+ 'ldap_attr' => 'mail',
+ 'user_method' => 'setEmail',
);
- $this->ldapManager = new LdapManager($this->connection, $this->userManager, $params);
+ $this->ldapManager = new LdapManager($this->connection, $this->userManager, $this->params);
$reflectionClass = new \ReflectionClass('FR3D\LdapBundle\Ldap\LdapManager');
$propertyLdapAttributes = $reflectionClass->getProperty('ldapAttributes');
@@ -89,12 +82,24 @@ public function testFindUserByUsername()
{
$username = 'test_username';
- $entry = array('count' => 1, array('dn' => 'ou=group, dc=host, dc=foo', 'uid' => array('test_username')));
+ $entries = array(
+ 'count' => 1,
+ array(
+ 'dn' => 'ou=group, dc=host, dc=foo',
+ 'uid' => array(
+ 'count' => 1,
+ 0 => $username,
+ ),
+ ),
+ );
- $this->connection->expects($this->once())
- ->method('search')
- ->with($this->equalTo('ou=Groups,dc=example,dc=com'), $this->equalTo('(&(attr0=value0)(uid=test_username))'), $this->equalTo(array('uid')))
- ->will($this->returnValue($entry));
+ $this->connection
+ ->expects($this->once())
+ ->method('search')
+ ->with($this->equalTo('ou=Groups,dc=example,dc=com'),
+ $this->equalTo('(&(attr0=value0)(uid=test_username))'),
+ $this->equalTo(array('uid')))
+ ->will($this->returnValue($entries));
$resultUser = $this->ldapManager->findUserByUsername($username);
@@ -106,17 +111,31 @@ public function testFindUserByUsername()
*/
public function testFindUserBy()
{
- $user = new TestUser();
- $user->setUsername('test_username');
+ $username = 'test_username';
- $entry = array('count' => 1, array('dn' => 'ou=group, dc=host, dc=foo', 'uid' => array('test_username')));
+ $user = new TestUser();
+ $user->setUsername($username);
+
+ $entries = array(
+ 'count' => 1,
+ array(
+ 'dn' => 'ou=group, dc=host, dc=foo',
+ 'uid' => array(
+ 'count' => 1,
+ 0 => $username,
+ ),
+ ),
+ );
- $this->connection->expects($this->once())
- ->method('search')
- ->with($this->equalTo('ou=Groups,dc=example,dc=com'), $this->equalTo('(&(attr0=value0)(uid=test_username))'), $this->equalTo(array('uid')))
- ->will($this->returnValue($entry));
+ $this->connection
+ ->expects($this->once())
+ ->method('search')
+ ->with($this->equalTo('ou=Groups,dc=example,dc=com'),
+ $this->equalTo('(&(attr0=value0)(uid=test_username))'),
+ $this->equalTo(array('uid')))
+ ->will($this->returnValue($entries));
- $criteria = array('uid' => 'test_username');
+ $criteria = array('uid' => 'test_username');
$resultUser = $this->ldapManager->findUserBy($criteria);
$this->assertEquals($user->getUsername(), $resultUser->getUsername());
@@ -132,8 +151,8 @@ public function testBuildFilter()
$method->setAccessible(true);
$criteria = array(
- 'attr1' => 'value1',
- 'attr2' => 'value2',
+ 'attr1' => 'value1',
+ 'attr2' => 'value2',
);
$expected = '(&(attr0=value0)(attr1=value1)(attr2=value2))';
@@ -145,20 +164,63 @@ public function testBuildFilter()
*/
public function testHydrate()
{
+ $username = 'test_username';
+
$reflectionClass = new \ReflectionClass('FR3D\LdapBundle\Ldap\LdapManager');
$method = $reflectionClass->getMethod('hydrate');
$method->setAccessible(true);
$user = new TestUser();
$entry = array(
- 'dn' => 'ou=group, dc=host, dc=foo',
- 'uid' => array('test_username')
+ 'dn' => 'ou=group, dc=host, dc=foo',
+ 'count' => 1,
+ 'uid' => array(
+ 'count' => 1,
+ 0 => $username,
+ ),
+ );
+
+ $method->invoke($this->ldapManager, $user, $entry);
+
+ $this->assertEquals($username, $user->getUsername());
+ $this->assertTrue($user->isEnabled());
+ }
+
+ /**
+ * @covers FR3D\LdapBundle\Ldap\LdapManager::hydrate
+ */
+ public function testHydrateArray()
+ {
+ $this->params['attributes'] = array(
+ array(
+ 'ldap_attr' => 'roles',
+ 'user_method' => 'setRoles',
+ ),
+ );
+
+ $user = new TestUser();
+ $roles = array(
+ 'count' => 3,
+ 0 => 'ROLE1',
+ 1 => 'ROLE2',
+ 2 => 'ROLE3',
+ );
+
+ $entry = array(
+ 'dn' => 'ou=group, dc=host, dc=foo',
+ 'roles' => $roles,
);
+ $this->ldapManager = new LdapManager($this->connection, $this->userManager, $this->params);
+
+ $reflectionClass = new \ReflectionClass('FR3D\LdapBundle\Ldap\LdapManager');
+ $method = $reflectionClass->getMethod('hydrate');
+ $method->setAccessible(true);
+
$method->invoke($this->ldapManager, $user, $entry);
- $this->assertEquals('test_username', $user->getUsername());
+ $this->assertEquals(array_slice($roles, 1), $user->getRoles());
$this->assertTrue($user->isEnabled());
}
@@ -167,15 +229,19 @@ public function testHydrate()
*/
public function testBind()
{
+ $dn = 'dn=test_username';
+ $password = 'password';
+
$user = new TestUser();
- $user->setDn('dn=test_username');
+ $user->setDn($dn);
$this->connection->expects($this->once())
->method('bind')
- ->with($this->equalTo('dn=test_username'), $this->equalTo('password'))
+ ->with($this->equalTo($dn),
+ $this->equalTo($password))
->will($this->returnValue(true));
- $this->assertTrue($this->ldapManager->bind($user, 'password'));
+ $this->assertTrue($this->ldapManager->bind($user, $password));
}
public function testFilterEscapeBasicOperation()
View
@@ -86,6 +86,11 @@ public function setLocked($locked)
$this->locked = $locked;
}
+ public function setRoles($roles)
+ {
+ $this->roles = $roles;
+ }
+
/**
* {@inheritDoc}
*/

0 comments on commit 97c63c2

Please sign in to comment.