Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix user default entity injection #433

Merged
merged 12 commits into from
Nov 28, 2024
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