From 8b80d9f9de03de50aa86b4401886e4b2b5d6094b Mon Sep 17 00:00:00 2001 From: Lainow Date: Wed, 16 Oct 2024 12:15:32 +0200 Subject: [PATCH 01/12] Fix user default entity injection --- inc/commoninjectionlib.class.php | 4 +++- inc/engine.class.php | 4 +++- inc/userinjection.class.php | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/inc/commoninjectionlib.class.php b/inc/commoninjectionlib.class.php index 66ab8d0..4080495 100644 --- a/inc/commoninjectionlib.class.php +++ b/inc/commoninjectionlib.class.php @@ -1359,7 +1359,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/userinjection.class.php b/inc/userinjection.class.php index cbb0dad..ebac1cf 100644 --- a/inc/userinjection.class.php +++ b/inc/userinjection.class.php @@ -104,7 +104,7 @@ 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, 77, 79, 81, 82], "multiline_text" => [16], "bool" => [8], "password" => [4] From eae1ecbbf1ab3466424e3d44838f174ccadf7f9b Mon Sep 17 00:00:00 2001 From: Lainow Date: Thu, 17 Oct 2024 11:46:45 +0200 Subject: [PATCH 02/12] Add suggestion --- inc/userinjection.class.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/inc/userinjection.class.php b/inc/userinjection.class.php index ebac1cf..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, 77, 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] From be2fdd443abddb350864f3e88f6ec4d360130f02 Mon Sep 17 00:00:00 2001 From: Lainow Date: Thu, 17 Oct 2024 14:46:47 +0200 Subject: [PATCH 03/12] Replace finId by another method only for entity --- inc/commoninjectionlib.class.php | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/inc/commoninjectionlib.class.php b/inc/commoninjectionlib.class.php index 4080495..57517ef 100644 --- a/inc/commoninjectionlib.class.php +++ b/inc/commoninjectionlib.class.php @@ -610,10 +610,29 @@ private function getFieldValue( 'entities_id' => $this->entity ]; - if ($item->canCreate() && $this->rights['add_dropdown']) { - $id = $item->import($input); + if ($item->getType() == 'Entity') { + $entity = new Entity(); + $result = $entity->getFromDBByCrit( + [ + 'name' => $input['completename'], + 'entities_id' => $input['entities_id'], + ] + ); + if ($item->canCreate() && $this->rights['add_dropdown']) { + if ($result !== false) { + $id = $entity->fields['id']; + } else { + $id = $entity->add($input); + } + } else { + $id = $entity->fields['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']) { From 224f54187e87d866d37b0f5d524fbe1ffbad48e2 Mon Sep 17 00:00:00 2001 From: Lainow Date: Fri, 18 Oct 2024 16:38:09 +0200 Subject: [PATCH 04/12] Add second check with complete name --- inc/commoninjectionlib.class.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/inc/commoninjectionlib.class.php b/inc/commoninjectionlib.class.php index 57517ef..296de98 100644 --- a/inc/commoninjectionlib.class.php +++ b/inc/commoninjectionlib.class.php @@ -618,6 +618,14 @@ private function getFieldValue( 'entities_id' => $input['entities_id'], ] ); + if ($result === false) { + $result = $entity->getFromDBByCrit( + [ + 'completename' => $input['completename'], + 'entities_id' => $input['entities_id'], + ] + ); + } if ($item->canCreate() && $this->rights['add_dropdown']) { if ($result !== false) { $id = $entity->fields['id']; From eaf5f6233f71eb2cc466694200965dbbdc13400a Mon Sep 17 00:00:00 2001 From: Lainow Date: Mon, 21 Oct 2024 16:42:03 +0200 Subject: [PATCH 05/12] Check in childs entity --- inc/commoninjectionlib.class.php | 33 +++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/inc/commoninjectionlib.class.php b/inc/commoninjectionlib.class.php index 296de98..850108e 100644 --- a/inc/commoninjectionlib.class.php +++ b/inc/commoninjectionlib.class.php @@ -606,26 +606,41 @@ 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->getType() == 'Entity') { $entity = new Entity(); + $sons = getSonsOf('glpi_entities', $input['entities_id']); + if (strpos($value, '>')) { + $critname = 'completename'; + } else { + $critname = 'name'; + } + $result = $entity->getFromDBByCrit( [ - 'name' => $input['completename'], + $critname => $input['completename'], 'entities_id' => $input['entities_id'], ] ); - if ($result === false) { - $result = $entity->getFromDBByCrit( - [ - 'completename' => $input['completename'], - 'entities_id' => $input['entities_id'], - ] - ); + + if ($result === false && !empty($sons)) { + foreach ($sons as $son_id) { + $result = $entity->getFromDBByCrit( + [ + $critname => $input['completename'], + 'entities_id' => $son_id, + ] + ); + if ($result !== false) { + break; + } + } } + if ($item->canCreate() && $this->rights['add_dropdown']) { if ($result !== false) { $id = $entity->fields['id']; From 4e14fa1efe5f621cda4ae29622dec604741d3a4e Mon Sep 17 00:00:00 2001 From: Lainow Date: Wed, 23 Oct 2024 10:54:50 +0200 Subject: [PATCH 06/12] Add suggestions --- inc/commoninjectionlib.class.php | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/inc/commoninjectionlib.class.php b/inc/commoninjectionlib.class.php index 850108e..af16514 100644 --- a/inc/commoninjectionlib.class.php +++ b/inc/commoninjectionlib.class.php @@ -607,34 +607,45 @@ private function getFieldValue( if ($item instanceof CommonTreeDropdown) { // use findID instead of getID $input = [ - 'completename' => $value, + 'name' => $value, 'entities_id' => $this->entity ]; if ($item->getType() == 'Entity') { $entity = new Entity(); $sons = getSonsOf('glpi_entities', $input['entities_id']); - if (strpos($value, '>')) { - $critname = 'completename'; - } else { - $critname = 'name'; - } $result = $entity->getFromDBByCrit( [ - $critname => $input['completename'], + 'name' => $input['name'], 'entities_id' => $input['entities_id'], ] ); + if ($result === false) { + $result = $entity->getFromDBByCrit( + [ + 'completename' => $input['name'], + 'entities_id' => $input['entities_id'], + ] + ); + } if ($result === false && !empty($sons)) { foreach ($sons as $son_id) { $result = $entity->getFromDBByCrit( [ - $critname => $input['completename'], + 'name' => $input['name'], 'entities_id' => $son_id, ] ); + if ($result === false) { + $result = $entity->getFromDBByCrit( + [ + 'completename' => $input['name'], + 'entities_id' => $input['entities_id'], + ] + ); + } if ($result !== false) { break; } From eec3dd129ba5e8f096ca96d12ededbff8b6c7752 Mon Sep 17 00:00:00 2001 From: Lainow Date: Wed, 23 Oct 2024 17:04:33 +0200 Subject: [PATCH 07/12] Add suggestions --- inc/commoninjectionlib.class.php | 46 +++++++++++--------------------- 1 file changed, 15 insertions(+), 31 deletions(-) diff --git a/inc/commoninjectionlib.class.php b/inc/commoninjectionlib.class.php index af16514..aac2412 100644 --- a/inc/commoninjectionlib.class.php +++ b/inc/commoninjectionlib.class.php @@ -607,60 +607,44 @@ private function getFieldValue( if ($item instanceof CommonTreeDropdown) { // use findID instead of getID $input = [ - 'name' => $value, + 'completename' => $value, 'entities_id' => $this->entity ]; if ($item->getType() == 'Entity') { + $crit = 'name'; + if (strpos($input['completename'], '>')) { + $crit = 'completename'; + } $entity = new Entity(); - $sons = getSonsOf('glpi_entities', $input['entities_id']); - $result = $entity->getFromDBByCrit( [ - 'name' => $input['name'], - 'entities_id' => $input['entities_id'], + $crit => $input['completename'], + 'entities_id' => $input['entities_id'] ] ); - if ($result === false) { - $result = $entity->getFromDBByCrit( - [ - 'completename' => $input['name'], - '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( [ - 'name' => $input['name'], - 'entities_id' => $son_id, + $crit => $input['completename'], + 'entities_id' => $son_id ] ); - if ($result === false) { - $result = $entity->getFromDBByCrit( - [ - 'completename' => $input['name'], - 'entities_id' => $input['entities_id'], - ] - ); - } if ($result !== false) { + $input['entities_id'] = $entity->fields['id']; break; } } } - if ($item->canCreate() && $this->rights['add_dropdown']) { - if ($result !== false) { - $id = $entity->fields['id']; - } else { - $id = $entity->add($input); - } - } else { - $id = $entity->fields['id']; - } + $id = $input['entities_id']; } else { if ($item->canCreate() && $this->rights['add_dropdown']) { $id = $item->import($input); From 0ecaf9f16b9458e9e055bb9139925c7895166586 Mon Sep 17 00:00:00 2001 From: Lainow Date: Thu, 24 Oct 2024 09:26:11 +0200 Subject: [PATCH 08/12] CHange label --- inc/model.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 ""; From c2f230615f30d7a40fba37d4962e9b7b1e974668 Mon Sep 17 00:00:00 2001 From: Lainow Date: Tue, 5 Nov 2024 10:40:53 +0100 Subject: [PATCH 09/12] Add comments --- inc/commoninjectionlib.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/commoninjectionlib.class.php b/inc/commoninjectionlib.class.php index aac2412..46cef6e 100644 --- a/inc/commoninjectionlib.class.php +++ b/inc/commoninjectionlib.class.php @@ -611,7 +611,7 @@ private function getFieldValue( 'entities_id' => $this->entity ]; - if ($item->getType() == 'Entity') { + if ($item->getType() == 'Entity') { // This will block the creation of entities $crit = 'name'; if (strpos($input['completename'], '>')) { $crit = 'completename'; From ef8bd7edcefdfef086452d2b770607c12bb5feb5 Mon Sep 17 00:00:00 2001 From: LAUNAY Samuel <107540223+Lainow@users.noreply.github.com> Date: Thu, 7 Nov 2024 16:02:19 +0100 Subject: [PATCH 10/12] Develop comment --- inc/commoninjectionlib.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/commoninjectionlib.class.php b/inc/commoninjectionlib.class.php index 46cef6e..cc8067b 100644 --- a/inc/commoninjectionlib.class.php +++ b/inc/commoninjectionlib.class.php @@ -611,7 +611,7 @@ private function getFieldValue( 'entities_id' => $this->entity ]; - if ($item->getType() == 'Entity') { // This will block the creation of entities + if ($item->getType() == 'Entity') { // This will block the creation of entities. In addition, the findID method only searches for an entity if it is directly a sub-entity of the root entity, and this does not work for sub-sub entities. $crit = 'name'; if (strpos($input['completename'], '>')) { $crit = 'completename'; From 6e5d7e8ed90e668bb019e9d9aad8aa1b6bfcf735 Mon Sep 17 00:00:00 2001 From: LAUNAY Samuel <107540223+Lainow@users.noreply.github.com> Date: Thu, 7 Nov 2024 16:47:49 +0100 Subject: [PATCH 11/12] Update inc/commoninjectionlib.class.php Co-authored-by: Romain B. <8530352+Rom1-B@users.noreply.github.com> --- inc/commoninjectionlib.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/commoninjectionlib.class.php b/inc/commoninjectionlib.class.php index cc8067b..86513b8 100644 --- a/inc/commoninjectionlib.class.php +++ b/inc/commoninjectionlib.class.php @@ -611,7 +611,7 @@ private function getFieldValue( 'entities_id' => $this->entity ]; - if ($item->getType() == 'Entity') { // This will block the creation of entities. In addition, the findID method only searches for an entity if it is directly a sub-entity of the root entity, and this does not work for sub-sub entities. + 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'; From d5717f5aaf474a06165fd4dc914e3f48ca4afb3f Mon Sep 17 00:00:00 2001 From: Lainow Date: Thu, 28 Nov 2024 14:35:24 +0100 Subject: [PATCH 12/12] Update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) 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