Merge pull request #20482 from MegaphoneJon/translation-69
[civicrm-core.git] / CRM / ACL / BAO / Cache.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Access Control Cache.
20 */
21 class CRM_ACL_BAO_Cache extends CRM_ACL_DAO_ACLCache {
22
23 public static $_cache = NULL;
24
25 /**
26 * Build an array of ACLs for a specific ACLed user
27 *
28 * @param int $id - contact_id of the ACLed user
29 *
30 * @return mixed
31 * @throws \CRM_Core_Exception
32 */
33 public static function &build($id) {
34 if (!self::$_cache) {
35 self::$_cache = [];
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
49 self::$_cache[$id] = CRM_ACL_BAO_ACL::getAllByContact((int) $id);
50 self::store($id, self::$_cache[$id]);
51 return self::$_cache[$id];
52 }
53
54 /**
55 * @param int $id
56 *
57 * @return array
58 */
59 protected static function retrieve($id) {
60 $query = "
61 SELECT acl_id
62 FROM civicrm_acl_cache
63 WHERE contact_id = %1
64 ";
65 $params = [1 => [$id, 'Integer']];
66
67 if ($id == 0) {
68 $query .= " OR contact_id IS NULL";
69 }
70
71 $dao = CRM_Core_DAO::executeQuery($query, $params);
72
73 $cache = [];
74 while ($dao->fetch()) {
75 $cache[$dao->acl_id] = 1;
76 }
77 return $cache;
78 }
79
80 /**
81 * Store ACLs for a specific user in the `civicrm_acl_cache` table
82 * @param int $id - contact_id of the ACLed user
83 * @param array $cache - key civicrm_acl.id - values is the details of the ACL.
84 *
85 */
86 protected static function store($id, &$cache) {
87 foreach ($cache as $aclID => $data) {
88 $dao = new CRM_ACL_BAO_Cache();
89 if ($id) {
90 $dao->contact_id = $id;
91 }
92 $dao->acl_id = $aclID;
93
94 $cache[$aclID] = 1;
95
96 $dao->save();
97 }
98 }
99
100 /**
101 * Remove entries from civicrm_acl_cache for a specified ACLed user
102 * @param int $id - contact_id of the ACLed user
103 *
104 */
105 public static function deleteEntry($id) {
106 if (self::$_cache &&
107 array_key_exists($id, self::$_cache)
108 ) {
109 unset(self::$_cache[$id]);
110 }
111
112 $query = "
113 DELETE FROM civicrm_acl_cache
114 WHERE contact_id = %1
115 ";
116 $params = [1 => [$id, 'Integer']];
117 CRM_Core_DAO::executeQuery($query, $params);
118 }
119
120 /**
121 * Do an opportunistic cache refresh if the site is configured for these.
122 *
123 * Sites that use acls and do not run the acl cache clearing cron job should
124 * refresh the caches on demand. The user session will be forced to wait
125 * and this is a common source of deadlocks, so it is less ideal.
126 */
127 public static function opportunisticCacheFlush(): void {
128 if (Civi::settings()->get('acl_cache_refresh_mode') === 'opportunistic') {
129 self::resetCache();
130 }
131 }
132
133 /**
134 * Deletes all the cache entries.
135 */
136 public static function resetCache(): void {
137 if (!CRM_Core_Config::isPermitCacheFlushMode()) {
138 return;
139 }
140 // reset any static caching
141 self::$_cache = NULL;
142
143 $query = "
144 DELETE
145 FROM civicrm_acl_cache
146 WHERE modified_date IS NULL
147 OR (modified_date <= %1)
148 ";
149 $params = [
150 1 => [
151 CRM_Contact_BAO_GroupContactCache::getCacheInvalidDateTime(),
152 'String',
153 ],
154 ];
155 CRM_Core_DAO::singleValueQuery($query, $params);
156 unset(Civi::$statics['CRM_ACL_API']);
157 // CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache"); // No, force-commits transaction
158 // CRM_Core_DAO::singleValueQuery("DELETE FROM civicrm_acl_contact_cache"); // Transaction-safe
159 if (CRM_Core_Transaction::isActive()) {
160 CRM_Core_Transaction::addCallback(CRM_Core_Transaction::PHASE_POST_COMMIT, function () {
161 CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache");
162 });
163 }
164 else {
165 CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache");
166 }
167 }
168
169 /**
170 * Remove Entries from `civicrm_acl_contact_cache` for a specific ACLed user
171 * @param int $userID - contact_id of the ACLed user
172 *
173 */
174 public static function deleteContactCacheEntry($userID) {
175 CRM_Core_DAO::executeQuery("DELETE FROM civicrm_acl_contact_cache WHERE user_id = %1", [1 => [$userID, 'Positive']]);
176 }
177
178 }