Commit | Line | Data |
---|---|---|
7f254ad8 AE |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.6 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2015 | | |
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-2015 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | class CRM_Contact_BAO_Contact_Permission { | |
36 | ||
37 | /** | |
38 | * Check if the logged in user has permissions for the operation type. | |
39 | * | |
40 | * @param int $id | |
41 | * Contact id. | |
42 | * @param int|string $type the type of operation (view|edit) | |
43 | * | |
44 | * @return bool | |
45 | * true if the user has permission, false otherwise | |
46 | */ | |
47 | public 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 | /** | |
84 | * Fill the acl contact cache for this contact id if empty. | |
85 | * | |
86 | * @param int $userID | |
87 | * @param int|string $type the type of operation (view|edit) | |
88 | * @param bool $force | |
89 | * Should we force a recompute. | |
90 | * | |
91 | * @return void | |
92 | */ | |
93 | public static function cache($userID, $type = CRM_Core_Permission::VIEW, $force = FALSE) { | |
94 | static $_processed = array(); | |
95 | ||
96 | if ($type = CRM_Core_Permission::VIEW) { | |
97 | $operationClause = " operation IN ( 'Edit', 'View' ) "; | |
98 | $operation = 'View'; | |
99 | } | |
100 | else { | |
101 | $operationClause = " operation = 'Edit' "; | |
102 | $operation = 'Edit'; | |
103 | } | |
104 | ||
105 | if (!$force) { | |
106 | if (!empty($_processed[$userID])) { | |
107 | return; | |
108 | } | |
109 | ||
110 | // run a query to see if the cache is filled | |
111 | $sql = " | |
112 | SELECT count(id) | |
113 | FROM civicrm_acl_contact_cache | |
114 | WHERE user_id = %1 | |
115 | AND $operationClause | |
116 | "; | |
117 | $params = array(1 => array($userID, 'Integer')); | |
118 | $count = CRM_Core_DAO::singleValueQuery($sql, $params); | |
119 | if ($count > 0) { | |
120 | $_processed[$userID] = 1; | |
121 | return; | |
122 | } | |
123 | } | |
124 | ||
125 | $tables = array(); | |
126 | $whereTables = array(); | |
127 | ||
128 | $permission = CRM_ACL_API::whereClause($type, $tables, $whereTables, $userID); | |
129 | ||
130 | $from = CRM_Contact_BAO_Query::fromClause($whereTables); | |
131 | ||
132 | CRM_Core_DAO::executeQuery(" | |
133 | INSERT INTO civicrm_acl_contact_cache ( user_id, contact_id, operation ) | |
134 | SELECT $userID as user_id, contact_a.id as contact_id, '$operation' as operation | |
135 | $from | |
136 | WHERE $permission | |
137 | GROUP BY contact_a.id | |
138 | ON DUPLICATE KEY UPDATE | |
139 | user_id=VALUES(user_id), | |
140 | contact_id=VALUES(contact_id), | |
141 | operation=VALUES(operation)" | |
142 | ); | |
143 | ||
144 | $_processed[$userID] = 1; | |
145 | } | |
146 | ||
147 | /** | |
148 | * Check if there are any contacts in cache table. | |
149 | * | |
150 | * @param int|string $type the type of operation (view|edit) | |
151 | * @param int $contactID | |
152 | * Contact id. | |
153 | * | |
154 | * @return bool | |
155 | */ | |
156 | public static function hasContactsInCache( | |
157 | $type = CRM_Core_Permission::VIEW, | |
158 | $contactID = NULL | |
159 | ) { | |
160 | if (!$contactID) { | |
161 | $session = CRM_Core_Session::singleton(); | |
162 | $contactID = $session->get('userID'); | |
163 | } | |
164 | ||
165 | if ($type = CRM_Core_Permission::VIEW) { | |
166 | $operationClause = " operation IN ( 'Edit', 'View' ) "; | |
167 | $operation = 'View'; | |
168 | } | |
169 | else { | |
170 | $operationClause = " operation = 'Edit' "; | |
171 | $operation = 'Edit'; | |
172 | } | |
173 | ||
174 | // fill cache | |
175 | self::cache($contactID); | |
176 | ||
177 | $sql = " | |
178 | SELECT id | |
179 | FROM civicrm_acl_contact_cache | |
180 | WHERE user_id = %1 | |
181 | AND $operationClause LIMIT 1"; | |
182 | ||
183 | $params = array(1 => array($contactID, 'Integer')); | |
184 | return (bool) CRM_Core_DAO::singleValueQuery($sql, $params); | |
185 | } | |
186 | ||
187 | /** | |
188 | * @param string $contactAlias | |
189 | * @param int $contactID | |
190 | * | |
191 | * @return array | |
192 | */ | |
193 | public static function cacheClause($contactAlias = 'contact_a', $contactID = NULL) { | |
194 | if (CRM_Core_Permission::check('view all contacts') || | |
195 | CRM_Core_Permission::check('edit all contacts') | |
196 | ) { | |
197 | if (is_array($contactAlias)) { | |
198 | $wheres = array(); | |
199 | foreach ($contactAlias as $alias) { | |
200 | // CRM-6181 | |
201 | $wheres[] = "$alias.is_deleted = 0"; | |
202 | } | |
203 | return array(NULL, '(' . implode(' AND ', $wheres) . ')'); | |
204 | } | |
205 | else { | |
206 | // CRM-6181 | |
207 | return array(NULL, "$contactAlias.is_deleted = 0"); | |
208 | } | |
209 | } | |
210 | ||
211 | $session = CRM_Core_Session::singleton(); | |
212 | $contactID = $session->get('userID'); | |
213 | if (!$contactID) { | |
214 | $contactID = 0; | |
215 | } | |
216 | $contactID = CRM_Utils_Type::escape($contactID, 'Integer'); | |
217 | ||
218 | self::cache($contactID); | |
219 | ||
220 | if (is_array($contactAlias) && !empty($contactAlias)) { | |
221 | //More than one contact alias | |
222 | $clauses = array(); | |
223 | foreach ($contactAlias as $k => $alias) { | |
224 | $clauses[] = " INNER JOIN civicrm_acl_contact_cache aclContactCache_{$k} ON {$alias}.id = aclContactCache_{$k}.contact_id AND aclContactCache_{$k}.user_id = $contactID "; | |
225 | } | |
226 | ||
227 | $fromClause = implode(" ", $clauses); | |
228 | $whereClase = NULL; | |
229 | } | |
230 | else { | |
231 | $fromClause = " INNER JOIN civicrm_acl_contact_cache aclContactCache ON {$contactAlias}.id = aclContactCache.contact_id "; | |
232 | $whereClase = " aclContactCache.user_id = $contactID AND $contactAlias.is_deleted = 0"; | |
233 | } | |
234 | ||
235 | return array($fromClause, $whereClase); | |
236 | } | |
237 | ||
238 | /** | |
239 | * Get the permission base on its relationship. | |
240 | * | |
241 | * @param int $selectedContactID | |
242 | * Contact id of selected contact. | |
243 | * @param int $contactID | |
244 | * Contact id of the current contact. | |
245 | * | |
246 | * @return bool | |
247 | * true if logged in user has permission to view | |
248 | * selected contact record else false | |
249 | */ | |
250 | public static function relationship($selectedContactID, $contactID = NULL) { | |
251 | $session = CRM_Core_Session::singleton(); | |
252 | $config = CRM_Core_Config::singleton(); | |
253 | if (!$contactID) { | |
254 | $contactID = $session->get('userID'); | |
255 | if (!$contactID) { | |
256 | return FALSE; | |
257 | } | |
258 | } | |
259 | if ($contactID == $selectedContactID && | |
260 | (CRM_Core_Permission::check('edit my contact')) | |
261 | ) { | |
262 | return TRUE; | |
263 | } | |
264 | else { | |
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 | |
289 | WHERE | |
290 | ( | |
291 | ( firstdeg.contact_id_a = %1 AND firstdeg.contact_id_b = %2 AND firstdeg.is_permission_a_b = 1 ) | |
292 | OR ( firstdeg.contact_id_a = %2 AND firstdeg.contact_id_b = %1 AND firstdeg.is_permission_b_a = 1 ) | |
293 | OR ( | |
294 | firstdeg.contact_id_a = %1 AND seconddegba.contact_id_a = %2 | |
295 | AND (seconddegba.contact_id_a NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1)) | |
296 | ) | |
297 | OR ( | |
298 | firstdeg.contact_id_a = %1 AND seconddegbb.contact_id_b = %2 | |
299 | AND (seconddegbb.contact_id_b NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1)) | |
300 | ) | |
301 | OR ( | |
302 | firstdeg.contact_id_b = %1 AND seconddegab.contact_id_b = %2 | |
303 | AND (seconddegab.contact_id_b NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1)) | |
304 | ) | |
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)) | |
307 | ) | |
308 | ) | |
309 | AND (firstdeg.contact_id_a NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1)) | |
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 = " | |
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 | "; | |
324 | } | |
325 | $params = array( | |
326 | 1 => array($contactID, 'Integer'), | |
327 | 2 => array($selectedContactID, 'Integer'), | |
328 | ); | |
329 | return CRM_Core_DAO::singleValueQuery($query, $params); | |
330 | } | |
331 | } | |
332 | ||
333 | ||
334 | /** | |
335 | * @param int $contactID | |
336 | * @param CRM_Core_Form $form | |
337 | * @param bool $redirect | |
338 | * | |
339 | * @return bool | |
340 | */ | |
341 | public static function validateOnlyChecksum($contactID, &$form, $redirect = TRUE) { | |
342 | // check if this is of the format cs=XXX | |
343 | if (!CRM_Contact_BAO_Contact_Utils::validChecksum($contactID, | |
344 | CRM_Utils_Request::retrieve('cs', 'String', $form, FALSE) | |
345 | ) | |
346 | ) { | |
347 | if ($redirect) { | |
348 | // also set a message in the UF framework | |
349 | $message = ts('You do not have permission to edit this contact record. Contact the site administrator if you need assistance.'); | |
350 | CRM_Utils_System::setUFMessage($message); | |
351 | ||
352 | $config = CRM_Core_Config::singleton(); | |
353 | CRM_Core_Error::statusBounce($message, | |
354 | $config->userFrameworkBaseURL | |
355 | ); | |
356 | // does not come here, we redirect in the above statement | |
357 | } | |
358 | return FALSE; | |
359 | } | |
360 | ||
361 | // set appropriate AUTH source | |
362 | self::initChecksumAuthSrc(TRUE, $form); | |
363 | ||
364 | // so here the contact is posing as $contactID, lets set the logging contact ID variable | |
365 | // CRM-8965 | |
366 | CRM_Core_DAO::executeQuery('SET @civicrm_user_id = %1', | |
367 | array(1 => array($contactID, 'Integer')) | |
368 | ); | |
369 | ||
370 | return TRUE; | |
371 | } | |
372 | ||
373 | /** | |
374 | * @param bool $checkSumValidationResult | |
375 | * @param null $form | |
376 | */ | |
377 | public static function initChecksumAuthSrc($checkSumValidationResult = FALSE, $form = NULL) { | |
378 | $session = CRM_Core_Session::singleton(); | |
379 | if ($checkSumValidationResult && $form && CRM_Utils_Request::retrieve('cs', 'String', $form, FALSE)) { | |
380 | // if result is already validated, and url has cs, set the flag. | |
381 | $session->set('authSrc', CRM_Core_Permission::AUTH_SRC_CHECKSUM); | |
382 | } | |
383 | elseif (($session->get('authSrc') & CRM_Core_Permission::AUTH_SRC_CHECKSUM) == CRM_Core_Permission::AUTH_SRC_CHECKSUM) { | |
384 | // if checksum wasn't present in REQUEST OR checksum result validated as FALSE, | |
385 | // and flag was already set exactly as AUTH_SRC_CHECKSUM, unset it. | |
386 | $session->set('authSrc', CRM_Core_Permission::AUTH_SRC_UNKNOWN); | |
387 | } | |
388 | } | |
389 | ||
390 | /** | |
391 | * @param int $contactID | |
392 | * @param CRM_Core_Form $form | |
393 | * @param bool $redirect | |
394 | * | |
395 | * @return bool | |
396 | */ | |
397 | public static function validateChecksumContact($contactID, &$form, $redirect = TRUE) { | |
398 | if (!self::allow($contactID, CRM_Core_Permission::EDIT)) { | |
399 | // check if this is of the format cs=XXX | |
400 | return self::validateOnlyChecksum($contactID, $form, $redirect); | |
401 | } | |
402 | return TRUE; | |
403 | } | |
404 | ||
405 | } |