Skip to content

Commit

Permalink
Feat(user): add default entity value = Full structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Rom1-B authored Oct 9, 2024
1 parent 0927a14 commit 27164d7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
19 changes: 17 additions & 2 deletions src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,21 @@ public static function shouldReloadActiveEntities(): bool
if (count($glpiactiveentities)) {
$glpiactive_entity = $_SESSION['glpiactive_entity'];
$glpiactive_entity_recursive = $_SESSION['glpiactive_entity_recursive'] ?? false;
$entities = [$glpiactive_entity];
if ($glpiactive_entity_recursive) {
$entities = [$glpiactive_entity => $glpiactive_entity];
if (
($_SESSION["glpientity_fullstructure"] ?? false)
&& isset($_SESSION['glpiactiveprofile']['entities'])
) {
foreach ($_SESSION['glpiactiveprofile']['entities'] as $val) {
$entities[$val['id']] = $val['id'];
if ($val['is_recursive']) {
$sons = getSonsOf("glpi_entities", $val['id']);
foreach ($sons as $key2 => $val2) {
$entities[$key2] = $key2;
}
}
}
} elseif ($glpiactive_entity_recursive) {
$entities = getSonsOf("glpi_entities", $glpiactive_entity);
}

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

$_SESSION["glpientity_fullstructure"] = ($ID === 'all');

if (isset($_SESSION['glpiactiveprofile'])) {
if ($ID === "all") {
foreach ($_SESSION['glpiactiveprofile']['entities'] as $val) {
Expand Down
24 changes: 18 additions & 6 deletions src/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -1093,10 +1093,12 @@ public function prepareInputForUpdate($input)
}

// Security on default entity update
if (isset($input['entities_id'])) {
if (!in_array($input['entities_id'], Profile_User::getUserEntities($input['id']))) {
unset($input['entities_id']);
}
if (
isset($input['entities_id'])
&& ($input['entities_id'] > 0)
&& (!in_array($input['entities_id'], Profile_User::getUserEntities($input['id'])))
) {
unset($input['entities_id']);
}

// Security on default group update
Expand Down Expand Up @@ -2921,9 +2923,14 @@ public function showForm($ID, array $options = [])
$entrand = mt_rand();
echo "</td><td><label for='dropdown_entities_id$entrand'>" . __('Default entity') . "</label></td><td>";
$entities = $this->getEntities();
$toadd = [];
if (!in_array(0, $entities)) {
$toadd = [0 => __('Full structure')];
}
Entity::dropdown(['value' => $this->fields["entities_id"],
'rand' => $entrand,
'entity' => $entities
'entity' => $entities,
'toadd' => $toadd,
]);
echo "</td></tr>";

Expand Down Expand Up @@ -3309,9 +3316,14 @@ public function showMyForm($target, $ID)
) {
$entrand = mt_rand();
echo "<td><label for='dropdown_entities_id$entrand'>" . __('Default entity') . "</td><td>";
$toadd = [];
if (!in_array(0, $entities)) {
$toadd = [0 => __('Full structure')];
}
Entity::dropdown(['value' => $this->fields['entities_id'],
'rand' => $entrand,
'entity' => $entities
'entity' => $entities,
'toadd' => $toadd,
]);
} else {
echo "<td colspan='2'>&nbsp;";
Expand Down

0 comments on commit 27164d7

Please sign in to comment.