-
- Notifications
You must be signed in to change notification settings - Fork 1.5k
Fix - Notification template cache shared between users #22104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RomainLvr wants to merge 1 commit into glpi-project:10.0/bugfixes Choose a base branch from RomainLvr:fix/notification-template-cache-per-user
base: 10.0/bugfixes
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+248 −13
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
219 changes: 219 additions & 0 deletions 219 phpunit/functional/NotificationTemplateUserSpecificTest.php
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,219 @@ | ||
| <?php | ||
| | ||
| /** | ||
| * --------------------------------------------------------------------- | ||
| * | ||
| * GLPI - Gestionnaire Libre de Parc Informatique | ||
| * | ||
| * http://glpi-project.org | ||
| * | ||
| * @copyright 2015-2025 Teclib' and contributors. | ||
| * @licence https://www.gnu.org/licenses/gpl-3.0.html | ||
| * | ||
| * --------------------------------------------------------------------- | ||
| * | ||
| * LICENSE | ||
| * | ||
| * This file is part of GLPI. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| * | ||
| * --------------------------------------------------------------------- | ||
| */ | ||
| | ||
| namespace tests\units; | ||
| | ||
| use DbTestCase; | ||
| | ||
| /** | ||
| * Test for user-specific template generation fix. | ||
| * This validates the fix for the bug where users of the same type were sharing | ||
| * the same cached template, causing plugins to generate identical data (like tokens) | ||
| * for all users of the same type. | ||
| * | ||
| * @see https://github.com/glpi-project/glpi/pull/20186 | ||
| */ | ||
| class NotificationTemplateUserSpecificTest extends DbTestCase | ||
| { | ||
| /** | ||
| * Test that each user gets a unique template cache key | ||
| * even when they have the same language and user type. | ||
| */ | ||
| public function testUniqueTemplateCacheKeyPerUser(): void | ||
| { | ||
| $this->login(); | ||
| | ||
| $template = new \NotificationTemplate(); | ||
| $template->getFromDB(1); // Use an existing template | ||
| | ||
| $user = new \User(); | ||
| $target = new \NotificationTargetUser(0, 'passwordexpires', $user); | ||
| | ||
| // Two different users with the same language and type | ||
| $user1_infos = [ | ||
| 'language' => 'en_GB', | ||
| 'users_id' => 2, // TU_USER | ||
| 'additionnaloption' => ['usertype' => \NotificationTarget::GLPI_USER], | ||
| ]; | ||
| | ||
| $user2_infos = [ | ||
| 'language' => 'en_GB', // Same language as user1 | ||
| 'users_id' => 4, // tech user | ||
| 'additionnaloption' => ['usertype' => \NotificationTarget::GLPI_USER], // Same type as user1 | ||
| ]; | ||
| | ||
| // Reset template cache to start fresh | ||
| $template->resetComputedTemplates(); | ||
| | ||
| // Get template for first user | ||
| $template_id1 = $template->getTemplateByLanguage($target, $user1_infos, 'passwordexpires', []); | ||
| $this->assertNotFalse($template_id1); | ||
| | ||
| // Get template for second user (same language and type, but different users_id) | ||
| $template_id2 = $template->getTemplateByLanguage($target, $user2_infos, 'passwordexpires', []); | ||
| $this->assertNotFalse($template_id2); | ||
| | ||
| // Verify that different template IDs are generated for different users | ||
| $this->assertNotEquals( | ||
| $template_id1, | ||
| $template_id2, | ||
| 'Each user should get a unique template cache key to ensure user-specific data is generated' | ||
| ); | ||
| } | ||
| | ||
| /** | ||
| * Test that current_user_infos is properly set on NotificationTarget | ||
| * when getForTemplate is called. | ||
| */ | ||
| public function testCurrentUserInfosIsSetOnTarget(): void | ||
| { | ||
| $this->login(); | ||
| | ||
| $user = new \User(); | ||
| $target = new \NotificationTargetUser(0, 'passwordexpires', $user); | ||
| | ||
| $user_infos = [ | ||
| 'language' => 'en_GB', | ||
| 'users_id' => 2, | ||
| 'email' => 'test@example.com', | ||
| 'additionnaloption' => ['usertype' => \NotificationTarget::GLPI_USER], | ||
| ]; | ||
| | ||
| $options = [ | ||
| '_user_infos' => $user_infos, | ||
| 'additionnaloption' => $user_infos['additionnaloption'], | ||
| ]; | ||
| | ||
| // Call getForTemplate with user_infos in options | ||
| $target->getForTemplate('passwordexpires', $options); | ||
| | ||
| // Verify that current_user_infos is set | ||
| $this->assertNotEmpty($target->current_user_infos); | ||
| $this->assertEquals($user_infos['users_id'], $target->current_user_infos['users_id']); | ||
| $this->assertEquals($user_infos['email'], $target->current_user_infos['email']); | ||
| } | ||
| | ||
| /** | ||
| * Test that different email users get unique template cache keys | ||
| */ | ||
| public function testUniqueTemplateCacheKeyPerEmailUser(): void | ||
| { | ||
| $this->login(); | ||
| | ||
| $template = new \NotificationTemplate(); | ||
| $template->getFromDB(1); | ||
| | ||
| $user = new \User(); | ||
| $target = new \NotificationTargetUser(0, 'passwordexpires', $user); | ||
| | ||
| $email_user1 = [ | ||
| 'language' => 'en_GB', | ||
| 'email' => 'user1@example.com', | ||
| 'additionnaloption' => ['usertype' => \NotificationTarget::EXTERNAL_USER], | ||
| ]; | ||
| | ||
| $email_user2 = [ | ||
| 'language' => 'en_GB', | ||
| 'email' => 'user2@example.com', | ||
| 'additionnaloption' => ['usertype' => \NotificationTarget::EXTERNAL_USER], | ||
| ]; | ||
| | ||
| $template->resetComputedTemplates(); | ||
| | ||
| $template_id1 = $template->getTemplateByLanguage($target, $email_user1, 'passwordexpires', []); | ||
| $template_id2 = $template->getTemplateByLanguage($target, $email_user2, 'passwordexpires', []); | ||
| | ||
| $this->assertNotFalse($template_id1); | ||
| $this->assertNotFalse($template_id2); | ||
| $this->assertNotEquals( | ||
| $template_id1, | ||
| $template_id2, | ||
| 'Email users should get unique templates based on their email address' | ||
| ); | ||
| } | ||
| | ||
| /** | ||
| * Test that mixed users (GLPI users and email users) all get unique templates | ||
| */ | ||
| public function testMixedUserTypesGetUniqueTemplates(): void | ||
| { | ||
| $this->login(); | ||
| | ||
| $template = new \NotificationTemplate(); | ||
| $template->getFromDB(1); | ||
| | ||
| $user = new \User(); | ||
| $target = new \NotificationTargetUser(0, 'passwordexpires', $user); | ||
| | ||
| $scenarios = [ | ||
| 'glpi_user_1' => [ | ||
| 'language' => 'en_GB', | ||
| 'users_id' => 2, | ||
| 'additionnaloption' => ['usertype' => \NotificationTarget::GLPI_USER], | ||
| ], | ||
| 'glpi_user_2' => [ | ||
| 'language' => 'en_GB', | ||
| 'users_id' => 4, | ||
| 'additionnaloption' => ['usertype' => \NotificationTarget::GLPI_USER], | ||
| ], | ||
| 'external_email_1' => [ | ||
| 'language' => 'en_GB', | ||
| 'email' => 'external1@example.com', | ||
| 'additionnaloption' => ['usertype' => \NotificationTarget::EXTERNAL_USER], | ||
| ], | ||
| 'external_email_2' => [ | ||
| 'language' => 'en_GB', | ||
| 'email' => 'external2@example.com', | ||
| 'additionnaloption' => ['usertype' => \NotificationTarget::EXTERNAL_USER], | ||
| ], | ||
| ]; | ||
| | ||
| $template->resetComputedTemplates(); | ||
| $template_ids = []; | ||
| | ||
| foreach ($scenarios as $name => $user_infos) { | ||
| $tid = $template->getTemplateByLanguage($target, $user_infos, 'passwordexpires', []); | ||
| $this->assertNotFalse($tid, "Template generation should succeed for scenario: $name"); | ||
| $template_ids[$name] = $tid; | ||
| } | ||
| | ||
| // All template IDs should be unique | ||
| $unique_ids = array_unique($template_ids); | ||
| $this->assertCount( | ||
| count($scenarios), | ||
| $unique_ids, | ||
| 'All scenarios should generate unique template cache keys' | ||
| ); | ||
| } | ||
| } |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can simplify the solution and do it only in the plugin by passing the user ID in
additionaloption?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually considered this approach, but unfortunately it won't work due to how the template caching mechanism works.
The issue is that
additionnaloptionis already included in the cache key (viaserialize($additionnaloption)). So in theory, addingusers_idtoadditionnaloptionfrom the plugin should create unique cache keys per user.However, the problem is when the plugin hook is called:
NotificationEventAbstract::raise()loops over each user and callsgetTemplateByLanguage()getTemplateByLanguage()computes the cache key using$user_infos['additionnaloption'](which comes from the core, not the plugin)getForTemplate()which triggers theITEM_GET_DATAhook where plugins can add dataplugin_approvalbymail_get_datas) is called after the cache key is computed and inside the cache miss blockSo even if the plugin wanted to add
users_idtoadditionnaloption, it's too late - the cache decision has already been made based on the originaladditionnaloptionfrom the core.The only way to solve this from the plugin side would be to somehow modify
$user_infos['additionnaloption']beforegetTemplateByLanguage()is called, but there's no hook available at that point.That's why the fix needs to be in the core: either by including the user identifier in the cache key (as done here), or by providing a hook before the cache key computation.