LScli: fix handling LDAP servers with configured subDn

In case of LDAP server with configured subDn, always select a subDn, 
even if user didn't make it with --sub-dn parameter. Futhermore, in BASH 
autocompletion, autocomplete LSobject types with only available ones in 
current subDn.
This commit is contained in:
Benjamin Renard 2020-08-07 10:39:41 +02:00
parent 5e3071d24c
commit 5bbe076e23

View file

@ -135,6 +135,7 @@ class LScli extends LSlog_staticLoggerClass {
$console_log = false; $console_log = false;
$quiet = false; $quiet = false;
$ldap_server_id = false; $ldap_server_id = false;
$ldap_server_subDn = false;
$command = false; $command = false;
$command_args = array(); $command_args = array();
self :: log_debug("handle_args :\n".varDump($argv)); self :: log_debug("handle_args :\n".varDump($argv));
@ -176,10 +177,7 @@ class LScli extends LSlog_staticLoggerClass {
break; break;
case '--sub-dn': case '--sub-dn':
$i++; $i++;
$subDn = $argv[$i]; $ldap_server_subDn = $argv[$i];
self :: need_ldap_con();
if(!LSsession :: setSubDn($subDn))
self :: usage("Fail to select sub DN '$subDn'.");
break; break;
case '-L': case '-L':
case '--load-class': case '--load-class':
@ -223,6 +221,24 @@ class LScli extends LSlog_staticLoggerClass {
// - otherwise: log only errors // - otherwise: log only errors
LSlog :: logOnConsole(($console_log?$log_level:'ERROR')); LSlog :: logOnConsole(($console_log?$log_level:'ERROR'));
// If no LDAP server subDn selected, check if current LDAP server need it,
// and select the first one
if (!$ldap_server_subDn) {
self :: need_ldap_con();
$subDns = LSsession :: getSubDnLdapServer();
if (is_array($subDns)) {
asort($subDns);
$ldap_server_subDn = key($subDns);
}
}
// Select LDAP server subDn (if need)
if ($ldap_server_subDn) {
self :: need_ldap_con();
if(!LSsession :: setSubDn($ldap_server_subDn))
self :: usage("Fail to select sub DN '$ldap_server_subDn'.");
}
if (!$command) { if (!$command) {
self :: log_debug("LScli :: handle_args() : no detected command => show usage"); self :: log_debug("LScli :: handle_args() : no detected command => show usage");
self :: usage(); self :: usage();
@ -422,8 +438,18 @@ class LScli extends LSlog_staticLoggerClass {
self :: unquote_word($ldap_server_id); self :: unquote_word($ldap_server_id);
if(!LSsession :: setLdapServer($ldap_server_id)) if(!LSsession :: setLdapServer($ldap_server_id))
self :: usage("Fail to select LDAP server #$ldap_server_id."); self :: usage("Fail to select LDAP server #$ldap_server_id.");
// Check if LDAP server has subDn and select the first one if true
self :: need_ldap_con();
$subDns = LSsession :: getSubDnLdapServer();
if (is_array($subDns)) {
asort($subDns);
$subDn = key($subDns);
if(!LSsession :: setSubDn($subDn))
self :: usage("Fail to select sub DN '$subDn'.");
$opts[] = '--sub-dn'; $opts[] = '--sub-dn';
} }
}
break; break;
case '--sub-dn': case '--sub-dn':
$i++; $i++;
@ -622,24 +648,45 @@ class LScli extends LSlog_staticLoggerClass {
* @retval array List of available options * @retval array List of available options
**/ **/
public static function autocomplete_LSobject_types($prefix='', $case_sensitive=true, $quote_char='') { public static function autocomplete_LSobject_types($prefix='', $case_sensitive=true, $quote_char='') {
$types = LSconfig :: get('LSaccess', array(), null, LSsession :: $ldapServer);
$subdn_config = LSconfig :: get('subDn', null, null, LSsession :: $ldapServer); $subdn_config = LSconfig :: get('subDn', null, null, LSsession :: $ldapServer);
if (is_array($subdn_config)) { if (is_array($subdn_config)) {
foreach ($subdn_config as $key => $value) { $types = array();
if (!is_array($value)) continue; $currentSubDn = LSsession :: getTopDn();
if ($key == 'LSobject') { foreach($subdn_config as $name => $config) {
foreach ($value as $subDnObjType => $objConfig) if ($name=='LSobject') {
if (is_array($objConfig) && isset($objConfig['LSobjects']) && is_array($objConfig['LSobjects'])) self :: need_ldap_con();
foreach ($objConfig['LSobjects'] as $type) if (is_array($config)) {
if (!in_array($type, $types)) foreach($config as $objectType => $objectConf) {
$types[] = $type; if (LSsession :: loadLSobject($objectType)) {
} if ($subdnobject = new $objectType()) {
else if (isset($value['LSobjects']) && is_array($value['LSobjects'])) { $tbl = $subdnobject -> getSelectArray(NULL,LSsession::getRootDn(),NULL,NULL,false,NULL,array('onlyAccessible' => False));
foreach ($value['LSobjects'] as $type) if (is_array($tbl) && array_key_exists($currentSubDn, $tbl)) {
if (is_array($objectConf['LSobjects'])) {
foreach($objectConf['LSobjects'] as $type) {
if (!in_array($type, $types)) if (!in_array($type, $types))
$types[] = $type; $types[] = $type;
} }
} }
break;
}
}
}
}
}
}
else if (isset($config['dn']) && $config['dn'] == $currentSubDn) {
if (is_array($config['LSobjects'])) {
foreach($config['LSobjects'] as $type) {
if (!in_array($type, $types))
$types[] = $type;
}
}
break;
}
}
}
else {
$types = LSconfig :: get('LSaccess', array(), null, LSsession :: $ldapServer);
} }
return self :: autocomplete_opts($types, $prefix, $case_sensitive, $quote_char); return self :: autocomplete_opts($types, $prefix, $case_sensitive, $quote_char);
} }