Merge pull request #15604 from mattwire/multiplelocks
[civicrm-core.git] / CRM / ACL / BAO / Cache.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * Access Control Cache.
36 */
37 class CRM_ACL_BAO_Cache extends CRM_ACL_DAO_ACLCache {
38
39 public static $_cache = NULL;
40
41 /**
42 * Build an array of ACLs for a specific ACLed user
43 * @param int $id - contact_id of the ACLed user
44 *
45 * @return mixed
46 */
47 public static function &build($id) {
48 if (!self::$_cache) {
49 self::$_cache = [];
50 }
51
52 if (array_key_exists($id, self::$_cache)) {
53 return self::$_cache[$id];
54 }
55
56 // check if this entry exists in db
57 // if so retrieve and return
58 self::$_cache[$id] = self::retrieve($id);
59 if (self::$_cache[$id]) {
60 return self::$_cache[$id];
61 }
62
63 self::$_cache[$id] = CRM_ACL_BAO_ACL::getAllByContact($id);
64 self::store($id, self::$_cache[$id]);
65 return self::$_cache[$id];
66 }
67
68 /**
69 * @param int $id
70 *
71 * @return array
72 */
73 public static function retrieve($id) {
74 $query = "
75 SELECT acl_id
76 FROM civicrm_acl_cache
77 WHERE contact_id = %1
78 ";
79 $params = [1 => [$id, 'Integer']];
80
81 if ($id == 0) {
82 $query .= " OR contact_id IS NULL";
83 }
84
85 $dao = CRM_Core_DAO::executeQuery($query, $params);
86
87 $cache = [];
88 while ($dao->fetch()) {
89 $cache[$dao->acl_id] = 1;
90 }
91 return $cache;
92 }
93
94 /**
95 * Store ACLs for a specific user in the `civicrm_acl_cache` table
96 * @param int $id - contact_id of the ACLed user
97 * @param array $cache - key civicrm_acl.id - values is the details of the ACL.
98 *
99 */
100 public static function store($id, &$cache) {
101 foreach ($cache as $aclID => $data) {
102 $dao = new CRM_ACL_BAO_Cache();
103 if ($id) {
104 $dao->contact_id = $id;
105 }
106 $dao->acl_id = $aclID;
107
108 $cache[$aclID] = 1;
109
110 $dao->save();
111 }
112 }
113
114 /**
115 * Remove entries from civicrm_acl_cache for a specified ACLed user
116 * @param int $id - contact_id of the ACLed user
117 *
118 */
119 public static function deleteEntry($id) {
120 if (self::$_cache &&
121 array_key_exists($id, self::$_cache)
122 ) {
123 unset(self::$_cache[$id]);
124 }
125
126 $query = "
127 DELETE FROM civicrm_acl_cache
128 WHERE contact_id = %1
129 ";
130 $params = [1 => [$id, 'Integer']];
131 CRM_Core_DAO::executeQuery($query, $params);
132 }
133
134 /**
135 * Update ACL caches `civicrm_acl_cache` and `civicrm_acl_contact_cache for the specified ACLed user
136 * @param int $id - contact_id of ACLed user to update caches for.
137 *
138 */
139 public static function updateEntry($id) {
140 // rebuilds civicrm_acl_cache
141 self::deleteEntry($id);
142 self::build($id);
143
144 // rebuilds civicrm_acl_contact_cache
145 CRM_Contact_BAO_Contact_Permission::cache($id, CRM_Core_Permission::VIEW, TRUE);
146 }
147
148 /**
149 * Deletes all the cache entries.
150 */
151 public static function resetCache() {
152 if (!CRM_Core_Config::isPermitCacheFlushMode()) {
153 return;
154 }
155 // reset any static caching
156 self::$_cache = NULL;
157
158 $query = "
159 DELETE
160 FROM civicrm_acl_cache
161 WHERE modified_date IS NULL
162 OR (modified_date <= %1)
163 ";
164 $params = [
165 1 => [
166 CRM_Contact_BAO_GroupContactCache::getCacheInvalidDateTime(),
167 'String',
168 ],
169 ];
170 CRM_Core_DAO::singleValueQuery($query, $params);
171
172 // CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache"); // No, force-commits transaction
173 // CRM_Core_DAO::singleValueQuery("DELETE FROM civicrm_acl_contact_cache"); // Transaction-safe
174 if (CRM_Core_Transaction::isActive()) {
175 CRM_Core_Transaction::addCallback(CRM_Core_Transaction::PHASE_POST_COMMIT, function () {
176 CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache");
177 });
178 }
179 else {
180 CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache");
181 }
182 }
183
184 }