Skip to content

Commit b45efac

Browse files
authored
Fix active entities filtering
1 parent 6f2d01d commit b45efac

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

Diff for: src/Session.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ public static function changeActiveEntities($ID = "all", $is_recursive = false)
414414
}
415415
}
416416
} else {
417+
$ID = (int)$ID;
418+
417419
/// Check entity validity
418420
$ancestors = getAncestorsOf("glpi_entities", $ID);
419421
$ok = false;
@@ -1981,7 +1983,19 @@ public static function getMatchingActiveEntities(/*int|array*/ $entities_ids)/*:
19811983
return [];
19821984
}
19831985

1984-
$active_entities_ids = $_SESSION['glpiactiveentities'] ?? [];
1986+
$active_entities_ids = [];
1987+
foreach ($_SESSION['glpiactiveentities'] ?? [] as $active_entity_id) {
1988+
if (!is_int($active_entity_id) && !ctype_digit($active_entity_id)) {
1989+
// Ensure no unexpected value converted to int
1990+
// as it would be converted to `0` and would permit access to root entity
1991+
trigger_error(
1992+
sprintf('Unexpected value `%s` found in `$_SESSION[\'glpiactiveentities\']`.', $active_entity_id),
1993+
E_USER_WARNING
1994+
);
1995+
continue;
1996+
}
1997+
$active_entities_ids[] = (int)$active_entity_id;
1998+
}
19851999

19862000
if (!is_array($entities_ids) && in_array((int)$entities_ids, $active_entities_ids, true)) {
19872001
return (int)$entities_ids;

Diff for: tests/functional/Session.php

+30-6
Original file line numberDiff line numberDiff line change
@@ -632,21 +632,21 @@ public function testGetRightNameForError($module, $right, $expected)
632632
$this->string(\Session::getRightNameForError($module, $right))->isEqualTo($expected);
633633
}
634634

635-
protected function entitiesRestricProvider(): iterable
635+
protected function entitiesRestrictProvider(): iterable
636636
{
637637
// Special case for -1
638638
foreach ([-1, "-1", [-1], ["-1"]] as $value) {
639639
yield [
640640
'entity_restrict' => $value,
641641
'active_entities' => [0, 1, 2, 3],
642-
'result' => is_array($value) ? [-1] : -1,
642+
'result' => $value,
643643
];
644644
}
645645

646646
// Integer input, matching
647647
yield [
648648
'entity_restrict' => 2,
649-
'active_entities' => [0, 1, 2, 3],
649+
'active_entities' => [0, 1, '2', 3],
650650
'result' => 2,
651651
];
652652

@@ -682,7 +682,7 @@ protected function entitiesRestricProvider(): iterable
682682
yield [
683683
'entity_restrict' => [0, '2', 3, 12, 54, 96],
684684
'active_entities' => [0, 1, 2, 3],
685-
'result' => [0, '2', 3],
685+
'result' => [0, 2, 3],
686686
];
687687

688688
// Array input, NONE matching
@@ -712,14 +712,38 @@ protected function entitiesRestricProvider(): iterable
712712
'active_entities' => [0, 1, 2, 3],
713713
'result' => [0, 3],
714714
];
715+
716+
// Active entity may contain a string value
717+
// do not know why, but is is the case when only one entity is selected
718+
foreach ([2, '2', [2], ['2']] as $entity_restrict) {
719+
yield [
720+
'entity_restrict' => $entity_restrict,
721+
'active_entities' => [0, 1, '2', 3],
722+
'result' => is_array($entity_restrict) ? [2] : 2,
723+
];
724+
}
715725
}
716726

717727
/**
718-
* @dataProvider entitiesRestricProvider
728+
* @dataProvider entitiesRestrictProvider
719729
*/
720730
public function testGetMatchingActiveEntities(/*int|array*/ $entity_restrict, ?array $active_entities, /*int|array*/ $result): void
721731
{
722732
$_SESSION['glpiactiveentities'] = $active_entities;
723-
$this->variable(\Session::getMatchingActiveEntities($entity_restrict))->isEqualTo($result);
733+
$this->variable(\Session::getMatchingActiveEntities($entity_restrict))->isIdenticalTo($result);
734+
}
735+
736+
public function testGetMatchingActiveEntitiesWithUnexpectedValue(): void
737+
{
738+
$_SESSION['glpiactiveentities'] = [0, 1, 2, 'foo', 3];
739+
740+
$this->when(
741+
function () {
742+
$this->variable(\Session::getMatchingActiveEntities([2, 3]))->isIdenticalTo([2, 3]);
743+
}
744+
)->error
745+
->withType(E_USER_WARNING)
746+
->withMessage('Unexpected value `foo` found in `$_SESSION[\'glpiactiveentities\']`.')
747+
->exists();
724748
}
725749
}

0 commit comments

Comments
 (0)