Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Access Control Cache | |
38 | */ | |
39 | class CRM_ACL_BAO_Cache extends CRM_ACL_DAO_Cache { | |
40 | ||
41 | static $_cache = NULL; | |
42 | ||
28518c90 | 43 | /** |
100fef9d | 44 | * @param int $id |
28518c90 EM |
45 | * |
46 | * @return mixed | |
47 | */ | |
00be9182 | 48 | public static function &build($id) { |
6a488035 TO |
49 | if (!self::$_cache) { |
50 | self::$_cache = array(); | |
51 | } | |
52 | ||
53 | if (array_key_exists($id, self::$_cache)) { | |
54 | return self::$_cache[$id]; | |
55 | } | |
56 | ||
57 | // check if this entry exists in db | |
58 | // if so retrieve and return | |
59 | self::$_cache[$id] = self::retrieve($id); | |
60 | if (self::$_cache[$id]) { | |
61 | return self::$_cache[$id]; | |
62 | } | |
63 | ||
64 | self::$_cache[$id] = CRM_ACL_BAO_ACL::getAllByContact($id); | |
65 | self::store($id, self::$_cache[$id]); | |
66 | return self::$_cache[$id]; | |
67 | } | |
68 | ||
28518c90 | 69 | /** |
100fef9d | 70 | * @param int $id |
28518c90 EM |
71 | * |
72 | * @return array | |
73 | */ | |
00be9182 | 74 | public static function retrieve($id) { |
6a488035 TO |
75 | $query = " |
76 | SELECT acl_id | |
77 | FROM civicrm_acl_cache | |
78 | WHERE contact_id = %1 | |
79 | "; | |
80 | $params = array(1 => array($id, 'Integer')); | |
81 | ||
82 | if ($id == 0) { | |
83 | $query .= " OR contact_id IS NULL"; | |
84 | } | |
85 | ||
86 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
87 | ||
88 | $cache = array(); | |
89 | while ($dao->fetch()) { | |
90 | $cache[$dao->acl_id] = 1; | |
91 | } | |
92 | return $cache; | |
93 | } | |
94 | ||
28518c90 | 95 | /** |
100fef9d CW |
96 | * @param int $id |
97 | * @param array $cache | |
28518c90 | 98 | */ |
00be9182 | 99 | public static function store($id, &$cache) { |
6a488035 TO |
100 | foreach ($cache as $aclID => $data) { |
101 | $dao = new CRM_ACL_DAO_Cache(); | |
102 | if ($id) { | |
103 | $dao->contact_id = $id; | |
104 | } | |
105 | $dao->acl_id = $aclID; | |
106 | ||
107 | $cache[$aclID] = 1; | |
108 | ||
109 | $dao->save(); | |
110 | } | |
111 | } | |
112 | ||
28518c90 | 113 | /** |
100fef9d | 114 | * @param int $id |
28518c90 | 115 | */ |
00be9182 | 116 | public static function deleteEntry($id) { |
6a488035 TO |
117 | if (self::$_cache && |
118 | array_key_exists($id, self::$_cache) | |
119 | ) { | |
120 | unset(self::$_cache[$id]); | |
121 | } | |
122 | ||
123 | $query = " | |
124 | DELETE FROM civicrm_acl_cache | |
125 | WHERE contact_id = %1 | |
126 | "; | |
127 | $params = array(1 => array($id, 'Integer')); | |
7e1f3142 | 128 | CRM_Core_DAO::executeQuery($query, $params); |
6a488035 TO |
129 | } |
130 | ||
28518c90 | 131 | /** |
100fef9d | 132 | * @param int $id |
28518c90 | 133 | */ |
00be9182 | 134 | public static function updateEntry($id) { |
6a488035 TO |
135 | // rebuilds civicrm_acl_cache |
136 | self::deleteEntry($id); | |
137 | self::build($id); | |
138 | ||
139 | // rebuilds civicrm_acl_contact_cache | |
140 | CRM_Contact_BAO_Contact_Permission::cache($id, CRM_Core_Permission::VIEW, TRUE); | |
141 | } | |
142 | ||
d3e86119 TO |
143 | /** |
144 | * Deletes all the cache entries. | |
145 | */ | |
00be9182 | 146 | public static function resetCache() { |
6a488035 TO |
147 | // reset any static caching |
148 | self::$_cache = NULL; | |
149 | ||
150 | // reset any db caching | |
151 | $config = CRM_Core_Config::singleton(); | |
152 | $smartGroupCacheTimeout = CRM_Contact_BAO_GroupContactCache::smartGroupCacheTimeout(); | |
153 | ||
154 | //make sure to give original timezone settings again. | |
155 | $now = CRM_Utils_Date::getUTCTime(); | |
156 | ||
157 | $query = " | |
158 | DELETE | |
159 | FROM civicrm_acl_cache | |
160 | WHERE modified_date IS NULL | |
161 | OR (TIMESTAMPDIFF(MINUTE, modified_date, $now) >= $smartGroupCacheTimeout) | |
162 | "; | |
163 | CRM_Core_DAO::singleValueQuery($query); | |
164 | ||
cded6e48 TO |
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()) { | |
353ffa53 | 168 | CRM_Core_Transaction::addCallback(CRM_Core_Transaction::PHASE_POST_COMMIT, function () { |
cded6e48 TO |
169 | CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache"); |
170 | }); | |
0db6c3e1 TO |
171 | } |
172 | else { | |
cded6e48 TO |
173 | CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache"); |
174 | } | |
6a488035 | 175 | } |
96025800 | 176 | |
6a488035 | 177 | } |