Repository navigation
Conversation
…e active getEntries() is called on every request. It checked the entity visibility and the profile right container by container. Compute visibility once per entity, and read the rights of all visible containers in a single query.
Rom1-B
requested changes
Nov 13, 2026
Comment on lines
+1583
to
+1612
| // First pass: entity restriction. Visibility is computed once per entity instead of | ||
| // once per container, as getSonsOf() of a high level entity can be thousands of rows. | ||
| $visible = []; | ||
| $active_entities = null; | ||
| $visible_by_entity = []; | ||
| foreach ($found as $item) { | ||
| //entities restriction | ||
| if (!in_array($item['entities_id'], $_SESSION['glpiactiveentities'])) { | ||
| $active_entities ??= array_flip($_SESSION['glpiactiveentities']); | ||
| if (!isset($active_entities[(int) $item['entities_id']])) { | ||
| if ($item['is_recursive'] == 1) { | ||
| $entities = getSonsOf('glpi_entities', $item['entities_id']); | ||
| if (array_intersect($entities, $_SESSION['glpiactiveentities']) === []) { | ||
| $visible_by_entity[$item['entities_id']] ??= array_intersect_key( | ||
| array_flip(getSonsOf('glpi_entities', $item['entities_id'])), | ||
| $active_entities, | ||
| ) !== []; | ||
| if (!$visible_by_entity[$item['entities_id']]) { | ||
| continue; | ||
| } | ||
| } else { | ||
| continue; | ||
| } | ||
| } | ||
| $visible[$item['id']] = $item; | ||
| } | ||
|
|
||
| if (Session::isCron() || !isset($_SESSION['glpiactiveprofile']['id'])) { | ||
| continue; | ||
| } | ||
| if ( | ||
| $visible === [] | ||
| || Session::isCron() | ||
| || !isset($_SESSION['glpiactiveprofile']['id']) | ||
| || $_SESSION['glpiactiveprofile']['id'] == null | ||
| ) { | ||
| return $itemtypes; |
Contributor
There was a problem hiding this comment.
If the cron / no profile check moves before the entity loop, those requests skip getSonsOf() entirely and the lazy ??= goes away:
Suggested change
| // First pass: entity restriction. Visibility is computed once per entity instead of | |
| // once per container, as getSonsOf() of a high level entity can be thousands of rows. | |
| $visible = []; | |
| $active_entities = null; | |
| $visible_by_entity = []; | |
| foreach ($found as $item) { | |
| //entities restriction | |
| if (!in_array($item['entities_id'], $_SESSION['glpiactiveentities'])) { | |
| $active_entities ??= array_flip($_SESSION['glpiactiveentities']); | |
| if (!isset($active_entities[(int) $item['entities_id']])) { | |
| if ($item['is_recursive'] == 1) { | |
| $entities = getSonsOf('glpi_entities', $item['entities_id']); | |
| if (array_intersect($entities, $_SESSION['glpiactiveentities']) === []) { | |
| $visible_by_entity[$item['entities_id']] ??= array_intersect_key( | |
| array_flip(getSonsOf('glpi_entities', $item['entities_id'])), | |
| $active_entities, | |
| ) !== []; | |
| if (!$visible_by_entity[$item['entities_id']]) { | |
| continue; | |
| } | |
| } else { | |
| continue; | |
| } | |
| } | |
| $visible[$item['id']] = $item; | |
| } | |
| if (Session::isCron() || !isset($_SESSION['glpiactiveprofile']['id'])) { | |
| continue; | |
| } | |
| if ( | |
| $visible === [] | |
| || Session::isCron() | |
| || !isset($_SESSION['glpiactiveprofile']['id']) | |
| || $_SESSION['glpiactiveprofile']['id'] == null | |
| ) { | |
| return $itemtypes; | |
| if ( | |
| $found === [] | |
| || Session::isCron() | |
| || !isset($_SESSION['glpiactiveprofile']['id']) | |
| || $_SESSION['glpiactiveprofile']['id'] == null | |
| ) { | |
| return $itemtypes; | |
| } | |
| // First pass: entity restriction. Visibility is computed once per entity instead of | |
| // once per container, as getSonsOf() of a high level entity can be thousands of rows. | |
| $visible = []; | |
| $active_entities = array_flip($_SESSION['glpiactiveentities']); | |
| $visible_by_entity = []; | |
| foreach ($found as $item) { | |
| if (!isset($active_entities[(int) $item['entities_id']])) { | |
| if ($item['is_recursive'] == 1) { | |
| $visible_by_entity[$item['entities_id']] ??= array_intersect_key( | |
| array_flip(getSonsOf('glpi_entities', $item['entities_id'])), | |
| $active_entities, | |
| ) !== []; | |
| if (!$visible_by_entity[$item['entities_id']]) { | |
| continue; | |
| } | |
| } else { | |
| continue; | |
| } | |
| } | |
| $visible[$item['id']] = $item; |
Author
|
Thanks, applied: the cron / no active profile check now runs before the entity loop. I also fixed the Rector findings that made the CI fail. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Checklist before requesting a review
Description
PluginFieldsContainer::getEntries(), called byplugin_init_fields()on every authenticated request, checked the entity visibility and the profile right container by container: onegetSonsOf()+array_intersect()per container outside the active entities, and one SQL query per container for the right.Changes:
PluginFieldsProfile::getRightsOnContainers();PluginFieldsProfile::getRightOnContainer()now relies ongetRightsOnContainers(), so the rights query exists only once.The behaviour is unchanged: same entity rule, same
MAX(right)per container and profile, a container without right (no row,NULLor0) is still skipped, and the cron / no active profile cases still return no entry.On an instance with 22 containers on the root entity and 5591 entities,
getEntries()goes from about 47 ms to about 2 ms, with identical results for 39 user/profile combinations and forgetRightOnContainer()on 700 profile × container pairs.New tests in
tests/Units/ContainerGetEntriesTest.phpcover profile rights (none, read, create), recursive and non recursive containers seen from a child entity, the no active profile case, and the equivalence ofgetRightsOnContainers()withgetRightOnContainer().