3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
35 class CRM_Contact_BAO_Contact_Permission
{
38 * check if the logged in user has permissions for the operation type
40 * @param int $id contact id
41 * @param string $type the type of operation (view|edit)
43 * @return boolean true if the user has permission, false otherwise
47 static function allow($id, $type = CRM_Core_Permission
::VIEW
) {
49 $whereTables = array();
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) {
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'))
65 //check permission based on relationship, CRM-2963
66 if (self
::relationship($id)) {
70 $permission = CRM_ACL_API
::whereClause($type, $tables, $whereTables);
72 $from = CRM_Contact_BAO_Query
::fromClause($whereTables);
75 SELECT count(DISTINCT contact_a.id)
77 WHERE contact_a.id = %1 AND $permission";
78 $params = array(1 => array($id, 'Integer'));
80 return (CRM_Core_DAO
::singleValueQuery($query, $params) > 0) ?
TRUE : FALSE;
84 * fill the acl contact cache for this contact id if empty
86 * @param int $id contact id
87 * @param string $type the type of operation (view|edit)
88 * @param boolean $force should we force a recompute
94 static function cache($userID, $type = CRM_Core_Permission
::VIEW
, $force = FALSE) {
95 static $_processed = array();
97 if ($type = CRM_Core_Permission
::VIEW
) {
98 $operationClause = " operation IN ( 'Edit', 'View' ) ";
102 $operationClause = " operation = 'Edit' ";
107 if (CRM_Utils_Array
::value($userID, $_processed)) {
111 // run a query to see if the cache is filled
114 FROM civicrm_acl_contact_cache
118 $params = array(1 => array($userID, 'Integer'));
119 $count = CRM_Core_DAO
::singleValueQuery($sql, $params);
121 $_processed[$userID] = 1;
127 $whereTables = array();
129 $permission = CRM_ACL_API
::whereClause($type, $tables, $whereTables, $userID);
131 $from = CRM_Contact_BAO_Query
::fromClause($whereTables);
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
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)"
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;
152 * Function to check if there are any contacts in cache table
154 * @param string $type the type of operation (view|edit)
155 * @param int $contactID contact id
161 static function hasContactsInCache($type = CRM_Core_Permission
::VIEW
,
165 $session = CRM_Core_Session
::singleton();
166 $contactID = $session->get('userID');
169 if ($type = CRM_Core_Permission
::VIEW
) {
170 $operationClause = " operation IN ( 'Edit', 'View' ) ";
174 $operationClause = " operation = 'Edit' ";
179 self
::cache($contactID);
183 FROM civicrm_acl_contact_cache
185 AND $operationClause LIMIT 1";
187 $params = array(1 => array($contactID, 'Integer'));
188 return (bool) CRM_Core_DAO
::singleValueQuery($sql, $params);
191 static function cacheClause($contactAlias = 'contact_a', $contactID = NULL) {
192 if (CRM_Core_Permission
::check('view all contacts') ||
193 CRM_Core_Permission
::check('edit all contacts')
195 if (is_array($contactAlias)) {
197 foreach ($contactAlias as $alias) {
199 $wheres[] = "$alias.is_deleted = 0";
201 return array(NULL, '(' . implode(' AND ', $wheres) . ')');
205 return array(NULL, "$contactAlias.is_deleted = 0");
209 $session = CRM_Core_Session
::singleton();
210 $contactID = $session->get('userID');
214 $contactID = CRM_Utils_Type
::escape($contactID, 'Integer');
216 self
::cache($contactID);
218 if (is_array($contactAlias) && !empty($contactAlias)) {
219 //More than one contact alias
221 foreach ($contactAlias as $k => $alias) {
222 $clauses[] = " INNER JOIN civicrm_acl_contact_cache aclContactCache_{$k} ON {$alias}.id = aclContactCache_{$k}.contact_id AND aclContactCache_{$k}.user_id = $contactID ";
225 $fromClause = implode(" ", $clauses);
229 $fromClause = " INNER JOIN civicrm_acl_contact_cache aclContactCache ON {$contactAlias}.id = aclContactCache.contact_id ";
230 $whereClase = " aclContactCache.user_id = $contactID ";
233 return array($fromClause, $whereClase);
237 * Function to get the permission base on its relationship
239 * @param int $selectedContactId contact id of selected contact
240 * @param int $contactId contact id of the current contact
242 * @return booleab true if logged in user has permission to view
243 * selected contact record else false
246 static function relationship($selectedContactID, $contactID = NULL) {
247 $session = CRM_Core_Session
::singleton();
248 $config = CRM_Core_Config
::singleton();
250 $contactID = $session->get('userID');
255 if ($contactID == $selectedContactID && CRM_Core_Permission
::check('edit my contact')) {
259 if ($config->secondDegRelPermissions
) {
262 FROM civicrm_relationship firstdeg
263 LEFT JOIN civicrm_relationship seconddegaa
264 on firstdeg.contact_id_a = seconddegaa.contact_id_b
265 and seconddegaa.is_permission_b_a = 1
266 and firstdeg.is_permission_b_a = 1
267 and seconddegaa.is_active = 1
268 LEFT JOIN civicrm_relationship seconddegab
269 on firstdeg.contact_id_a = seconddegab.contact_id_a
270 and seconddegab.is_permission_a_b = 1
271 and firstdeg.is_permission_b_a = 1
272 and seconddegab.is_active = 1
273 LEFT JOIN civicrm_relationship seconddegba
274 on firstdeg.contact_id_b = seconddegba.contact_id_b
275 and seconddegba.is_permission_b_a = 1
276 and firstdeg.is_permission_a_b = 1
277 and seconddegba.is_active = 1
278 LEFT JOIN civicrm_relationship seconddegbb
279 on firstdeg.contact_id_b = seconddegbb.contact_id_a
280 and seconddegbb.is_permission_a_b = 1
281 and firstdeg.is_permission_a_b = 1
282 and seconddegbb.is_active = 1
285 ( firstdeg.contact_id_a = %1 AND firstdeg.contact_id_b = %2 AND firstdeg.is_permission_a_b = 1 )
286 OR ( firstdeg.contact_id_a = %2 AND firstdeg.contact_id_b = %1 AND firstdeg.is_permission_b_a = 1 )
288 firstdeg.contact_id_a = %1 AND seconddegba.contact_id_a = %2
289 AND (seconddegba.contact_id_a NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1))
292 firstdeg.contact_id_a = %1 AND seconddegbb.contact_id_b = %2
293 AND (seconddegbb.contact_id_b NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1))
296 firstdeg.contact_id_b = %1 AND seconddegab.contact_id_b = %2
297 AND (seconddegab.contact_id_b NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1))
300 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))
303 AND (firstdeg.contact_id_a NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1))
304 AND (firstdeg.contact_id_b NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1))
305 AND ( firstdeg.is_active = 1)
311 FROM civicrm_relationship
312 WHERE (( contact_id_a = %1 AND contact_id_b = %2 AND is_permission_a_b = 1 ) OR
313 ( contact_id_a = %2 AND contact_id_b = %1 AND is_permission_b_a = 1 )) AND
314 (contact_id_a NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1)) AND
315 (contact_id_b NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1))
316 AND ( civicrm_relationship.is_active = 1 )
319 $params = array(1 => array($contactID, 'Integer'),
320 2 => array($selectedContactID, 'Integer'),
322 return CRM_Core_DAO
::singleValueQuery($query, $params);
327 static function validateOnlyChecksum($contactID, &$form, $redirect = TRUE) {
328 // check if this is of the format cs=XXX
329 if (!CRM_Contact_BAO_Contact_Utils
::validChecksum($contactID,
330 CRM_Utils_Request
::retrieve('cs', 'String', $form, FALSE)
333 // also set a message in the UF framework
334 $message = ts('You do not have permission to edit this contact record. Contact the site administrator if you need assistance.');
335 CRM_Utils_System
::setUFMessage($message);
337 $config = CRM_Core_Config
::singleton();
338 CRM_Core_Error
::statusBounce($message,
339 $config->userFrameworkBaseURL
341 // does not come here, we redirect in the above statement
346 // set appropriate AUTH source
347 self
::initChecksumAuthSrc(TRUE, $form);
349 // so here the contact is posing as $contactID, lets set the logging contact ID variable
351 CRM_Core_DAO
::executeQuery('SET @civicrm_user_id = %1',
352 array(1 => array($contactID, 'Integer'))
358 static function initChecksumAuthSrc($checkSumValidationResult = FALSE, $form = NULL) {
359 $session = CRM_Core_Session
::singleton();
360 if ($checkSumValidationResult && $form && CRM_Utils_Request
::retrieve('cs', 'String', $form, FALSE)) {
361 // if result is already validated, and url has cs, set the flag.
362 $session->set('authSrc', CRM_Core_Permission
::AUTH_SRC_CHECKSUM
);
363 } else if (($session->get('authSrc') & CRM_Core_Permission
::AUTH_SRC_CHECKSUM
) == CRM_Core_Permission
::AUTH_SRC_CHECKSUM
) {
364 // if checksum wasn't present in REQUEST OR checksum result validated as FALSE,
365 // and flag was already set exactly as AUTH_SRC_CHECKSUM, unset it.
366 $session->set('authSrc', CRM_Core_Permission
::AUTH_SRC_UNKNOWN
);
370 static function validateChecksumContact($contactID, &$form, $redirect = TRUE) {
371 if (!self
::allow($contactID, CRM_Core_Permission
::EDIT
)) {
372 // check if this is of the format cs=XXX
373 return self
::validateOnlyChecksum($contactID, $form, $redirect);