From 92f38c2aed8a2a2944118f9e471edbe4432b129b Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 16 Jul 2021 15:17:10 +1200 Subject: [PATCH] [REF] Extract the function to load from the acl table --- CRM/ACL/BAO/ACL.php | 80 +++++++++++++-------- CRM/ACL/BAO/Cache.php | 2 +- tests/phpunit/CiviTest/CiviUnitTestCase.php | 2 - 3 files changed, 50 insertions(+), 34 deletions(-) diff --git a/CRM/ACL/BAO/ACL.php b/CRM/ACL/BAO/ACL.php index e9ecf99847..e105339192 100644 --- a/CRM/ACL/BAO/ACL.php +++ b/CRM/ACL/BAO/ACL.php @@ -349,36 +349,7 @@ SELECT g.* $cache = CRM_Utils_Cache::singleton(); $ids = $cache->get($cacheKey); if (!is_array($ids)) { - $ids = []; - $query = " -SELECT a.operation, a.object_id - FROM civicrm_acl_cache c, civicrm_acl a - WHERE c.acl_id = a.id - AND a.is_active = 1 - AND a.object_table = %1 - AND a.id IN ( $aclKeys ) -GROUP BY a.operation,a.object_id -ORDER BY a.object_id -"; - $params = [1 => [$tableName, 'String']]; - $dao = CRM_Core_DAO::executeQuery($query, $params); - while ($dao->fetch()) { - if ($dao->object_id) { - if (self::matchType($type, $dao->operation)) { - $ids[] = $dao->object_id; - } - } - else { - // this user has got the permission for all objects of this type - // check if the type matches - if (self::matchType($type, $dao->operation)) { - foreach ($allGroups as $id => $dontCare) { - $ids[] = $id; - } - } - break; - } - } + $ids = self::loadPermittedIDs((int) $contactID, $tableName, $type, $allGroups); $cache->set($cacheKey, $ids); } } @@ -393,7 +364,7 @@ ORDER BY a.object_id if ($contactID) { $groupWhere = ''; if (!empty($allGroups)) { - $groupWhere = " AND id IN (" . implode(',', array_keys($allGroups)) . ")"; + $groupWhere = ' AND id IN (' . implode(',', array_keys($allGroups)) . ")"; } // Contacts create hidden groups from search results. They should be able to retrieve their own. $ownHiddenGroupsList = CRM_Core_DAO::singleValueQuery(" @@ -474,4 +445,51 @@ ORDER BY a.object_id $acl->delete(); } + /** + * Load permitted acl IDs. + * + * @param int $contactID + * @param string $tableName + * @param int $type + * @param $allGroups + * + * @return array + */ + protected static function loadPermittedIDs(int $contactID, string $tableName, int $type, $allGroups): array { + $ids = []; + $acls = CRM_ACL_BAO_Cache::build($contactID); + $aclKeys = array_keys($acls); + $aclKeys = implode(',', $aclKeys); + $query = " +SELECT a.operation, a.object_id + FROM civicrm_acl_cache c, civicrm_acl a + WHERE c.acl_id = a.id + AND a.is_active = 1 + AND a.object_table = %1 + AND a.id IN ( $aclKeys ) +GROUP BY a.operation,a.object_id +ORDER BY a.object_id +"; + $params = [1 => [$tableName, 'String']]; + $dao = CRM_Core_DAO::executeQuery($query, $params); + while ($dao->fetch()) { + if ($dao->object_id) { + if (self::matchType($type, $dao->operation)) { + $ids[] = $dao->object_id; + } + } + else { + // this user has got the permission for all objects of this type + // check if the type matches + if (self::matchType($type, $dao->operation)) { + foreach ($allGroups as $id => $dontCare) { + $ids[] = $id; + } + } + break; + } + } + return $ids; + } + } diff --git a/CRM/ACL/BAO/Cache.php b/CRM/ACL/BAO/Cache.php index c7c630b9c3..138ee803f2 100644 --- a/CRM/ACL/BAO/Cache.php +++ b/CRM/ACL/BAO/Cache.php @@ -153,7 +153,7 @@ WHERE modified_date IS NULL ], ]; CRM_Core_DAO::singleValueQuery($query, $params); - + unset(Civi::$statics['CRM_ACL_API']); // CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache"); // No, force-commits transaction // CRM_Core_DAO::singleValueQuery("DELETE FROM civicrm_acl_contact_cache"); // Transaction-safe if (CRM_Core_Transaction::isActive()) { diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 9b77be94d8..5c46e94887 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -2462,9 +2462,7 @@ VALUES ]); if (!$isProfile) { - //flush cache CRM_ACL_BAO_Cache::resetCache(); - CRM_ACL_API::groupPermission('whatever', 9999, NULL, 'civicrm_saved_search', NULL, NULL); } } -- 2.25.1