From 53d22fe9cabeed866342543060127d1467525e74 Mon Sep 17 00:00:00 2001 From: pkamps Date: Fri, 15 Sep 2017 23:19:34 +0200 Subject: [PATCH] Ezp 27427 improving cache generation performance (#68) * Cache generation performance improvement * Load correct groups/roles in ezuser --- kernel/classes/datatypes/ezuser/ezuser.php | 95 +++++++++++----------- 1 file changed, 47 insertions(+), 48 deletions(-) diff --git a/kernel/classes/datatypes/ezuser/ezuser.php b/kernel/classes/datatypes/ezuser/ezuser.php index 8bce8beca3f..e4f5db9d5eb 100644 --- a/kernel/classes/datatypes/ezuser/ezuser.php +++ b/kernel/classes/datatypes/ezuser/ezuser.php @@ -1415,8 +1415,12 @@ private static function generateUserCacheData( $userId ) 'password_hash' => $user->attribute( 'password_hash' ), 'password_hash_type' => $user->attribute( 'password_hash_type' ) ); + // function groups relies on caching. The function generateUserCacheData relies on function groups. + // To avoid an infinitive loop, the code is disabling caching in function generateUserCacheData. + // At the end of this method flag is set to previous value again + $previousCachingState = $user->setCachingEnabled( false ); // user groups list (session: eZUserGroupsCache) - $groups = $user->generateGroupIdList(); + $groups = $user->groups(); $data['groups'] = $groups; // role list (session: eZRoleIDList) @@ -1424,7 +1428,7 @@ private static function generateUserCacheData( $userId ) $data['roles'] = eZRole::fetchIDListByUser( $groups ); // role limitation list (session: eZRoleLimitationValueList) - $limitList = $user->limitList( false ); + $limitList = $user->limitList(); foreach ( $limitList as $limit ) { $data['role_limitations'][] = $limit['limit_value']; @@ -1436,6 +1440,8 @@ private static function generateUserCacheData( $userId ) // discount rules (session: eZUserDiscountRules) $data['discount_rules'] = eZUserDiscountRule::generateIDListByUserID( $userId ); + $user->setCachingEnabled( $previousCachingState ); + return $data; } @@ -1920,7 +1926,7 @@ function accessArray() */ function generateAccessArray() { - $idList = $this->generateGroupIdList(); + $idList = $this->groups(); $idList[] = $this->attribute( 'contentobject_id' ); return eZRole::accessArrayByUserID( $idList ); @@ -2470,64 +2476,29 @@ function groups( $asObject = false ) { if ( !isset( $this->GroupsAsObjects ) ) { - $db = eZDB::instance(); - $contentobjectID = $this->attribute( 'contentobject_id' ); - $userGroups = $db->arrayQuery( "SELECT d.*, c.path_string - FROM ezcontentobject_tree b, - ezcontentobject_tree c, - ezcontentobject d - WHERE b.contentobject_id='$contentobjectID' AND - b.parent_node_id = c.node_id AND - d.id = c.contentobject_id - ORDER BY c.contentobject_id "); - $userGroupArray = array(); - $pathArray = array(); - foreach ( $userGroups as $group ) - { - $pathItems = explode( '/', $group["path_string"] ); - array_pop( $pathItems ); - array_pop( $pathItems ); - foreach ( $pathItems as $pathItem ) - { - if ( $pathItem != '' && $pathItem > 1 ) - $pathArray[] = $pathItem; - } - $userGroupArray[] = new eZContentObject( $group ); - } - $pathArray = array_unique( $pathArray ); + $this->GroupsAsObjects = array(); - if ( !empty( $pathArray ) ) + foreach ( $this->groups() as $group ) { - $extraGroups = $db->arrayQuery( "SELECT d.* - FROM ezcontentobject_tree c, - ezcontentobject d - WHERE c.node_id in ( " . implode( ', ', $pathArray ) . " ) AND - d.id = c.contentobject_id - ORDER BY c.contentobject_id "); - foreach ( $extraGroups as $group ) - { - $userGroupArray[] = new eZContentObject( $group ); - } + $this->GroupsAsObjects[] = new eZContentObject( $group ); } - - $this->GroupsAsObjects = $userGroupArray; } + return $this->GroupsAsObjects; } else { if ( !isset( $this->Groups ) ) { - if ( eZINI::instance()->variable( 'RoleSettings', 'EnableCaching' ) === 'true' ) - { - $userCache = $this->getUserCache(); - $this->Groups = $userCache['groups']; - } - else + $this->fetchUserCacheIfNeeded(); + if ( !isset($this->UserCache['groups']) ) { - $this->Groups = $this->generateGroupIdList(); + $this->UserCache['groups'] = $this->generateGroupIdList(); } + + $this->Groups = $this->UserCache['groups']; } + return $this->Groups; } } @@ -2896,6 +2867,32 @@ public function canLoginToSiteAccess( $access ) return $hasAccessToSite; } + /** + * Fetches user cache when given preconditions are met + */ + protected function fetchUserCacheIfNeeded() + { + if ( null === $this->UserCache && true === $this->CachingEnabled && 'true' === eZINI::instance()->variable( 'RoleSettings', 'EnableCaching' )) + { + $this->UserCache = $this->getUserCache(); + } + } + + /** + * Sets cachingEnabled flag to given value. Returns previous value of cachingEnabled flag + * + * @param bool $cachingEnabled + * + * @return bool + */ + protected function setCachingEnabled( $cachingEnabled ) + { + $previousValue = $this->CachingEnabled; + $this->CachingEnabled = $cachingEnabled; + + return $previousValue; + } + /// \privatesection public $Login; public $Email; @@ -2921,6 +2918,8 @@ public function canLoginToSiteAccess( $access ) * @since 4.3 */ protected static $userHasLoggedOut = false; + + private $CachingEnabled = true; } ?>