Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
3819f101 | 19 | * Access Control Cache. |
6a488035 | 20 | */ |
28fec8aa | 21 | class CRM_ACL_BAO_Cache extends CRM_ACL_DAO_ACLCache { |
6a488035 | 22 | |
683bf891 | 23 | public static $_cache = NULL; |
6a488035 | 24 | |
28518c90 | 25 | /** |
5b27235a SL |
26 | * Build an array of ACLs for a specific ACLed user |
27 | * @param int $id - contact_id of the ACLed user | |
28518c90 EM |
28 | * |
29 | * @return mixed | |
30 | */ | |
00be9182 | 31 | public static function &build($id) { |
6a488035 | 32 | if (!self::$_cache) { |
cf0d1c08 | 33 | self::$_cache = []; |
6a488035 TO |
34 | } |
35 | ||
36 | if (array_key_exists($id, self::$_cache)) { | |
37 | return self::$_cache[$id]; | |
38 | } | |
39 | ||
40 | // check if this entry exists in db | |
41 | // if so retrieve and return | |
42 | self::$_cache[$id] = self::retrieve($id); | |
43 | if (self::$_cache[$id]) { | |
44 | return self::$_cache[$id]; | |
45 | } | |
46 | ||
47 | self::$_cache[$id] = CRM_ACL_BAO_ACL::getAllByContact($id); | |
48 | self::store($id, self::$_cache[$id]); | |
49 | return self::$_cache[$id]; | |
50 | } | |
51 | ||
28518c90 | 52 | /** |
100fef9d | 53 | * @param int $id |
28518c90 EM |
54 | * |
55 | * @return array | |
56 | */ | |
00be9182 | 57 | public static function retrieve($id) { |
6a488035 TO |
58 | $query = " |
59 | SELECT acl_id | |
60 | FROM civicrm_acl_cache | |
61 | WHERE contact_id = %1 | |
62 | "; | |
cf0d1c08 | 63 | $params = [1 => [$id, 'Integer']]; |
6a488035 TO |
64 | |
65 | if ($id == 0) { | |
66 | $query .= " OR contact_id IS NULL"; | |
67 | } | |
68 | ||
69 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
70 | ||
cf0d1c08 | 71 | $cache = []; |
6a488035 TO |
72 | while ($dao->fetch()) { |
73 | $cache[$dao->acl_id] = 1; | |
74 | } | |
75 | return $cache; | |
76 | } | |
77 | ||
28518c90 | 78 | /** |
5b27235a SL |
79 | * Store ACLs for a specific user in the `civicrm_acl_cache` table |
80 | * @param int $id - contact_id of the ACLed user | |
81 | * @param array $cache - key civicrm_acl.id - values is the details of the ACL. | |
82 | * | |
28518c90 | 83 | */ |
00be9182 | 84 | public static function store($id, &$cache) { |
6a488035 | 85 | foreach ($cache as $aclID => $data) { |
28fec8aa | 86 | $dao = new CRM_ACL_BAO_Cache(); |
6a488035 TO |
87 | if ($id) { |
88 | $dao->contact_id = $id; | |
89 | } | |
90 | $dao->acl_id = $aclID; | |
91 | ||
92 | $cache[$aclID] = 1; | |
93 | ||
94 | $dao->save(); | |
95 | } | |
96 | } | |
97 | ||
28518c90 | 98 | /** |
5b27235a SL |
99 | * Remove entries from civicrm_acl_cache for a specified ACLed user |
100 | * @param int $id - contact_id of the ACLed user | |
101 | * | |
28518c90 | 102 | */ |
00be9182 | 103 | public static function deleteEntry($id) { |
6a488035 TO |
104 | if (self::$_cache && |
105 | array_key_exists($id, self::$_cache) | |
106 | ) { | |
107 | unset(self::$_cache[$id]); | |
108 | } | |
109 | ||
110 | $query = " | |
111 | DELETE FROM civicrm_acl_cache | |
112 | WHERE contact_id = %1 | |
113 | "; | |
cf0d1c08 | 114 | $params = [1 => [$id, 'Integer']]; |
7e1f3142 | 115 | CRM_Core_DAO::executeQuery($query, $params); |
6a488035 TO |
116 | } |
117 | ||
d3e86119 TO |
118 | /** |
119 | * Deletes all the cache entries. | |
120 | */ | |
00be9182 | 121 | public static function resetCache() { |
0626851e | 122 | if (!CRM_Core_Config::isPermitCacheFlushMode()) { |
123 | return; | |
124 | } | |
6a488035 TO |
125 | // reset any static caching |
126 | self::$_cache = NULL; | |
127 | ||
6a488035 TO |
128 | $query = " |
129 | DELETE | |
130 | FROM civicrm_acl_cache | |
131 | WHERE modified_date IS NULL | |
4052239b | 132 | OR (modified_date <= %1) |
6a488035 | 133 | "; |
cf0d1c08 | 134 | $params = [ |
135 | 1 => [ | |
136 | CRM_Contact_BAO_GroupContactCache::getCacheInvalidDateTime(), | |
137 | 'String', | |
138 | ], | |
139 | ]; | |
4052239b | 140 | CRM_Core_DAO::singleValueQuery($query, $params); |
6a488035 | 141 | |
cded6e48 TO |
142 | // CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache"); // No, force-commits transaction |
143 | // CRM_Core_DAO::singleValueQuery("DELETE FROM civicrm_acl_contact_cache"); // Transaction-safe | |
144 | if (CRM_Core_Transaction::isActive()) { | |
353ffa53 | 145 | CRM_Core_Transaction::addCallback(CRM_Core_Transaction::PHASE_POST_COMMIT, function () { |
cded6e48 TO |
146 | CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache"); |
147 | }); | |
0db6c3e1 TO |
148 | } |
149 | else { | |
cded6e48 TO |
150 | CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache"); |
151 | } | |
6a488035 | 152 | } |
96025800 | 153 | |
a92e7856 SL |
154 | /** |
155 | * Remove Entries from `civicrm_acl_contact_cache` for a specific ACLed user | |
156 | * @param int $userID - contact_id of the ACLed user | |
157 | * | |
158 | */ | |
159 | public static function deleteContactCacheEntry($userID) { | |
160 | CRM_Core_DAO::executeQuery("DELETE FROM civicrm_acl_contact_cache WHERE user_id = %1", [1 => [$userID, 'Positive']]); | |
161 | } | |
162 | ||
6a488035 | 163 | } |