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 * Available operations for pseudoconstant.
36 public static function operation() {
37 if (!self
::$_operation) {
41 'Create' => ts('Create'),
42 'Delete' => ts('Delete'),
43 'Search' => ts('Search'),
47 return self
::$_operation;
51 * Construct an associative array of an ACL rule's properties
53 * @param string $format
54 * Sprintf format for array.
55 * @param bool $hideEmpty
56 * Only return elements that have a value set.
59 * Assoc. array of the ACL rule's properties
61 public function toArray($format = '%s', $hideEmpty = FALSE) {
64 if (!self
::$_fieldKeys) {
65 $fields = CRM_ACL_DAO_ACL
::fields();
66 self
::$_fieldKeys = array_keys($fields);
69 foreach (self
::$_fieldKeys as $field) {
70 $result[$field] = $this->$field;
76 * Retrieve ACLs for a contact or group. Note that including a contact id
77 * without a group id will return those ACL rules which are granted
78 * directly to the contact, but not those granted to the contact through
79 * any/all of his group memberships.
81 * @param int $contact_id
82 * ID of a contact to search for.
85 * Array of assoc. arrays of ACL rules
87 * @throws \CRM_Core_Exception
89 protected static function getACLs(int $contact_id) {
91 $query = " SELECT acl.*
93 WHERE acl.entity_table = 'civicrm_contact'
94 AND acl.entity_id = $contact_id";
96 $rule = CRM_Core_DAO
::executeQuery($query);
98 while ($rule->fetch()) {
99 $results[$rule->id
] = $rule->toArray();
102 $results +
= self
::getACLRoles($contact_id);
108 * Get all of the ACLs through ACL groups.
110 * @param int $contact_id
111 * ID of a contact to search for.
114 * Array of assoc. arrays of ACL rules
116 * @throws \CRM_Core_Exception
118 protected static function getACLRoles($contact_id = NULL) {
119 $contact_id = CRM_Utils_Type
::escape($contact_id, 'Integer');
121 $query = 'SELECT acl.* FROM civicrm_acl acl';
122 $where = ['acl.entity_table = "civicrm_acl_role" AND acl.entity_id IN (' . implode(',', array_keys(CRM_Core_OptionGroup
::values('acl_role'))) . ')'];
124 if (!empty($contact_id)) {
130 $rule = CRM_Core_DAO
::executeQuery($query . ' WHERE ' . implode(' AND ', $where));
132 while ($rule->fetch()) {
133 $results[$rule->id
] = $rule->toArray();
140 * Get all ACLs granted to a contact through all group memberships.
142 * @param int $contact_id
144 * @param bool $aclRoles
145 * Include ACL Roles?.
148 * Assoc array of ACL rules
149 * @throws \CRM_Core_Exception
151 protected static function getGroupACLs($contact_id, $aclRoles = FALSE) {
152 $contact_id = CRM_Utils_Type
::escape($contact_id, 'Integer');
160 INNER JOIN civicrm_group_contact group_contact
161 ON acl.entity_id = group_contact.group_id
162 WHERE acl.entity_table = 'civicrm_group'
163 AND group_contact.contact_id = $contact_id
164 AND group_contact.status = 'Added'";
166 $rule = CRM_Core_DAO
::executeQuery($query);
168 while ($rule->fetch()) {
169 $results[$rule->id
] = $rule->toArray();
174 $results +
= self
::getGroupACLRoles($contact_id);
181 * Get all of the ACLs for a contact through ACL groups owned by Contact.
184 * @param int $contact_id
185 * ID of a contact to search for.
188 * Array of assoc. arrays of ACL rules
189 * @throws \CRM_Core_Exception
191 protected static function getGroupACLRoles($contact_id) {
192 $contact_id = CRM_Utils_Type
::escape($contact_id, 'Integer');
194 $query = " SELECT acl.*
196 INNER JOIN civicrm_option_group og
197 ON og.name = 'acl_role'
198 INNER JOIN civicrm_option_value ov
199 ON acl.entity_table = 'civicrm_acl_role'
200 AND ov.option_group_id = og.id
201 AND acl.entity_id = ov.value
203 INNER JOIN civicrm_acl_entity_role acl_entity_role
204 ON acl_entity_role.acl_role_id = acl.entity_id
205 AND acl_entity_role.is_active = 1
206 INNER JOIN civicrm_group_contact group_contact
207 ON acl_entity_role.entity_id = group_contact.group_id
208 AND acl_entity_role.entity_table = 'civicrm_group'
209 WHERE acl.entity_table = 'civicrm_acl_role'
210 AND acl.is_active = 1
211 AND group_contact.contact_id = $contact_id
212 AND group_contact.status = 'Added'";
216 $rule = CRM_Core_DAO
::executeQuery($query);
218 while ($rule->fetch()) {
219 $results[$rule->id
] = $rule->toArray();
222 // also get all acls for "Any Role" case
223 // and authenticated User Role if present
225 $session = CRM_Core_Session
::singleton();
226 if ($session->get('ufID') > 0) {
233 WHERE acl.entity_id IN ( $roles )
234 AND acl.entity_table = 'civicrm_acl_role'
237 $rule = CRM_Core_DAO
::executeQuery($query);
238 while ($rule->fetch()) {
239 $results[$rule->id
] = $rule->toArray();
246 * Get all ACLs owned by a given contact, including domain and group-level.
248 * @param int $contact_id
252 * Assoc array of ACL rules
254 * @throws \CRM_Core_Exception
256 public static function getAllByContact($contact_id) {
259 /* First, the contact-specific ACLs, including ACL Roles */
261 $result +
= self
::getACLs((int) $contact_id);
264 /* Then, all ACLs granted through group membership */
265 $result +
= self
::getGroupACLs($contact_id, TRUE);
271 * @param array $params
273 * @return CRM_ACL_DAO_ACL
275 public static function create($params) {
276 $dao = new CRM_ACL_DAO_ACL();
277 $dao->copyValues($params);
283 * @param array $params
284 * @param array $defaults
286 public static function retrieve(&$params, &$defaults) {
287 CRM_Core_DAO
::commonRetrieve('CRM_ACL_DAO_ACL', $params, $defaults);
291 * Update the is_active flag in the db.
294 * Id of the database record.
295 * @param bool $is_active
296 * Value we want to set the is_active field.
299 * true if we found and updated the object, else false
301 public static function setIsActive($id, $is_active) {
302 Civi
::cache('fields')->flush();
303 // reset ACL and system caches.
304 CRM_Core_BAO_Cache
::resetCaches();
306 return CRM_Core_DAO
::setFieldValue('CRM_ACL_DAO_ACL', $id, 'is_active', $is_active);
311 * @param int $contactID
315 public static function check($str, $contactID) {
317 $acls = CRM_ACL_BAO_Cache
::build($contactID);
319 $aclKeys = array_keys($acls);
320 $aclKeys = implode(',', $aclKeys);
322 if (empty($aclKeys)) {
328 FROM civicrm_acl_cache c, civicrm_acl a
329 WHERE c.acl_id = a.id
331 AND a.object_table = %1
332 AND a.id IN ( $aclKeys )
334 $params = [1 => [$str, 'String']];
336 $count = CRM_Core_DAO
::singleValueQuery($query, $params);
337 return (bool) $count;
343 * @param $whereTables
344 * @param int $contactID
346 * @return null|string
348 public static function whereClause($type, &$tables, &$whereTables, $contactID = NULL) {
349 $acls = CRM_ACL_BAO_Cache
::build($contactID);
355 $aclKeys = array_keys($acls);
356 $aclKeys = implode(',', $aclKeys);
359 SELECT a.operation, a.object_id
360 FROM civicrm_acl_cache c, civicrm_acl a
361 WHERE c.acl_id = a.id
363 AND a.object_table = 'civicrm_saved_search'
364 AND a.id IN ( $aclKeys )
368 $dao = CRM_Core_DAO
::executeQuery($query);
370 // do an or of all the where clauses u see
372 while ($dao->fetch()) {
373 // make sure operation matches the type TODO
374 if (self
::matchType($type, $dao->operation
)) {
375 if (!$dao->object_id
) {
377 $whereClause = ' ( 1 ) ';
380 $ids[] = $dao->object_id
;
385 $ids = implode(',', $ids);
389 WHERE g.id IN ( $ids )
392 $dao = CRM_Core_DAO
::executeQuery($query);
394 $groupContactCacheClause = FALSE;
395 while ($dao->fetch()) {
396 $groupIDs[] = $dao->id
;
398 if (($dao->saved_search_id ||
$dao->children ||
$dao->parents
)) {
399 if ($dao->cache_date
== NULL) {
400 CRM_Contact_BAO_GroupContactCache
::load($dao);
402 $groupContactCacheClause = " UNION SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id IN (" . implode(', ', $groupIDs) . ")";
410 SELECT contact_id FROM civicrm_group_contact WHERE group_id IN (" . implode(', ', $groupIDs) . ") AND status = 'Added'
411 $groupContactCacheClause
418 if (!empty($clauses)) {
419 $whereClause = ' ( ' . implode(' OR ', $clauses) . ' ) ';
422 // call the hook to get additional whereClauses
423 CRM_Utils_Hook
::aclWhereClause($type, $tables, $whereTables, $contactID, $whereClause);
425 if (empty($whereClause)) {
426 $whereClause = ' ( 0 ) ';
434 * @param int $contactID
435 * @param string $tableName
436 * @param null $allGroups
437 * @param null $includedGroups
441 public static function group(
444 $tableName = 'civicrm_saved_search',
446 $includedGroups = NULL
448 $userCacheKey = "{$contactID}_{$type}_{$tableName}_" . CRM_Core_Config
::domainID() . '_' . md5(implode(',', array_merge((array) $allGroups, (array) $includedGroups)));
449 if (empty(Civi
::$statics[__CLASS__
]['permissioned_groups'])) {
450 Civi
::$statics[__CLASS__
]['permissioned_groups'] = [];
452 if (!empty(Civi
::$statics[__CLASS__
]['permissioned_groups'][$userCacheKey])) {
453 return Civi
::$statics[__CLASS__
]['permissioned_groups'][$userCacheKey];
456 if ($allGroups == NULL) {
457 $allGroups = CRM_Contact_BAO_Contact
::buildOptions('group_id', 'get');
460 $acls = CRM_ACL_BAO_Cache
::build($contactID);
464 $aclKeys = array_keys($acls);
465 $aclKeys = implode(',', $aclKeys);
467 $cacheKey = CRM_Utils_Cache
::cleanKey("$type-$tableName-$aclKeys");
468 $cache = CRM_Utils_Cache
::singleton();
469 $ids = $cache->get($cacheKey);
470 if (!is_array($ids)) {
473 SELECT a.operation, a.object_id
474 FROM civicrm_acl_cache c, civicrm_acl a
475 WHERE c.acl_id = a.id
477 AND a.object_table = %1
478 AND a.id IN ( $aclKeys )
479 GROUP BY a.operation,a.object_id
482 $params = [1 => [$tableName, 'String']];
483 $dao = CRM_Core_DAO
::executeQuery($query, $params);
484 while ($dao->fetch()) {
485 if ($dao->object_id
) {
486 if (self
::matchType($type, $dao->operation
)) {
487 $ids[] = $dao->object_id
;
491 // this user has got the permission for all objects of this type
492 // check if the type matches
493 if (self
::matchType($type, $dao->operation
)) {
494 foreach ($allGroups as $id => $dontCare) {
501 $cache->set($cacheKey, $ids);
505 if (empty($ids) && !empty($includedGroups) &&
506 is_array($includedGroups)
508 // This is pretty alarming - we 'sometimes' include all included groups
509 // seems problematic per https://lab.civicrm.org/dev/core/-/issues/1879
510 $ids = $includedGroups;
514 if (!empty($allGroups)) {
515 $groupWhere = " AND id IN (" . implode(',', array_keys($allGroups)) . ")";
517 // Contacts create hidden groups from search results. They should be able to retrieve their own.
518 $ownHiddenGroupsList = CRM_Core_DAO
::singleValueQuery("
519 SELECT GROUP_CONCAT(id) FROM civicrm_group WHERE is_hidden =1 AND created_id = $contactID
522 if ($ownHiddenGroupsList) {
523 $ownHiddenGroups = explode(',', $ownHiddenGroupsList);
524 $ids = array_merge((array) $ids, $ownHiddenGroups);
529 CRM_Utils_Hook
::aclGroup($type, $contactID, $tableName, $allGroups, $ids);
530 Civi
::$statics[__CLASS__
]['permissioned_groups'][$userCacheKey] = $ids;
540 protected static function matchType($type, $operation) {
542 switch ($operation) {
548 if ($type == CRM_ACL_API
::VIEW
) {
554 if ($type == CRM_ACL_API
::VIEW ||
$type == CRM_ACL_API
::EDIT
) {
560 if ($type == CRM_ACL_API
::CREATE
) {
566 if ($type == CRM_ACL_API
::DELETE
) {
572 if ($type == CRM_ACL_API
::SEARCH
) {
581 * Delete ACL records.
584 * ID of the ACL record to be deleted.
587 public static function del($aclId) {
588 // delete all entries from the acl cache
589 CRM_ACL_BAO_Cache
::resetCache();
591 $acl = new CRM_ACL_DAO_ACL();