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(int $contact_id) {
161 $rule = new CRM_ACL_BAO_ACL();
163 $acl = self
::getTableName();
164 $contact = CRM_Contact_BAO_Contact
::getTableName();
166 $query = " SELECT acl.*
168 WHERE acl.entity_table = '$contact'
169 AND acl.entity_id = $contact_id";
171 $rule->query($query);
173 while ($rule->fetch()) {
174 $results[$rule->id
] = $rule->toArray();
177 $results +
= self
::getACLRoles($contact_id);
183 * Get all of the ACLs through ACL groups.
185 * @param int $contact_id
186 * ID of a contact to search for.
189 * Array of assoc. arrays of ACL rules
191 * @throws \CRM_Core_Exception
193 protected static function getACLRoles($contact_id = NULL) {
194 $contact_id = CRM_Utils_Type
::escape($contact_id, 'Integer');
196 $rule = new CRM_ACL_BAO_ACL();
198 $contact = CRM_Contact_BAO_Contact
::getTableName();
200 $query = 'SELECT acl.* FROM civicrm_acl acl';
201 $where = ['acl.entity_table = "civicrm_acl_role" AND acl.entity_id IN (' . implode(',', array_keys(CRM_Core_OptionGroup
::values('acl_role'))) . ')'];
203 if (!empty($contact_id)) {
204 $where[] = " acl.entity_table = '$contact' AND acl.is_active = 1 AND acl.entity_id = $contact_id";
209 $rule->query($query . ' WHERE ' . implode(' AND ', $where));
211 while ($rule->fetch()) {
212 $results[$rule->id
] = $rule->toArray();
219 * Get all ACLs granted to a contact through all group memberships.
221 * @param int $contact_id
223 * @param bool $aclRoles
224 * Include ACL Roles?.
227 * Assoc array of ACL rules
228 * @throws \CRM_Core_Exception
230 protected static function getGroupACLs($contact_id, $aclRoles = FALSE) {
231 $contact_id = CRM_Utils_Type
::escape($contact_id, 'Integer');
233 $rule = new CRM_ACL_BAO_ACL();
235 $acl = self
::getTableName();
236 $c2g = CRM_Contact_BAO_GroupContact
::getTableName();
237 $group = CRM_Contact_BAO_Group
::getTableName();
244 INNER JOIN $c2g group_contact
245 ON acl.entity_id = group_contact.group_id
246 WHERE acl.entity_table = '$group'
247 AND group_contact.contact_id = $contact_id
248 AND group_contact.status = 'Added'";
250 $rule->query($query);
252 while ($rule->fetch()) {
253 $results[$rule->id
] = $rule->toArray();
258 $results +
= self
::getGroupACLRoles($contact_id);
265 * Get all of the ACLs for a contact through ACL groups owned by Contact.
268 * @param int $contact_id
269 * ID of a contact to search for.
272 * Array of assoc. arrays of ACL rules
273 * @throws \CRM_Core_Exception
275 protected static function getGroupACLRoles($contact_id) {
276 $contact_id = CRM_Utils_Type
::escape($contact_id, 'Integer');
278 $rule = new CRM_ACL_BAO_ACL();
280 $acl = self
::getTableName();
281 $aclRole = 'civicrm_acl_role';
283 $aclER = CRM_ACL_DAO_EntityRole
::getTableName();
284 $c2g = CRM_Contact_BAO_GroupContact
::getTableName();
286 $query = " SELECT acl.*
288 INNER JOIN civicrm_option_group og
289 ON og.name = 'acl_role'
290 INNER JOIN civicrm_option_value ov
291 ON acl.entity_table = '$aclRole'
292 AND ov.option_group_id = og.id
293 AND acl.entity_id = ov.value
296 ON $aclER.acl_role_id = acl.entity_id
297 AND $aclER.is_active = 1
299 ON $aclER.entity_id = $c2g.group_id
300 AND $aclER.entity_table = 'civicrm_group'
301 WHERE acl.entity_table = '$aclRole'
302 AND acl.is_active = 1
303 AND $c2g.contact_id = $contact_id
304 AND $c2g.status = 'Added'";
308 $rule->query($query);
310 while ($rule->fetch()) {
311 $results[$rule->id
] = $rule->toArray();
314 // also get all acls for "Any Role" case
315 // and authenticated User Role if present
317 $session = CRM_Core_Session
::singleton();
318 if ($session->get('ufID') > 0) {
325 WHERE acl.entity_id IN ( $roles )
326 AND acl.entity_table = 'civicrm_acl_role'
329 $rule->query($query);
330 while ($rule->fetch()) {
331 $results[$rule->id
] = $rule->toArray();
338 * Get all ACLs owned by a given contact, including domain and group-level.
340 * @param int $contact_id
344 * Assoc array of ACL rules
346 * @throws \CRM_Core_Exception
348 public static function getAllByContact($contact_id) {
351 /* First, the contact-specific ACLs, including ACL Roles */
353 $result +
= self
::getACLs((int) $contact_id);
356 /* Then, all ACLs granted through group membership */
357 $result +
= self
::getGroupACLs($contact_id, TRUE);
363 * @param array $params
365 * @return CRM_ACL_DAO_ACL
367 public static function create($params) {
368 $dao = new CRM_ACL_DAO_ACL();
369 $dao->copyValues($params);
375 * @param array $params
376 * @param array $defaults
378 public static function retrieve(&$params, &$defaults) {
379 CRM_Core_DAO
::commonRetrieve('CRM_ACL_DAO_ACL', $params, $defaults);
383 * Update the is_active flag in the db.
386 * Id of the database record.
387 * @param bool $is_active
388 * Value we want to set the is_active field.
391 * true if we found and updated the object, else false
393 public static function setIsActive($id, $is_active) {
394 Civi
::cache('fields')->flush();
395 // reset ACL and system caches.
396 CRM_Core_BAO_Cache
::resetCaches();
398 return CRM_Core_DAO
::setFieldValue('CRM_ACL_DAO_ACL', $id, 'is_active', $is_active);
403 * @param int $contactID
407 public static function check($str, $contactID) {
409 $acls = CRM_ACL_BAO_Cache
::build($contactID);
411 $aclKeys = array_keys($acls);
412 $aclKeys = implode(',', $aclKeys);
414 if (empty($aclKeys)) {
420 FROM civicrm_acl_cache c, civicrm_acl a
421 WHERE c.acl_id = a.id
423 AND a.object_table = %1
424 AND a.id IN ( $aclKeys )
426 $params = [1 => [$str, 'String']];
428 $count = CRM_Core_DAO
::singleValueQuery($query, $params);
429 return ($count) ?
TRUE : FALSE;
435 * @param $whereTables
436 * @param int $contactID
438 * @return null|string
440 public static function whereClause($type, &$tables, &$whereTables, $contactID = NULL) {
441 $acls = CRM_ACL_BAO_Cache
::build($contactID);
447 $aclKeys = array_keys($acls);
448 $aclKeys = implode(',', $aclKeys);
451 SELECT a.operation, a.object_id
452 FROM civicrm_acl_cache c, civicrm_acl a
453 WHERE c.acl_id = a.id
455 AND a.object_table = 'civicrm_saved_search'
456 AND a.id IN ( $aclKeys )
460 $dao = CRM_Core_DAO
::executeQuery($query);
462 // do an or of all the where clauses u see
464 while ($dao->fetch()) {
465 // make sure operation matches the type TODO
466 if (self
::matchType($type, $dao->operation
)) {
467 if (!$dao->object_id
) {
469 $whereClause = ' ( 1 ) ';
472 $ids[] = $dao->object_id
;
477 $ids = implode(',', $ids);
481 WHERE g.id IN ( $ids )
484 $dao = CRM_Core_DAO
::executeQuery($query);
486 $groupContactCacheClause = FALSE;
487 while ($dao->fetch()) {
488 $groupIDs[] = $dao->id
;
490 if (($dao->saved_search_id ||
$dao->children ||
$dao->parents
)) {
491 if ($dao->cache_date
== NULL) {
492 CRM_Contact_BAO_GroupContactCache
::load($dao);
494 $groupContactCacheClause = " UNION SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id IN (" . implode(', ', $groupIDs) . ")";
502 SELECT contact_id FROM civicrm_group_contact WHERE group_id IN (" . implode(', ', $groupIDs) . ") AND status = 'Added'
503 $groupContactCacheClause
510 if (!empty($clauses)) {
511 $whereClause = ' ( ' . implode(' OR ', $clauses) . ' ) ';
514 // call the hook to get additional whereClauses
515 CRM_Utils_Hook
::aclWhereClause($type, $tables, $whereTables, $contactID, $whereClause);
517 if (empty($whereClause)) {
518 $whereClause = ' ( 0 ) ';
526 * @param int $contactID
527 * @param string $tableName
528 * @param null $allGroups
529 * @param null $includedGroups
533 public static function group(
536 $tableName = 'civicrm_saved_search',
538 $includedGroups = NULL
540 $userCacheKey = "{$contactID}_{$type}_{$tableName}_" . CRM_Core_Config
::domainID() . '_' . md5(implode(',', array_merge((array) $allGroups, (array) $includedGroups)));
541 if (empty(Civi
::$statics[__CLASS__
]['permissioned_groups'])) {
542 Civi
::$statics[__CLASS__
]['permissioned_groups'] = [];
544 if (!empty(Civi
::$statics[__CLASS__
]['permissioned_groups'][$userCacheKey])) {
545 return Civi
::$statics[__CLASS__
]['permissioned_groups'][$userCacheKey];
548 if ($allGroups == NULL) {
549 $allGroups = CRM_Contact_BAO_Contact
::buildOptions('group_id', NULL, ['onlyActive' => FALSE]);
552 $acls = CRM_ACL_BAO_Cache
::build($contactID);
556 $aclKeys = array_keys($acls);
557 $aclKeys = implode(',', $aclKeys);
559 $cacheKey = CRM_Utils_Cache
::cleanKey("$tableName-$aclKeys");
560 $cache = CRM_Utils_Cache
::singleton();
561 $ids = $cache->get($cacheKey);
565 SELECT a.operation, a.object_id
566 FROM civicrm_acl_cache c, civicrm_acl a
567 WHERE c.acl_id = a.id
569 AND a.object_table = %1
570 AND a.id IN ( $aclKeys )
571 GROUP BY a.operation,a.object_id
574 $params = [1 => [$tableName, 'String']];
575 $dao = CRM_Core_DAO
::executeQuery($query, $params);
576 while ($dao->fetch()) {
577 if ($dao->object_id
) {
578 if (self
::matchType($type, $dao->operation
)) {
579 $ids[] = $dao->object_id
;
583 // this user has got the permission for all objects of this type
584 // check if the type matches
585 if (self
::matchType($type, $dao->operation
)) {
586 foreach ($allGroups as $id => $dontCare) {
593 $cache->set($cacheKey, $ids);
597 if (empty($ids) && !empty($includedGroups) &&
598 is_array($includedGroups)
600 $ids = $includedGroups;
604 if (!empty($allGroups)) {
605 $groupWhere = " AND id IN (" . implode(',', array_keys($allGroups)) . ")";
607 // Contacts create hidden groups from search results. They should be able to retrieve their own.
608 $ownHiddenGroupsList = CRM_Core_DAO
::singleValueQuery("
609 SELECT GROUP_CONCAT(id) FROM civicrm_group WHERE is_hidden =1 AND created_id = $contactID
612 if ($ownHiddenGroupsList) {
613 $ownHiddenGroups = explode(',', $ownHiddenGroupsList);
614 $ids = array_merge((array) $ids, $ownHiddenGroups);
619 CRM_Utils_Hook
::aclGroup($type, $contactID, $tableName, $allGroups, $ids);
620 Civi
::$statics[__CLASS__
]['permissioned_groups'][$userCacheKey] = $ids;
630 protected static function matchType($type, $operation) {
632 switch ($operation) {
638 if ($type == CRM_ACL_API
::VIEW
) {
644 if ($type == CRM_ACL_API
::VIEW ||
$type == CRM_ACL_API
::EDIT
) {
650 if ($type == CRM_ACL_API
::CREATE
) {
656 if ($type == CRM_ACL_API
::DELETE
) {
662 if ($type == CRM_ACL_API
::SEARCH
) {
671 * Delete ACL records.
674 * ID of the ACL record to be deleted.
677 public static function del($aclId) {
678 // delete all entries from the acl cache
679 CRM_ACL_BAO_Cache
::resetCache();
681 $acl = new CRM_ACL_DAO_ACL();