LSldap: fix search & getNumberResult methods to use basedn from configuration instead of empty one.

This commit is contained in:
Benjamin Renard 2020-12-22 19:43:50 +01:00
parent 14352da6bd
commit 89ecbb8a5c

View file

@ -136,19 +136,32 @@ class LSldap extends LSlog_staticLoggerClass {
* et leur valeur (valeur). * et leur valeur (valeur).
*/ */
public static function search($filter, $basedn=NULL, $params=array()) { public static function search($filter, $basedn=NULL, $params=array()) {
$filterstr = (is_a($filter, 'Net_LDAP2_Filter')?$filter->as_string():$filter);
if (is_empty($basedn)) {
$basedn = self :: getConfig('basedn');
if (is_empty($basedn)) {
LSerror :: addErrorCode('LSldap_08');
return;
}
self :: log_debug("LSldap::search($filterstr): empty basedn provided, use basedn from configuration: ".varDump($basedn));
}
self :: log_trace("LSldap::search($filterstr, $basedn): run search with parameters: ".varDump($params));
$ret = self :: $cnx -> search($basedn, $filter, $params); $ret = self :: $cnx -> search($basedn, $filter, $params);
if (Net_LDAP2::isError($ret)) { if (Net_LDAP2::isError($ret)) {
LSerror :: addErrorCode('LSldap_02', $ret -> getMessage()); LSerror :: addErrorCode('LSldap_02', $ret -> getMessage());
return; return;
} }
self :: log_debug("LSldap::search() : return ".$ret->count()." objet(s)"); self :: log_debug("LSldap::search($filterstr, $basedn) : return ".$ret->count()." objet(s)");
$retInfos = array(); $retInfos = array();
foreach($ret as $dn => $entry) { foreach($ret as $dn => $entry) {
if (!$entry instanceof Net_LDAP2_Entry) { if (!$entry instanceof Net_LDAP2_Entry) {
LSerror :: addErrorCode('LSldap_02', "LDAP search return an ".get_class($entry).". object"); LSerror :: addErrorCode('LSldap_02', "LDAP search return an ".get_class($entry).". object");
continue; continue;
} }
$retInfos[]=array('dn' => $dn, 'attrs' => $entry -> getValues()); $retInfos[] = array(
'dn' => $dn,
'attrs' => $entry -> getValues()
);
} }
return $retInfos; return $retInfos;
} }
@ -172,12 +185,24 @@ class LSldap extends LSlog_staticLoggerClass {
public static function getNumberResult($filter, $basedn=NULL, $params=array()) { public static function getNumberResult($filter, $basedn=NULL, $params=array()) {
if (empty($filter)) if (empty($filter))
$filter = NULL; $filter = NULL;
$filterstr = (is_a($filter, 'Net_LDAP2_Filter')?$filter->as_string():$filter);
if (is_empty($basedn)) {
$basedn = self :: getConfig('basedn');
if (is_empty($basedn)) {
LSerror :: addErrorCode('LSldap_08');
return;
}
self :: log_debug("LSldap::getNumberResult($filterstr): empty basedn provided, use basedn from configuration: ".varDump($basedn));
}
self :: log_trace("LSldap::getNumberResult($filterstr, $basedn): run search with parameters: ".varDump($params));
$ret = self :: $cnx -> search($basedn, $filter, $params); $ret = self :: $cnx -> search($basedn, $filter, $params);
if (Net_LDAP2::isError($ret)) { if (Net_LDAP2::isError($ret)) {
LSerror :: addErrorCode('LSldap_02',$ret -> getMessage()); LSerror :: addErrorCode('LSldap_02',$ret -> getMessage());
return; return;
} }
return $ret -> count(); $count = $ret -> count();
self :: log_trace("LSldap::getNumberResult($filterstr, $basedn): result=$count");
return $count;
} }
/** /**
@ -534,6 +559,19 @@ class LSldap extends LSlog_staticLoggerClass {
} }
return; return;
} }
/**
* Return a configuration parameter (or default value)
*
* @param[] $param The configuration parameter
* @param[] $default The default value (default : null)
* @param[] $cast Cast resulting value in specific type (default : disabled)
*
* @retval mixed The configuration parameter value or default value if not set
**/
private static function getConfig($param, $default=null, $cast=null) {
return LSconfig :: get($param, $default, $cast, self :: $config);
}
} }
/* /*
@ -560,3 +598,6 @@ LSerror :: defineError('LSldap_06',
LSerror :: defineError('LSldap_07', LSerror :: defineError('LSldap_07',
___("LSldap: Error while changing the DN of the object.") ___("LSldap: Error while changing the DN of the object.")
); );
LSerror :: defineError('LSldap_08',
___("LSldap: LDAP server base DN not configured.")
);