From 4ba584e8c6f20e70e7bb27fc1ef61b2181e0f147 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Mon, 15 Jun 2020 11:26:57 +0200 Subject: [PATCH] Add autocompleter for CLI command show --- src/includes/class/class.LScli.php | 35 ++++++++++++++ src/includes/class/class.LSldapObject.php | 59 ++++++++++++++++++++++- 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/src/includes/class/class.LScli.php b/src/includes/class/class.LScli.php index 9acdbd0c..7daa9ce5 100644 --- a/src/includes/class/class.LScli.php +++ b/src/includes/class/class.LScli.php @@ -600,6 +600,41 @@ class LScli extends LSlog_staticLoggerClass { return self :: autocomplete_opts($types, $prefix, false); } + /** + * Autocomplete LSobject DN option + * + * @param[in] $objType string LSobject type + * @param[in] $prefix string Option prefix (optional, default=empty string) + * + * @retval array List of available options + **/ + public static function autocomplete_LSobject_dn($objType, $prefix='') { + if (!$prefix || !LSsession ::loadLSobject($objType, false)) + return array(); + self :: need_ldap_con(); + $rdn_attr = LSconfig :: get("LSobjects.$objType.rdn"); + if (!$rdn_attr || strlen($prefix) < (strlen($rdn_attr)+1) || substr($prefix, 0, (strlen($rdn_attr)+1)) != "$rdn_attr=") + return array(); + + // Split prefix by comma to keep only RDN + $prefix_parts = explode(',', $prefix); + $prefix_rdn = $prefix_parts[0]; + + // Search objects + $obj = new $objType(); + $objs = $obj -> listObjectsName("($prefix_rdn*)"); + if (is_array($objs)) { + $dns = array_keys($objs); + self :: log_debug("Matching $objType DNs with prefix '$prefix_rdn': ".implode(', ', $dns)); + // If prefix have been reduced for the search, use self :: autocomplete_opts() to keep only + // full match + if ($prefix_rdn != $prefix) + return self :: autocomplete_opts($dns, $prefix); + return $dns; + } + return array(); + } + } /* diff --git a/src/includes/class/class.LSldapObject.php b/src/includes/class/class.LSldapObject.php index 70e7126c..b03c4458 100644 --- a/src/includes/class/class.LSldapObject.php +++ b/src/includes/class/class.LSldapObject.php @@ -1836,7 +1836,7 @@ class LSldapObject extends LSlog_staticLoggerClass { * Check if object type have a specified attribute * * @param[in] $attr_name string The attribute name - * + * * @teval boolean True if object type have a attribute of this name, False otherwise */ public static function hasAttr($attr_name) { @@ -1945,6 +1945,58 @@ class LSldapObject extends LSlog_staticLoggerClass { return true; } + /** + * Args autocompleter for CLI show search + * + * @param[in] $command_args array List of already typed words of the command + * @param[in] $comp_word_num int The command word number to autocomplete + * @param[in] $comp_word string The command word to autocomplete state + * @param[in] $opts array List of global available options + * + * @retval array List of available options for the word to autocomplete + **/ + public static function cli_show_args_autocompleter($command_args, $comp_word_num, $comp_word, $opts) { + $opts = array_merge($opts, array ('-r', '--raw-values')); + + // Handle positional args + $objType = null; + $objType_arg_num = null; + $dn = null; + $dn_arg_num = null; + for ($i=0; $i < count($command_args); $i++) { + if (!in_array($command_args[$i], $opts)) { + // If object type not defined + if (is_null($objType)) { + // Check object type exists + $objTypes = LScli :: autocomplete_LSobject_types($command_args[$i]); + + // Load it if exist and not trying to complete it + if (in_array($command_args[$i], $objTypes) && $i != $comp_word_num) { + LSsession :: loadLSobject($command_args[$i], false); + } + + // Defined it + $objType = $command_args[$i]; + $objType_arg_num = $i; + } + elseif (is_null($dn)) { + $dn = $command_args[$i]; + $dn_arg_num = $i; + } + } + } + + // If objType not already choiced (or currently autocomplete), add LSobject types to available options + if (!$objType || $objType_arg_num == $comp_word_num) + $opts = array_merge($opts, LScli :: autocomplete_LSobject_types($comp_word)); + + // If dn not alreay choiced (or currently autocomplete), try autocomplete it + elseif (!$dn || $dn_arg_num == $comp_word_num) + $opts = array_merge($opts, LScli :: autocomplete_LSobject_dn($objType, $comp_word)); + + return LScli :: autocomplete_opts($opts, $comp_word); + } + /** * CLI helper to show the object info * @@ -2605,7 +2657,10 @@ LScli :: add_command( 'show', array('LSldapObject', 'cli_show'), 'Show an LSobject', - '[object type] [dn] [-r|--raw-values]' + '[object type] [dn] [-r|--raw-values]', + null, + true, + array('LSldapObject', 'cli_show_args_autocompleter') ); LScli :: add_command(