Skip to content

Commit 27164d7

Browse files
authored
Feat(user): add default entity value = Full structure
1 parent 0927a14 commit 27164d7

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

src/Session.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,21 @@ public static function shouldReloadActiveEntities(): bool
392392
if (count($glpiactiveentities)) {
393393
$glpiactive_entity = $_SESSION['glpiactive_entity'];
394394
$glpiactive_entity_recursive = $_SESSION['glpiactive_entity_recursive'] ?? false;
395-
$entities = [$glpiactive_entity];
396-
if ($glpiactive_entity_recursive) {
395+
$entities = [$glpiactive_entity => $glpiactive_entity];
396+
if (
397+
($_SESSION["glpientity_fullstructure"] ?? false)
398+
&& isset($_SESSION['glpiactiveprofile']['entities'])
399+
) {
400+
foreach ($_SESSION['glpiactiveprofile']['entities'] as $val) {
401+
$entities[$val['id']] = $val['id'];
402+
if ($val['is_recursive']) {
403+
$sons = getSonsOf("glpi_entities", $val['id']);
404+
foreach ($sons as $key2 => $val2) {
405+
$entities[$key2] = $key2;
406+
}
407+
}
408+
}
409+
} elseif ($glpiactive_entity_recursive) {
397410
$entities = getSonsOf("glpi_entities", $glpiactive_entity);
398411
}
399412

@@ -421,6 +434,8 @@ public static function changeActiveEntities($ID = "all", $is_recursive = false)
421434
$newentities = [];
422435
$ancestors = [];
423436

437+
$_SESSION["glpientity_fullstructure"] = ($ID === 'all');
438+
424439
if (isset($_SESSION['glpiactiveprofile'])) {
425440
if ($ID === "all") {
426441
foreach ($_SESSION['glpiactiveprofile']['entities'] as $val) {

src/User.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,10 +1093,12 @@ public function prepareInputForUpdate($input)
10931093
}
10941094

10951095
// Security on default entity update
1096-
if (isset($input['entities_id'])) {
1097-
if (!in_array($input['entities_id'], Profile_User::getUserEntities($input['id']))) {
1098-
unset($input['entities_id']);
1099-
}
1096+
if (
1097+
isset($input['entities_id'])
1098+
&& ($input['entities_id'] > 0)
1099+
&& (!in_array($input['entities_id'], Profile_User::getUserEntities($input['id'])))
1100+
) {
1101+
unset($input['entities_id']);
11001102
}
11011103

11021104
// Security on default group update
@@ -2921,9 +2923,14 @@ public function showForm($ID, array $options = [])
29212923
$entrand = mt_rand();
29222924
echo "</td><td><label for='dropdown_entities_id$entrand'>" . __('Default entity') . "</label></td><td>";
29232925
$entities = $this->getEntities();
2926+
$toadd = [];
2927+
if (!in_array(0, $entities)) {
2928+
$toadd = [0 => __('Full structure')];
2929+
}
29242930
Entity::dropdown(['value' => $this->fields["entities_id"],
29252931
'rand' => $entrand,
2926-
'entity' => $entities
2932+
'entity' => $entities,
2933+
'toadd' => $toadd,
29272934
]);
29282935
echo "</td></tr>";
29292936

@@ -3309,9 +3316,14 @@ public function showMyForm($target, $ID)
33093316
) {
33103317
$entrand = mt_rand();
33113318
echo "<td><label for='dropdown_entities_id$entrand'>" . __('Default entity') . "</td><td>";
3319+
$toadd = [];
3320+
if (!in_array(0, $entities)) {
3321+
$toadd = [0 => __('Full structure')];
3322+
}
33123323
Entity::dropdown(['value' => $this->fields['entities_id'],
33133324
'rand' => $entrand,
3314-
'entity' => $entities
3325+
'entity' => $entities,
3326+
'toadd' => $toadd,
33153327
]);
33163328
} else {
33173329
echo "<td colspan='2'>&nbsp;";

0 commit comments

Comments
 (0)