RT#765026, patch for null values in civicrm_acl_cache
[civicrm-core.git] / CRM / ACL / BAO / Cache.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
3819f101 19 * Access Control Cache.
6a488035 20 */
28fec8aa 21class CRM_ACL_BAO_Cache extends CRM_ACL_DAO_ACLCache {
6a488035 22
683bf891 23 public static $_cache = NULL;
6a488035 24
28518c90 25 /**
5b27235a 26 * Build an array of ACLs for a specific ACLed user
581a9d2e 27 *
5b27235a 28 * @param int $id - contact_id of the ACLed user
28518c90
EM
29 *
30 * @return mixed
581a9d2e 31 * @throws \CRM_Core_Exception
28518c90 32 */
00be9182 33 public static function &build($id) {
6a488035 34 if (!self::$_cache) {
cf0d1c08 35 self::$_cache = [];
6a488035
TO
36 }
37
38 if (array_key_exists($id, self::$_cache)) {
39 return self::$_cache[$id];
40 }
41
42 // check if this entry exists in db
43 // if so retrieve and return
44 self::$_cache[$id] = self::retrieve($id);
45 if (self::$_cache[$id]) {
46 return self::$_cache[$id];
47 }
48
581a9d2e 49 self::$_cache[$id] = CRM_ACL_BAO_ACL::getAllByContact((int) $id);
6a488035
TO
50 self::store($id, self::$_cache[$id]);
51 return self::$_cache[$id];
52 }
53
28518c90 54 /**
100fef9d 55 * @param int $id
28518c90
EM
56 *
57 * @return array
58 */
06b1ee60 59 protected static function retrieve($id) {
6a488035
TO
60 $query = "
61SELECT acl_id
62 FROM civicrm_acl_cache
63 WHERE contact_id = %1
64";
cf0d1c08 65 $params = [1 => [$id, 'Integer']];
6a488035 66
9a2e2287
LMM
67 /* this is related to RT#765026
68 Something is occasionally inserting records in civicrm_acl_cache with id null.
69 That causes i for our anonymous visitors who want to sign petitions.
70 Removed. Ward, 2012-07-20 */
71 /*
72 if ($id == 0) {
73 query .= " OR contact_id IS NULL";
74 }
75 t*/
6a488035
TO
76
77 $dao = CRM_Core_DAO::executeQuery($query, $params);
78
cf0d1c08 79 $cache = [];
6a488035
TO
80 while ($dao->fetch()) {
81 $cache[$dao->acl_id] = 1;
82 }
83 return $cache;
84 }
85
28518c90 86 /**
5b27235a
SL
87 * Store ACLs for a specific user in the `civicrm_acl_cache` table
88 * @param int $id - contact_id of the ACLed user
89 * @param array $cache - key civicrm_acl.id - values is the details of the ACL.
90 *
28518c90 91 */
06b1ee60 92 protected static function store($id, &$cache) {
6a488035 93 foreach ($cache as $aclID => $data) {
28fec8aa 94 $dao = new CRM_ACL_BAO_Cache();
6a488035
TO
95 if ($id) {
96 $dao->contact_id = $id;
97 }
98 $dao->acl_id = $aclID;
99
100 $cache[$aclID] = 1;
101
102 $dao->save();
103 }
104 }
105
28518c90 106 /**
5b27235a
SL
107 * Remove entries from civicrm_acl_cache for a specified ACLed user
108 * @param int $id - contact_id of the ACLed user
109 *
28518c90 110 */
00be9182 111 public static function deleteEntry($id) {
6a488035
TO
112 if (self::$_cache &&
113 array_key_exists($id, self::$_cache)
114 ) {
115 unset(self::$_cache[$id]);
116 }
117
118 $query = "
119DELETE FROM civicrm_acl_cache
120WHERE contact_id = %1
121";
cf0d1c08 122 $params = [1 => [$id, 'Integer']];
7e1f3142 123 CRM_Core_DAO::executeQuery($query, $params);
6a488035
TO
124 }
125
67769683 126 /**
127 * Do an opportunistic cache refresh if the site is configured for these.
128 *
129 * Sites that use acls and do not run the acl cache clearing cron job should
130 * refresh the caches on demand. The user session will be forced to wait
131 * and this is a common source of deadlocks, so it is less ideal.
132 */
133 public static function opportunisticCacheFlush(): void {
134 if (Civi::settings()->get('acl_cache_refresh_mode') === 'opportunistic') {
135 self::resetCache();
136 }
137 }
138
d3e86119
TO
139 /**
140 * Deletes all the cache entries.
141 */
67769683 142 public static function resetCache(): void {
0626851e 143 if (!CRM_Core_Config::isPermitCacheFlushMode()) {
144 return;
145 }
6a488035
TO
146 // reset any static caching
147 self::$_cache = NULL;
148
6a488035
TO
149 $query = "
150DELETE
151FROM civicrm_acl_cache
152WHERE modified_date IS NULL
4052239b 153 OR (modified_date <= %1)
6a488035 154";
cf0d1c08 155 $params = [
156 1 => [
157 CRM_Contact_BAO_GroupContactCache::getCacheInvalidDateTime(),
158 'String',
159 ],
160 ];
4052239b 161 CRM_Core_DAO::singleValueQuery($query, $params);
ed2f3757
EM
162 self::flushACLContactCache();
163 }
164
165 /**
166 * Remove Entries from `civicrm_acl_contact_cache` for a specific ACLed user
167 * @param int $userID - contact_id of the ACLed user
168 *
169 */
170 public static function deleteContactCacheEntry($userID) {
171 CRM_Core_DAO::executeQuery("DELETE FROM civicrm_acl_contact_cache WHERE user_id = %1", [1 => [$userID, 'Positive']]);
172 }
173
174 /**
175 * Flush the contents of the acl contact cache.
176 */
177 protected static function flushACLContactCache(): void {
92f38c2a 178 unset(Civi::$statics['CRM_ACL_API']);
cded6e48
TO
179 // CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache"); // No, force-commits transaction
180 // CRM_Core_DAO::singleValueQuery("DELETE FROM civicrm_acl_contact_cache"); // Transaction-safe
181 if (CRM_Core_Transaction::isActive()) {
353ffa53 182 CRM_Core_Transaction::addCallback(CRM_Core_Transaction::PHASE_POST_COMMIT, function () {
ed2f3757 183 CRM_Core_DAO::singleValueQuery('TRUNCATE TABLE civicrm_acl_contact_cache');
cded6e48 184 });
0db6c3e1
TO
185 }
186 else {
cded6e48
TO
187 CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache");
188 }
6a488035 189 }
96025800 190
6a488035 191}