Skip to content

Commit

Permalink
Fix user default entity injection (#433)
Browse files Browse the repository at this point in the history
* 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. <[email protected]>

* Update changelog

---------

Co-authored-by: Romain B. <[email protected]>
  • Loading branch information
Lainow and Rom1-B authored Nov 28, 2024
1 parent 730ef0a commit 4c28f23
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 44 additions & 5 deletions inc/commoninjectionlib.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']) {
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion inc/engine.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}


Expand Down
4 changes: 2 additions & 2 deletions inc/model.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];

Expand Down Expand Up @@ -719,7 +719,7 @@ public function showAdvancedForm($ID, $options = [])
"</th></tr>";

echo "<tr class='tab_bg_1'>";
echo "<td>" . __('Allow creation of dropdowns', 'datainjection') . "</td>";
echo "<td>" . __('Allow creation of dropdowns (Except Entity)', 'datainjection') . "</td>";
echo "<td>";
Dropdown::showYesNo("can_add_dropdown", $this->fields['can_add_dropdown']);
echo "</td>";
Expand Down
9 changes: 8 additions & 1 deletion inc/userinjection.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 4c28f23

Please sign in to comment.