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 ACLs granted to a contact through all group memberships.
78 * @param int $contact_id
80 * @param bool $aclRoles
84 * Assoc array of ACL rules
85 * @throws \CRM_Core_Exception
87 protected static function getGroupACLs($contact_id, $aclRoles = FALSE) {
88 $contact_id = CRM_Utils_Type
::escape($contact_id, 'Integer');
96 INNER JOIN civicrm_group_contact group_contact
97 ON acl.entity_id = group_contact.group_id
98 WHERE acl.entity_table = 'civicrm_group'
99 AND group_contact.contact_id = $contact_id
100 AND group_contact.status = 'Added'";
102 $rule = CRM_Core_DAO
::executeQuery($query);
104 while ($rule->fetch()) {
105 $results[$rule->id
] = $rule->toArray();
110 $results +
= self
::getGroupACLRoles($contact_id);
117 * Get all of the ACLs for a contact through ACL groups owned by Contact.
120 * @param int $contact_id
121 * ID of a contact to search for.
124 * Array of assoc. arrays of ACL rules
125 * @throws \CRM_Core_Exception
127 protected static function getGroupACLRoles($contact_id) {
128 $contact_id = CRM_Utils_Type
::escape($contact_id, 'Integer');
130 $query = " SELECT acl.*
132 INNER JOIN civicrm_option_group og
133 ON og.name = 'acl_role'
134 INNER JOIN civicrm_option_value ov
135 ON acl.entity_table = 'civicrm_acl_role'
136 AND ov.option_group_id = og.id
137 AND acl.entity_id = ov.value
139 INNER JOIN civicrm_acl_entity_role acl_entity_role
140 ON acl_entity_role.acl_role_id = acl.entity_id
141 AND acl_entity_role.is_active = 1
142 INNER JOIN civicrm_group_contact group_contact
143 ON acl_entity_role.entity_id = group_contact.group_id
144 AND acl_entity_role.entity_table = 'civicrm_group'
145 WHERE acl.entity_table = 'civicrm_acl_role'
146 AND acl.is_active = 1
147 AND group_contact.contact_id = $contact_id
148 AND group_contact.status = 'Added'";
152 $rule = CRM_Core_DAO
::executeQuery($query);
154 while ($rule->fetch()) {
155 $results[$rule->id
] = $rule->toArray();
158 // also get all acls for "Any Role" case
159 // and authenticated User Role if present
161 $session = CRM_Core_Session
::singleton();
162 if ($session->get('ufID') > 0) {
169 WHERE acl.entity_id IN ( $roles )
170 AND acl.entity_table = 'civicrm_acl_role'
173 $rule = CRM_Core_DAO
::executeQuery($query);
174 while ($rule->fetch()) {
175 $results[$rule->id
] = $rule->toArray();
182 * Get all ACLs owned by a given contact, including domain and group-level.
184 * @param int $contact_id
188 * Assoc array of ACL rules
190 * @throws \CRM_Core_Exception
192 public static function getAllByContact($contact_id) {
195 /* First, the contact-specific ACLs, including ACL Roles */
197 $query = " SELECT acl.*
199 WHERE acl.entity_table = 'civicrm_contact'
200 AND acl.entity_id = $contact_id";
202 $rule = CRM_Core_DAO
::executeQuery($query);
204 while ($rule->fetch()) {
205 $result[$rule->id
] = $rule->toArray();
209 /* Then, all ACLs granted through group membership */
210 $result +
= self
::getGroupACLs($contact_id, TRUE);
216 * @param array $params
218 * @return CRM_ACL_DAO_ACL
220 public static function create($params) {
221 $dao = new CRM_ACL_DAO_ACL();
222 $dao->copyValues($params);
228 * @param array $params
229 * @param array $defaults
231 public static function retrieve(&$params, &$defaults) {
232 CRM_Core_DAO
::commonRetrieve('CRM_ACL_DAO_ACL', $params, $defaults);
236 * Update the is_active flag in the db.
239 * Id of the database record.
240 * @param bool $is_active
241 * Value we want to set the is_active field.
244 * true if we found and updated the object, else false
246 public static function setIsActive($id, $is_active) {
247 Civi
::cache('fields')->flush();
248 // reset ACL and system caches.
249 CRM_Core_BAO_Cache
::resetCaches();
251 return CRM_Core_DAO
::setFieldValue('CRM_ACL_DAO_ACL', $id, 'is_active', $is_active);
256 * @param int $contactID
260 public static function check($str, $contactID) {
262 $acls = CRM_ACL_BAO_Cache
::build($contactID);
264 $aclKeys = array_keys($acls);
265 $aclKeys = implode(',', $aclKeys);
267 if (empty($aclKeys)) {
273 FROM civicrm_acl_cache c, civicrm_acl a
274 WHERE c.acl_id = a.id
276 AND a.object_table = %1
277 AND a.id IN ( $aclKeys )
279 $params = [1 => [$str, 'String']];
281 $count = CRM_Core_DAO
::singleValueQuery($query, $params);
282 return (bool) $count;
288 * @param $whereTables
289 * @param int $contactID
291 * @return null|string
293 public static function whereClause($type, &$tables, &$whereTables, $contactID = NULL) {
294 $acls = CRM_ACL_BAO_Cache
::build($contactID);
300 $aclKeys = array_keys($acls);
301 $aclKeys = implode(',', $aclKeys);
304 SELECT a.operation, a.object_id
305 FROM civicrm_acl_cache c, civicrm_acl a
306 WHERE c.acl_id = a.id
308 AND a.object_table = 'civicrm_saved_search'
309 AND a.id IN ( $aclKeys )
313 $dao = CRM_Core_DAO
::executeQuery($query);
315 // do an or of all the where clauses u see
317 while ($dao->fetch()) {
318 // make sure operation matches the type TODO
319 if (self
::matchType($type, $dao->operation
)) {
320 if (!$dao->object_id
) {
322 $whereClause = ' ( 1 ) ';
325 $ids[] = $dao->object_id
;
330 $ids = implode(',', $ids);
334 WHERE g.id IN ( $ids )
337 $dao = CRM_Core_DAO
::executeQuery($query);
339 $groupContactCacheClause = FALSE;
340 while ($dao->fetch()) {
341 $groupIDs[] = $dao->id
;
343 if (($dao->saved_search_id ||
$dao->children ||
$dao->parents
)) {
344 if ($dao->cache_date
== NULL) {
345 CRM_Contact_BAO_GroupContactCache
::load($dao);
347 $groupContactCacheClause = " UNION SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id IN (" . implode(', ', $groupIDs) . ")";
355 SELECT contact_id FROM civicrm_group_contact WHERE group_id IN (" . implode(', ', $groupIDs) . ") AND status = 'Added'
356 $groupContactCacheClause
363 if (!empty($clauses)) {
364 $whereClause = ' ( ' . implode(' OR ', $clauses) . ' ) ';
367 // call the hook to get additional whereClauses
368 CRM_Utils_Hook
::aclWhereClause($type, $tables, $whereTables, $contactID, $whereClause);
370 if (empty($whereClause)) {
371 $whereClause = ' ( 0 ) ';
379 * @param int $contactID
380 * @param string $tableName
381 * @param null $allGroups
382 * @param null $includedGroups
386 public static function group(
389 $tableName = 'civicrm_saved_search',
391 $includedGroups = NULL
393 $userCacheKey = "{$contactID}_{$type}_{$tableName}_" . CRM_Core_Config
::domainID() . '_' . md5(implode(',', array_merge((array) $allGroups, (array) $includedGroups)));
394 if (empty(Civi
::$statics[__CLASS__
]['permissioned_groups'])) {
395 Civi
::$statics[__CLASS__
]['permissioned_groups'] = [];
397 if (!empty(Civi
::$statics[__CLASS__
]['permissioned_groups'][$userCacheKey])) {
398 return Civi
::$statics[__CLASS__
]['permissioned_groups'][$userCacheKey];
401 if ($allGroups == NULL) {
402 $allGroups = CRM_Contact_BAO_Contact
::buildOptions('group_id', 'get');
405 $acls = CRM_ACL_BAO_Cache
::build($contactID);
409 $aclKeys = array_keys($acls);
410 $aclKeys = implode(',', $aclKeys);
412 $cacheKey = CRM_Utils_Cache
::cleanKey("$type-$tableName-$aclKeys");
413 $cache = CRM_Utils_Cache
::singleton();
414 $ids = $cache->get($cacheKey);
415 if (!is_array($ids)) {
418 SELECT a.operation, a.object_id
419 FROM civicrm_acl_cache c, civicrm_acl a
420 WHERE c.acl_id = a.id
422 AND a.object_table = %1
423 AND a.id IN ( $aclKeys )
424 GROUP BY a.operation,a.object_id
427 $params = [1 => [$tableName, 'String']];
428 $dao = CRM_Core_DAO
::executeQuery($query, $params);
429 while ($dao->fetch()) {
430 if ($dao->object_id
) {
431 if (self
::matchType($type, $dao->operation
)) {
432 $ids[] = $dao->object_id
;
436 // this user has got the permission for all objects of this type
437 // check if the type matches
438 if (self
::matchType($type, $dao->operation
)) {
439 foreach ($allGroups as $id => $dontCare) {
446 $cache->set($cacheKey, $ids);
450 if (empty($ids) && !empty($includedGroups) &&
451 is_array($includedGroups)
453 // This is pretty alarming - we 'sometimes' include all included groups
454 // seems problematic per https://lab.civicrm.org/dev/core/-/issues/1879
455 $ids = $includedGroups;
459 if (!empty($allGroups)) {
460 $groupWhere = " AND id IN (" . implode(',', array_keys($allGroups)) . ")";
462 // Contacts create hidden groups from search results. They should be able to retrieve their own.
463 $ownHiddenGroupsList = CRM_Core_DAO
::singleValueQuery("
464 SELECT GROUP_CONCAT(id) FROM civicrm_group WHERE is_hidden =1 AND created_id = $contactID
467 if ($ownHiddenGroupsList) {
468 $ownHiddenGroups = explode(',', $ownHiddenGroupsList);
469 $ids = array_merge((array) $ids, $ownHiddenGroups);
474 CRM_Utils_Hook
::aclGroup($type, $contactID, $tableName, $allGroups, $ids);
475 Civi
::$statics[__CLASS__
]['permissioned_groups'][$userCacheKey] = $ids;
485 protected static function matchType($type, $operation) {
487 switch ($operation) {
493 if ($type == CRM_ACL_API
::VIEW
) {
499 if ($type == CRM_ACL_API
::VIEW ||
$type == CRM_ACL_API
::EDIT
) {
505 if ($type == CRM_ACL_API
::CREATE
) {
511 if ($type == CRM_ACL_API
::DELETE
) {
517 if ($type == CRM_ACL_API
::SEARCH
) {
526 * Delete ACL records.
529 * ID of the ACL record to be deleted.
532 public static function del($aclId) {
533 // delete all entries from the acl cache
534 CRM_ACL_BAO_Cache
::resetCache();
536 $acl = new CRM_ACL_DAO_ACL();