From 4c28f2347d079b068b9a8bc02cc3a3895b60bb2e Mon Sep 17 00:00:00 2001 From: LAUNAY Samuel <107540223+Lainow@users.noreply.github.com> Date: Thu, 28 Nov 2024 15:35:06 +0100 Subject: [PATCH] Fix user default entity injection (#433) * Fix user default entity injection * Add suggestion * Replace finId by another method only for entity * Add second check with complete name * Check in childs entity * Add suggestions * Add suggestions * CHange label * Add comments * Develop comment * Update inc/commoninjectionlib.class.php Co-authored-by: Romain B. <8530352+Rom1-B@users.noreply.github.com> * Update changelog --------- Co-authored-by: Romain B. <8530352+Rom1-B@users.noreply.github.com> --- CHANGELOG.md | 4 +++ inc/commoninjectionlib.class.php | 49 ++++++++++++++++++++++++++++---- inc/engine.class.php | 4 ++- inc/model.class.php | 4 +-- inc/userinjection.class.php | 9 +++++- 5 files changed, 61 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2171183..db96851 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Fixed + +- Fix default entity insertion for a user + ## [2.14.0] - 2024-10-10 ### Added diff --git a/inc/commoninjectionlib.class.php b/inc/commoninjectionlib.class.php index 66ab8d0..86513b8 100644 --- a/inc/commoninjectionlib.class.php +++ b/inc/commoninjectionlib.class.php @@ -606,14 +606,51 @@ private function getFieldValue( $item = new $tmptype(); if ($item instanceof CommonTreeDropdown) { // use findID instead of getID - $input = ['completename' => $value, + $input = [ + 'completename' => $value, 'entities_id' => $this->entity ]; - if ($item->canCreate() && $this->rights['add_dropdown']) { - $id = $item->import($input); + if ($item->getType() == 'Entity') { // Blocks entity creation. The findID method only searches for direct sub-entities of the root, not deeper levels. + $crit = 'name'; + if (strpos($input['completename'], '>')) { + $crit = 'completename'; + } + $entity = new Entity(); + $result = $entity->getFromDBByCrit( + [ + $crit => $input['completename'], + 'entities_id' => $input['entities_id'] + ] + ); + + if ($result !== false) { + $input['entities_id'] = $entity->fields['id']; + } + + $sons = getSonsOf('glpi_entities', $input['entities_id']); + if ($result === false && !empty($sons)) { + foreach ($sons as $son_id) { + $result = $entity->getFromDBByCrit( + [ + $crit => $input['completename'], + 'entities_id' => $son_id + ] + ); + if ($result !== false) { + $input['entities_id'] = $entity->fields['id']; + break; + } + } + } + + $id = $input['entities_id']; } else { - $id = $item->findID($input); + if ($item->canCreate() && $this->rights['add_dropdown']) { + $id = $item->import($input); + } else { + $id = $item->findID($input); + } } } else if ($item instanceof CommonDropdown) { if ($item->canCreate() && $this->rights['add_dropdown']) { @@ -1359,7 +1396,9 @@ private function checkType($injectionClass, $option, $field_name, $data, $mandat private function addNecessaryFields() { - $this->setValueForItemtype($this->primary_type, 'entities_id', $this->entity); + if (!isset($this->values[$this->primary_type]['entities_id'])) { + $this->setValueForItemtype($this->primary_type, 'entities_id', $this->entity); + } if (method_exists($this->injectionClass, 'addSpecificNeededFields')) { $specific_fields = $this->injectionClass->addSpecificNeededFields( $this->primary_type, diff --git a/inc/engine.class.php b/inc/engine.class.php index d3ce084..7f353f0 100644 --- a/inc/engine.class.php +++ b/inc/engine.class.php @@ -173,7 +173,9 @@ public function injectLine($line, $index) public function addRequiredFields($itemtype, &$fields_toinject = []): void { //Add entity to the primary type - $fields_toinject[$itemtype]['entities_id'] = $this->entity; + if (!isset($fields_toinject[$itemtype]['entities_id'])) { + $fields_toinject[$itemtype]['entities_id'] = $this->entity; + } } diff --git a/inc/model.class.php b/inc/model.class.php index 07fa0e2..975662b 100644 --- a/inc/model.class.php +++ b/inc/model.class.php @@ -446,7 +446,7 @@ public function rawSearchOptions() 'id' => 6, 'table' => $this->getTable(), 'field' => 'can_add_dropdown', - 'name' => __('Allow creation of dropdowns', 'datainjection'), + 'name' => __('Allow creation of dropdowns (Except Entity)', 'datainjection'), 'datatype' => 'bool', ]; @@ -719,7 +719,7 @@ public function showAdvancedForm($ID, $options = []) ""; echo ""; - echo "" . __('Allow creation of dropdowns', 'datainjection') . ""; + echo "" . __('Allow creation of dropdowns (Except Entity)', 'datainjection') . ""; echo ""; Dropdown::showYesNo("can_add_dropdown", $this->fields['can_add_dropdown']); echo ""; diff --git a/inc/userinjection.class.php b/inc/userinjection.class.php index cbb0dad..15567dd 100644 --- a/inc/userinjection.class.php +++ b/inc/userinjection.class.php @@ -104,7 +104,14 @@ public function getOptions($primary_type = '') $options['ignore_fields'] = array_merge($blacklist, $notimportable); //Add displaytype value - $options['displaytype'] = ["dropdown" => [3, 79, 81, 82], + $options['displaytype'] = [ + "dropdown" => [ + 3, // location + 77, // default entity + 79, // default profile + 81, // title + 82 // category + ], "multiline_text" => [16], "bool" => [8], "password" => [4]