Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Contact_BAO_Contact_Permission { | |
36 | ||
37 | /** | |
100fef9d | 38 | * Check if the logged in user has permissions for the operation type |
6a488035 | 39 | * |
77b97be7 EM |
40 | * @param int $id contact id |
41 | * @param int|string $type the type of operation (view|edit) | |
6a488035 TO |
42 | * |
43 | * @return boolean true if the user has permission, false otherwise | |
44 | * @access public | |
45 | * @static | |
46 | */ | |
47 | static function allow($id, $type = CRM_Core_Permission::VIEW) { | |
48 | $tables = array(); | |
49 | $whereTables = array(); | |
50 | ||
51 | # FIXME: push this somewhere below, to not give this permission so many rights | |
52 | $isDeleted = (bool) CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $id, 'is_deleted'); | |
53 | if (CRM_Core_Permission::check('access deleted contacts') && $isDeleted) { | |
54 | return TRUE; | |
55 | } | |
56 | ||
57 | // short circuit for admin rights here so we avoid unneeeded queries | |
58 | // some duplication of code, but we skip 3-5 queries | |
59 | if (CRM_Core_Permission::check('edit all contacts') || | |
60 | ($type == CRM_ACL_API::VIEW && CRM_Core_Permission::check('view all contacts')) | |
61 | ) { | |
62 | return TRUE; | |
63 | } | |
64 | ||
65 | //check permission based on relationship, CRM-2963 | |
66 | if (self::relationship($id)) { | |
67 | return TRUE; | |
68 | } | |
69 | ||
70 | $permission = CRM_ACL_API::whereClause($type, $tables, $whereTables); | |
71 | ||
72 | $from = CRM_Contact_BAO_Query::fromClause($whereTables); | |
73 | ||
74 | $query = " | |
75 | SELECT count(DISTINCT contact_a.id) | |
76 | $from | |
77 | WHERE contact_a.id = %1 AND $permission"; | |
78 | $params = array(1 => array($id, 'Integer')); | |
79 | ||
80 | return (CRM_Core_DAO::singleValueQuery($query, $params) > 0) ? TRUE : FALSE; | |
81 | } | |
82 | ||
83 | /** | |
100fef9d | 84 | * Fill the acl contact cache for this contact id if empty |
6a488035 | 85 | * |
c490a46a | 86 | * @param int $userID |
dd244018 EM |
87 | * @param int|string $type the type of operation (view|edit) |
88 | * @param boolean $force should we force a recompute | |
6a488035 TO |
89 | * |
90 | * @return void | |
91 | * @access public | |
92 | * @static | |
93 | */ | |
94 | static function cache($userID, $type = CRM_Core_Permission::VIEW, $force = FALSE) { | |
95 | static $_processed = array(); | |
96 | ||
97 | if ($type = CRM_Core_Permission::VIEW) { | |
98 | $operationClause = " operation IN ( 'Edit', 'View' ) "; | |
99 | $operation = 'View'; | |
100 | } | |
101 | else { | |
102 | $operationClause = " operation = 'Edit' "; | |
103 | $operation = 'Edit'; | |
104 | } | |
105 | ||
106 | if (!$force) { | |
a7488080 | 107 | if (!empty($_processed[$userID])) { |
6a488035 TO |
108 | return; |
109 | } | |
110 | ||
111 | // run a query to see if the cache is filled | |
112 | $sql = " | |
113 | SELECT count(id) | |
114 | FROM civicrm_acl_contact_cache | |
115 | WHERE user_id = %1 | |
116 | AND $operationClause | |
117 | "; | |
118 | $params = array(1 => array($userID, 'Integer')); | |
119 | $count = CRM_Core_DAO::singleValueQuery($sql, $params); | |
120 | if ($count > 0) { | |
121 | $_processed[$userID] = 1; | |
122 | return; | |
123 | } | |
124 | } | |
125 | ||
126 | $tables = array(); | |
127 | $whereTables = array(); | |
128 | ||
129 | $permission = CRM_ACL_API::whereClause($type, $tables, $whereTables, $userID); | |
130 | ||
131 | $from = CRM_Contact_BAO_Query::fromClause($whereTables); | |
132 | ||
133 | CRM_Core_DAO::executeQuery(" | |
134 | INSERT INTO civicrm_acl_contact_cache ( user_id, contact_id, operation ) | |
135 | SELECT $userID as user_id, contact_a.id as contact_id, '$operation' as operation | |
136 | $from | |
137 | WHERE $permission | |
138 | GROUP BY contact_a.id | |
139 | ON DUPLICATE KEY UPDATE | |
140 | user_id=VALUES(user_id), | |
141 | contact_id=VALUES(contact_id), | |
142 | operation=VALUES(operation)" | |
143 | ); | |
144 | ||
145 | CRM_Core_DAO::executeQuery('DELETE FROM civicrm_acl_contact_cache WHERE contact_id IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1)'); | |
146 | $_processed[$userID] = 1; | |
147 | ||
148 | return; | |
149 | } | |
150 | ||
151 | /** | |
100fef9d | 152 | * Check if there are any contacts in cache table |
6a488035 | 153 | * |
da6b46f4 EM |
154 | * @param int|string $type the type of operation (view|edit) |
155 | * @param int $contactID contact id | |
6a488035 TO |
156 | * |
157 | * @return boolean | |
158 | * @access public | |
159 | * @static | |
160 | */ | |
161 | static function hasContactsInCache($type = CRM_Core_Permission::VIEW, | |
162 | $contactID = NULL | |
163 | ) { | |
164 | if (!$contactID) { | |
165 | $session = CRM_Core_Session::singleton(); | |
166 | $contactID = $session->get('userID'); | |
167 | } | |
168 | ||
169 | if ($type = CRM_Core_Permission::VIEW) { | |
170 | $operationClause = " operation IN ( 'Edit', 'View' ) "; | |
171 | $operation = 'View'; | |
172 | } | |
173 | else { | |
174 | $operationClause = " operation = 'Edit' "; | |
175 | $operation = 'Edit'; | |
176 | } | |
177 | ||
178 | // fill cache | |
179 | self::cache($contactID); | |
180 | ||
181 | $sql = " | |
182 | SELECT id | |
183 | FROM civicrm_acl_contact_cache | |
184 | WHERE user_id = %1 | |
185 | AND $operationClause LIMIT 1"; | |
186 | ||
187 | $params = array(1 => array($contactID, 'Integer')); | |
188 | return (bool) CRM_Core_DAO::singleValueQuery($sql, $params); | |
189 | } | |
190 | ||
86538308 EM |
191 | /** |
192 | * @param string $contactAlias | |
100fef9d | 193 | * @param int $contactID |
86538308 EM |
194 | * |
195 | * @return array | |
196 | */ | |
6a488035 TO |
197 | static function cacheClause($contactAlias = 'contact_a', $contactID = NULL) { |
198 | if (CRM_Core_Permission::check('view all contacts') || | |
199 | CRM_Core_Permission::check('edit all contacts') | |
200 | ) { | |
201 | if (is_array($contactAlias)) { | |
202 | $wheres = array(); | |
203 | foreach ($contactAlias as $alias) { | |
204 | // CRM-6181 | |
205 | $wheres[] = "$alias.is_deleted = 0"; | |
206 | } | |
207 | return array(NULL, '(' . implode(' AND ', $wheres) . ')'); | |
208 | } | |
209 | else { | |
210 | // CRM-6181 | |
211 | return array(NULL, "$contactAlias.is_deleted = 0"); | |
212 | } | |
213 | } | |
214 | ||
215 | $session = CRM_Core_Session::singleton(); | |
216 | $contactID = $session->get('userID'); | |
217 | if (!$contactID) { | |
218 | $contactID = 0; | |
219 | } | |
220 | $contactID = CRM_Utils_Type::escape($contactID, 'Integer'); | |
221 | ||
222 | self::cache($contactID); | |
223 | ||
224 | if (is_array($contactAlias) && !empty($contactAlias)) { | |
225 | //More than one contact alias | |
226 | $clauses = array(); | |
227 | foreach ($contactAlias as $k => $alias) { | |
228 | $clauses[] = " INNER JOIN civicrm_acl_contact_cache aclContactCache_{$k} ON {$alias}.id = aclContactCache_{$k}.contact_id AND aclContactCache_{$k}.user_id = $contactID "; | |
229 | } | |
230 | ||
231 | $fromClause = implode(" ", $clauses); | |
232 | $whereClase = NULL; | |
233 | } | |
234 | else { | |
235 | $fromClause = " INNER JOIN civicrm_acl_contact_cache aclContactCache ON {$contactAlias}.id = aclContactCache.contact_id "; | |
236 | $whereClase = " aclContactCache.user_id = $contactID "; | |
237 | } | |
238 | ||
239 | return array($fromClause, $whereClase); | |
240 | } | |
241 | ||
242 | /** | |
100fef9d | 243 | * Get the permission base on its relationship |
6a488035 | 244 | * |
c490a46a CW |
245 | * @param int $selectedContactID contact id of selected contact |
246 | * @param int $contactID contact id of the current contact | |
6a488035 | 247 | * |
c490a46a CW |
248 | * @return bool true if logged in user has permission to view |
249 | * selected contact record else false | |
6a488035 TO |
250 | * @static |
251 | */ | |
252 | static function relationship($selectedContactID, $contactID = NULL) { | |
253 | $session = CRM_Core_Session::singleton(); | |
d5f1ee75 | 254 | $config = CRM_Core_Config::singleton(); |
6a488035 TO |
255 | if (!$contactID) { |
256 | $contactID = $session->get('userID'); | |
257 | if (!$contactID) { | |
258 | return FALSE; | |
259 | } | |
260 | } | |
ad623fd4 | 261 | if ($contactID == $selectedContactID && CRM_Core_Permission::check('edit my contact')) { |
6a488035 TO |
262 | return TRUE; |
263 | } | |
264 | else { | |
d5f1ee75 DG |
265 | if ($config->secondDegRelPermissions) { |
266 | $query = " | |
267 | SELECT firstdeg.id | |
268 | FROM civicrm_relationship firstdeg | |
269 | LEFT JOIN civicrm_relationship seconddegaa | |
270 | on firstdeg.contact_id_a = seconddegaa.contact_id_b | |
271 | and seconddegaa.is_permission_b_a = 1 | |
272 | and firstdeg.is_permission_b_a = 1 | |
273 | and seconddegaa.is_active = 1 | |
274 | LEFT JOIN civicrm_relationship seconddegab | |
275 | on firstdeg.contact_id_a = seconddegab.contact_id_a | |
276 | and seconddegab.is_permission_a_b = 1 | |
277 | and firstdeg.is_permission_b_a = 1 | |
278 | and seconddegab.is_active = 1 | |
279 | LEFT JOIN civicrm_relationship seconddegba | |
280 | on firstdeg.contact_id_b = seconddegba.contact_id_b | |
281 | and seconddegba.is_permission_b_a = 1 | |
282 | and firstdeg.is_permission_a_b = 1 | |
283 | and seconddegba.is_active = 1 | |
284 | LEFT JOIN civicrm_relationship seconddegbb | |
285 | on firstdeg.contact_id_b = seconddegbb.contact_id_a | |
286 | and seconddegbb.is_permission_a_b = 1 | |
287 | and firstdeg.is_permission_a_b = 1 | |
288 | and seconddegbb.is_active = 1 | |
2efcf0c2 | 289 | WHERE |
d5f1ee75 | 290 | ( |
2efcf0c2 | 291 | ( firstdeg.contact_id_a = %1 AND firstdeg.contact_id_b = %2 AND firstdeg.is_permission_a_b = 1 ) |
d5f1ee75 | 292 | OR ( firstdeg.contact_id_a = %2 AND firstdeg.contact_id_b = %1 AND firstdeg.is_permission_b_a = 1 ) |
2efcf0c2 | 293 | OR ( |
d5f1ee75 | 294 | firstdeg.contact_id_a = %1 AND seconddegba.contact_id_a = %2 |
2efcf0c2 | 295 | AND (seconddegba.contact_id_a NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1)) |
d5f1ee75 | 296 | ) |
2efcf0c2 | 297 | OR ( |
d5f1ee75 | 298 | firstdeg.contact_id_a = %1 AND seconddegbb.contact_id_b = %2 |
2efcf0c2 | 299 | AND (seconddegbb.contact_id_b NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1)) |
d5f1ee75 | 300 | ) |
2efcf0c2 | 301 | OR ( |
d5f1ee75 | 302 | firstdeg.contact_id_b = %1 AND seconddegab.contact_id_b = %2 |
2efcf0c2 | 303 | AND (seconddegab.contact_id_b NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1)) |
d5f1ee75 | 304 | ) |
2efcf0c2 | 305 | OR ( |
306 | firstdeg.contact_id_b = %1 AND seconddegaa.contact_id_a = %2 AND (seconddegaa.contact_id_a NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1)) | |
d5f1ee75 | 307 | ) |
2efcf0c2 | 308 | ) |
309 | AND (firstdeg.contact_id_a NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1)) | |
d5f1ee75 DG |
310 | AND (firstdeg.contact_id_b NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1)) |
311 | AND ( firstdeg.is_active = 1) | |
312 | "; | |
313 | } | |
314 | else { | |
315 | $query = " | |
6a488035 TO |
316 | SELECT id |
317 | FROM civicrm_relationship | |
318 | WHERE (( contact_id_a = %1 AND contact_id_b = %2 AND is_permission_a_b = 1 ) OR | |
319 | ( contact_id_a = %2 AND contact_id_b = %1 AND is_permission_b_a = 1 )) AND | |
320 | (contact_id_a NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1)) AND | |
321 | (contact_id_b NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1)) | |
322 | AND ( civicrm_relationship.is_active = 1 ) | |
323 | "; | |
d5f1ee75 | 324 | } |
6a488035 TO |
325 | $params = array(1 => array($contactID, 'Integer'), |
326 | 2 => array($selectedContactID, 'Integer'), | |
327 | ); | |
328 | return CRM_Core_DAO::singleValueQuery($query, $params); | |
329 | } | |
330 | } | |
331 | ||
332 | ||
86538308 | 333 | /** |
100fef9d | 334 | * @param int $contactID |
c490a46a | 335 | * @param CRM_Core_Form $form |
86538308 EM |
336 | * @param bool $redirect |
337 | * | |
338 | * @return bool | |
339 | */ | |
6a488035 TO |
340 | static function validateOnlyChecksum($contactID, &$form, $redirect = TRUE) { |
341 | // check if this is of the format cs=XXX | |
342 | if (!CRM_Contact_BAO_Contact_Utils::validChecksum($contactID, | |
343 | CRM_Utils_Request::retrieve('cs', 'String', $form, FALSE) | |
344 | )) { | |
345 | if ($redirect) { | |
346 | // also set a message in the UF framework | |
347 | $message = ts('You do not have permission to edit this contact record. Contact the site administrator if you need assistance.'); | |
348 | CRM_Utils_System::setUFMessage($message); | |
349 | ||
350 | $config = CRM_Core_Config::singleton(); | |
351 | CRM_Core_Error::statusBounce($message, | |
352 | $config->userFrameworkBaseURL | |
353 | ); | |
354 | // does not come here, we redirect in the above statement | |
355 | } | |
356 | return FALSE; | |
357 | } | |
358 | ||
a9a1ea2c | 359 | // set appropriate AUTH source |
e8f14831 | 360 | self::initChecksumAuthSrc(TRUE, $form); |
a9a1ea2c | 361 | |
6a488035 TO |
362 | // so here the contact is posing as $contactID, lets set the logging contact ID variable |
363 | // CRM-8965 | |
364 | CRM_Core_DAO::executeQuery('SET @civicrm_user_id = %1', | |
365 | array(1 => array($contactID, 'Integer')) | |
366 | ); | |
77b97be7 | 367 | |
6a488035 TO |
368 | return TRUE; |
369 | } | |
370 | ||
86538308 EM |
371 | /** |
372 | * @param bool $checkSumValidationResult | |
373 | * @param null $form | |
374 | */ | |
e8f14831 | 375 | static function initChecksumAuthSrc($checkSumValidationResult = FALSE, $form = NULL) { |
a9a1ea2c | 376 | $session = CRM_Core_Session::singleton(); |
e8f14831 | 377 | if ($checkSumValidationResult && $form && CRM_Utils_Request::retrieve('cs', 'String', $form, FALSE)) { |
a9a1ea2c DS |
378 | // if result is already validated, and url has cs, set the flag. |
379 | $session->set('authSrc', CRM_Core_Permission::AUTH_SRC_CHECKSUM); | |
380 | } else if (($session->get('authSrc') & CRM_Core_Permission::AUTH_SRC_CHECKSUM) == CRM_Core_Permission::AUTH_SRC_CHECKSUM) { | |
77b97be7 | 381 | // if checksum wasn't present in REQUEST OR checksum result validated as FALSE, |
a9a1ea2c DS |
382 | // and flag was already set exactly as AUTH_SRC_CHECKSUM, unset it. |
383 | $session->set('authSrc', CRM_Core_Permission::AUTH_SRC_UNKNOWN); | |
384 | } | |
385 | } | |
386 | ||
86538308 | 387 | /** |
100fef9d | 388 | * @param int $contactID |
c490a46a | 389 | * @param CRM_Core_Form $form |
86538308 EM |
390 | * @param bool $redirect |
391 | * | |
392 | * @return bool | |
393 | */ | |
6a488035 TO |
394 | static function validateChecksumContact($contactID, &$form, $redirect = TRUE) { |
395 | if (!self::allow($contactID, CRM_Core_Permission::EDIT)) { | |
396 | // check if this is of the format cs=XXX | |
397 | return self::validateOnlyChecksum($contactID, $form, $redirect); | |
398 | } | |
399 | return TRUE; | |
400 | } | |
401 | } | |
402 |