3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 class CRM_ACL_BAO_ACL
extends CRM_ACL_DAO_ACL
{
25 public static $_entityTable = NULL;
26 public static $_objectTable = NULL;
27 public static $_operation = NULL;
29 public static $_fieldKeys = NULL;
32 * Get ACL entity table.
36 public static function entityTable() {
37 CRM_Core_Error
::deprecatedFunctionWarning('unused function to be removed');
38 if (!self
::$_entityTable) {
39 self
::$_entityTable = [
40 'civicrm_contact' => ts('Contact'),
41 'civicrm_acl_role' => ts('ACL Role'),
44 return self
::$_entityTable;
50 public static function objectTable() {
51 CRM_Core_Error
::deprecatedFunctionWarning('unused function to be removed');
52 if (!self
::$_objectTable) {
53 self
::$_objectTable = [
54 'civicrm_contact' => ts('Contact'),
55 'civicrm_group' => ts('Group'),
56 'civicrm_saved_search' => ts('Contact Group'),
57 'civicrm_admin' => ts('Import'),
60 return self
::$_objectTable;
64 * Available operations for pseudoconstant.
68 public static function operation() {
69 if (!self
::$_operation) {
73 'Create' => ts('Create'),
74 'Delete' => ts('Delete'),
75 'Search' => ts('Search'),
79 return self
::$_operation;
83 * Given a table and id pair, return the filter clause
85 * @param string $table
86 * The table owning the object.
88 * The ID of the object.
89 * @param array $tables
90 * Tables that will be needed in the FROM.
93 * WHERE-style clause to filter results,
94 * or null if $table or $id is null
96 * @throws \CRM_Core_Exception
98 public static function getClause($table, $id, &$tables) {
99 CRM_Core_Error
::deprecatedFunctionWarning('unused function to be removed');
100 $table = CRM_Utils_Type
::escape($table, 'String');
101 $id = CRM_Utils_Type
::escape($id, 'Integer');
104 $ssTable = CRM_Contact_BAO_SavedSearch
::getTableName();
109 elseif ($table == $ssTable) {
110 return CRM_Contact_BAO_SavedSearch
::whereClause($id, $tables, $whereTables);
112 elseif (!empty($id)) {
113 $tables[$table] = TRUE;
114 return "$table.id = $id";
120 * Construct an associative array of an ACL rule's properties
122 * @param string $format
123 * Sprintf format for array.
124 * @param bool $hideEmpty
125 * Only return elements that have a value set.
128 * Assoc. array of the ACL rule's properties
130 public function toArray($format = '%s', $hideEmpty = FALSE) {
133 if (!self
::$_fieldKeys) {
134 $fields = CRM_ACL_DAO_ACL
::fields();
135 self
::$_fieldKeys = array_keys($fields);
138 foreach (self
::$_fieldKeys as $field) {
139 $result[$field] = $this->$field;
145 * Retrieve ACLs for a contact or group. Note that including a contact id
146 * without a group id will return those ACL rules which are granted
147 * directly to the contact, but not those granted to the contact through
148 * any/all of his group memberships.
150 * @param int $contact_id
151 * ID of a contact to search for.
154 * Array of assoc. arrays of ACL rules
156 * @throws \CRM_Core_Exception
158 protected static function getACLs($contact_id = NULL) {
161 if (empty($contact_id)) {
165 $contact_id = CRM_Utils_Type
::escape($contact_id, 'Integer');
167 $rule = new CRM_ACL_BAO_ACL();
169 $acl = self
::getTableName();
170 $contact = CRM_Contact_BAO_Contact
::getTableName();
171 $c2g = CRM_Contact_BAO_GroupContact
::getTableName();
172 $group = CRM_Contact_BAO_Group
::getTableName();
174 $query = " SELECT acl.*
177 if (!empty($contact_id)) {
178 $query .= " WHERE acl.entity_table = '$contact'
179 AND acl.entity_id = $contact_id";
182 $rule->query($query);
184 while ($rule->fetch()) {
185 $results[$rule->id
] = $rule->toArray();
188 $results +
= self
::getACLRoles($contact_id);
194 * Get all of the ACLs through ACL groups.
196 * @param int $contact_id
197 * ID of a contact to search for.
200 * Array of assoc. arrays of ACL rules
202 * @throws \CRM_Core_Exception
204 protected static function getACLRoles($contact_id = NULL) {
205 $contact_id = CRM_Utils_Type
::escape($contact_id, 'Integer');
207 $rule = new CRM_ACL_BAO_ACL();
209 $contact = CRM_Contact_BAO_Contact
::getTableName();
211 $query = 'SELECT acl.* FROM civicrm_acl acl';
212 $where = ['acl.entity_table = "civicrm_acl_role" AND acl.entity_id IN (' . implode(',', array_keys(CRM_Core_OptionGroup
::values('acl_role'))) . ')'];
214 if (!empty($contact_id)) {
215 $where[] = " acl.entity_table = '$contact' AND acl.is_active = 1 AND acl.entity_id = $contact_id";
220 $rule->query($query . ' WHERE ' . implode(' AND ', $where));
222 while ($rule->fetch()) {
223 $results[$rule->id
] = $rule->toArray();
230 * Get all ACLs granted to a contact through all group memberships.
232 * @param int $contact_id
234 * @param bool $aclRoles
235 * Include ACL Roles?.
238 * Assoc array of ACL rules
239 * @throws \CRM_Core_Exception
241 protected static function getGroupACLs($contact_id, $aclRoles = FALSE) {
242 $contact_id = CRM_Utils_Type
::escape($contact_id, 'Integer');
244 $rule = new CRM_ACL_BAO_ACL();
246 $acl = self
::getTableName();
247 $c2g = CRM_Contact_BAO_GroupContact
::getTableName();
248 $group = CRM_Contact_BAO_Group
::getTableName();
255 INNER JOIN $c2g group_contact
256 ON acl.entity_id = group_contact.group_id
257 WHERE acl.entity_table = '$group'
258 AND group_contact.contact_id = $contact_id
259 AND group_contact.status = 'Added'";
261 $rule->query($query);
263 while ($rule->fetch()) {
264 $results[$rule->id
] = $rule->toArray();
269 $results +
= self
::getGroupACLRoles($contact_id);
276 * Get all of the ACLs for a contact through ACL groups owned by Contact.
279 * @param int $contact_id
280 * ID of a contact to search for.
283 * Array of assoc. arrays of ACL rules
284 * @throws \CRM_Core_Exception
286 protected static function getGroupACLRoles($contact_id) {
287 $contact_id = CRM_Utils_Type
::escape($contact_id, 'Integer');
289 $rule = new CRM_ACL_BAO_ACL();
291 $acl = self
::getTableName();
292 $aclRole = 'civicrm_acl_role';
294 $aclER = CRM_ACL_DAO_EntityRole
::getTableName();
295 $c2g = CRM_Contact_BAO_GroupContact
::getTableName();
296 $group = CRM_Contact_BAO_Group
::getTableName();
298 $query = " SELECT acl.*
300 INNER JOIN civicrm_option_group og
301 ON og.name = 'acl_role'
302 INNER JOIN civicrm_option_value ov
303 ON acl.entity_table = '$aclRole'
304 AND ov.option_group_id = og.id
305 AND acl.entity_id = ov.value
308 ON $aclER.acl_role_id = acl.entity_id
309 AND $aclER.is_active = 1
311 ON $aclER.entity_id = $c2g.group_id
312 AND $aclER.entity_table = 'civicrm_group'
313 WHERE acl.entity_table = '$aclRole'
314 AND acl.is_active = 1
315 AND $c2g.contact_id = $contact_id
316 AND $c2g.status = 'Added'";
320 $rule->query($query);
322 while ($rule->fetch()) {
323 $results[$rule->id
] = $rule->toArray();
326 // also get all acls for "Any Role" case
327 // and authenticated User Role if present
329 $session = CRM_Core_Session
::singleton();
330 if ($session->get('ufID') > 0) {
337 WHERE acl.entity_id IN ( $roles )
338 AND acl.entity_table = 'civicrm_acl_role'
341 $rule->query($query);
342 while ($rule->fetch()) {
343 $results[$rule->id
] = $rule->toArray();
350 * Get all ACLs owned by a given contact, including domain and group-level.
352 * @param int $contact_id
356 * Assoc array of ACL rules
358 * @throws \CRM_Core_Exception
360 public static function getAllByContact($contact_id) {
363 /* First, the contact-specific ACLs, including ACL Roles */
364 $result +
= self
::getACLs($contact_id);
366 /* Then, all ACLs granted through group membership */
367 $result +
= self
::getGroupACLs($contact_id, TRUE);
373 * @param array $params
375 * @return CRM_ACL_DAO_ACL
377 public static function create($params) {
378 $dao = new CRM_ACL_DAO_ACL();
379 $dao->copyValues($params);
385 * @param array $params
386 * @param array $defaults
388 public static function retrieve(&$params, &$defaults) {
389 CRM_Core_DAO
::commonRetrieve('CRM_ACL_DAO_ACL', $params, $defaults);
393 * Update the is_active flag in the db.
396 * Id of the database record.
397 * @param bool $is_active
398 * Value we want to set the is_active field.
401 * true if we found and updated the object, else false
403 public static function setIsActive($id, $is_active) {
404 Civi
::cache('fields')->flush();
405 // reset ACL and system caches.
406 CRM_Core_BAO_Cache
::resetCaches();
408 return CRM_Core_DAO
::setFieldValue('CRM_ACL_DAO_ACL', $id, 'is_active', $is_active);
413 * @param int $contactID
417 public static function check($str, $contactID) {
419 $acls = CRM_ACL_BAO_Cache
::build($contactID);
421 $aclKeys = array_keys($acls);
422 $aclKeys = implode(',', $aclKeys);
424 if (empty($aclKeys)) {
430 FROM civicrm_acl_cache c, civicrm_acl a
431 WHERE c.acl_id = a.id
433 AND a.object_table = %1
434 AND a.id IN ( $aclKeys )
436 $params = [1 => [$str, 'String']];
438 $count = CRM_Core_DAO
::singleValueQuery($query, $params);
439 return ($count) ?
TRUE : FALSE;
445 * @param $whereTables
446 * @param int $contactID
448 * @return null|string
450 public static function whereClause($type, &$tables, &$whereTables, $contactID = NULL) {
451 $acls = CRM_ACL_BAO_Cache
::build($contactID);
457 $aclKeys = array_keys($acls);
458 $aclKeys = implode(',', $aclKeys);
461 SELECT a.operation, a.object_id
462 FROM civicrm_acl_cache c, civicrm_acl a
463 WHERE c.acl_id = a.id
465 AND a.object_table = 'civicrm_saved_search'
466 AND a.id IN ( $aclKeys )
470 $dao = CRM_Core_DAO
::executeQuery($query);
472 // do an or of all the where clauses u see
474 while ($dao->fetch()) {
475 // make sure operation matches the type TODO
476 if (self
::matchType($type, $dao->operation
)) {
477 if (!$dao->object_id
) {
479 $whereClause = ' ( 1 ) ';
482 $ids[] = $dao->object_id
;
487 $ids = implode(',', $ids);
491 WHERE g.id IN ( $ids )
494 $dao = CRM_Core_DAO
::executeQuery($query);
496 $groupContactCacheClause = FALSE;
497 while ($dao->fetch()) {
498 $groupIDs[] = $dao->id
;
500 if (($dao->saved_search_id ||
$dao->children ||
$dao->parents
)) {
501 if ($dao->cache_date
== NULL) {
502 CRM_Contact_BAO_GroupContactCache
::load($dao);
504 $groupContactCacheClause = " UNION SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id IN (" . implode(', ', $groupIDs) . ")";
512 SELECT contact_id FROM civicrm_group_contact WHERE group_id IN (" . implode(', ', $groupIDs) . ") AND status = 'Added'
513 $groupContactCacheClause
520 if (!empty($clauses)) {
521 $whereClause = ' ( ' . implode(' OR ', $clauses) . ' ) ';
524 // call the hook to get additional whereClauses
525 CRM_Utils_Hook
::aclWhereClause($type, $tables, $whereTables, $contactID, $whereClause);
527 if (empty($whereClause)) {
528 $whereClause = ' ( 0 ) ';
536 * @param int $contactID
537 * @param string $tableName
538 * @param null $allGroups
539 * @param null $includedGroups
543 public static function group(
546 $tableName = 'civicrm_saved_search',
548 $includedGroups = NULL
550 $userCacheKey = "{$contactID}_{$type}_{$tableName}_" . CRM_Core_Config
::domainID() . '_' . md5(implode(',', array_merge((array) $allGroups, (array) $includedGroups)));
551 if (empty(Civi
::$statics[__CLASS__
]['permissioned_groups'])) {
552 Civi
::$statics[__CLASS__
]['permissioned_groups'] = [];
554 if (!empty(Civi
::$statics[__CLASS__
]['permissioned_groups'][$userCacheKey])) {
555 return Civi
::$statics[__CLASS__
]['permissioned_groups'][$userCacheKey];
558 if ($allGroups == NULL) {
559 $allGroups = CRM_Contact_BAO_Contact
::buildOptions('group_id', NULL, ['onlyActive' => FALSE]);
562 $acls = CRM_ACL_BAO_Cache
::build($contactID);
566 $aclKeys = array_keys($acls);
567 $aclKeys = implode(',', $aclKeys);
569 $cacheKey = CRM_Utils_Cache
::cleanKey("$tableName-$aclKeys");
570 $cache = CRM_Utils_Cache
::singleton();
571 $ids = $cache->get($cacheKey);
575 SELECT a.operation, a.object_id
576 FROM civicrm_acl_cache c, civicrm_acl a
577 WHERE c.acl_id = a.id
579 AND a.object_table = %1
580 AND a.id IN ( $aclKeys )
581 GROUP BY a.operation,a.object_id
584 $params = [1 => [$tableName, 'String']];
585 $dao = CRM_Core_DAO
::executeQuery($query, $params);
586 while ($dao->fetch()) {
587 if ($dao->object_id
) {
588 if (self
::matchType($type, $dao->operation
)) {
589 $ids[] = $dao->object_id
;
593 // this user has got the permission for all objects of this type
594 // check if the type matches
595 if (self
::matchType($type, $dao->operation
)) {
596 foreach ($allGroups as $id => $dontCare) {
603 $cache->set($cacheKey, $ids);
607 if (empty($ids) && !empty($includedGroups) &&
608 is_array($includedGroups)
610 $ids = $includedGroups;
614 if (!empty($allGroups)) {
615 $groupWhere = " AND id IN (" . implode(',', array_keys($allGroups)) . ")";
617 // Contacts create hidden groups from search results. They should be able to retrieve their own.
618 $ownHiddenGroupsList = CRM_Core_DAO
::singleValueQuery("
619 SELECT GROUP_CONCAT(id) FROM civicrm_group WHERE is_hidden =1 AND created_id = $contactID
622 if ($ownHiddenGroupsList) {
623 $ownHiddenGroups = explode(',', $ownHiddenGroupsList);
624 $ids = array_merge((array) $ids, $ownHiddenGroups);
629 CRM_Utils_Hook
::aclGroup($type, $contactID, $tableName, $allGroups, $ids);
630 Civi
::$statics[__CLASS__
]['permissioned_groups'][$userCacheKey] = $ids;
640 protected static function matchType($type, $operation) {
642 switch ($operation) {
648 if ($type == CRM_ACL_API
::VIEW
) {
654 if ($type == CRM_ACL_API
::VIEW ||
$type == CRM_ACL_API
::EDIT
) {
660 if ($type == CRM_ACL_API
::CREATE
) {
666 if ($type == CRM_ACL_API
::DELETE
) {
672 if ($type == CRM_ACL_API
::SEARCH
) {
681 * Delete ACL records.
684 * ID of the ACL record to be deleted.
687 public static function del($aclId) {
688 // delete all entries from the acl cache
689 CRM_ACL_BAO_Cache
::resetCache();
691 $acl = new CRM_ACL_DAO_ACL();