diff options
author | Christophe Coevoet <stof@notk.org> | 2012-10-06 15:04:42 +0200 |
---|---|---|
committer | Christophe Coevoet <stof@notk.org> | 2012-10-06 15:04:42 +0200 |
commit | 2b47ade008e802ab838c739b401a7afd6a5eba4c (patch) | |
tree | df40f178a65f6cbaa65ab0d38e35cccc385a41d6 | |
parent | c415a8f6d04dc4f9647af8ef65f1f4a9c971a2e7 (diff) |
Moved more ldap configuration to the Configuration class
-rw-r--r-- | lib/Bdu/Configuration.php | 75 | ||||
-rw-r--r-- | lib/Bdu/Util/Ldap.php | 18 | ||||
-rw-r--r-- | test/Bdu/Tests/ConfigurationTest.php | 2 | ||||
-rw-r--r-- | test/Bdu/Tests/TestCase.php | 6 | ||||
-rw-r--r-- | test/Bdu/Tests/Util/LdapTest.php | 97 |
5 files changed, 119 insertions, 79 deletions
diff --git a/lib/Bdu/Configuration.php b/lib/Bdu/Configuration.php index 899283c..8d22e39 100644 --- a/lib/Bdu/Configuration.php +++ b/lib/Bdu/Configuration.php @@ -41,7 +41,11 @@ class Bdu_Configuration /** * Url du webservice */ - const WEBSERVICE_HOST = "https://webservice.campus.ecp.fr/"; + const WEBSERVICE_HOST = 'https://webservice.campus.ecp.fr/'; + + const LDAP_HOST = 'ldap.campus.ecp.fr'; + const LDAP_ROOT = 'dc=campus,dc=ecp,dc=fr'; + const LDAP_BASE_DN = 'ou=people,dc=campus,dc=ecp,dc=fr'; /** * @var Bdu_Log_LoggerInterface @@ -56,7 +60,12 @@ class Bdu_Configuration /** * @var string */ - private $binddn; + private $login; + + /** + * @var string + */ + private $accountType; /** * @var string @@ -64,6 +73,21 @@ class Bdu_Configuration private $credentials; /** + * @var string + */ + private $ldapRoot = self::LDAP_ROOT; + + /** + * @var string + */ + private $ldapBaseDn = self::LDAP_BASE_DN; + + /** + * @var string + */ + private $ldapHost = self::LDAP_HOST; + + /** * Spécifie un logger * * Pour supprimer le logger, il suffit d'appeler la méthode sans argument @@ -141,7 +165,8 @@ class Bdu_Configuration throw new Bdu_Exception_InvalidCredentials(); } - $this->binddn = sprintf("uid=%s,ou=%s", $bindlogin, $type); + $this->login = $bindlogin; + $this->accountType = $type; $this->credentials = $credentials; } @@ -151,7 +176,7 @@ class Bdu_Configuration */ public function getCredentials() { - if (null === $this->binddn || null === $this->credentials) { + if (null === $this->login || null === $this->accountType || null === $this->credentials) { if (null !== $this->logger) { $this->logger->err('Les identifiants doivent être renseignés avant de pouvoir faire une requête'); } @@ -159,6 +184,46 @@ class Bdu_Configuration throw new Bdu_Exception_UnsufficientConnectionParameters(); } - return array($this->binddn, $this->credentials); + return array(sprintf('uid=%s,ou=%s,%s', $this->login, $this->accountType, $this->ldapRoot), $this->credentials); + } + + /** + * @param string $ldapBaseDn + */ + public function setLdapBaseDn($ldapBaseDn) + { + $this->ldapBaseDn = $ldapBaseDn; + } + + /** + * @return string + */ + public function getLdapBaseDn() + { + return $this->ldapBaseDn; + } + + /** + * @param string $ldapRoot + */ + public function setLdapRoot($ldapRoot) + { + $this->ldapRoot = $ldapRoot; + } + + /** + * @param string $ldapHost + */ + public function setLdapHost($ldapHost) + { + $this->ldapHost = $ldapHost; + } + + /** + * @return string + */ + public function getLdapHost() + { + return $this->ldapHost; } } diff --git a/lib/Bdu/Util/Ldap.php b/lib/Bdu/Util/Ldap.php index 85c4c31..d57c707 100644 --- a/lib/Bdu/Util/Ldap.php +++ b/lib/Bdu/Util/Ldap.php @@ -27,10 +27,6 @@ */ class Bdu_Util_Ldap { - const ldaphost = "ldap.campus.ecp.fr"; - const basedn = "ou=people,dc=campus,dc=ecp,dc=fr"; - const baseroot = "dc=campus,dc=ecp,dc=fr"; - private $connection; /** @@ -81,7 +77,7 @@ class Bdu_Util_Ldap if (null !== $logger) { $logger->debug('Connexion au ldap'); } - $this->connection = @ldap_connect(self::ldaphost); + $this->connection = @ldap_connect($this->configuration->getLdapHost()); if (!$this->connection) { // @codeCoverageIgnoreStart if (null !== $logger) { @@ -91,9 +87,9 @@ class Bdu_Util_Ldap // @codeCoverageIgnoreEnd } - list($binddn, $credentials) = $this->configuration->getCredentials(); + list($bindDn, $credentials) = $this->configuration->getCredentials(); - $bind = @ldap_bind($this->connection, $binddn.",".self::baseroot, utf8_encode($credentials)); + $bind = @ldap_bind($this->connection, $bindDn, utf8_encode($credentials)); if (!$bind) { $error = @ldap_error($this->connection); if (null !== $logger) { @@ -115,10 +111,12 @@ class Bdu_Util_Ldap * * @param string $filter Filtre de recherche * @param array $attrs (Facultatif) Liste des attributs à retourner + * * @return array Tableau de résultat LDAP + * * @throws Bdu_Exception_LDAPSearchFailed si la recherche ldap échoue */ - public function search($filter, $attrs = array()) + public function search($filter, array $attrs = array()) { $this->connect(); $filter = $this->encoder->convert($filter); @@ -129,9 +127,9 @@ class Bdu_Util_Ldap } if (count($attrs) > 0) { - $search = @ldap_search($this->connection, self::basedn, $filter, $attrs); + $search = @ldap_search($this->connection, $this->configuration->getLdapBaseDn(), $filter, $attrs); } else { - $search = @ldap_search($this->connection, self::basedn, $filter); + $search = @ldap_search($this->connection, $this->configuration->getLdapBaseDn(), $filter); } if (!$search) { $error = @ldap_error($this->connection); diff --git a/test/Bdu/Tests/ConfigurationTest.php b/test/Bdu/Tests/ConfigurationTest.php index 06947cc..25a3abe 100644 --- a/test/Bdu/Tests/ConfigurationTest.php +++ b/test/Bdu/Tests/ConfigurationTest.php @@ -23,7 +23,7 @@ class Bdu_Tests_ConfigurationTest extends PHPUnit_Framework_TestCase $config = new Bdu_Configuration(); $config->setCredentials('11goreti', Bdu_Configuration::TYPE_PEOPLE, '1234poney'); - $this->assertSame(array('uid=11goreti,ou=people', '1234poney'), $config->getCredentials()); + $this->assertSame(array('uid=11goreti,ou=people,dc=campus,dc=ecp,dc=fr', '1234poney'), $config->getCredentials()); } public function testLogger() diff --git a/test/Bdu/Tests/TestCase.php b/test/Bdu/Tests/TestCase.php index e92ffb1..6270723 100644 --- a/test/Bdu/Tests/TestCase.php +++ b/test/Bdu/Tests/TestCase.php @@ -24,11 +24,11 @@ class Bdu_Tests_TestCase extends PHPUnit_Framework_TestCase $login = BDU_LDAP_LOGIN; } if (BDU_LDAP_TYPE == "people") { - return sprintf("uid=%s,ou=people", $login); + return sprintf("uid=%s,ou=people,%s", $login, Bdu_Configuration::LDAP_ROOT); } elseif (BDU_LDAP_TYPE == "group") { - return sprintf("uid=%s,ou=groups", $login); + return sprintf("uid=%s,ou=groups,%s", $login, Bdu_Configuration::LDAP_ROOT); } elseif (BDU_LDAP_TYPE == "appli") { - return sprintf("uid=%s,ou=applications", $login); + return sprintf("uid=%s,ou=applications,%s", $login, Bdu_Configuration::LDAP_ROOT); } throw new Bdu_Exception_InvalidCredentials(); } diff --git a/test/Bdu/Tests/Util/LdapTest.php b/test/Bdu/Tests/Util/LdapTest.php index fb2a30f..848ef41 100644 --- a/test/Bdu/Tests/Util/LdapTest.php +++ b/test/Bdu/Tests/Util/LdapTest.php @@ -21,18 +21,7 @@ class Bdu_Tests_Util_LdapTest extends Bdu_Tests_TestCase public function testSearch() { $logger = new Bdu_Log_ArrayLogger(Bdu_Log_AbstractLogger::DEBUG); - $config = $this->getMockBuilder('Bdu_Configuration') - ->disableOriginalConstructor() - ->getMock(); - $config->expects($this->once()) - ->method('getCredentials') - ->will($this->returnValue(array(self::getLdapBindDn(), self::getLdapCredentials()))); - $config->expects($this->any()) - ->method('getLogger') - ->will($this->returnValue($logger)); - $config->expects($this->any()) - ->method('getCharset') - ->will($this->returnValue('UTF-8')); + $config = $this->getMockConfiguration($logger); $connection = new Bdu_Util_Ldap($config, new Bdu_Util_Encoder($config)); @@ -48,18 +37,7 @@ class Bdu_Tests_Util_LdapTest extends Bdu_Tests_TestCase public function testEncoding() { $logger = new Bdu_Log_ArrayLogger(Bdu_Log_AbstractLogger::DEBUG); - $config = $this->getMockBuilder('Bdu_Configuration') - ->disableOriginalConstructor() - ->getMock(); - $config->expects($this->once()) - ->method('getCredentials') - ->will($this->returnValue(array(self::getLdapBindDn(), self::getLdapCredentials()))); - $config->expects($this->any()) - ->method('getLogger') - ->will($this->returnValue($logger)); - $config->expects($this->any()) - ->method('getCharset') - ->will($this->returnValue('ISO-8859-15')); + $config = $this->getMockConfiguration($logger, null, null, 'ISO-8859-15'); $connection = new Bdu_Util_Ldap($config, new Bdu_Util_Encoder($config)); @@ -76,18 +54,7 @@ class Bdu_Tests_Util_LdapTest extends Bdu_Tests_TestCase public function testBadCredentials() { $logger = new Bdu_Log_ArrayLogger(); - $config = $this->getMockBuilder('Bdu_Configuration') - ->disableOriginalConstructor() - ->getMock(); - $config->expects($this->once()) - ->method('getCredentials') - ->will($this->returnValue(array(self::getLdapBindDn('foo'), 'bar'))); - $config->expects($this->any()) - ->method('getLogger') - ->will($this->returnValue($logger)); - $config->expects($this->any()) - ->method('getCharset') - ->will($this->returnValue('UTF-8')); + $config = $this->getMockConfiguration($logger, 'foo', 'bar'); $connection = new Bdu_Util_Ldap($config, new Bdu_Util_Encoder($config)); try { @@ -105,18 +72,7 @@ class Bdu_Tests_Util_LdapTest extends Bdu_Tests_TestCase public function testConnectOnce() { $logger = new Bdu_Log_ArrayLogger(Bdu_Log_AbstractLogger::DEBUG); - $config = $this->getMockBuilder('Bdu_Configuration') - ->disableOriginalConstructor() - ->getMock(); - $config->expects($this->once()) - ->method('getCredentials') - ->will($this->returnValue(array(self::getLdapBindDn(), self::getLdapCredentials()))); - $config->expects($this->any()) - ->method('getLogger') - ->will($this->returnValue($logger)); - $config->expects($this->any()) - ->method('getCharset') - ->will($this->returnValue('UTF-8')); + $config = $this->getMockConfiguration($logger); $connection = new Bdu_Util_Ldap($config, new Bdu_Util_Encoder($config)); $connection->connect(); @@ -126,18 +82,7 @@ class Bdu_Tests_Util_LdapTest extends Bdu_Tests_TestCase public function testBadFilter() { $logger = new Bdu_Log_ArrayLogger(Bdu_Log_AbstractLogger::DEBUG); - $config = $this->getMockBuilder('Bdu_Configuration') - ->disableOriginalConstructor() - ->getMock(); - $config->expects($this->once()) - ->method('getCredentials') - ->will($this->returnValue(array(self::getLdapBindDn(), self::getLdapCredentials()))); - $config->expects($this->any()) - ->method('getLogger') - ->will($this->returnValue($logger)); - $config->expects($this->any()) - ->method('getCharset') - ->will($this->returnValue('UTF-8')); + $config = $this->getMockConfiguration($logger); $connection = new Bdu_Util_Ldap($config, new Bdu_Util_Encoder($config)); $this->setExpectedException('Bdu_Exception_LDAPSearchFailed'); @@ -153,4 +98,36 @@ class Bdu_Tests_Util_LdapTest extends Bdu_Tests_TestCase } $this->fail('a Bdu_Exception_LDAPSearchFailed is thrown if the ldap search failed'); } + + private function getMockConfiguration($logger, $login = null, $password = null, $charset = 'UTF-8') + { + if (null === $password) { + $password = self::getLdapCredentials(); + } + + $config = $this->getMockBuilder('Bdu_Configuration') + ->disableOriginalConstructor() + ->getMock(); + + $config->expects($this->once()) + ->method('getCredentials') + ->will($this->returnValue(array(self::getLdapBindDn($login), $password))); + $config->expects($this->any()) + ->method('getLogger') + ->will($this->returnValue($logger)); + $config->expects($this->any()) + ->method('getCharset') + ->will($this->returnValue($charset)); + $config->expects($this->any()) + ->method('getLdapHost') + ->will($this->returnValue(Bdu_Configuration::LDAP_HOST)); + $config->expects($this->any()) + ->method('getLdapRoot') + ->will($this->returnValue(Bdu_Configuration::LDAP_ROOT)); + $config->expects($this->any()) + ->method('getLdapBaseDn') + ->will($this->returnValue(Bdu_Configuration::LDAP_BASE_DN)); + + return $config; + } } |