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 * Get all of the ACLs through ACL groups.
78 * @param int $contact_id
79 * ID of a contact to search for.
82 * Array of assoc. arrays of ACL rules
84 * @throws \CRM_Core_Exception
86 protected static function getACLRoles($contact_id = NULL) {
87 $contact_id = CRM_Utils_Type
::escape($contact_id, 'Integer');
89 $query = 'SELECT acl.* FROM civicrm_acl acl';
90 $where = ['acl.entity_table = "civicrm_acl_role" AND acl.entity_id IN (' . implode(',', array_keys(CRM_Core_OptionGroup
::values('acl_role'))) . ')'];
92 if (!empty($contact_id)) {
98 $rule = CRM_Core_DAO
::executeQuery($query . ' WHERE ' . implode(' AND ', $where));
100 while ($rule->fetch()) {
101 $results[$rule->id
] = $rule->toArray();
108 * Get all ACLs granted to a contact through all group memberships.
110 * @param int $contact_id
112 * @param bool $aclRoles
113 * Include ACL Roles?.
116 * Assoc array of ACL rules
117 * @throws \CRM_Core_Exception
119 protected static function getGroupACLs($contact_id, $aclRoles = FALSE) {
120 $contact_id = CRM_Utils_Type
::escape($contact_id, 'Integer');
128 INNER JOIN civicrm_group_contact group_contact
129 ON acl.entity_id = group_contact.group_id
130 WHERE acl.entity_table = 'civicrm_group'
131 AND group_contact.contact_id = $contact_id
132 AND group_contact.status = 'Added'";
134 $rule = CRM_Core_DAO
::executeQuery($query);
136 while ($rule->fetch()) {
137 $results[$rule->id
] = $rule->toArray();
142 $results +
= self
::getGroupACLRoles($contact_id);
149 * Get all of the ACLs for a contact through ACL groups owned by Contact.
152 * @param int $contact_id
153 * ID of a contact to search for.
156 * Array of assoc. arrays of ACL rules
157 * @throws \CRM_Core_Exception
159 protected static function getGroupACLRoles($contact_id) {
160 $contact_id = CRM_Utils_Type
::escape($contact_id, 'Integer');
162 $query = " SELECT acl.*
164 INNER JOIN civicrm_option_group og
165 ON og.name = 'acl_role'
166 INNER JOIN civicrm_option_value ov
167 ON acl.entity_table = 'civicrm_acl_role'
168 AND ov.option_group_id = og.id
169 AND acl.entity_id = ov.value
171 INNER JOIN civicrm_acl_entity_role acl_entity_role
172 ON acl_entity_role.acl_role_id = acl.entity_id
173 AND acl_entity_role.is_active = 1
174 INNER JOIN civicrm_group_contact group_contact
175 ON acl_entity_role.entity_id = group_contact.group_id
176 AND acl_entity_role.entity_table = 'civicrm_group'
177 WHERE acl.entity_table = 'civicrm_acl_role'
178 AND acl.is_active = 1
179 AND group_contact.contact_id = $contact_id
180 AND group_contact.status = 'Added'";
184 $rule = CRM_Core_DAO
::executeQuery($query);
186 while ($rule->fetch()) {
187 $results[$rule->id
] = $rule->toArray();
190 // also get all acls for "Any Role" case
191 // and authenticated User Role if present
193 $session = CRM_Core_Session
::singleton();
194 if ($session->get('ufID') > 0) {
201 WHERE acl.entity_id IN ( $roles )
202 AND acl.entity_table = 'civicrm_acl_role'
205 $rule = CRM_Core_DAO
::executeQuery($query);
206 while ($rule->fetch()) {
207 $results[$rule->id
] = $rule->toArray();
214 * Get all ACLs owned by a given contact, including domain and group-level.
216 * @param int $contact_id
220 * Assoc array of ACL rules
222 * @throws \CRM_Core_Exception
224 public static function getAllByContact($contact_id) {
227 /* First, the contact-specific ACLs, including ACL Roles */
229 $query = " SELECT acl.*
231 WHERE acl.entity_table = 'civicrm_contact'
232 AND acl.entity_id = $contact_id";
234 $rule = CRM_Core_DAO
::executeQuery($query);
236 while ($rule->fetch()) {
237 $result[$rule->id
] = $rule->toArray();
240 $result +
= self
::getACLRoles($contact_id);
243 /* Then, all ACLs granted through group membership */
244 $result +
= self
::getGroupACLs($contact_id, TRUE);
250 * @param array $params
252 * @return CRM_ACL_DAO_ACL
254 public static function create($params) {
255 $dao = new CRM_ACL_DAO_ACL();
256 $dao->copyValues($params);
262 * @param array $params
263 * @param array $defaults
265 public static function retrieve(&$params, &$defaults) {
266 CRM_Core_DAO
::commonRetrieve('CRM_ACL_DAO_ACL', $params, $defaults);
270 * Update the is_active flag in the db.
273 * Id of the database record.
274 * @param bool $is_active
275 * Value we want to set the is_active field.
278 * true if we found and updated the object, else false
280 public static function setIsActive($id, $is_active) {
281 Civi
::cache('fields')->flush();
282 // reset ACL and system caches.
283 CRM_Core_BAO_Cache
::resetCaches();
285 return CRM_Core_DAO
::setFieldValue('CRM_ACL_DAO_ACL', $id, 'is_active', $is_active);
290 * @param int $contactID
294 public static function check($str, $contactID) {
296 $acls = CRM_ACL_BAO_Cache
::build($contactID);
298 $aclKeys = array_keys($acls);
299 $aclKeys = implode(',', $aclKeys);
301 if (empty($aclKeys)) {
307 FROM civicrm_acl_cache c, civicrm_acl a
308 WHERE c.acl_id = a.id
310 AND a.object_table = %1
311 AND a.id IN ( $aclKeys )
313 $params = [1 => [$str, 'String']];
315 $count = CRM_Core_DAO
::singleValueQuery($query, $params);
316 return (bool) $count;
322 * @param $whereTables
323 * @param int $contactID
325 * @return null|string
327 public static function whereClause($type, &$tables, &$whereTables, $contactID = NULL) {
328 $acls = CRM_ACL_BAO_Cache
::build($contactID);
334 $aclKeys = array_keys($acls);
335 $aclKeys = implode(',', $aclKeys);
338 SELECT a.operation, a.object_id
339 FROM civicrm_acl_cache c, civicrm_acl a
340 WHERE c.acl_id = a.id
342 AND a.object_table = 'civicrm_saved_search'
343 AND a.id IN ( $aclKeys )
347 $dao = CRM_Core_DAO
::executeQuery($query);
349 // do an or of all the where clauses u see
351 while ($dao->fetch()) {
352 // make sure operation matches the type TODO
353 if (self
::matchType($type, $dao->operation
)) {
354 if (!$dao->object_id
) {
356 $whereClause = ' ( 1 ) ';
359 $ids[] = $dao->object_id
;
364 $ids = implode(',', $ids);
368 WHERE g.id IN ( $ids )
371 $dao = CRM_Core_DAO
::executeQuery($query);
373 $groupContactCacheClause = FALSE;
374 while ($dao->fetch()) {
375 $groupIDs[] = $dao->id
;
377 if (($dao->saved_search_id ||
$dao->children ||
$dao->parents
)) {
378 if ($dao->cache_date
== NULL) {
379 CRM_Contact_BAO_GroupContactCache
::load($dao);
381 $groupContactCacheClause = " UNION SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id IN (" . implode(', ', $groupIDs) . ")";
389 SELECT contact_id FROM civicrm_group_contact WHERE group_id IN (" . implode(', ', $groupIDs) . ") AND status = 'Added'
390 $groupContactCacheClause
397 if (!empty($clauses)) {
398 $whereClause = ' ( ' . implode(' OR ', $clauses) . ' ) ';
401 // call the hook to get additional whereClauses
402 CRM_Utils_Hook
::aclWhereClause($type, $tables, $whereTables, $contactID, $whereClause);
404 if (empty($whereClause)) {
405 $whereClause = ' ( 0 ) ';
413 * @param int $contactID
414 * @param string $tableName
415 * @param null $allGroups
416 * @param null $includedGroups
420 public static function group(
423 $tableName = 'civicrm_saved_search',
425 $includedGroups = NULL
427 $userCacheKey = "{$contactID}_{$type}_{$tableName}_" . CRM_Core_Config
::domainID() . '_' . md5(implode(',', array_merge((array) $allGroups, (array) $includedGroups)));
428 if (empty(Civi
::$statics[__CLASS__
]['permissioned_groups'])) {
429 Civi
::$statics[__CLASS__
]['permissioned_groups'] = [];
431 if (!empty(Civi
::$statics[__CLASS__
]['permissioned_groups'][$userCacheKey])) {
432 return Civi
::$statics[__CLASS__
]['permissioned_groups'][$userCacheKey];
435 if ($allGroups == NULL) {
436 $allGroups = CRM_Contact_BAO_Contact
::buildOptions('group_id', 'get');
439 $acls = CRM_ACL_BAO_Cache
::build($contactID);
443 $aclKeys = array_keys($acls);
444 $aclKeys = implode(',', $aclKeys);
446 $cacheKey = CRM_Utils_Cache
::cleanKey("$type-$tableName-$aclKeys");
447 $cache = CRM_Utils_Cache
::singleton();
448 $ids = $cache->get($cacheKey);
449 if (!is_array($ids)) {
452 SELECT a.operation, a.object_id
453 FROM civicrm_acl_cache c, civicrm_acl a
454 WHERE c.acl_id = a.id
456 AND a.object_table = %1
457 AND a.id IN ( $aclKeys )
458 GROUP BY a.operation,a.object_id
461 $params = [1 => [$tableName, 'String']];
462 $dao = CRM_Core_DAO
::executeQuery($query, $params);
463 while ($dao->fetch()) {
464 if ($dao->object_id
) {
465 if (self
::matchType($type, $dao->operation
)) {
466 $ids[] = $dao->object_id
;
470 // this user has got the permission for all objects of this type
471 // check if the type matches
472 if (self
::matchType($type, $dao->operation
)) {
473 foreach ($allGroups as $id => $dontCare) {
480 $cache->set($cacheKey, $ids);
484 if (empty($ids) && !empty($includedGroups) &&
485 is_array($includedGroups)
487 // This is pretty alarming - we 'sometimes' include all included groups
488 // seems problematic per https://lab.civicrm.org/dev/core/-/issues/1879
489 $ids = $includedGroups;
493 if (!empty($allGroups)) {
494 $groupWhere = " AND id IN (" . implode(',', array_keys($allGroups)) . ")";
496 // Contacts create hidden groups from search results. They should be able to retrieve their own.
497 $ownHiddenGroupsList = CRM_Core_DAO
::singleValueQuery("
498 SELECT GROUP_CONCAT(id) FROM civicrm_group WHERE is_hidden =1 AND created_id = $contactID
501 if ($ownHiddenGroupsList) {
502 $ownHiddenGroups = explode(',', $ownHiddenGroupsList);
503 $ids = array_merge((array) $ids, $ownHiddenGroups);
508 CRM_Utils_Hook
::aclGroup($type, $contactID, $tableName, $allGroups, $ids);
509 Civi
::$statics[__CLASS__
]['permissioned_groups'][$userCacheKey] = $ids;
519 protected static function matchType($type, $operation) {
521 switch ($operation) {
527 if ($type == CRM_ACL_API
::VIEW
) {
533 if ($type == CRM_ACL_API
::VIEW ||
$type == CRM_ACL_API
::EDIT
) {
539 if ($type == CRM_ACL_API
::CREATE
) {
545 if ($type == CRM_ACL_API
::DELETE
) {
551 if ($type == CRM_ACL_API
::SEARCH
) {
560 * Delete ACL records.
563 * ID of the ACL record to be deleted.
566 public static function del($aclId) {
567 // delete all entries from the acl cache
568 CRM_ACL_BAO_Cache
::resetCache();
570 $acl = new CRM_ACL_DAO_ACL();