diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b19cb7d..a0326ed2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fix search criterion value on multiple dropdown fields being reset after a search, which emptied the criterion when sorting or paging results - Fix item clone failing when a mandatory GLPI item field is in an "Insert in form" block - Fix `Some mandatory fields are empty` error displayed when creating an item with an assigned technician +- 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 a18b68df..45e79462 100644 --- a/inc/container.class.php +++ b/inc/container.class.php @@ -1584,12 +1584,29 @@ public static function getEntries($type = 'tab', $full = false): array $itemtypes = []; $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 = array_flip($_SESSION['glpiactiveentities']); + $visible_by_entity = []; foreach ($found as $item) { - //entities restriction - if (!in_array($item['entities_id'], $_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 { @@ -1597,17 +1614,18 @@ public static function getEntries($type = 'tab', $full = false): array } } - if (Session::isCron() || !isset($_SESSION['glpiactiveprofile']['id'])) { - continue; - } + $visible[$item['id']] = $item; + } + + // 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..d0e38b53 100644 --- a/inc/profile.class.php +++ b/inc/profile.class.php @@ -219,21 +219,39 @@ 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[] $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..1633a635 --- /dev/null +++ b/tests/Units/ContainerGetEntriesTest.php @@ -0,0 +1,159 @@ +. + * ------------------------------------------------------------------------- + * @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]); + } +}