Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-04-21-08-12-12
[civicrm-core.git] / CRM / ACL / BAO / Cache.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
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
43 static function &build($id) {
44 if (!self::$_cache) {
45 self::$_cache = array();
46 }
47
48 if (array_key_exists($id, self::$_cache)) {
49 return self::$_cache[$id];
50 }
51
52 // check if this entry exists in db
53 // if so retrieve and return
54 self::$_cache[$id] = self::retrieve($id);
55 if (self::$_cache[$id]) {
56 return self::$_cache[$id];
57 }
58
59 self::$_cache[$id] = CRM_ACL_BAO_ACL::getAllByContact($id);
60 self::store($id, self::$_cache[$id]);
61 return self::$_cache[$id];
62 }
63
64 static function retrieve($id) {
65 $query = "
66 SELECT acl_id
67 FROM civicrm_acl_cache
68 WHERE contact_id = %1
69 ";
70 $params = array(1 => array($id, 'Integer'));
71
72 if ($id == 0) {
73 $query .= " OR contact_id IS NULL";
74 }
75
76 $dao = CRM_Core_DAO::executeQuery($query, $params);
77
78 $cache = array();
79 while ($dao->fetch()) {
80 $cache[$dao->acl_id] = 1;
81 }
82 return $cache;
83 }
84
85 static function store($id, &$cache) {
86 foreach ($cache as $aclID => $data) {
87 $dao = new CRM_ACL_DAO_Cache();
88 if ($id) {
89 $dao->contact_id = $id;
90 }
91 $dao->acl_id = $aclID;
92
93 $cache[$aclID] = 1;
94
95 $dao->save();
96 }
97 }
98
99 static function deleteEntry($id) {
100 if (self::$_cache &&
101 array_key_exists($id, self::$_cache)
102 ) {
103 unset(self::$_cache[$id]);
104 }
105
106 $query = "
107 DELETE FROM civicrm_acl_cache
108 WHERE contact_id = %1
109 ";
110 $params = array(1 => array($id, 'Integer'));
111 $dao = CRM_Core_DAO::executeQuery($query, $params);
112 }
113
114 static function updateEntry($id) {
115 // rebuilds civicrm_acl_cache
116 self::deleteEntry($id);
117 self::build($id);
118
119 // rebuilds civicrm_acl_contact_cache
120 CRM_Contact_BAO_Contact_Permission::cache($id, CRM_Core_Permission::VIEW, TRUE);
121 }
122
123 // deletes all the cache entries
124 static function resetCache() {
125 // reset any static caching
126 self::$_cache = NULL;
127
128 // reset any db caching
129 $config = CRM_Core_Config::singleton();
130 $smartGroupCacheTimeout = CRM_Contact_BAO_GroupContactCache::smartGroupCacheTimeout();
131
132 //make sure to give original timezone settings again.
133 $now = CRM_Utils_Date::getUTCTime();
134
135 $query = "
136 DELETE
137 FROM civicrm_acl_cache
138 WHERE modified_date IS NULL
139 OR (TIMESTAMPDIFF(MINUTE, modified_date, $now) >= $smartGroupCacheTimeout)
140 ";
141 CRM_Core_DAO::singleValueQuery($query);
142
143 // CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache"); // No, force-commits transaction
144 // CRM_Core_DAO::singleValueQuery("DELETE FROM civicrm_acl_contact_cache"); // Transaction-safe
145 if (CRM_Core_Transaction::isActive()) {
146 CRM_Core_Transaction::addCallback(CRM_Core_Transaction::PHASE_POST_COMMIT, function(){
147 CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache");
148 });
149 } else {
150 CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache");
151 }
152 }
153 }
154