From 2e0338e678fe321c4200efa262aebbd249db31b0 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Wed, 20 Nov 2024 20:40:09 +0100 Subject: [PATCH] test_form_rule: add optional parameters to specify on which object's attribute to run the test --- src/includes/class/class.LSformRule.php | 122 +++++++++++++++++++++++- 1 file changed, 119 insertions(+), 3 deletions(-) diff --git a/src/includes/class/class.LSformRule.php b/src/includes/class/class.LSformRule.php index 3e1f1983..c049b837 100644 --- a/src/includes/class/class.LSformRule.php +++ b/src/includes/class/class.LSformRule.php @@ -114,11 +114,20 @@ class LSformRule extends LSlog_staticLoggerClass { * - values to test * - Optional arguments : * - -p|--param: LSformRule parameters (format: param=value) + * - Other optional arguments to specify on which LDAP object's attribute to run the test: + * - -t|--object-type: LSobject type + * - -D|--dn: LDAP object DN + * - -f|--form: LdapSaisie form name (create, modify, ...) + * - -a|--attr: the attribute name (must be included in the specified form) * * @return boolean True on success, false otherwise **/ public static function cli_test_form_rule($command_args) { $rule_name = null; + $objType = null; + $dn = null; + $form = null; + $attr = null; $values = array(); $params = array(); for ($i=0; $i < count($command_args); $i++) { @@ -133,6 +142,26 @@ class LSformRule extends LSlog_staticLoggerClass { LScli :: usage('Parameter "'.$param_parts[0].'" already specified.'); $params[$param_parts[0]] = LScli :: parse_arg_value($param_parts[1]); } + else if (in_array($command_args[$i], ['-t', '--object-type'])) { + $i++; + LScli :: unquote_word($command_args[$i]); + $objType = $command_args[$i]; + } + else if (in_array($command_args[$i], ['-D', '--dn'])) { + $i++; + LScli :: unquote_word($command_args[$i]); + $dn = $command_args[$i]; + } + else if (in_array($command_args[$i], ['-f', '--form'])) { + $i++; + LScli :: unquote_word($command_args[$i]); + $form = $command_args[$i]; + } + else if (in_array($command_args[$i], ['-a', '--attr'])) { + $i++; + LScli :: unquote_word($command_args[$i]); + $attr = $command_args[$i]; + } else if (is_null($rule_name)) { $rule_name = $command_args[$i]; } @@ -144,8 +173,41 @@ class LSformRule extends LSlog_staticLoggerClass { if (is_null($rule_name) || empty($values)) LScli :: usage('You must provide LSformRule type and at least one value to test.'); - self :: log_trace("test_form_rule($rule_name): params=".varDump($params)); - $formElement = null; + + if ($objType || $dn || $form || $attr) { + if ( ! ( $objType && $dn && $form && $attr ) ) + LScli :: usage( + 'To specify which form element you want to simulate the execution of a rule on, '. + 'you must provide the following set of information: the object type (-t/--type), '. + 'the object DN (-D/--dn), the form (-f/--form), and the attribute (-a/--attr).' + ); + if (!LSsession :: loadLSobject($objType)) + return false; + $obj = new $objType(); + if (!$obj->loadData($dn)) { + self :: log_fatal("Fail to load object $dn data from LDAP"); + return false; + } + $formObj = $obj->getForm($form); + if (!$formObj->hasElement($attr)) + LScli :: usage("The attribute $attr is not included in the form $form of $objType."); + $formElement = $formObj -> getElement($attr); + self :: log_debug("Run $rule_name rule as if we are on the attribute $attr of the $objType object $dn"); + + $attr_params = LSconfig :: get("LSobjects.$objType.attrs.$attr.check_data.$rule_name.params", [], "array"); + if ($attr_params) { + self :: log_debug( + "Merge provided parameters with configured one from $objType configuration: ". + varDump($attr_params) + ); + $params = array_merge_recursive($attr_params, $params); + } + } + else + $formElement = null; + + self :: log_debug("Run $rule_name rule with following parameters: ".varDump($params)); + $errors = self :: validate_values($rule_name, $values, array('params' => $params), $formElement); if (is_array($errors)) { print "Test triggered errors :\n - ".implode("\n - ", $errors)."\n"; @@ -168,7 +230,10 @@ class LSformRule extends LSlog_staticLoggerClass { * @return array List of available options for the word to autocomplete **/ public static function cli_test_form_rule_args_autocompleter($command_args, $comp_word_num, $comp_word, $opts) { - $opts = array_merge($opts, array('-p', '--param')); + $opts = array_merge( + $opts, + ['-p', '--param', '-t', '--object-type', '-D', '--dn', '-f', '--form', '-a', '--attr'] + ); // Handle positional args $rule_name = null; @@ -176,10 +241,12 @@ class LSformRule extends LSlog_staticLoggerClass { $rule_name_arg_num = null; $rule_name_quote_char = null; $params = array(); + $objType = null; for ($i=0; $i < count($command_args); $i++) { switch ($command_args[$i]) { case '-p': case '--params': + if ($comp_word_num == $i) return LScli :: autocomplete_opts([$comp_word], $comp_word); $i++; $quote_char = LScli :: unquote_word($command_args[$i]); $param_parts = explode('=', $command_args[$i]); @@ -192,6 +259,42 @@ class LSformRule extends LSlog_staticLoggerClass { ); break; + case '-t': + case '--object-type': + if ($comp_word_num == $i) return LScli :: autocomplete_opts([$comp_word], $comp_word); + $i++; + if ($comp_word_num == $i) return LScli :: autocomplete_LSobject_types($comp_word); + if (isset($command_args[$i])) { + $objType = $command_args[$i]; + LScli :: unquote_word($objType); + } + $opts = array_diff($opts, ['-t', '--object-type']); + break; + + case '-D': + case '--dn': + if ($comp_word_num == $i) return LScli :: autocomplete_opts([$comp_word], $comp_word); + $i++; + if ($objType && $comp_word_num == $i) return LScli :: autocomplete_LSobject_dn($objType, $comp_word); + $opts = array_diff($opts, ['-d', '--dn']); + break; + + case '-f': + case '--form': + if ($comp_word_num == $i) return LScli :: autocomplete_opts([$comp_word], $comp_word); + $i++; + if ($comp_word_num == $i) return LScli :: autocomplete_LSform_name($comp_word); + $opts = array_diff($opts, ['-f', '--form']); + break; + + case '-a': + case '--attr': + if ($comp_word_num == $i) return LScli :: autocomplete_opts([$comp_word], $comp_word); + $i++; + if ($objType && $comp_word_num == $i) return LScli :: autocomplete_LSobject_attr_name($objType, $comp_word); + $opts = array_diff($opts, ['-a', '--attr']); + break; + default: // If rule name not defined if (is_null($rule_name)) { @@ -319,6 +422,19 @@ LScli :: add_command( ' param_name=param_value', ' Multiple parameters could be specified by using this', ' optional argument multiple time', + '', + ' - Other optional arguments to specify on which LDAP object\'s attribute', + ' to run the test:', + ' - -t|--object-type: LSobject type', + ' - -D|--dn: LDAP object DN', + ' - -f|--form: LdapSaisie form name (create, modify, ...)', + ' - -a|--attr: the attribute name (must be included in the specified', + ' form)', + ' In this case, the LSformRule validate() method will receive a real', + ' LSformElement object reference as third parameter and will have access', + ' to the associated context. Furthermore, if the rule is configured on', + ' the specified object\'s attribute, the configured rule\'s parameters', + ' will be automatically merged with the specified ones.', ), true, array('LSformRule', 'cli_test_form_rule_args_autocompleter')