Skip to content

Commit d601edc

Browse files
committed
[Ldap] Add exception for mapping ldap errors
1 parent 519ba3c commit d601edc

File tree

5 files changed

+102
-1
lines changed

5 files changed

+102
-1
lines changed

src/Symfony/Component/Ldap/Adapter/ExtLdap/Connection.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111

1212
namespace Symfony\Component\Ldap\Adapter\ExtLdap;
1313

14+
use phpDocumentor\Reflection\Types\Self_;
1415
use Symfony\Component\Ldap\Adapter\AbstractConnection;
16+
use Symfony\Component\Ldap\Exception\AlreadyExistsException;
1517
use Symfony\Component\Ldap\Exception\ConnectionException;
18+
use Symfony\Component\Ldap\Exception\ConnectionTimeoutException;
19+
use Symfony\Component\Ldap\Exception\InvalidCredentialsException;
1620
use Symfony\Component\Ldap\Exception\LdapException;
1721
use Symfony\Component\OptionsResolver\Options;
1822
use Symfony\Component\OptionsResolver\OptionsResolver;
@@ -22,6 +26,10 @@
2226
*/
2327
class Connection extends AbstractConnection
2428
{
29+
private const LDAP_INVALID_CREDENTIALS = '0x31';
30+
private const LDAP_TIMEOUT = '0x55';
31+
private const LDAP_ALREADY_EXISTS = '0x44';
32+
2533
/** @var bool */
2634
private $bound = false;
2735

@@ -51,7 +59,16 @@ public function bind($dn = null, $password = null)
5159
}
5260

5361
if (false === @ldap_bind($this->connection, $dn, $password)) {
54-
throw new ConnectionException(ldap_error($this->connection));
62+
$error = ldap_error($this->connection);
63+
switch (ldap_errno($this->connection)) {
64+
case self::LDAP_INVALID_CREDENTIALS:
65+
throw new InvalidCredentialsException($error);
66+
case self::LDAP_TIMEOUT:
67+
throw new ConnectionTimeoutException($error);
68+
case self::LDAP_ALREADY_EXISTS:
69+
throw new AlreadyExistsException($error);
70+
}
71+
throw new ConnectionException($error);
5572
}
5673

5774
$this->bound = true;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Ldap\Exception;
13+
14+
/**
15+
* ConnectionException is throw if binding to ldap can not be established.
16+
*
17+
* @author Hamza Amrouche <hamza.simperfit@gmail.com>
18+
*/
19+
class AlreadyExistsException extends ConnectionException implements ExceptionInterface
20+
{
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Ldap\Exception;
13+
14+
/**
15+
* ConnectionException is throw if binding to ldap can not be established.
16+
*
17+
* @author Hamza Amrouche <hamza.simperfit@gmail.com>
18+
*/
19+
class ConnectionTimeoutException extends ConnectionException implements ExceptionInterface
20+
{
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Ldap\Exception;
13+
14+
/**
15+
* ConnectionException is throw if binding to ldap can not be established.
16+
*
17+
* @author Hamza Amrouche <hamza.simperfit@gmail.com>
18+
*/
19+
class InvalidCredentialsException extends ConnectionException implements ExceptionInterface
20+
{
21+
}

src/Symfony/Component/Ldap/Tests/Adapter/ExtLdap/LdapManagerTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Ldap\Adapter\ExtLdap\Collection;
1616
use Symfony\Component\Ldap\Adapter\ExtLdap\UpdateOperation;
1717
use Symfony\Component\Ldap\Entry;
18+
use Symfony\Component\Ldap\Exception\AlreadyExistsException;
1819
use Symfony\Component\Ldap\Exception\LdapException;
1920
use Symfony\Component\Ldap\Exception\NotBoundException;
2021
use Symfony\Component\Ldap\Exception\UpdateOperationException;
@@ -75,6 +76,26 @@ public function testLdapAddInvalidEntry()
7576
$em->add($entry);
7677
}
7778

79+
/**
80+
* @group functional
81+
*/
82+
public function testLdapAddDouble()
83+
{
84+
$this->expectException(AlreadyExistsException::class);
85+
$this->executeSearchQuery(1);
86+
87+
$entry = new Entry('cn=Elsa Amrouche,dc=symfony,dc=com', [
88+
'sn' => ['eamrouche'],
89+
'objectclass' => [
90+
'inetOrgPerson',
91+
],
92+
]);
93+
94+
$em = $this->adapter->getEntryManager();
95+
$em->add($entry);
96+
$em->add($entry);
97+
}
98+
7899
/**
79100
* @group functional
80101
*/

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy