diff --git a/src/Symfony/Component/Ldap/Adapter/ExtLdap/Connection.php b/src/Symfony/Component/Ldap/Adapter/ExtLdap/Connection.php index 8ba19ee73137..3c6262c467ad 100644 --- a/src/Symfony/Component/Ldap/Adapter/ExtLdap/Connection.php +++ b/src/Symfony/Component/Ldap/Adapter/ExtLdap/Connection.php @@ -12,7 +12,10 @@ namespace Symfony\Component\Ldap\Adapter\ExtLdap; use Symfony\Component\Ldap\Adapter\AbstractConnection; +use Symfony\Component\Ldap\Exception\AlreadyExistsException; use Symfony\Component\Ldap\Exception\ConnectionException; +use Symfony\Component\Ldap\Exception\ConnectionTimeoutException; +use Symfony\Component\Ldap\Exception\InvalidCredentialsException; use Symfony\Component\Ldap\Exception\LdapException; use Symfony\Component\OptionsResolver\Options; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -22,6 +25,10 @@ */ class Connection extends AbstractConnection { + private const LDAP_INVALID_CREDENTIALS = '0x31'; + private const LDAP_TIMEOUT = '0x55'; + private const LDAP_ALREADY_EXISTS = '0x44'; + /** @var bool */ private $bound = false; @@ -51,7 +58,16 @@ public function bind($dn = null, $password = null) } if (false === @ldap_bind($this->connection, $dn, $password)) { - throw new ConnectionException(ldap_error($this->connection)); + $error = ldap_error($this->connection); + switch (ldap_errno($this->connection)) { + case self::LDAP_INVALID_CREDENTIALS: + throw new InvalidCredentialsException($error); + case self::LDAP_TIMEOUT: + throw new ConnectionTimeoutException($error); + case self::LDAP_ALREADY_EXISTS: + throw new AlreadyExistsException($error); + } + throw new ConnectionException($error); } $this->bound = true; diff --git a/src/Symfony/Component/Ldap/Exception/AlreadyExistsException.php b/src/Symfony/Component/Ldap/Exception/AlreadyExistsException.php new file mode 100644 index 000000000000..51635037ac26 --- /dev/null +++ b/src/Symfony/Component/Ldap/Exception/AlreadyExistsException.php @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Ldap\Exception; + +/** + * AlreadyExistsException is thrown if the element already exists. + * + * @author Hamza Amrouche + */ +class AlreadyExistsException extends ConnectionException implements ExceptionInterface +{ +} diff --git a/src/Symfony/Component/Ldap/Exception/ConnectionTimeoutException.php b/src/Symfony/Component/Ldap/Exception/ConnectionTimeoutException.php new file mode 100644 index 000000000000..41533412ddb8 --- /dev/null +++ b/src/Symfony/Component/Ldap/Exception/ConnectionTimeoutException.php @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Ldap\Exception; + +/** + * ConnectionException is thrown if binding to ldap time out. + * + * @author Hamza Amrouche + */ +class ConnectionTimeoutException extends ConnectionException implements ExceptionInterface +{ +} diff --git a/src/Symfony/Component/Ldap/Exception/InvalidCredentialsException.php b/src/Symfony/Component/Ldap/Exception/InvalidCredentialsException.php new file mode 100644 index 000000000000..b5cffce9e9c0 --- /dev/null +++ b/src/Symfony/Component/Ldap/Exception/InvalidCredentialsException.php @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Ldap\Exception; + +/** + * ConnectionException is thrown if binding to ldap has been done with invalid credentials . + * + * @author Hamza Amrouche + */ +class InvalidCredentialsException extends ConnectionException implements ExceptionInterface +{ +} diff --git a/src/Symfony/Component/Ldap/Tests/Adapter/ExtLdap/LdapManagerTest.php b/src/Symfony/Component/Ldap/Tests/Adapter/ExtLdap/LdapManagerTest.php index 7b557eee7ef6..bca5b0ec8f96 100644 --- a/src/Symfony/Component/Ldap/Tests/Adapter/ExtLdap/LdapManagerTest.php +++ b/src/Symfony/Component/Ldap/Tests/Adapter/ExtLdap/LdapManagerTest.php @@ -15,6 +15,7 @@ use Symfony\Component\Ldap\Adapter\ExtLdap\Collection; use Symfony\Component\Ldap\Adapter\ExtLdap\UpdateOperation; use Symfony\Component\Ldap\Entry; +use Symfony\Component\Ldap\Exception\AlreadyExistsException; use Symfony\Component\Ldap\Exception\LdapException; use Symfony\Component\Ldap\Exception\NotBoundException; use Symfony\Component\Ldap\Exception\UpdateOperationException; @@ -75,6 +76,26 @@ public function testLdapAddInvalidEntry() $em->add($entry); } + /** + * @group functional + */ + public function testLdapAddDouble() + { + $this->expectException(AlreadyExistsException::class); + $this->executeSearchQuery(1); + + $entry = new Entry('cn=Elsa Amrouche,dc=symfony,dc=com', [ + 'sn' => ['eamrouche'], + 'objectclass' => [ + 'inetOrgPerson', + ], + ]); + + $em = $this->adapter->getEntryManager(); + $em->add($entry); + $em->add($entry); + } + /** * @group functional */