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