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 TO |
36 | */ |
37 | class CRM_ACL_BAO_Cache extends CRM_ACL_DAO_Cache { | |
38 | ||
39 | static $_cache = NULL; | |
40 | ||
28518c90 | 41 | /** |
100fef9d | 42 | * @param int $id |
28518c90 EM |
43 | * |
44 | * @return mixed | |
45 | */ | |
00be9182 | 46 | public static function &build($id) { |
6a488035 TO |
47 | if (!self::$_cache) { |
48 | self::$_cache = array(); | |
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 | ||
28518c90 | 67 | /** |
100fef9d | 68 | * @param int $id |
28518c90 EM |
69 | * |
70 | * @return array | |
71 | */ | |
00be9182 | 72 | public static function retrieve($id) { |
6a488035 TO |
73 | $query = " |
74 | SELECT acl_id | |
75 | FROM civicrm_acl_cache | |
76 | WHERE contact_id = %1 | |
77 | "; | |
78 | $params = array(1 => array($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 = array(); | |
87 | while ($dao->fetch()) { | |
88 | $cache[$dao->acl_id] = 1; | |
89 | } | |
90 | return $cache; | |
91 | } | |
92 | ||
28518c90 | 93 | /** |
100fef9d CW |
94 | * @param int $id |
95 | * @param array $cache | |
28518c90 | 96 | */ |
00be9182 | 97 | public static function store($id, &$cache) { |
6a488035 TO |
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 | ||
28518c90 | 111 | /** |
100fef9d | 112 | * @param int $id |
28518c90 | 113 | */ |
00be9182 | 114 | public static function deleteEntry($id) { |
6a488035 TO |
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 = array(1 => array($id, 'Integer')); | |
7e1f3142 | 126 | CRM_Core_DAO::executeQuery($query, $params); |
6a488035 TO |
127 | } |
128 | ||
28518c90 | 129 | /** |
100fef9d | 130 | * @param int $id |
28518c90 | 131 | */ |
00be9182 | 132 | public static function updateEntry($id) { |
6a488035 TO |
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 | ||
d3e86119 TO |
141 | /** |
142 | * Deletes all the cache entries. | |
143 | */ | |
00be9182 | 144 | public static function resetCache() { |
0626851e | 145 | if (!CRM_Core_Config::isPermitCacheFlushMode()) { |
146 | return; | |
147 | } | |
6a488035 TO |
148 | // reset any static caching |
149 | self::$_cache = NULL; | |
150 | ||
6a488035 TO |
151 | $query = " |
152 | DELETE | |
153 | FROM civicrm_acl_cache | |
154 | WHERE modified_date IS NULL | |
4052239b | 155 | OR (modified_date <= %1) |
6a488035 | 156 | "; |
4052239b | 157 | $params = array(1 => array(CRM_Contact_BAO_GroupContactCache::getCacheInvalidDateTime(), 'String')); |
158 | CRM_Core_DAO::singleValueQuery($query, $params); | |
6a488035 | 159 | |
cded6e48 TO |
160 | // CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache"); // No, force-commits transaction |
161 | // CRM_Core_DAO::singleValueQuery("DELETE FROM civicrm_acl_contact_cache"); // Transaction-safe | |
162 | if (CRM_Core_Transaction::isActive()) { | |
353ffa53 | 163 | CRM_Core_Transaction::addCallback(CRM_Core_Transaction::PHASE_POST_COMMIT, function () { |
cded6e48 TO |
164 | CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache"); |
165 | }); | |
0db6c3e1 TO |
166 | } |
167 | else { | |
cded6e48 TO |
168 | CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache"); |
169 | } | |
6a488035 | 170 | } |
96025800 | 171 | |
6a488035 | 172 | } |