LSformElement::getApiValue(): add $details parameter

This parameter permit to control if the returned value contain or not 
details. By default (no details), the value used the format as accept by 
create/modify method. The details mode is more efficient to display the 
value.
This commit is contained in:
Benjamin Renard 2021-02-05 12:22:32 +01:00
parent 36a1719b26
commit 19dd343106
12 changed files with 189 additions and 132 deletions

View file

@ -248,14 +248,19 @@
<listitem> <listitem>
<para>Cette méthode permet de récupérer les informations d'un objet de l'annuaire au format <para>Cette méthode permet de récupérer les informations d'un objet de l'annuaire au format
JSON. Le type de l'objet et son DN sont précisés dans l'URL et doivent être encodés en JSON. Le type de l'objet et son DN sont précisés dans l'URL et doivent être encodés en
conséquence. conséquence. Par défaut, les valeurs des attributs retournées sont au format tel qu'attendu
en cas de création/modification de l'objet. Il est cependant possible d'ajouter le paramètre
<literal>details</literal> afin d'obtenir des informations complémentaires sur les valeurs
des attributs.
<programlisting linenumbering="unnumbered"> <programlisting linenumbering="unnumbered">
<citetitle>Exemple</citetitle> <citetitle>Exemple</citetitle>
<![CDATA[# curl -u username:secret 'https://ldapsaisie/api/1.0/object/LSpeople/uid=hmartin,ou=people,o=ls?pretty' <![CDATA[# curl -u username:secret 'https://ldapsaisie/api/1.0/object/LSpeople/uid=hmartin,ou=people,o=ls?pretty'
{ {
"success": true,
"dn": "uid=hmartin,ou=people,o=ls", "dn": "uid=hmartin,ou=people,o=ls",
"type": "LSpeople", "type": "LSpeople",
"name": "Henri MARTIN", "name": "Henri MARTIN",
"details": false,
"attributes": { "attributes": {
"uid": "hmartin", "uid": "hmartin",
"givenName": "Henri", "givenName": "Henri",
@ -265,12 +270,9 @@
"personalTitle": "M.", "personalTitle": "M.",
"description": [], "description": [],
"jpegPhoto": null, "jpegPhoto": null,
"lsGodfatherDn": { "lsGodfatherDn": [
"uid=eeggs,ou=people,o=ls": { "uid=eeggs,ou=people,o=ls"
"name": "Easter Eggs", ],
"object_type": "LSpeople"
}
},
"uidNumber": "101022", "uidNumber": "101022",
"gidNumber": "102001", "gidNumber": "102001",
"loginShell": "no", "loginShell": "no",

View file

@ -406,15 +406,28 @@ class LSformElement extends LSlog_staticLoggerClass {
/** /**
* Retreive value as return in API response * Retreive value as return in API response
* *
* @param[in] $details boolean If true, returned values will contain details if this field type
* support it (optional, default: false)
*
* @retval mixed API value(s) or null/empty array if no value * @retval mixed API value(s) or null/empty array if no value
*/ */
public function getApiValue() { public function getApiValue($details=false) {
if ($this -> isMultiple()) { if (method_exists($this, 'parseValue')) {
return ensureIsArray($this -> values); $values = array();
foreach(ensureIsArray($this -> values) as $value) {
$parsed_value = $this -> parseValue($value, $details);
if ($parsed_value != false)
$values[] = $parsed_value;
}
} }
if (!$this -> values) else {
$values = ensureIsArray($this -> values);
}
if ($this -> isMultiple())
return $values;
if (!$values)
return null; return null;
return $this -> values[0]; return $values[0];
} }
} }

View file

@ -167,9 +167,12 @@ class LSformElement_image extends LSformElement {
/** /**
* Retreive value as return in API response * Retreive value as return in API response
* *
* @param[in] $details boolean If true, returned values will contain details if this field type
* support it (optional, default: false)
*
* @retval mixed API value(s) or null/empty array if no value * @retval mixed API value(s) or null/empty array if no value
*/ */
public function getApiValue() { public function getApiValue($details=false) {
if ($this -> isMultiple()) { if ($this -> isMultiple()) {
$values = array(); $values = array();
for ($i=0; $i < count($this -> values); $i++) for ($i=0; $i < count($this -> values); $i++)

View file

@ -71,23 +71,23 @@ class LSformElement_jsonCompositeAttribute extends LSformElement {
* *
* @retval array Parsed values * @retval array Parsed values
*/ */
private function parseValues() { private function parseValues() {
self :: log_trace('values: '.varDump($this -> values)); self :: log_trace('values: '.varDump($this -> values));
$parseValues=array(); $parseValues=array();
foreach($this -> values as $val) { foreach($this -> values as $val) {
$decodedValue = json_decode($val, true); $decodedValue = json_decode($val, true);
self :: log_trace('decoded value: '.varDump($decodedValue)); self :: log_trace('decoded value: '.varDump($decodedValue));
if (is_array($decodedValue)) { if (is_array($decodedValue)) {
$parseValue = array('value' => $val); $parseValue = array('value' => $val);
foreach($decodedValue as $c => $cvalue) { foreach($decodedValue as $c => $cvalue) {
$parseValue[$c] = $this -> translateComponentValue($c,$cvalue); $parseValue[$c] = $this -> translateComponentValue($c,$cvalue);
} }
$parseValues[] = $parseValue; $parseValues[] = $parseValue;
} }
} }
self :: log_trace('parsed values: '.varDump($parseValues)); self :: log_trace('parsed values: '.varDump($parseValues));
return $parseValues; return $parseValues;
} }
/** /**
* Retourne les infos d'affichage de l'élément * Retourne les infos d'affichage de l'élément
@ -448,9 +448,12 @@ class LSformElement_jsonCompositeAttribute extends LSformElement {
/** /**
* Retreive value as return in API response * Retreive value as return in API response
* *
* @param[in] $details boolean If true, returned values will contain details if this field type
* support it (optional, default: false)
*
* @retval mixed API value(s) or null/empty array if no value * @retval mixed API value(s) or null/empty array if no value
*/ */
public function getApiValue() { public function getApiValue($details=false) {
$values = array(); $values = array();
foreach(ensureIsArray($this -> values) as $value) { foreach(ensureIsArray($this -> values) as $value) {
$decodedValue = json_decode($value, true); $decodedValue = json_decode($value, true);

View file

@ -85,14 +85,17 @@ class LSformElement_labeledValue extends LSformElement {
); );
} }
/** /**
* Parse une valeur * Parse one value
* *
* @param[in] $value La valeur * @param[in] $value string The value to parse
* * @param[in] $details boolean Enable/disable details return (optional, default: true)
* @retval array Un tableau cle->valeur contenant value et label *
**/ * @retval array Parsed value
public function parseValue($value) { */
public function parseValue($value, $details=true) {
if (!$details)
return $value;
$ret=array('raw_value' => $value); $ret=array('raw_value' => $value);
if (preg_match('/^\[([^\]]*)\](.*)$/',$value,$m)) { if (preg_match('/^\[([^\]]*)\](.*)$/',$value,$m)) {
$ret['label'] = $m[1]; $ret['label'] = $m[1];

View file

@ -45,14 +45,17 @@ class LSformElement_mailQuota extends LSformElement {
* Parse one value * Parse one value
* *
* @param[in] $value string The value to parse * @param[in] $value string The value to parse
* @param[in] $details boolean Enable/disable details return (optional, default: true)
* *
* @retval array Parsed value * @retval array Parsed value
*/ */
public function parseValue($value) { public function parseValue($value, $details=true) {
if (preg_match('/^([0-9]+)'.$this -> getSuffix().'$/',$value,$regs)) { if (preg_match('/^([0-9]+)'.$this -> getSuffix().'$/', $value, $regs)) {
$infos = array( $infos = array(
'size' => $regs[1], 'size' => $regs[1],
); );
if (!$details)
return $infos['size'];
if ($infos['size'] == 0) { if ($infos['size'] == 0) {
return array( return array(
'size' => 0, 'size' => 0,
@ -199,25 +202,4 @@ class LSformElement_mailQuota extends LSformElement {
return true; return true;
} }
/**
* Retreive value as return in API response
*
* @retval mixed API value(s) or null/empty array if no value
*/
public function getApiValue() {
$values = array();
foreach(ensureIsArray($this -> values) as $value) {
$parsed_value = $this -> parseValue($value);
if (is_array($parsed_value)) {
$values[] = $parsed_value['size'];
}
}
if ($this -> isMultiple()) {
return $values;
}
if (!$values)
return null;
return $values[0];
}
} }

View file

@ -42,19 +42,21 @@ class LSformElement_quota extends LSformElement {
1099511627776 => 'To', 1099511627776 => 'To',
); );
/** /**
* Parse one value * Parse one value
* *
* @param[in] $value string The value to parse * @param[in] $value string The value to parse
* @param[in] $details boolean Enable/disable details return (optional, default: true)
* *
* @retval array Parsed value * @retval array Parsed value
*/ */
public function parseValue($value) { public function parseValue($value, $details=true) {
if (preg_match('/^([0-9]+)$/', $value, $regs)) { if (preg_match('/^([0-9]+)$/', $value, $regs)) {
$infos = array( $infos = array(
'size' => ceil($regs[1]/$this -> getFactor()), 'size' => ceil($regs[1]/$this -> getFactor()),
); );
if (!$details)
return $infos['size'];
if ($infos['size'] == 0) { if ($infos['size'] == 0) {
return array( return array(
'size' => 0, 'size' => 0,
@ -194,25 +196,4 @@ class LSformElement_quota extends LSformElement {
return $this -> getParam('html_options.factor', 1, 'int'); return $this -> getParam('html_options.factor', 1, 'int');
} }
/**
* Retreive value as return in API response
*
* @retval mixed API value(s) or null/empty array if no value
*/
public function getApiValue() {
$values = array();
foreach(ensureIsArray($this -> values) as $value) {
$parsed_value = $this -> parseValue($value);
if (is_array($parsed_value)) {
$values[] = $parsed_value['size'];
}
}
if ($this -> isMultiple()) {
return $values;
}
if (!$values)
return null;
return $values[0];
}
} }

View file

@ -141,6 +141,37 @@ class LSformElement_select extends LSformElement {
} }
} }
/**
* Retreive value as return in API response
*
* @param[in] $details boolean If true, returned values will contain details if this field type
* support it (optional, default: false)
*
* @retval mixed API value(s) or null/empty array if no value
*/
public function getApiValue($details=false) {
$values = array();
foreach(ensureIsArray($this -> values) as $value) {
$label = $this -> _isValidValue($value, $this -> params['text_possible_values']);
if ($label === false) continue;
if ($details)
$values[] = array(
'label' => $label,
'value' => $value,
);
else
$values[] = $value;
}
if ($this -> isMultiple()) {
return ensureIsArray($values);
}
if (!$values)
return null;
if ($details)
return $values[0];
}
} }
/** /**

View file

@ -322,4 +322,23 @@ class LSformElement_select_object extends LSformElement {
} }
} }
/**
* Retreive value as return in API response
*
* @param[in] $details boolean If true, returned values will contain details if this field type
* support it (optional, default: false)
*
* @retval mixed API value(s) or null/empty array if no value
*/
public function getApiValue($details=false) {
$values = ($details?$this -> values:array_keys(ensureIsArray($this -> values)));
self :: log_debug("getApiValue(): ".varDump($values));
if ($this -> isMultiple()) {
return ensureIsArray($values);
}
if (!$values)
return null;
return $values[0];
}
} }

View file

@ -35,6 +35,32 @@ class LSformElement_ssh_key extends LSformElement {
var $template = 'LSformElement_ssh_key.tpl'; var $template = 'LSformElement_ssh_key.tpl';
var $fieldTemplate = 'LSformElement_ssh_key_field.tpl'; var $fieldTemplate = 'LSformElement_ssh_key_field.tpl';
/**
* Parse one value
*
* @param[in] $value string The value to parse
* @param[in] $details boolean Enable/disable details return (optional, default: true)
*
* @retval array Parsed value
*/
public function parseValue($value, $details=true) {
if (!$details)
return $value;
if (preg_match('/^ssh-([a-z0-9]+) +([^ ]+) +(.*)$/', $value, $regs)) {
return array(
'type' => $regs[1],
'mail' => $regs[3],
'value' => $value
);
}
return array(
'type' => null,
'mail' => null,
'value' => $value
);
}
/** /**
* Retourne les infos d'affichage de l'élément * Retourne les infos d'affichage de l'élément
* *
@ -60,20 +86,9 @@ class LSformElement_ssh_key extends LSformElement {
$values_txt = array(); $values_txt = array();
foreach ($this -> values as $value) { foreach ($this -> values as $value) {
if (preg_match('/^ssh-([a-z0-9]+) +([^ ]+) +(.*)$/',$value,$regs)) { $parsedValue = $this -> parseValue($value);
$values_txt[] = array( $parsedValue['shortTxt'] = substr($value, 0, 15);
'type' => $regs[1], $values_txt[] = $parsedValue;
'shortTxt' => substr($regs[2],0,10),
'mail' => $regs[3],
'value' => $value
);
}
else {
$values_txt[] = array(
'shortTxt' => substr($value,0,15),
'value' => $value
);
}
} }
$params['values_txt'] = $values_txt; $params['values_txt'] = $values_txt;
$params['unknowTypeTxt'] = _('Unknown type'); $params['unknowTypeTxt'] = _('Unknown type');

View file

@ -74,6 +74,37 @@ class LSformElement_valueWithUnit extends LSformElement {
); );
} }
/**
* Parse one value
*
* @param[in] $value string The value to parse
* @param[in] $details boolean Enable/disable details return (optional, default: true)
*
* @retval array Parsed value
*/
public function parseValue($value, $details=true) {
if (preg_match('/^([0-9]*)$/' ,$value, $regs)) {
$infos = array(
'value' => intval($regs[1]),
);
if (!$details)
return $infos['value'];
$units = $this -> getUnits();
if ($units) {
foreach($units as $sill => $label) {
if ($infos['value'] >= $sill) {
$infos['valueWithUnit'] = $this -> formatNumber($infos['value']/$sill);
$infos['unitSill'] = $sill;
$infos['unitLabel'] = $label;
break;
}
}
}
return $infos;
}
return false;
}
/** /**
* Retourne les infos d'affichage de l'élément * Retourne les infos d'affichage de l'élément
* *
@ -89,25 +120,15 @@ class LSformElement_valueWithUnit extends LSformElement {
if ($units) { if ($units) {
foreach ($this -> values as $value) { foreach ($this -> values as $value) {
if (preg_match('/^([0-9]*)$/',$value,$regs)) { $parsedValue = $this -> parseValue($value);
$infos = array( if ($parsedValue === false) {
'value' => $regs[1]
);
foreach($units as $sill => $label) {
if ($infos['value'] >= $sill) {
$infos['valueWithUnit']=$this -> formatNumber($infos['value']/$sill);
$infos['unitSill']=$sill;
$infos['unitLabel']=$label;
break;
}
}
$values_and_units[$value] = $infos;
}
else {
$values_and_units[$value] = array( $values_and_units[$value] = array(
'unknown' => _('Incorrect value') 'unknown' => _('Incorrect value')
); );
} }
else {
$values_and_units[$value] = $parsedValue;
}
} }
} }
@ -204,23 +225,6 @@ class LSformElement_valueWithUnit extends LSformElement {
return true; return true;
} }
/**
* Retreive value as return in API response
*
* @retval mixed API value(s) or null/empty array if no value
*/
public function getApiValue() {
$values = array();
foreach (ensureIsArray($this -> values) as $value)
if (preg_match('/^([0-9]*)$/', $value, $regs))
$values[] = intval($regs[1]);
if ($this -> isMultiple()) {
return $values;
}
if (!$values)
return null;
return $values[0];
}
} }
/* /*

View file

@ -1693,13 +1693,14 @@ function handle_api_LSobject_show($request) {
'dn' => $object -> getDn(), 'dn' => $object -> getDn(),
'type' => $object -> getType(), 'type' => $object -> getType(),
'name' => $object -> getDisplayName(), 'name' => $object -> getDisplayName(),
'details' => isset($_REQUEST['details']),
'attributes' => array(), 'attributes' => array(),
'relations' => array(), 'relations' => array(),
); );
$view = $object -> getView(); $view = $object -> getView();
foreach($view -> elements as $element) { foreach($view -> elements as $element) {
$data['attributes'][$element -> name] = $element -> getApiValue(); $data['attributes'][$element -> name] = $element -> getApiValue($data['details']);
} }
if (LSsession :: loadLSclass('LSrelation')) { if (LSsession :: loadLSclass('LSrelation')) {