Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
3819f101 | 35 | * Access Control Cache. |
6a488035 | 36 | */ |
28fec8aa | 37 | class CRM_ACL_BAO_Cache extends CRM_ACL_DAO_ACLCache { |
6a488035 | 38 | |
683bf891 | 39 | public static $_cache = NULL; |
6a488035 | 40 | |
28518c90 | 41 | /** |
5b27235a SL |
42 | * Build an array of ACLs for a specific ACLed user |
43 | * @param int $id - contact_id of the ACLed user | |
28518c90 EM |
44 | * |
45 | * @return mixed | |
46 | */ | |
00be9182 | 47 | public static function &build($id) { |
6a488035 | 48 | if (!self::$_cache) { |
cf0d1c08 | 49 | self::$_cache = []; |
6a488035 TO |
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 | ||
28518c90 | 68 | /** |
100fef9d | 69 | * @param int $id |
28518c90 EM |
70 | * |
71 | * @return array | |
72 | */ | |
00be9182 | 73 | public static function retrieve($id) { |
6a488035 TO |
74 | $query = " |
75 | SELECT acl_id | |
76 | FROM civicrm_acl_cache | |
77 | WHERE contact_id = %1 | |
78 | "; | |
cf0d1c08 | 79 | $params = [1 => [$id, 'Integer']]; |
6a488035 TO |
80 | |
81 | if ($id == 0) { | |
82 | $query .= " OR contact_id IS NULL"; | |
83 | } | |
84 | ||
85 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
86 | ||
cf0d1c08 | 87 | $cache = []; |
6a488035 TO |
88 | while ($dao->fetch()) { |
89 | $cache[$dao->acl_id] = 1; | |
90 | } | |
91 | return $cache; | |
92 | } | |
93 | ||
28518c90 | 94 | /** |
5b27235a SL |
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 | * | |
28518c90 | 99 | */ |
00be9182 | 100 | public static function store($id, &$cache) { |
6a488035 | 101 | foreach ($cache as $aclID => $data) { |
28fec8aa | 102 | $dao = new CRM_ACL_BAO_Cache(); |
6a488035 TO |
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 | ||
28518c90 | 114 | /** |
5b27235a SL |
115 | * Remove entries from civicrm_acl_cache for a specified ACLed user |
116 | * @param int $id - contact_id of the ACLed user | |
117 | * | |
28518c90 | 118 | */ |
00be9182 | 119 | public static function deleteEntry($id) { |
6a488035 TO |
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 | "; | |
cf0d1c08 | 130 | $params = [1 => [$id, 'Integer']]; |
7e1f3142 | 131 | CRM_Core_DAO::executeQuery($query, $params); |
6a488035 TO |
132 | } |
133 | ||
28518c90 | 134 | /** |
5b27235a SL |
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 | * | |
28518c90 | 138 | */ |
00be9182 | 139 | public static function updateEntry($id) { |
6a488035 TO |
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 | ||
d3e86119 TO |
148 | /** |
149 | * Deletes all the cache entries. | |
150 | */ | |
00be9182 | 151 | public static function resetCache() { |
0626851e | 152 | if (!CRM_Core_Config::isPermitCacheFlushMode()) { |
153 | return; | |
154 | } | |
6a488035 TO |
155 | // reset any static caching |
156 | self::$_cache = NULL; | |
157 | ||
6a488035 TO |
158 | $query = " |
159 | DELETE | |
160 | FROM civicrm_acl_cache | |
161 | WHERE modified_date IS NULL | |
4052239b | 162 | OR (modified_date <= %1) |
6a488035 | 163 | "; |
cf0d1c08 | 164 | $params = [ |
165 | 1 => [ | |
166 | CRM_Contact_BAO_GroupContactCache::getCacheInvalidDateTime(), | |
167 | 'String', | |
168 | ], | |
169 | ]; | |
4052239b | 170 | CRM_Core_DAO::singleValueQuery($query, $params); |
6a488035 | 171 | |
cded6e48 TO |
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()) { | |
353ffa53 | 175 | CRM_Core_Transaction::addCallback(CRM_Core_Transaction::PHASE_POST_COMMIT, function () { |
cded6e48 TO |
176 | CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache"); |
177 | }); | |
0db6c3e1 TO |
178 | } |
179 | else { | |
cded6e48 TO |
180 | CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache"); |
181 | } | |
6a488035 | 182 | } |
96025800 | 183 | |
6a488035 | 184 | } |