implémenter reconnect()

This commit is contained in:
Jephté Clain 2024-05-16 11:46:37 +04:00
parent eeee4cfffc
commit 9084233540
2 changed files with 26 additions and 2 deletions

View File

@ -133,6 +133,27 @@ class LdapConn extends Parametrable implements ICloseable {
$this->conn = $this->tryConnect($binddn, $password, $controls, $this->conn); $this->conn = $this->tryConnect($binddn, $password, $controls, $this->conn);
} }
/**
* se reconnecter, mais seulement s'il y a une erreur sur la connection
*
* @return true si la reconnexion a effectivement eu lieu
*/
function reconnect(bool $force=false): bool {
if (!$force) {
try {
$this->_search(null, [
"attrs" => ["objectClass"],
"scope" => "base",
"suffix" => "",
])->first();
} catch (LdapException $e) {
$force = true;
}
}
if ($force) $this->connect();
return $force;
}
/** @return resource */ /** @return resource */
protected function conn() { protected function conn() {
if ($this->conn === null) $this->connect(); if ($this->conn === null) $this->connect();

View File

@ -132,6 +132,7 @@ class LdapSearch extends Parametrable implements IteratorAggregate {
/** @var array */ /** @var array */
protected $ppControls; protected $ppControls;
/** @throws LdapException */
function getIterator() { function getIterator() {
$conn = $this->conn; $conn = $this->conn;
$args = [$conn]; $args = [$conn];
@ -161,12 +162,12 @@ class LdapSearch extends Parametrable implements IteratorAggregate {
$rr = LdapException::check("search", $conn, $rr); $rr = LdapException::check("search", $conn, $rr);
try { try {
$er = ldap_first_entry($conn, $rr); $er = LdapException::check("first_entry", $conn, ldap_first_entry($conn, $rr));
while ($er !== false) { while ($er !== false) {
$dn = ldap_get_dn($conn, $er); $dn = ldap_get_dn($conn, $er);
$entry = ldap_get_attributes($conn, $er); $entry = ldap_get_attributes($conn, $er);
yield $dn => $entry; yield $dn => $entry;
$er = ldap_next_entry($conn, $er); $er = LdapException::check("next_entry", $conn, ldap_next_entry($conn, $er));
} }
} catch (StopException $e) { } catch (StopException $e) {
} finally { } finally {
@ -177,6 +178,8 @@ class LdapSearch extends Parametrable implements IteratorAggregate {
/** /**
* retourner la première entrée du résultat de la recherche ou null si la * retourner la première entrée du résultat de la recherche ou null si la
* recherche ne retourne aucun résultat * recherche ne retourne aucun résultat
*
* @throws LdapException
*/ */
function first(?string &$dn=null): ?array { function first(?string &$dn=null): ?array {
$it = $this->getIterator(); $it = $this->getIterator();