8000 [Ldap] Add LDAP error codes to exceptions by dontub · Pull Request #33339 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Ldap] Add LDAP error codes to exceptions #33339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Symfony/Component/Ldap/Adapter/ExtLdap/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Component\Ldap\Adapter\ExtLdap;

use Symfony\Component\Ldap\Adapter\AdapterInterface;
use Symfony\Component\Ldap\Exception\LdapException;
use Symfony\Component\Ldap\Exception\ExtensionNotLoadedException;

/**
* @author Charles Sarrazin <charles@sarraz.in>
Expand All @@ -26,7 +26,7 @@ class Adapter implements AdapterInterface
public function __construct(array $config = [])
{
if (!\extension_loaded('ldap')) {
throw new LdapException('The LDAP PHP extension is not enabled.');
throw new ExtensionNotLoadedException('The LDAP PHP extension is not enabled.');
}

$this->config = $config;
Expand Down
16 changes: 8 additions & 8 deletions src/Symfony/Component/Ldap/Adapter/ExtLdap/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ public function count()
$searches = $this->search->getResources();
$count = 0;
foreach ($searches as $search) {
$searchCount = ldap_count_entries($con, $search);
$searchCount = @ldap_count_entries($con, $search);
if (false === $searchCount) {
throw new LdapException(sprintf('Error while retrieving entry count: %s.', ldap_error($con)));
throw LdapException::create('Error while retrieving entry count: [{errorCode}] {errorMsg}.', ldap_errno($con));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not doing this in Symfony, can you please revert the ::create() method?

}
$count += $searchCount;
}
Expand All @@ -73,10 +73,10 @@ public function getIterator()
$con = $this->connection->getResource();
$searches = $this->search->getResources();
foreach ($searches as $search) {
$current = ldap_first_entry($con, $search);
$current = @ldap_first_entry($con, $search);

if (false === $current) {
throw new LdapException(sprintf('Could not rewind entries array: %s.', ldap_error($con)));
throw LdapException::create('Could not rewind entries array: [{errorCode}] {errorMsg}.', ldap_errno($con));
}

yield $this->getSingleEntry($con, $current);
Expand Down Expand Up @@ -120,18 +120,18 @@ public function offsetUnset($offset)

private function getSingleEntry($con, $current): Entry
{
$attributes = ldap_get_attributes($con, $current);
$attributes = @ldap_get_attributes($con, $current);

if (false === $attributes) {
throw new LdapException(sprintf('Could not fetch attributes: %s.', ldap_error($con)));
throw LdapException::create('Could not fetch attributes: [{errorCode}] {errorMsg}.', ldap_errno($con));
}

$attributes = $this->cleanupAttributes($attributes);

$dn = ldap_get_dn($con, $current);
$dn = @ldap_get_dn($con, $current);

if (false === $dn) {
throw new LdapException(sprintf('Could not fetch DN: %s.', ldap_error($con)));
throw LdapException::create('Could not fetch DN: [{errorCode}] {errorMsg}.', ldap_errno($con));
}

return new Entry($dn, $attributes);
Expand Down
26 changes: 12 additions & 14 deletions src/Symfony/Component/Ldap/Adapter/ExtLdap/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ public function bind($dn = null, $password = null)
}

if (false === @ldap_bind($this->connection, $dn, $password)) {
$error = ldap_error($this->connection);
switch (ldap_errno($this->connection)) {
$errorCode = ldap_errno($this->connection);
switch ($errorCode) {
case self::LDAP_INVALID_CREDENTIALS:
throw new InvalidCredentialsException($error);
throw InvalidCredentialsException::create('{errorMsg}', $errorCode);
case self::LDAP_TIMEOUT:
throw new ConnectionTimeoutException($error);
throw ConnectionTimeoutException::create('{errorMsg}', $errorCode);
case self::LDAP_ALREADY_EXISTS:
throw new AlreadyExistsException($error);
throw AlreadyExistsException::create('{errorMsg}', $errorCode);
}
throw new ConnectionException($error);
throw ConnectionException::create('{errorMsg}', $errorCode);
}

$this->bound = true;
Expand All @@ -88,14 +88,14 @@ public function getResource()
public function setOption($name, $value)
{
if (!@ldap_set_option($this->connection, ConnectionOptions::getOption($name), $value)) {
throw new LdapException(sprintf('Could not set value "%s" for option "%s".', $value, $name));
throw LdapException::create(sprintf('Could not set value "%s" for option "%s": [{errorCode}] {errorMsg}.', $value, $name), ldap_errno($this->connection));
}
}

public function getOption($name)
{
if (!@ldap_get_option($this->connection, ConnectionOptions::getOption($name), $ret)) {
throw new LdapException(sprintf('Could not retrieve value for option "%s".', $name));
throw LdapException::create(sprintf('Could not retrieve value for option "%s": [{errorCode}] {errorMsg}.', $name), ldap_errno($this->connection));
}

return $ret;
Expand Down Expand Up @@ -135,19 +135,17 @@ private function connect()
$this->setOption($name, $value);
}

if (false === $this->connection) {
throw new LdapException(sprintf('Could not connect to Ldap server: %s.', ldap_error($this->connection)));
}

if ('tls' === $this->config['encryption'] && false === @ldap_start_tls($this->connection)) {
throw new LdapException(sprintf('Could not initiate TLS connection: %s.', ldap_error($this->connection)));
throw LdapException::create('Could not initiate TLS connection: [{errorCode}] {errorMsg}.', ldap_errno($this->connection));
}
}

private function disconnect()
{
if ($this->connection && \is_resource($this->connection)) {
ldap_unbind($this->connection);
if (!@ldap_unbind($this->connection)) {
throw LdapException::create('Could not unbind from LDAP: [{errorCode}] {errorMsg}', ldap_errno($this->connection));
}
}

$this->connection = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Component\Ldap\Adapter\ExtLdap;

use Symfony\Component\Ldap\Exception\LdapException;
use Symfony\Component\Ldap\Exception\UnexpectedValueException;

/**
* A class representing the Ldap extension's options, which can be used with
Expand Down Expand Up @@ -71,15 +71,15 @@ public static function getOptionName(string $name): string
* Fetches an option's corresponding constant value from an option name.
* The option name can either be in snake or camel case.
*
* @throws LdapException
* @throws UnexpectedValueException
*/
public static function getOption(string $name): int
{
// Convert
$constantName = self::getOptionName($name);

if (!\defined($constantName)) {
throw new LdapException(sprintf('Unknown option "%s".', $name));
throw new UnexpectedValueException(sprintf('Unknown option "%s".', $name));
}

return \constant($constantName);
Expand Down
24 changes: 13 additions & 11 deletions src/Symfony/Component/Ldap/Adapter/ExtLdap/EntryManager.php
use Symfony\Component\Ldap\Exception\LdapException;
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Ldap\Adapter\EntryManagerInterface;
use Symfony\Component\Ldap\Entry;
use Symfony\Component\Ldap\Exception\MalformedDistinguishedNameException;
use Symfony\Component\Ldap\Exception\NotBoundException;
use Symfony\Component\Ldap\Exception\UpdateOperationException;

Expand All @@ -38,7 +39,7 @@ public function add(Entry $entry)
$con = $this->getConnectionResource();

if (!@ldap_add($con, $entry->getDn(), $entry->getAttributes())) {
throw new LdapException(sprintf('Could not add entry "%s": %s.', $entry->getDn(), ldap_error($con)));
throw LdapException::create(sprintf('Could not add entry "%s": [{errorCode}] {errorMsg}.', $entry->getDn()), ldap_errno($con));
}

return $this;
Expand All @@ -52,7 +53,7 @@ public function update(Entry $entry)
$con = $this->getConnectionResource();

if (!@ldap_modify($con, $entry->getDn(), $entry->getAttributes())) {
throw new LdapException(sprintf('Could not update entry "%s": %s.', $entry->getDn(), ldap_error($con)));
throw LdapException::create(sprintf('Could not update entry "%s": [{errorCode}] {errorMsg}.', $entry->getDn()), ldap_errno($con));
}
}

Expand All @@ -64,7 +65,7 @@ public function remove(Entry $entry)
$con = $this->getConnectionResource();

if (!@ldap_delete($con, $entry->getDn())) {
throw new LdapException(sprintf('Could not remove entry "%s": %s.', $entry->getDn(), ldap_error($con)));
throw LdapException::create(sprintf('Could not remove entry "%s": [{errorCode}] {errorMsg}.', $entry->getDn()), ldap_errno($con));
}
}

Expand All @@ -79,7 +80,7 @@ public function addAttributeValues(Entry $entry, string $attribute, array $value
$con = $this->getConnectionResource();

if (!@ldap_mod_add($con, $entry->getDn(), [$attribute => $values])) {
throw new LdapException(sprintf('Could not add values to entry "%s", attribute %s: %s.', $entry->getDn(), $attribute, ldap_error($con)));
throw LdapException::create(sprintf('Could not add values to entry "%s", attribute "%s": [{errorCode}] {errorMsg}.', $entry->getDn(), $attribute), ldap_errno($con));
}
}

Expand All @@ -94,7 +95,7 @@ public function removeAttributeValues(Entry $entry, string $attribute, array $va
$con = $this->getConnectionResource();

if (!@ldap_mod_del($con, $entry->getDn(), [$attribute => $values])) {
throw new LdapException(sprintf('Could not remove values from entry "%s", attribute %s: %s.', $entry->getDn(), $attribute, ldap_error($con)));
throw LdapException::create(sprintf('Could not remove values from entry "%s", attribute "%s": [{errorCode}] {errorMsg}.', $entry->getDn(), $attribute), ldap_errno($con));
}
}

Expand All @@ -106,23 +107,24 @@ public function rename(Entry $entry, $newRdn, $removeOldRdn = true)
$con = $this->getConnectionResource();

if (!@ldap_rename($con, $entry->getDn(), $newRdn, null, $removeOldRdn)) {
throw new LdapException(sprintf('Could not rename entry "%s" to "%s": %s.', $entry->getDn(), $newRdn, ldap_error($con)));
throw LdapException::create(sprintf('Could not rename entry "%s" to "%s": [{errorCode}] {errorMsg}.', $entry->getDn(), $newRdn), ldap_errno($con));
}
}

/**
* Moves an entry on the Ldap server.
*
* @throws NotBoundException if the connection has not been previously bound
* @throws LdapException if an error is thrown during the rename operation
* @throws NotBoundException if the connection has not been previously bound
* @throws LdapException if an error is thrown during the rename operation
* @throws MalformedDistinguishedNameException if entry contains a malformed DN
*/
public function move(Entry $entry, string $newParent)
{
$con = $this->getConnectionResource();
$rdn = $this->parseRdnFromEntry($entry);
// deleteOldRdn does not matter here, since the Rdn will not be changing in the move.
if (!@ldap_rename($con, $entry->getDn(), $rdn, $newParent, true)) {
throw new LdapException(sprintf('Could not move entry "%s" to "%s": %s.', $entry->getDn(), $newParent, ldap_error($con)));
throw LdapException::create(sprintf('Could not move entry "%s" to "%s": [{errorCode}] {errorMsg}.', $entry->getDn(), $newParent), ldap_errno($con));
}
}

Expand Down Expand Up @@ -152,14 +154,14 @@ public function applyOperations(string $dn, iterable $operations): void
}

if (!@ldap_modify_batch($this->getConnectionResource(), $dn, $operationsMapped)) {
throw new UpdateOperationException(sprintf('Error executing UpdateOperation on "%s": "%s".', $dn, ldap_error($this->getConnectionResource())));
throw UpdateOperationException::create(sprintf('Error executing UpdateOperation on "%s": [{errorCode}] {errorMsg}.', $dn), ldap_errno($this->getConnectionResource()));
}
}

private function parseRdnFromEntry(Entry $entry): string
{
if (!preg_match('/^([^,]+),/', $entry->getDn(), $matches)) {
throw new LdapException(sprintf('Entry "%s" malformed, could not parse RDN.', $entry->getDn()));
throw new MalformedDistinguishedNameException(sprintf('Entry "%s" malformed, could not parse RDN.', $entry->getDn()));
}

return $matches[1];
Expand Down
22 changes: 12 additions & 10 deletions src/Symfony/Component/Ldap/Adapter/ExtLdap/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Ldap\Adapter\ExtLdap;

use Symfony\Component\Ldap\Adapter\AbstractQuery;
use Symfony\Component\Ldap\Exception\DomainException;
use Symfony\Component\Ldap\Exception\LdapException;
use Symfony\Component\Ldap\Exception\NotBoundException;

Expand Down Expand Up @@ -48,8 +49,8 @@ public function __destruct()
if (false === $result || null === $result) {
continue;
}
if (!ldap_free_result($result)) {
throw new LdapException(sprintf('Could not free results: %s.', ldap_error($con)));
if (!@ldap_free_result($result)) {
throw LdapException::create('Could not free results: [{errorCode}] {errorMsg}.', ldap_errno($con));
}
}
$this->results = null;
Expand Down Expand Up @@ -80,7 +81,7 @@ public function execute()
$func = 'ldap_search';
break;
default:
throw new LdapException(sprintf('Could not search in scope "%s".', $this->options['scope']));
throw new DomainException(sprintf('Could not search in scope "%s".', $this->options['scope']));
}

$itemsLeft = $maxItems = $this->options['maxItems'];
Expand All @@ -97,7 +98,9 @@ public function execute()
$cookie = '';
do {
if ($pageControl) {
ldap_control_paged_result($con, $pageSize, true, $cookie);
if (!@ldap_control_paged_result($con, $pageSize, true, $cookie)) {
throw LdapException::create('Could not send the LDAP pagination control: [{errorCode}] {errorMsg}.', ldap_errno($con));
}
}
$sizeLimit = $itemsLeft;
if ($pageSize > 0 && $sizeLimit >= $pageSize) {
Expand All @@ -115,15 +118,12 @@ public function execute()
);

if (false === $search) {
$ldapError = '';
if ($errno = ldap_errno($con)) {
$ldapError = sprintf(' LDAP error was [%d] %s', $errno, ldap_error($con));
}
$errorCode = ldap_errno($con);
if ($pageControl) {
$this->resetPagination();
}

throw new LdapException(sprintf('Could not complete search with dn "%s", query "%s" and filters "%s".%s', $this->dn, $this->query, implode(',', $this->options['filter']), $ldapError));
throw LdapException::create(sprintf('Could not complete search with dn "%s", query "%s" and filters "%s": [{errorCode}] {errorMsg}.', $this->dn, $this->query, implode(',', $this->options['filter'])), $errorCode);
}

$this->results[] = $search;
Expand All @@ -133,7 +133,9 @@ public function execute()
break;
}
if ($pageControl) {
ldap_control_paged_result_response($con, $search, $cookie);
if (!@ldap_control_paged_result_response($con, $search, $cookie)) {
throw LdapException::create('Could not retrieve the LDAP pagination cookie: [{errorCode}] {errorMsg}.', ldap_errno($con));
}
}
} while (null !== $cookie && '' !== $cookie);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Component\Ldap\Adapter\ExtLdap;

use Symfony\Component\Ldap\Exception\UpdateOperationException;
use Symfony\Component\Ldap\Exception\UnexpectedValueException;

class UpdateOperation
{
Expand All @@ -30,15 +30,15 @@ class UpdateOperation
* @param int $operationType An LDAP_MODIFY_BATCH_* constant
* @param string $attribute The attribute to batch modify on
*
* @throws UpdateOperationException on consistency errors during construction
* @throws UnexpectedValueException on consistency errors during construction
*/
public function __construct(int $operationType, string $attribute, ?array $values)
{
if (!\in_array($operationType, $this->validOperationTypes, true)) {
throw new UpdateOperationException(sprintf('"%s" is not a valid modification type.', $operationType));
throw new UnexpectedValueException(sprintf('"%s" is not a valid modification type.', $operationType));
}
if (LDAP_MODIFY_BATCH_REMOVE_ALL === $operationType && null !== $values) {
throw new UpdateOperationException(sprintf('$values must be null for LDAP_MODIFY_BATCH_REMOVE_ALL operation, "%s" given.', \gettype($values)));
throw new UnexpectedValueException(sprintf('$values must be null for LDAP_MODIFY_BATCH_REMOVE_ALL operation, "%s" given.', \gettype($values)));
}

2851 $this->operationType = $operationType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
*
* @author Grégoire Pineau <lyrixx@lyrixx.info>
*/
class ConnectionException extends \RuntimeException implements ExceptionInterface
class ConnectionException extends LdapException implements ExceptionInterface
{
}
16 changes: 16 additions & 0 deletions src/Symfony/Component/Ldap/Exception/DomainException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?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;

class DomainException extends \DomainException implements ExceptionInterface
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Component\Ldap\Exception;

/**
* LdapException is thrown if php ldap module is not loaded.
* DriverNotFoundException is thrown if adapter is not found in adapter map.
*
* @author Charles Sarrazin <charles@sarraz.in>
*/
Expand Down
Loading
0