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