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 if (!self
::$_entityTable) {
38 self
::$_entityTable = [
39 'civicrm_contact' => ts('Contact'),
40 'civicrm_acl_role' => ts('ACL Role'),
43 return self
::$_entityTable;
49 public static function objectTable() {
50 if (!self
::$_objectTable) {
51 self
::$_objectTable = [
52 'civicrm_contact' => ts('Contact'),
53 'civicrm_group' => ts('Group'),
54 'civicrm_saved_search' => ts('Contact Group'),
55 'civicrm_admin' => ts('Import'),
58 return self
::$_objectTable;
64 public static function operation() {
65 if (!self
::$_operation) {
69 'Create' => ts('Create'),
70 'Delete' => ts('Delete'),
71 'Search' => ts('Search'),
75 return self
::$_operation;
79 * Given a table and id pair, return the filter clause
81 * @param string $table
82 * The table owning the object.
84 * The ID of the object.
85 * @param array $tables
86 * Tables that will be needed in the FROM.
89 * WHERE-style clause to filter results,
90 * or null if $table or $id is null
92 * @throws \CRM_Core_Exception
94 public static function getClause($table, $id, &$tables) {
95 $table = CRM_Utils_Type
::escape($table, 'String');
96 $id = CRM_Utils_Type
::escape($id, 'Integer');
99 $ssTable = CRM_Contact_BAO_SavedSearch
::getTableName();
104 elseif ($table == $ssTable) {
105 return CRM_Contact_BAO_SavedSearch
::whereClause($id, $tables, $whereTables);
107 elseif (!empty($id)) {
108 $tables[$table] = TRUE;
109 return "$table.id = $id";
115 * Construct an associative array of an ACL rule's properties
117 * @param string $format
118 * Sprintf format for array.
119 * @param bool $hideEmpty
120 * Only return elements that have a value set.
123 * Assoc. array of the ACL rule's properties
125 public function toArray($format = '%s', $hideEmpty = FALSE) {
128 if (!self
::$_fieldKeys) {
129 $fields = CRM_ACL_DAO_ACL
::fields();
130 self
::$_fieldKeys = array_keys($fields);
133 foreach (self
::$_fieldKeys as $field) {
134 $result[$field] = $this->$field;
140 * Retrieve ACLs for a contact or group. Note that including a contact id
141 * without a group id will return those ACL rules which are granted
142 * directly to the contact, but not those granted to the contact through
143 * any/all of his group memberships.
145 * @param int $contact_id
146 * ID of a contact to search for.
149 * Array of assoc. arrays of ACL rules
151 * @throws \CRM_Core_Exception
153 public static function getACLs($contact_id = NULL) {
156 if (empty($contact_id)) {
160 $contact_id = CRM_Utils_Type
::escape($contact_id, 'Integer');
162 $rule = new CRM_ACL_BAO_ACL();
164 $acl = self
::getTableName();
165 $contact = CRM_Contact_BAO_Contact
::getTableName();
166 $c2g = CRM_Contact_BAO_GroupContact
::getTableName();
167 $group = CRM_Contact_BAO_Group
::getTableName();
169 $query = " SELECT acl.*
172 if (!empty($contact_id)) {
173 $query .= " WHERE acl.entity_table = '$contact'
174 AND acl.entity_id = $contact_id";
177 $rule->query($query);
179 while ($rule->fetch()) {
180 $results[$rule->id
] = $rule->toArray();
183 $results +
= self
::getACLRoles($contact_id);
189 * Get all of the ACLs through ACL groups.
191 * @param int $contact_id
192 * ID of a contact to search for.
195 * Array of assoc. arrays of ACL rules
197 * @throws \CRM_Core_Exception
199 public static function getACLRoles($contact_id = NULL) {
200 $contact_id = CRM_Utils_Type
::escape($contact_id, 'Integer');
202 $rule = new CRM_ACL_BAO_ACL();
204 $contact = CRM_Contact_BAO_Contact
::getTableName();
206 $query = 'SELECT acl.* FROM civicrm_acl acl';
207 $where = ['acl.entity_table = "civicrm_acl_role" AND acl.entity_id IN (' . implode(',', array_keys(CRM_Core_OptionGroup
::values('acl_role'))) . ')'];
209 if (!empty($contact_id)) {
210 $where[] = " acl.entity_table = '$contact' AND acl.is_active = 1 AND acl.entity_id = $contact_id";
215 $rule->query($query . ' WHERE ' . implode(' AND ', $where));
217 while ($rule->fetch()) {
218 $results[$rule->id
] = $rule->toArray();
225 * Get all ACLs granted to a contact through all group memberships.
227 * @param int $contact_id
229 * @param bool $aclRoles
230 * Include ACL Roles?.
233 * Assoc array of ACL rules
234 * @throws \CRM_Core_Exception
236 public static function getGroupACLs($contact_id, $aclRoles = FALSE) {
237 $contact_id = CRM_Utils_Type
::escape($contact_id, 'Integer');
239 $rule = new CRM_ACL_BAO_ACL();
241 $acl = self
::getTableName();
242 $c2g = CRM_Contact_BAO_GroupContact
::getTableName();
243 $group = CRM_Contact_BAO_Group
::getTableName();
250 INNER JOIN $c2g group_contact
251 ON acl.entity_id = group_contact.group_id
252 WHERE acl.entity_table = '$group'
253 AND group_contact.contact_id = $contact_id
254 AND group_contact.status = 'Added'";
256 $rule->query($query);
258 while ($rule->fetch()) {
259 $results[$rule->id
] = $rule->toArray();
264 $results +
= self
::getGroupACLRoles($contact_id);
271 * Get all of the ACLs for a contact through ACL groups owned by Contact.
274 * @param int $contact_id
275 * ID of a contact to search for.
278 * Array of assoc. arrays of ACL rules
279 * @throws \CRM_Core_Exception
281 public static function getGroupACLRoles($contact_id) {
282 $contact_id = CRM_Utils_Type
::escape($contact_id, 'Integer');
284 $rule = new CRM_ACL_BAO_ACL();
286 $acl = self
::getTableName();
287 $aclRole = 'civicrm_acl_role';
289 $aclER = CRM_ACL_DAO_EntityRole
::getTableName();
290 $c2g = CRM_Contact_BAO_GroupContact
::getTableName();
291 $group = CRM_Contact_BAO_Group
::getTableName();
293 $query = " SELECT acl.*
295 INNER JOIN civicrm_option_group og
296 ON og.name = 'acl_role'
297 INNER JOIN civicrm_option_value ov
298 ON acl.entity_table = '$aclRole'
299 AND ov.option_group_id = og.id
300 AND acl.entity_id = ov.value
303 ON $aclER.acl_role_id = acl.entity_id
304 AND $aclER.is_active = 1
306 ON $aclER.entity_id = $c2g.group_id
307 AND $aclER.entity_table = 'civicrm_group'
308 WHERE acl.entity_table = '$aclRole'
309 AND acl.is_active = 1
310 AND $c2g.contact_id = $contact_id
311 AND $c2g.status = 'Added'";
315 $rule->query($query);
317 while ($rule->fetch()) {
318 $results[$rule->id
] = $rule->toArray();
321 // also get all acls for "Any Role" case
322 // and authenticated User Role if present
324 $session = CRM_Core_Session
::singleton();
325 if ($session->get('ufID') > 0) {
332 WHERE acl.entity_id IN ( $roles )
333 AND acl.entity_table = 'civicrm_acl_role'
336 $rule->query($query);
337 while ($rule->fetch()) {
338 $results[$rule->id
] = $rule->toArray();
345 * Get all ACLs owned by a given contact, including domain and group-level.
347 * @param int $contact_id
351 * Assoc array of ACL rules
353 * @throws \CRM_Core_Exception
355 public static function getAllByContact($contact_id) {
358 /* First, the contact-specific ACLs, including ACL Roles */
359 $result +
= self
::getACLs($contact_id);
361 /* Then, all ACLs granted through group membership */
362 $result +
= self
::getGroupACLs($contact_id, TRUE);
368 * @param array $params
370 * @return CRM_ACL_DAO_ACL
372 public static function create($params) {
373 $dao = new CRM_ACL_DAO_ACL();
374 $dao->copyValues($params);
380 * @param array $params
381 * @param array $defaults
383 public static function retrieve(&$params, &$defaults) {
384 CRM_Core_DAO
::commonRetrieve('CRM_ACL_DAO_ACL', $params, $defaults);
388 * Update the is_active flag in the db.
391 * Id of the database record.
392 * @param bool $is_active
393 * Value we want to set the is_active field.
396 * true if we found and updated the object, else false
398 public static function setIsActive($id, $is_active) {
399 Civi
::cache('fields')->flush();
400 // reset ACL and system caches.
401 CRM_Core_BAO_Cache
::resetCaches();
403 return CRM_Core_DAO
::setFieldValue('CRM_ACL_DAO_ACL', $id, 'is_active', $is_active);
408 * @param int $contactID
412 public static function check($str, $contactID) {
414 $acls = CRM_ACL_BAO_Cache
::build($contactID);
416 $aclKeys = array_keys($acls);
417 $aclKeys = implode(',', $aclKeys);
419 if (empty($aclKeys)) {
425 FROM civicrm_acl_cache c, civicrm_acl a
426 WHERE c.acl_id = a.id
428 AND a.object_table = %1
429 AND a.id IN ( $aclKeys )
431 $params = [1 => [$str, 'String']];
433 $count = CRM_Core_DAO
::singleValueQuery($query, $params);
434 return ($count) ?
TRUE : FALSE;
440 * @param $whereTables
441 * @param int $contactID
443 * @return null|string
445 public static function whereClause($type, &$tables, &$whereTables, $contactID = NULL) {
446 $acls = CRM_ACL_BAO_Cache
::build($contactID);
452 $aclKeys = array_keys($acls);
453 $aclKeys = implode(',', $aclKeys);
456 SELECT a.operation, a.object_id
457 FROM civicrm_acl_cache c, civicrm_acl a
458 WHERE c.acl_id = a.id
460 AND a.object_table = 'civicrm_saved_search'
461 AND a.id IN ( $aclKeys )
465 $dao = CRM_Core_DAO
::executeQuery($query);
467 // do an or of all the where clauses u see
469 while ($dao->fetch()) {
470 // make sure operation matches the type TODO
471 if (self
::matchType($type, $dao->operation
)) {
472 if (!$dao->object_id
) {
474 $whereClause = ' ( 1 ) ';
477 $ids[] = $dao->object_id
;
482 $ids = implode(',', $ids);
486 WHERE g.id IN ( $ids )
489 $dao = CRM_Core_DAO
::executeQuery($query);
491 $groupContactCacheClause = FALSE;
492 while ($dao->fetch()) {
493 $groupIDs[] = $dao->id
;
495 if (($dao->saved_search_id ||
$dao->children ||
$dao->parents
)) {
496 if ($dao->cache_date
== NULL) {
497 CRM_Contact_BAO_GroupContactCache
::load($dao);
499 $groupContactCacheClause = " UNION SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id IN (" . implode(', ', $groupIDs) . ")";
507 SELECT contact_id FROM civicrm_group_contact WHERE group_id IN (" . implode(', ', $groupIDs) . ") AND status = 'Added'
508 $groupContactCacheClause
515 if (!empty($clauses)) {
516 $whereClause = ' ( ' . implode(' OR ', $clauses) . ' ) ';
519 // call the hook to get additional whereClauses
520 CRM_Utils_Hook
::aclWhereClause($type, $tables, $whereTables, $contactID, $whereClause);
522 if (empty($whereClause)) {
523 $whereClause = ' ( 0 ) ';
531 * @param int $contactID
532 * @param string $tableName
533 * @param null $allGroups
534 * @param null $includedGroups
538 public static function group(
541 $tableName = 'civicrm_saved_search',
543 $includedGroups = NULL
545 $userCacheKey = "{$contactID}_{$type}_{$tableName}_" . CRM_Core_Config
::domainID() . '_' . md5(implode(',', array_merge((array) $allGroups, (array) $includedGroups)));
546 if (empty(Civi
::$statics[__CLASS__
]['permissioned_groups'])) {
547 Civi
::$statics[__CLASS__
]['permissioned_groups'] = [];
549 if (!empty(Civi
::$statics[__CLASS__
]['permissioned_groups'][$userCacheKey])) {
550 return Civi
::$statics[__CLASS__
]['permissioned_groups'][$userCacheKey];
553 if ($allGroups == NULL) {
554 $allGroups = CRM_Contact_BAO_Contact
::buildOptions('group_id', NULL, ['onlyActive' => FALSE]);
557 $acls = CRM_ACL_BAO_Cache
::build($contactID);
561 $aclKeys = array_keys($acls);
562 $aclKeys = implode(',', $aclKeys);
564 $cacheKey = CRM_Utils_Cache
::cleanKey("$tableName-$aclKeys");
565 $cache = CRM_Utils_Cache
::singleton();
566 $ids = $cache->get($cacheKey);
570 SELECT a.operation, a.object_id
571 FROM civicrm_acl_cache c, civicrm_acl a
572 WHERE c.acl_id = a.id
574 AND a.object_table = %1
575 AND a.id IN ( $aclKeys )
576 GROUP BY a.operation,a.object_id
579 $params = [1 => [$tableName, 'String']];
580 $dao = CRM_Core_DAO
::executeQuery($query, $params);
581 while ($dao->fetch()) {
582 if ($dao->object_id
) {
583 if (self
::matchType($type, $dao->operation
)) {
584 $ids[] = $dao->object_id
;
588 // this user has got the permission for all objects of this type
589 // check if the type matches
590 if (self
::matchType($type, $dao->operation
)) {
591 foreach ($allGroups as $id => $dontCare) {
598 $cache->set($cacheKey, $ids);
602 if (empty($ids) && !empty($includedGroups) &&
603 is_array($includedGroups)
605 $ids = $includedGroups;
609 if (!empty($allGroups)) {
610 $groupWhere = " AND id IN (" . implode(',', array_keys($allGroups)) . ")";
612 // Contacts create hidden groups from search results. They should be able to retrieve their own.
613 $ownHiddenGroupsList = CRM_Core_DAO
::singleValueQuery("
614 SELECT GROUP_CONCAT(id) FROM civicrm_group WHERE is_hidden =1 AND created_id = $contactID
617 if ($ownHiddenGroupsList) {
618 $ownHiddenGroups = explode(',', $ownHiddenGroupsList);
619 $ids = array_merge((array) $ids, $ownHiddenGroups);
624 CRM_Utils_Hook
::aclGroup($type, $contactID, $tableName, $allGroups, $ids);
625 Civi
::$statics[__CLASS__
]['permissioned_groups'][$userCacheKey] = $ids;
635 public static function matchType($type, $operation) {
637 switch ($operation) {
643 if ($type == CRM_ACL_API
::VIEW
) {
649 if ($type == CRM_ACL_API
::VIEW ||
$type == CRM_ACL_API
::EDIT
) {
655 if ($type == CRM_ACL_API
::CREATE
) {
661 if ($type == CRM_ACL_API
::DELETE
) {
667 if ($type == CRM_ACL_API
::SEARCH
) {
676 * Delete ACL records.
679 * ID of the ACL record to be deleted.
682 public static function del($aclId) {
683 // delete all entries from the acl cache
684 CRM_ACL_BAO_Cache
::resetCache();
686 $acl = new CRM_ACL_DAO_ACL();