Merge pull request #13968 from eileenmcnaughton/array_format
[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_Cache {
38
39 static $_cache = NULL;
40
41 /**
42 * @param int $id
43 *
44 * @return mixed
45 */
46 public static function &build($id) {
47 if (!self::$_cache) {
48 self::$_cache = [];
49 }
50
51 if (array_key_exists($id, self::$_cache)) {
52 return self::$_cache[$id];
53 }
54
55 // check if this entry exists in db
56 // if so retrieve and return
57 self::$_cache[$id] = self::retrieve($id);
58 if (self::$_cache[$id]) {
59 return self::$_cache[$id];
60 }
61
62 self::$_cache[$id] = CRM_ACL_BAO_ACL::getAllByContact($id);
63 self::store($id, self::$_cache[$id]);
64 return self::$_cache[$id];
65 }
66
67 /**
68 * @param int $id
69 *
70 * @return array
71 */
72 public static function retrieve($id) {
73 $query = "
74 SELECT acl_id
75 FROM civicrm_acl_cache
76 WHERE contact_id = %1
77 ";
78 $params = [1 => [$id, 'Integer']];
79
80 if ($id == 0) {
81 $query .= " OR contact_id IS NULL";
82 }
83
84 $dao = CRM_Core_DAO::executeQuery($query, $params);
85
86 $cache = [];
87 while ($dao->fetch()) {
88 $cache[$dao->acl_id] = 1;
89 }
90 return $cache;
91 }
92
93 /**
94 * @param int $id
95 * @param array $cache
96 */
97 public static function store($id, &$cache) {
98 foreach ($cache as $aclID => $data) {
99 $dao = new CRM_ACL_DAO_Cache();
100 if ($id) {
101 $dao->contact_id = $id;
102 }
103 $dao->acl_id = $aclID;
104
105 $cache[$aclID] = 1;
106
107 $dao->save();
108 }
109 }
110
111 /**
112 * @param int $id
113 */
114 public static function deleteEntry($id) {
115 if (self::$_cache &&
116 array_key_exists($id, self::$_cache)
117 ) {
118 unset(self::$_cache[$id]);
119 }
120
121 $query = "
122 DELETE FROM civicrm_acl_cache
123 WHERE contact_id = %1
124 ";
125 $params = [1 => [$id, 'Integer']];
126 CRM_Core_DAO::executeQuery($query, $params);
127 }
128
129 /**
130 * @param int $id
131 */
132 public static function updateEntry($id) {
133 // rebuilds civicrm_acl_cache
134 self::deleteEntry($id);
135 self::build($id);
136
137 // rebuilds civicrm_acl_contact_cache
138 CRM_Contact_BAO_Contact_Permission::cache($id, CRM_Core_Permission::VIEW, TRUE);
139 }
140
141 /**
142 * Deletes all the cache entries.
143 */
144 public static function resetCache() {
145 if (!CRM_Core_Config::isPermitCacheFlushMode()) {
146 return;
147 }
148 // reset any static caching
149 self::$_cache = NULL;
150
151 $query = "
152 DELETE
153 FROM civicrm_acl_cache
154 WHERE modified_date IS NULL
155 OR (modified_date <= %1)
156 ";
157 $params = [
158 1 => [
159 CRM_Contact_BAO_GroupContactCache::getCacheInvalidDateTime(),
160 'String',
161 ],
162 ];
163 CRM_Core_DAO::singleValueQuery($query, $params);
164
165 // CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache"); // No, force-commits transaction
166 // CRM_Core_DAO::singleValueQuery("DELETE FROM civicrm_acl_contact_cache"); // Transaction-safe
167 if (CRM_Core_Transaction::isActive()) {
168 CRM_Core_Transaction::addCallback(CRM_Core_Transaction::PHASE_POST_COMMIT, function () {
169 CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache");
170 });
171 }
172 else {
173 CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache");
174 }
175 }
176
177 }