From 83a70daf688ff78abf9caf2af9fa497cef670a64 Mon Sep 17 00:00:00 2001 From: Anne-Catherine <57360138+accognet@users.noreply.github.com> Date: Wed, 8 Nov 2023 14:57:28 +0100 Subject: [PATCH] =?UTF-8?q?N=C2=B06887=20-=20Fix=20excessive=20OQL=20reque?= =?UTF-8?q?sts=20to=20display=20user's=20grant=20matrix=20(#564)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * N°6887 - Fix excessive OQL requests to display user's grant matrix * N°6887 - Rename variable and add PHPDoc --------- Co-authored-by: Molkobain --- .../userrightsprofile.class.inc.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/addons/userrights/userrightsprofile.class.inc.php b/addons/userrights/userrightsprofile.class.inc.php index 4d2b352d2..9e0ed3230 100644 --- a/addons/userrights/userrightsprofile.class.inc.php +++ b/addons/userrights/userrightsprofile.class.inc.php @@ -425,6 +425,12 @@ class UserRightsProfile extends UserRightsAddOnAPI UR_ACTION_BULK_DELETE => 'bd', ); + /** + * @var array $aUsersProfilesList Cache of users' profiles. Hash array of user ID => [profile ID => profile friendlyname, profile ID => profile friendlyname, ...] + * @since 2.7.10 3.0.4 3.1.1 3.2.0 N°6887 + */ + private $aUsersProfilesList = []; + // Installation: create the very first user public function CreateAdministrator($sAdminUser, $sAdminPwd, $sLanguage = 'EN US') { @@ -654,8 +660,12 @@ class UserRightsProfile extends UserRightsAddOnAPI $sAction = self::$m_aActionCodes[$iActionCode]; $bStatus = null; + // Cache user's profiles + if(false === array_key_exists($iUser, $this->aUsersProfilesList)){ + $this->aUsersProfilesList[$iUser] = UserRights::ListProfiles($oUser); + } // Call the API of UserRights because it caches the list for us - foreach(UserRights::ListProfiles($oUser) as $iProfile => $oProfile) + foreach($this->aUsersProfilesList[$iUser] as $iProfile => $oProfile) { $bGrant = $this->GetProfileActionGrant($iProfile, $sClass, $sAction); if (!is_null($bGrant)) @@ -781,11 +791,16 @@ class UserRightsProfile extends UserRightsAddOnAPI // Note: this code is VERY close to the code of IsActionAllowed() $iUser = $oUser->GetKey(); + // Cache user's profiles + if(false === array_key_exists($iUser, $this->aUsersProfilesList)){ + $this->aUsersProfilesList[$iUser] = UserRights::ListProfiles($oUser); + } + // Note: The object set is ignored because it was interesting to optimize for huge data sets // and acceptable to consider only the root class of the object set $bStatus = null; // Call the API of UserRights because it caches the list for us - foreach(UserRights::ListProfiles($oUser) as $iProfile => $oProfile) + foreach($this->aUsersProfilesList[$iUser] as $iProfile => $oProfile) { $bGrant = $this->GetClassStimulusGrant($iProfile, $sClass, $sStimulusCode); if (!is_null($bGrant))