Skip to content

Commit

Permalink
feature #35913 [LDAP] Add error code in exceptions generated by ldap …
Browse files Browse the repository at this point in the history
…(Victor Garcia)

This PR was squashed before being merged into the 5.1-dev branch.

Discussion
----------

[LDAP] Add error code in exceptions generated by ldap

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  |no
| Deprecations? | no
| Tickets       |
| License       | MIT
| Doc PR        |

This PR add the exception code returned by ldap PHP component to LdapException, and allow users a better way to detect the errors. Before this LdapException allways return code 0, and make neccesary evaluate the string returned to detect the error.

Commits
-------

b4c90f0 [LDAP] Add error code in exceptions generated by ldap
  • Loading branch information
fabpot committed Mar 2, 2020
2 parents 6429999 + b4c90f0 commit db46f3b
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/Symfony/Component/Ldap/Adapter/ExtLdap/EntryManager.php
Expand Up @@ -38,7 +38,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 new LdapException(sprintf('Could not add entry "%s": %s.', $entry->getDn(), ldap_error($con)), ldap_errno($con));
}

return $this;
Expand All @@ -52,7 +52,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 new LdapException(sprintf('Could not update entry "%s": %s.', $entry->getDn(), ldap_error($con)), ldap_errno($con));
}
}

Expand All @@ -64,7 +64,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 new LdapException(sprintf('Could not remove entry "%s": %s.', $entry->getDn(), ldap_error($con)), ldap_errno($con));
}
}

Expand All @@ -79,7 +79,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 new LdapException(sprintf('Could not add values to entry "%s", attribute %s: %s.', $entry->getDn(), $attribute, ldap_error($con)), ldap_errno($con));
}
}

Expand All @@ -94,7 +94,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 new LdapException(sprintf('Could not remove values from entry "%s", attribute %s: %s.', $entry->getDn(), $attribute, ldap_error($con)), ldap_errno($con));
}
}

Expand All @@ -106,7 +106,7 @@ public function rename(Entry $entry, string $newRdn, bool $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 new LdapException(sprintf('Could not rename entry "%s" to "%s": %s.', $entry->getDn(), $newRdn, ldap_error($con)), ldap_errno($con));
}
}

Expand All @@ -122,7 +122,7 @@ public function move(Entry $entry, string $newParent)
$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 new LdapException(sprintf('Could not move entry "%s" to "%s": %s.', $entry->getDn(), $newParent, ldap_error($con)), ldap_errno($con));
}
}

Expand Down Expand Up @@ -152,7 +152,7 @@ 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 new UpdateOperationException(sprintf('Error executing UpdateOperation on "%s": "%s".', $dn, ldap_error($this->getConnectionResource())), ldap_errno($con));
}
}

Expand Down

0 comments on commit db46f3b

Please sign in to comment.