From 2f58e42eecf9098d37e062fcff1ef734b0db2184 Mon Sep 17 00:00:00 2001 From: Cedric Grard Date: Thu, 12 Nov 2026 16:36:14 +0100 Subject: [PATCH 1/2] Fix(Core): Optimize containers tabs computation when many entities are 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. --- CHANGELOG.md | 1 + inc/container.class.php | 44 +++++-- inc/profile.class.php | 40 ++++-- tests/Units/ContainerGetEntriesTest.php | 158 ++++++++++++++++++++++++ 4 files changed, 220 insertions(+), 23 deletions(-) create mode 100644 tests/Units/ContainerGetEntriesTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index a57ea7bf..ddc598c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fix form submission error when a multiple dropdown field question is left empty - Fix search criterion value on multiple dropdown fields being reset after a search, which emptied the criterion when sorting or paging results +- Fix slow page loads when many entities are active: the containers available to the user were computed with one query per container on every request ## [1.24.7] - 2026-10-28 diff --git a/inc/container.class.php b/inc/container.class.php index a5a78af4..bfe5ee47 100644 --- a/inc/container.class.php +++ b/inc/container.class.php @@ -1579,30 +1579,48 @@ public static function getEntries($type = 'tab', $full = false): array $itemtypes = []; $container = new self(); $found = $container->find($condition, 'label'); + + // 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; + } + + // Rights of the active profile on all visible containers, in a single query. + $rights = PluginFieldsProfile::getRightsOnContainers( + (int) $_SESSION['glpiactiveprofile']['id'], + array_keys($visible), + ); + foreach ($visible as $item) { //profiles restriction - if (isset($_SESSION['glpiactiveprofile']['id']) && $_SESSION['glpiactiveprofile']['id'] != null && $item['id'] > 0) { - $right = PluginFieldsProfile::getRightOnContainer($_SESSION['glpiactiveprofile']['id'], $item['id']); - if ($right < READ) { - continue; - } - } else { + if (($rights[$item['id']] ?? 0) < READ) { continue; } diff --git a/inc/profile.class.php b/inc/profile.class.php index 6cd97e32..652ffb99 100644 --- a/inc/profile.class.php +++ b/inc/profile.class.php @@ -219,21 +219,41 @@ public static function deleteProfile(Profile $profile) } public static function getRightOnContainer(int $profile_id, int $container_id): int + { + return self::getRightsOnContainers($profile_id, [$container_id])[$container_id] ?? 0; + } + + /** + * Get rights of a profile on several containers, in a single query. + * + * @param int $profile_id + * @param int[] $container_ids + * + * @return array rights indexed by container id, containers without right are absent + */ + public static function getRightsOnContainers(int $profile_id, array $container_ids): array { /** @var DBmysql $DB */ global $DB; - $container_profile = $DB->request( - [ - 'SELECT' => ['MAX' => 'right AS right'], - 'FROM' => self::getTable(), - 'WHERE' => [ - 'profiles_id' => $profile_id, - 'plugin_fields_containers_id' => $container_id, - ], + if ($container_ids === []) { + return []; + } + + $rights = []; + $iterator = $DB->request([ + 'SELECT' => ['plugin_fields_containers_id', 'MAX' => 'right AS right'], + 'FROM' => self::getTable(), + 'WHERE' => [ + 'profiles_id' => $profile_id, + 'plugin_fields_containers_id' => $container_ids, ], - ); + 'GROUPBY' => 'plugin_fields_containers_id', + ]); + foreach ($iterator as $row) { + $rights[(int) $row['plugin_fields_containers_id']] = (int) $row['right']; + } - return (int) $container_profile->current()['right']; + return $rights; } } diff --git a/tests/Units/ContainerGetEntriesTest.php b/tests/Units/ContainerGetEntriesTest.php new file mode 100644 index 00000000..edef226d --- /dev/null +++ b/tests/Units/ContainerGetEntriesTest.php @@ -0,0 +1,158 @@ +. + * ------------------------------------------------------------------------- + * @copyright Copyright (C) 2013-2023 by Fields plugin team. + * @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html + * @link https://github.com/pluginsGLPI/fields + * ------------------------------------------------------------------------- + */ + +declare(strict_types=1); + +namespace GlpiPlugin\Field\Tests\Units; + +use Computer; +use Entity; +use Glpi\Tests\DbTestCase; +use Glpi\Tests\GLPITestCase; +use GlpiPlugin\Field\Tests\FieldTestTrait; +use Monitor; +use PluginFieldsContainer; +use PluginFieldsProfile; +use Printer; + +require_once __DIR__ . '/../FieldTestCase.php'; + +final class ContainerGetEntriesTest extends DbTestCase +{ + use FieldTestTrait; + + public function setUp(): void + { + GLPITestCase::setUp(); + $this->login(); + } + + public function tearDown(): void + { + $this->tearDownFieldTest(); + GLPITestCase::tearDown(); + } + + public function testGetEntriesFollowsProfileRightsAndEntities(): void + { + $root_entity_id = getItemByTypeName(Entity::class, '_test_root_entity', true); + $child_entity_id = getItemByTypeName(Entity::class, '_test_child_1', true); + + // Recursive container on the root entity, readable + $readable = $this->createTabContainer(Computer::class, $root_entity_id, true); + $this->setRightOnContainer($readable->getID(), READ); + + // Recursive container on the root entity, without right + $no_right = $this->createTabContainer(Monitor::class, $root_entity_id, true); + $this->setRightOnContainer($no_right->getID(), 0); + + // Non recursive container on the root entity, writable + $not_recursive = $this->createTabContainer(Printer::class, $root_entity_id, false); + $this->setRightOnContainer($not_recursive->getID(), CREATE); + + // From the root entity (with sub-entities), readable and writable containers are listed + $this->setEntity($root_entity_id, true); + $entries = PluginFieldsContainer::getEntries(); + $this->assertContains(Computer::class, $entries); + $this->assertNotContains(Monitor::class, $entries); + $this->assertContains(Printer::class, $entries); + + // From a child entity only, the non recursive container of the parent is not listed + $this->setEntity($child_entity_id, false); + $entries = PluginFieldsContainer::getEntries(); + $this->assertContains(Computer::class, $entries); + $this->assertNotContains(Monitor::class, $entries); + $this->assertNotContains(Printer::class, $entries); + } + + public function testGetEntriesWithoutActiveProfile(): void + { + $root_entity_id = getItemByTypeName(Entity::class, '_test_root_entity', true); + $this->createTabContainer(Computer::class, $root_entity_id, true); + $this->setEntity($root_entity_id, true); + + $profile = $_SESSION['glpiactiveprofile']; + try { + $_SESSION['glpiactiveprofile']['id'] = 0; + $this->assertSame([], PluginFieldsContainer::getEntries()); + + unset($_SESSION['glpiactiveprofile']); + $this->assertSame([], PluginFieldsContainer::getEntries()); + } finally { + $_SESSION['glpiactiveprofile'] = $profile; + } + } + + public function testGetRightsOnContainersMatchesGetRightOnContainer(): void + { + $root_entity_id = getItemByTypeName(Entity::class, '_test_root_entity', true); + $containers = [ + $this->createTabContainer(Computer::class, $root_entity_id, true), + $this->createTabContainer(Monitor::class, $root_entity_id, true), + $this->createTabContainer(Printer::class, $root_entity_id, true), + ]; + $this->setRightOnContainer($containers[0]->getID(), 0); + $this->setRightOnContainer($containers[1]->getID(), READ); + $this->setRightOnContainer($containers[2]->getID(), CREATE); + + $profile_id = (int) $_SESSION['glpiactiveprofile']['id']; + $ids = array_map(static fn($container) => $container->getID(), $containers); + $rights = PluginFieldsProfile::getRightsOnContainers($profile_id, $ids); + + foreach ($ids as $id) { + $this->assertSame(PluginFieldsProfile::getRightOnContainer($profile_id, $id), $rights[$id] ?? 0); + } + $this->assertSame([0, READ, CREATE], array_map(static fn($id) => $rights[$id] ?? 0, $ids)); + $this->assertSame([], PluginFieldsProfile::getRightsOnContainers($profile_id, [])); + $this->assertSame(0, PluginFieldsProfile::getRightOnContainer($profile_id, 999999)); + } + + private function createTabContainer(string $itemtype, int $entities_id, bool $is_recursive): PluginFieldsContainer + { + return $this->createFieldContainer([ + 'label' => 'Tab container ' . $this->getUniqueString(), + 'type' => 'tab', + 'itemtypes' => [$itemtype], + 'is_active' => 1, + 'entities_id' => $entities_id, + 'is_recursive' => $is_recursive ? 1 : 0, + ]); + } + + private function setRightOnContainer(int $containers_id, int $right): void + { + $profile_right = new PluginFieldsProfile(); + $this->assertTrue($profile_right->getFromDBByCrit([ + 'profiles_id' => $_SESSION['glpiactiveprofile']['id'], + 'plugin_fields_containers_id' => $containers_id, + ])); + $this->updateItem(PluginFieldsProfile::class, $profile_right->getID(), ['right' => $right]); + } +} From 1347d5f215775e2bb4fa5c23bd056a4b8ffc39bc Mon Sep 17 00:00:00 2001 From: Cedric Grard Date: Fri, 13 Nov 2026 09:08:57 +0100 Subject: [PATCH 2/2] Apply review suggestions --- inc/container.class.php | 22 +++++++++++----------- inc/profile.class.php | 2 -- tests/Units/ContainerGetEntriesTest.php | 1 + 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/inc/container.class.php b/inc/container.class.php index bfe5ee47..b41bbf71 100644 --- a/inc/container.class.php +++ b/inc/container.class.php @@ -1580,13 +1580,21 @@ public static function getEntries($type = 'tab', $full = false): array $container = new self(); $found = $container->find($condition, 'label'); + 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 = null; + $active_entities = array_flip($_SESSION['glpiactiveentities']); $visible_by_entity = []; foreach ($found as $item) { - $active_entities ??= array_flip($_SESSION['glpiactiveentities']); if (!isset($active_entities[(int) $item['entities_id']])) { if ($item['is_recursive'] == 1) { $visible_by_entity[$item['entities_id']] ??= array_intersect_key( @@ -1600,16 +1608,8 @@ public static function getEntries($type = 'tab', $full = false): array continue; } } - $visible[$item['id']] = $item; - } - if ( - $visible === [] - || Session::isCron() - || !isset($_SESSION['glpiactiveprofile']['id']) - || $_SESSION['glpiactiveprofile']['id'] == null - ) { - return $itemtypes; + $visible[$item['id']] = $item; } // Rights of the active profile on all visible containers, in a single query. diff --git a/inc/profile.class.php b/inc/profile.class.php index 652ffb99..d0e38b53 100644 --- a/inc/profile.class.php +++ b/inc/profile.class.php @@ -226,9 +226,7 @@ public static function getRightOnContainer(int $profile_id, int $container_id): /** * Get rights of a profile on several containers, in a single query. * - * @param int $profile_id * @param int[] $container_ids - * * @return array rights indexed by container id, containers without right are absent */ public static function getRightsOnContainers(int $profile_id, array $container_ids): array diff --git a/tests/Units/ContainerGetEntriesTest.php b/tests/Units/ContainerGetEntriesTest.php index edef226d..1633a635 100644 --- a/tests/Units/ContainerGetEntriesTest.php +++ b/tests/Units/ContainerGetEntriesTest.php @@ -129,6 +129,7 @@ public function testGetRightsOnContainersMatchesGetRightOnContainer(): void foreach ($ids as $id) { $this->assertSame(PluginFieldsProfile::getRightOnContainer($profile_id, $id), $rights[$id] ?? 0); } + $this->assertSame([0, READ, CREATE], array_map(static fn($id) => $rights[$id] ?? 0, $ids)); $this->assertSame([], PluginFieldsProfile::getRightsOnContainers($profile_id, [])); $this->assertSame(0, PluginFieldsProfile::getRightOnContainer($profile_id, 999999));