From 9a73aabe4f08453d16b0dc2d381045a63245ebca Mon Sep 17 00:00:00 2001 From: RomainLvr Date: Thu, 12 Feb 2026 13:55:02 +0100 Subject: [PATCH 1/5] Fix - Loss of profile rights when updating the plugin --- inc/profile.class.php | 52 ++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/inc/profile.class.php b/inc/profile.class.php index a496a1ac..8db1bac0 100644 --- a/inc/profile.class.php +++ b/inc/profile.class.php @@ -108,7 +108,7 @@ public function getFromDBByProfile($profiles_id) ], ]; $result = $DB->request($query); - if ($result->numrows() != 1) { + if (empty($result->numrows())) { return false; } $this->fields = $result->current(); @@ -127,14 +127,23 @@ public static function addRightToAllProfiles() $result_config = $DB->request('SELECT `id` FROM `glpi_plugin_mreporting_configs`'); foreach ($DB->request('SELECT `id` FROM `glpi_profiles`') as $prof) { foreach ($result_config as $report) { - $DB->updateOrInsert('glpi_plugin_mreporting_profiles', [ - 'profiles_id' => $prof['id'], - 'reports' => $report['id'], - 'right' => null, - ], [ - 'profiles_id' => $prof['id'], - 'reports' => $report['id'], + // Only insert if it doesn't exist yet (don't overwrite existing rights) + $existing = $DB->request([ + 'FROM' => 'glpi_plugin_mreporting_profiles', + 'WHERE' => [ + 'profiles_id' => $prof['id'], + 'reports' => $report['id'], + ], + 'LIMIT' => 1 ]); + + if ($existing->numrows() == 0) { + $DB->insert('glpi_plugin_mreporting_profiles', [ + 'profiles_id' => $prof['id'], + 'reports' => $report['id'], + 'right' => null, + ]); + } } } } @@ -168,16 +177,23 @@ public static function addRightToProfile($idProfile) //get all reports $config = new PluginMreportingConfig(); foreach ($config->find() as $report) { - // add right for any reports for profile - // Add manual request because Add function get error : right is set to NULL - $DB->updateOrInsert('glpi_plugin_mreporting_profiles', [ - 'profiles_id' => $idProfile, - 'reports' => $report['id'], - 'right' => READ, - ], [ - 'profiles_id' => $idProfile, - 'reports' => $report['id'], - ]) or die('An error occurs during profile initialisation.'); + // Only insert if it doesn't exist yet (don't overwrite existing rights) + $existing = $DB->request([ + 'FROM' => 'glpi_plugin_mreporting_profiles', + 'WHERE' => [ + 'profiles_id' => $idProfile, + 'reports' => $report['id'], + ], + 'LIMIT' => 1 + ]); + + if ($existing->numrows() == 0) { + $DB->insert('glpi_plugin_mreporting_profiles', [ + 'profiles_id' => $idProfile, + 'reports' => $report['id'], + 'right' => READ, + ]) or die('An error occurs during profile initialisation.'); + } } } From 29d8e8c8689835aa773c6e3dbecc31d8b4d8edc3 Mon Sep 17 00:00:00 2001 From: RomainLvr Date: Thu, 12 Feb 2026 13:56:44 +0100 Subject: [PATCH 2/5] Update CHANGELOG --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 83f9a123..6cee62ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [UNRELEASED] + +### Fixed + +- Fix the loss of profile rights when updating the plugin ## [1.8.11] - 2025-02-05 From 404cd8cf128175ebe2161db466c32d031d2481c9 Mon Sep 17 00:00:00 2001 From: RomainLvr Date: Thu, 12 Feb 2026 14:46:00 +0100 Subject: [PATCH 3/5] Fix lints --- inc/profile.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/profile.class.php b/inc/profile.class.php index 8db1bac0..8ed130c2 100644 --- a/inc/profile.class.php +++ b/inc/profile.class.php @@ -134,7 +134,7 @@ public static function addRightToAllProfiles() 'profiles_id' => $prof['id'], 'reports' => $report['id'], ], - 'LIMIT' => 1 + 'LIMIT' => 1, ]); if ($existing->numrows() == 0) { @@ -184,7 +184,7 @@ public static function addRightToProfile($idProfile) 'profiles_id' => $idProfile, 'reports' => $report['id'], ], - 'LIMIT' => 1 + 'LIMIT' => 1, ]); if ($existing->numrows() == 0) { From df04d385cd166e00ead85edd1383b6a605019c44 Mon Sep 17 00:00:00 2001 From: RomainLvr Date: Fri, 13 Feb 2026 10:41:41 +0100 Subject: [PATCH 4/5] Apply suggestions --- inc/profile.class.php | 93 ++++++++++++++++++++++++------------------- 1 file changed, 52 insertions(+), 41 deletions(-) diff --git a/inc/profile.class.php b/inc/profile.class.php index 8ed130c2..80ba6cc1 100644 --- a/inc/profile.class.php +++ b/inc/profile.class.php @@ -124,27 +124,35 @@ public static function addRightToAllProfiles() /** @var \DBmysql $DB */ global $DB; - $result_config = $DB->request('SELECT `id` FROM `glpi_plugin_mreporting_configs`'); - foreach ($DB->request('SELECT `id` FROM `glpi_profiles`') as $prof) { - foreach ($result_config as $report) { - // Only insert if it doesn't exist yet (don't overwrite existing rights) - $existing = $DB->request([ - 'FROM' => 'glpi_plugin_mreporting_profiles', - 'WHERE' => [ - 'profiles_id' => $prof['id'], - 'reports' => $report['id'], - ], - 'LIMIT' => 1, - ]); - - if ($existing->numrows() == 0) { - $DB->insert('glpi_plugin_mreporting_profiles', [ - 'profiles_id' => $prof['id'], - 'reports' => $report['id'], - 'right' => null, - ]); - } - } + // Find all missing profile/report combinations using a single optimized query + $iterator = $DB->request([ + 'SELECT' => [ + 'configs.id AS reports', + 'profiles.id AS profiles_id' + ], + 'FROM' => [ + 'glpi_plugin_mreporting_configs AS configs', + 'glpi_profiles AS profiles' + ], + 'LEFT JOIN' => [ + 'glpi_plugin_mreporting_profiles AS mreporting_profiles' => [ + 'ON' => [ + 'mreporting_profiles.profiles_id' => new \QueryExpression($DB->quoteName('profiles.id')), + 'mreporting_profiles.reports' => new \QueryExpression($DB->quoteName('configs.id')) + ] + ] + ], + 'WHERE' => [ + 'mreporting_profiles.id' => null + ] + ]); + + foreach ($iterator as $row) { + $DB->insert('glpi_plugin_mreporting_profiles', [ + 'profiles_id' => $row['profiles_id'], + 'reports' => $row['reports'], + 'right' => null, + ]); } } @@ -174,26 +182,29 @@ public static function addRightToProfile($idProfile) /** @var \DBmysql $DB */ global $DB; - //get all reports - $config = new PluginMreportingConfig(); - foreach ($config->find() as $report) { - // Only insert if it doesn't exist yet (don't overwrite existing rights) - $existing = $DB->request([ - 'FROM' => 'glpi_plugin_mreporting_profiles', - 'WHERE' => [ - 'profiles_id' => $idProfile, - 'reports' => $report['id'], - ], - 'LIMIT' => 1, - ]); - - if ($existing->numrows() == 0) { - $DB->insert('glpi_plugin_mreporting_profiles', [ - 'profiles_id' => $idProfile, - 'reports' => $report['id'], - 'right' => READ, - ]) or die('An error occurs during profile initialisation.'); - } + // Find all missing reports for this specific profile using an optimized query + $iterator = $DB->request([ + 'SELECT' => 'configs.id AS reports', + 'FROM' => 'glpi_plugin_mreporting_configs AS configs', + 'LEFT JOIN' => [ + 'glpi_plugin_mreporting_profiles AS mreporting_profiles' => [ + 'ON' => [ + 'mreporting_profiles.reports' => new \QueryExpression($DB->quoteName('configs.id')), + 'mreporting_profiles.profiles_id' => $idProfile + ] + ] + ], + 'WHERE' => [ + 'mreporting_profiles.id' => null + ] + ]); + + foreach ($iterator as $row) { + $DB->insert('glpi_plugin_mreporting_profiles', [ + 'profiles_id' => $idProfile, + 'reports' => $row['reports'], + 'right' => READ, + ]) or die('An error occurs during profile initialisation.'); } } From c49da08e4de35b2916126641166a51234501f923 Mon Sep 17 00:00:00 2001 From: RomainLvr Date: Fri, 13 Feb 2026 10:47:44 +0100 Subject: [PATCH 5/5] Fix Lints --- inc/profile.class.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/inc/profile.class.php b/inc/profile.class.php index 80ba6cc1..fe070adb 100644 --- a/inc/profile.class.php +++ b/inc/profile.class.php @@ -128,23 +128,23 @@ public static function addRightToAllProfiles() $iterator = $DB->request([ 'SELECT' => [ 'configs.id AS reports', - 'profiles.id AS profiles_id' + 'profiles.id AS profiles_id', ], 'FROM' => [ 'glpi_plugin_mreporting_configs AS configs', - 'glpi_profiles AS profiles' + 'glpi_profiles AS profiles', ], 'LEFT JOIN' => [ 'glpi_plugin_mreporting_profiles AS mreporting_profiles' => [ 'ON' => [ 'mreporting_profiles.profiles_id' => new \QueryExpression($DB->quoteName('profiles.id')), - 'mreporting_profiles.reports' => new \QueryExpression($DB->quoteName('configs.id')) - ] - ] + 'mreporting_profiles.reports' => new \QueryExpression($DB->quoteName('configs.id')), + ], + ], ], 'WHERE' => [ - 'mreporting_profiles.id' => null - ] + 'mreporting_profiles.id' => null, + ], ]); foreach ($iterator as $row) { @@ -190,13 +190,13 @@ public static function addRightToProfile($idProfile) 'glpi_plugin_mreporting_profiles AS mreporting_profiles' => [ 'ON' => [ 'mreporting_profiles.reports' => new \QueryExpression($DB->quoteName('configs.id')), - 'mreporting_profiles.profiles_id' => $idProfile - ] - ] + 'mreporting_profiles.profiles_id' => $idProfile, + ], + ], ], 'WHERE' => [ - 'mreporting_profiles.id' => null - ] + 'mreporting_profiles.id' => null, + ], ]); foreach ($iterator as $row) {