3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
35 class CRM_Contact_BAO_Contact_Permission
{
38 * Check if the logged in user has permissions for the operation type
42 * @param int|string $type the type of operation (view|edit)
45 * true if the user has permission, false otherwise
48 public static function allow($id, $type = CRM_Core_Permission
::VIEW
) {
50 $whereTables = array();
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) {
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'))
66 //check permission based on relationship, CRM-2963
67 if (self
::relationship($id)) {
71 $permission = CRM_ACL_API
::whereClause($type, $tables, $whereTables);
73 $from = CRM_Contact_BAO_Query
::fromClause($whereTables);
76 SELECT count(DISTINCT contact_a.id)
78 WHERE contact_a.id = %1 AND $permission";
79 $params = array(1 => array($id, 'Integer'));
81 return (CRM_Core_DAO
::singleValueQuery($query, $params) > 0) ?
TRUE : FALSE;
85 * Fill the acl contact cache for this contact id if empty
88 * @param int|string $type the type of operation (view|edit)
90 * Should we force a recompute.
95 public static function cache($userID, $type = CRM_Core_Permission
::VIEW
, $force = FALSE) {
96 static $_processed = array();
98 if ($type = CRM_Core_Permission
::VIEW
) {
99 $operationClause = " operation IN ( 'Edit', 'View' ) ";
103 $operationClause = " operation = 'Edit' ";
108 if (!empty($_processed[$userID])) {
112 // run a query to see if the cache is filled
115 FROM civicrm_acl_contact_cache
119 $params = array(1 => array($userID, 'Integer'));
120 $count = CRM_Core_DAO
::singleValueQuery($sql, $params);
122 $_processed[$userID] = 1;
128 $whereTables = array();
130 $permission = CRM_ACL_API
::whereClause($type, $tables, $whereTables, $userID);
132 $from = CRM_Contact_BAO_Query
::fromClause($whereTables);
134 CRM_Core_DAO
::executeQuery("
135 INSERT INTO civicrm_acl_contact_cache ( user_id, contact_id, operation )
136 SELECT $userID as user_id, contact_a.id as contact_id, '$operation' as operation
139 GROUP BY contact_a.id
140 ON DUPLICATE KEY UPDATE
141 user_id=VALUES(user_id),
142 contact_id=VALUES(contact_id),
143 operation=VALUES(operation)"
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;
153 * Check if there are any contacts in cache table
155 * @param int|string $type the type of operation (view|edit)
156 * @param int $contactID
162 static function hasContactsInCache(
163 $type = CRM_Core_Permission
::VIEW
,
167 $session = CRM_Core_Session
::singleton();
168 $contactID = $session->get('userID');
171 if ($type = CRM_Core_Permission
::VIEW
) {
172 $operationClause = " operation IN ( 'Edit', 'View' ) ";
176 $operationClause = " operation = 'Edit' ";
181 self
::cache($contactID);
185 FROM civicrm_acl_contact_cache
187 AND $operationClause LIMIT 1";
189 $params = array(1 => array($contactID, 'Integer'));
190 return (bool) CRM_Core_DAO
::singleValueQuery($sql, $params);
194 * @param string $contactAlias
195 * @param int $contactID
199 public static function cacheClause($contactAlias = 'contact_a', $contactID = NULL) {
200 if (CRM_Core_Permission
::check('view all contacts') ||
201 CRM_Core_Permission
::check('edit all contacts')
203 if (is_array($contactAlias)) {
205 foreach ($contactAlias as $alias) {
207 $wheres[] = "$alias.is_deleted = 0";
209 return array(NULL, '(' . implode(' AND ', $wheres) . ')');
213 return array(NULL, "$contactAlias.is_deleted = 0");
217 $session = CRM_Core_Session
::singleton();
218 $contactID = $session->get('userID');
222 $contactID = CRM_Utils_Type
::escape($contactID, 'Integer');
224 self
::cache($contactID);
226 if (is_array($contactAlias) && !empty($contactAlias)) {
227 //More than one contact alias
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 ";
233 $fromClause = implode(" ", $clauses);
237 $fromClause = " INNER JOIN civicrm_acl_contact_cache aclContactCache ON {$contactAlias}.id = aclContactCache.contact_id ";
238 $whereClase = " aclContactCache.user_id = $contactID ";
241 return array($fromClause, $whereClase);
245 * Get the permission base on its relationship
247 * @param int $selectedContactID
248 * Contact id of selected contact.
249 * @param int $contactID
250 * Contact id of the current contact.
253 * true if logged in user has permission to view
254 * selected contact record else false
257 public static function relationship($selectedContactID, $contactID = NULL) {
258 $session = CRM_Core_Session
::singleton();
259 $config = CRM_Core_Config
::singleton();
261 $contactID = $session->get('userID');
266 if ($contactID == $selectedContactID && CRM_Core_Permission
::check('edit my contact')) {
270 if ($config->secondDegRelPermissions
) {
273 FROM civicrm_relationship firstdeg
274 LEFT 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
279 LEFT 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
284 LEFT 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
289 LEFT 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
296 ( firstdeg.contact_id_a = %1 AND firstdeg.contact_id_b = %2 AND firstdeg.is_permission_a_b = 1 )
297 OR ( firstdeg.contact_id_a = %2 AND firstdeg.contact_id_b = %1 AND firstdeg.is_permission_b_a = 1 )
299 firstdeg.contact_id_a = %1 AND seconddegba.contact_id_a = %2
300 AND (seconddegba.contact_id_a NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1))
303 firstdeg.contact_id_a = %1 AND seconddegbb.contact_id_b = %2
304 AND (seconddegbb.contact_id_b NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1))
307 firstdeg.contact_id_b = %1 AND seconddegab.contact_id_b = %2
308 AND (seconddegab.contact_id_b NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1))
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))
314 AND (firstdeg.contact_id_a NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1))
315 AND (firstdeg.contact_id_b NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1))
316 AND ( firstdeg.is_active = 1)
322 FROM civicrm_relationship
323 WHERE (( 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 )
331 1 => array($contactID, 'Integer'),
332 2 => array($selectedContactID, 'Integer'),
334 return CRM_Core_DAO
::singleValueQuery($query, $params);
340 * @param int $contactID
341 * @param CRM_Core_Form $form
342 * @param bool $redirect
346 public static function validateOnlyChecksum($contactID, &$form, $redirect = TRUE) {
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)
353 // also set a message in the UF framework
354 $message = ts('You do not have permission to edit this contact record. Contact the site administrator if you need assistance.');
355 CRM_Utils_System
::setUFMessage($message);
357 $config = CRM_Core_Config
::singleton();
358 CRM_Core_Error
::statusBounce($message,
359 $config->userFrameworkBaseURL
361 // does not come here, we redirect in the above statement
366 // set appropriate AUTH source
367 self
::initChecksumAuthSrc(TRUE, $form);
369 // so here the contact is posing as $contactID, lets set the logging contact ID variable
371 CRM_Core_DAO
::executeQuery('SET @civicrm_user_id = %1',
372 array(1 => array($contactID, 'Integer'))
379 * @param bool $checkSumValidationResult
382 public static function initChecksumAuthSrc($checkSumValidationResult = FALSE, $form = NULL) {
383 $session = CRM_Core_Session
::singleton();
384 if ($checkSumValidationResult && $form && CRM_Utils_Request
::retrieve('cs', 'String', $form, FALSE)) {
385 // if result is already validated, and url has cs, set the flag.
386 $session->set('authSrc', CRM_Core_Permission
::AUTH_SRC_CHECKSUM
);
388 elseif (($session->get('authSrc') & CRM_Core_Permission
::AUTH_SRC_CHECKSUM
) == CRM_Core_Permission
::AUTH_SRC_CHECKSUM
) {
389 // if checksum wasn't present in REQUEST OR checksum result validated as FALSE,
390 // and flag was already set exactly as AUTH_SRC_CHECKSUM, unset it.
391 $session->set('authSrc', CRM_Core_Permission
::AUTH_SRC_UNKNOWN
);
396 * @param int $contactID
397 * @param CRM_Core_Form $form
398 * @param bool $redirect
402 public static function validateChecksumContact($contactID, &$form, $redirect = TRUE) {
403 if (!self
::allow($contactID, CRM_Core_Permission
::EDIT
)) {
404 // check if this is of the format cs=XXX
405 return self
::validateOnlyChecksum($contactID, $form, $redirect);