[Ldap] Add exception for mapping ldap errors

This commit is contained in:
Amrouche Hamza 2019-05-19 21:01:28 +02:00
parent 519ba3cddb
commit 1b29cb1a5f
No known key found for this signature in database
GPG Key ID: E45A3DA456145BC1
5 changed files with 101 additions and 1 deletions

View File

@ -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 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
*/
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 @@ class Connection extends AbstractConnection
}
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;

View File

@ -0,0 +1,21 @@
<?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\Exception;
/**
* AlreadyExistsException is thrown if the element already exists.
*
* @author Hamza Amrouche <hamza.simperfit@gmail.com>
*/
class AlreadyExistsException extends ConnectionException implements ExceptionInterface
{
}

View File

@ -0,0 +1,21 @@
<?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\Exception;
/**
* ConnectionException is thrown if binding to ldap time out.
*
* @author Hamza Amrouche <hamza.simperfit@gmail.com>
*/
class ConnectionTimeoutException extends ConnectionException implements ExceptionInterface
{
}

View File

@ -0,0 +1,21 @@
<?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\Exception;
/**
* ConnectionException is thrown if binding to ldap has been done with invalid credentials .
*
* @author Hamza Amrouche <hamza.simperfit@gmail.com>
*/
class InvalidCredentialsException extends ConnectionException implements ExceptionInterface
{
}

View File

@ -15,6 +15,7 @@ use Symfony\Component\Ldap\Adapter\ExtLdap\Adapter;
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 @@ class LdapManagerTest extends LdapTestCase
$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
*/