3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * Access Control Cache.
37 class CRM_ACL_BAO_Cache
extends CRM_ACL_DAO_ACLCache
{
39 public static $_cache = NULL;
42 * Build an array of ACLs for a specific ACLed user
43 * @param int $id - contact_id of the ACLed user
47 public static function &build($id) {
52 if (array_key_exists($id, self
::$_cache)) {
53 return self
::$_cache[$id];
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];
63 self
::$_cache[$id] = CRM_ACL_BAO_ACL
::getAllByContact($id);
64 self
::store($id, self
::$_cache[$id]);
65 return self
::$_cache[$id];
73 public static function retrieve($id) {
76 FROM civicrm_acl_cache
79 $params = [1 => [$id, 'Integer']];
82 $query .= " OR contact_id IS NULL";
85 $dao = CRM_Core_DAO
::executeQuery($query, $params);
88 while ($dao->fetch()) {
89 $cache[$dao->acl_id
] = 1;
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.
100 public static function store($id, &$cache) {
101 foreach ($cache as $aclID => $data) {
102 $dao = new CRM_ACL_BAO_Cache();
104 $dao->contact_id
= $id;
106 $dao->acl_id
= $aclID;
115 * Remove entries from civicrm_acl_cache for a specified ACLed user
116 * @param int $id - contact_id of the ACLed user
119 public static function deleteEntry($id) {
121 array_key_exists($id, self
::$_cache)
123 unset(self
::$_cache[$id]);
127 DELETE FROM civicrm_acl_cache
128 WHERE contact_id = %1
130 $params = [1 => [$id, 'Integer']];
131 CRM_Core_DAO
::executeQuery($query, $params);
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.
139 public static function updateEntry($id) {
140 // rebuilds civicrm_acl_cache
141 self
::deleteEntry($id);
144 // rebuilds civicrm_acl_contact_cache
145 CRM_Contact_BAO_Contact_Permission
::cache($id, CRM_Core_Permission
::VIEW
, TRUE);
149 * Deletes all the cache entries.
151 public static function resetCache() {
152 if (!CRM_Core_Config
::isPermitCacheFlushMode()) {
155 // reset any static caching
156 self
::$_cache = NULL;
160 FROM civicrm_acl_cache
161 WHERE modified_date IS NULL
162 OR (modified_date <= %1)
166 CRM_Contact_BAO_GroupContactCache
::getCacheInvalidDateTime(),
170 CRM_Core_DAO
::singleValueQuery($query, $params);
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()) {
175 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_POST_COMMIT
, function () {
176 CRM_Core_DAO
::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache");
180 CRM_Core_DAO
::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache");
185 * Remove Entries from `civicrm_acl_contact_cache` for a specific ACLed user
186 * @param int $userID - contact_id of the ACLed user
189 public static function deleteContactCacheEntry($userID) {
190 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_acl_contact_cache WHERE user_id = %1", [1 => [$userID, 'Positive']]);