Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 792fdb4

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

File tree

5 files changed

+101
-1
lines changed

5 files changed

+101
-1
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
namespace Symfony\Component\Ldap\Adapter\ExtLdap;
1313

1414
use Symfony\Component\Ldap\Adapter\AbstractConnection;
15+
use Symfony\Component\Ldap\Exception\AlreadyExistsException;
1516
use Symfony\Component\Ldap\Exception\ConnectionException;
17+
use Symfony\Component\Ldap\Exception\ConnectionTimeoutException;
18+
use Symfony\Component\Ldap\Exception\InvalidCredentialsException;
1619
use Symfony\Component\Ldap\Exception\LdapException;
1720
use Symfony\Component\OptionsResolver\Options;
1821
use Symfony\Component\OptionsResolver\OptionsResolver;
@@ -22,6 +25,10 @@
2225
*/
2326
class Connection extends AbstractConnection
2427
{
28+
private const LDAP_INVALID_CREDENTIALS = '0x31';
29+
private const LDAP_TIMEOUT = '0x55';
30+
private const LDAP_ALREADY_EXISTS = '0x44';
31+
2532
/** @var bool */
2633
private $bound = false;
2734

@@ -51,7 +58,16 @@ public function bind($dn = null, $password = null)
5158
}
5259

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

5773
$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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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)