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) {
92 $rule = new CRM_ACL_BAO_ACL();
94 $contact = CRM_Contact_BAO_Contact
::getTableName();
96 $query = " SELECT acl.*
98 WHERE acl.entity_table = '$contact'
99 AND acl.entity_id = $contact_id";
101 $rule->query($query);
103 while ($rule->fetch()) {
104 $results[$rule->id
] = $rule->toArray();
107 $results +
= self
::getACLRoles($contact_id);
113 * Get all of the ACLs through ACL groups.
115 * @param int $contact_id
116 * ID of a contact to search for.
119 * Array of assoc. arrays of ACL rules
121 * @throws \CRM_Core_Exception
123 protected static function getACLRoles($contact_id = NULL) {
124 $contact_id = CRM_Utils_Type
::escape($contact_id, 'Integer');
126 $rule = new CRM_ACL_BAO_ACL();
128 $contact = CRM_Contact_BAO_Contact
::getTableName();
130 $query = 'SELECT acl.* FROM civicrm_acl acl';
131 $where = ['acl.entity_table = "civicrm_acl_role" AND acl.entity_id IN (' . implode(',', array_keys(CRM_Core_OptionGroup
::values('acl_role'))) . ')'];
133 if (!empty($contact_id)) {
134 $where[] = " acl.entity_table = '$contact' AND acl.is_active = 1 AND acl.entity_id = $contact_id";
139 $rule->query($query . ' WHERE ' . implode(' AND ', $where));
141 while ($rule->fetch()) {
142 $results[$rule->id
] = $rule->toArray();
149 * Get all ACLs granted to a contact through all group memberships.
151 * @param int $contact_id
153 * @param bool $aclRoles
154 * Include ACL Roles?.
157 * Assoc array of ACL rules
158 * @throws \CRM_Core_Exception
160 protected static function getGroupACLs($contact_id, $aclRoles = FALSE) {
161 $contact_id = CRM_Utils_Type
::escape($contact_id, 'Integer');
163 $rule = new CRM_ACL_BAO_ACL();
165 $c2g = CRM_Contact_BAO_GroupContact
::getTableName();
166 $group = CRM_Contact_BAO_Group
::getTableName();
173 INNER JOIN $c2g group_contact
174 ON acl.entity_id = group_contact.group_id
175 WHERE acl.entity_table = '$group'
176 AND group_contact.contact_id = $contact_id
177 AND group_contact.status = 'Added'";
179 $rule->query($query);
181 while ($rule->fetch()) {
182 $results[$rule->id
] = $rule->toArray();
187 $results +
= self
::getGroupACLRoles($contact_id);
194 * Get all of the ACLs for a contact through ACL groups owned by Contact.
197 * @param int $contact_id
198 * ID of a contact to search for.
201 * Array of assoc. arrays of ACL rules
202 * @throws \CRM_Core_Exception
204 protected static function getGroupACLRoles($contact_id) {
205 $contact_id = CRM_Utils_Type
::escape($contact_id, 'Integer');
207 $rule = new CRM_ACL_BAO_ACL();
209 $query = " SELECT acl.*
211 INNER JOIN civicrm_option_group og
212 ON og.name = 'acl_role'
213 INNER JOIN civicrm_option_value ov
214 ON acl.entity_table = 'civicrm_acl_role'
215 AND ov.option_group_id = og.id
216 AND acl.entity_id = ov.value
218 INNER JOIN civicrm_acl_entity_role acl_entity_role
219 ON acl_entity_role.acl_role_id = acl.entity_id
220 AND acl_entity_role.is_active = 1
221 INNER JOIN civicrm_group_contact group_contact
222 ON acl_entity_role.entity_id = group_contact.group_id
223 AND acl_entity_role.entity_table = 'civicrm_group'
224 WHERE acl.entity_table = 'civicrm_acl_role'
225 AND acl.is_active = 1
226 AND group_contact.contact_id = $contact_id
227 AND group_contact.status = 'Added'";
231 $rule->query($query);
233 while ($rule->fetch()) {
234 $results[$rule->id
] = $rule->toArray();
237 // also get all acls for "Any Role" case
238 // and authenticated User Role if present
240 $session = CRM_Core_Session
::singleton();
241 if ($session->get('ufID') > 0) {
248 WHERE acl.entity_id IN ( $roles )
249 AND acl.entity_table = 'civicrm_acl_role'
252 $rule->query($query);
253 while ($rule->fetch()) {
254 $results[$rule->id
] = $rule->toArray();
261 * Get all ACLs owned by a given contact, including domain and group-level.
263 * @param int $contact_id
267 * Assoc array of ACL rules
269 * @throws \CRM_Core_Exception
271 public static function getAllByContact($contact_id) {
274 /* First, the contact-specific ACLs, including ACL Roles */
276 $result +
= self
::getACLs((int) $contact_id);
279 /* Then, all ACLs granted through group membership */
280 $result +
= self
::getGroupACLs($contact_id, TRUE);
286 * @param array $params
288 * @return CRM_ACL_DAO_ACL
290 public static function create($params) {
291 $dao = new CRM_ACL_DAO_ACL();
292 $dao->copyValues($params);
298 * @param array $params
299 * @param array $defaults
301 public static function retrieve(&$params, &$defaults) {
302 CRM_Core_DAO
::commonRetrieve('CRM_ACL_DAO_ACL', $params, $defaults);
306 * Update the is_active flag in the db.
309 * Id of the database record.
310 * @param bool $is_active
311 * Value we want to set the is_active field.
314 * true if we found and updated the object, else false
316 public static function setIsActive($id, $is_active) {
317 Civi
::cache('fields')->flush();
318 // reset ACL and system caches.
319 CRM_Core_BAO_Cache
::resetCaches();
321 return CRM_Core_DAO
::setFieldValue('CRM_ACL_DAO_ACL', $id, 'is_active', $is_active);
326 * @param int $contactID
330 public static function check($str, $contactID) {
332 $acls = CRM_ACL_BAO_Cache
::build($contactID);
334 $aclKeys = array_keys($acls);
335 $aclKeys = implode(',', $aclKeys);
337 if (empty($aclKeys)) {
343 FROM civicrm_acl_cache c, civicrm_acl a
344 WHERE c.acl_id = a.id
346 AND a.object_table = %1
347 AND a.id IN ( $aclKeys )
349 $params = [1 => [$str, 'String']];
351 $count = CRM_Core_DAO
::singleValueQuery($query, $params);
352 return (bool) $count;
358 * @param $whereTables
359 * @param int $contactID
361 * @return null|string
363 public static function whereClause($type, &$tables, &$whereTables, $contactID = NULL) {
364 $acls = CRM_ACL_BAO_Cache
::build($contactID);
370 $aclKeys = array_keys($acls);
371 $aclKeys = implode(',', $aclKeys);
374 SELECT a.operation, a.object_id
375 FROM civicrm_acl_cache c, civicrm_acl a
376 WHERE c.acl_id = a.id
378 AND a.object_table = 'civicrm_saved_search'
379 AND a.id IN ( $aclKeys )
383 $dao = CRM_Core_DAO
::executeQuery($query);
385 // do an or of all the where clauses u see
387 while ($dao->fetch()) {
388 // make sure operation matches the type TODO
389 if (self
::matchType($type, $dao->operation
)) {
390 if (!$dao->object_id
) {
392 $whereClause = ' ( 1 ) ';
395 $ids[] = $dao->object_id
;
400 $ids = implode(',', $ids);
404 WHERE g.id IN ( $ids )
407 $dao = CRM_Core_DAO
::executeQuery($query);
409 $groupContactCacheClause = FALSE;
410 while ($dao->fetch()) {
411 $groupIDs[] = $dao->id
;
413 if (($dao->saved_search_id ||
$dao->children ||
$dao->parents
)) {
414 if ($dao->cache_date
== NULL) {
415 CRM_Contact_BAO_GroupContactCache
::load($dao);
417 $groupContactCacheClause = " UNION SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id IN (" . implode(', ', $groupIDs) . ")";
425 SELECT contact_id FROM civicrm_group_contact WHERE group_id IN (" . implode(', ', $groupIDs) . ") AND status = 'Added'
426 $groupContactCacheClause
433 if (!empty($clauses)) {
434 $whereClause = ' ( ' . implode(' OR ', $clauses) . ' ) ';
437 // call the hook to get additional whereClauses
438 CRM_Utils_Hook
::aclWhereClause($type, $tables, $whereTables, $contactID, $whereClause);
440 if (empty($whereClause)) {
441 $whereClause = ' ( 0 ) ';
449 * @param int $contactID
450 * @param string $tableName
451 * @param null $allGroups
452 * @param null $includedGroups
456 public static function group(
459 $tableName = 'civicrm_saved_search',
461 $includedGroups = NULL
463 $userCacheKey = "{$contactID}_{$type}_{$tableName}_" . CRM_Core_Config
::domainID() . '_' . md5(implode(',', array_merge((array) $allGroups, (array) $includedGroups)));
464 if (empty(Civi
::$statics[__CLASS__
]['permissioned_groups'])) {
465 Civi
::$statics[__CLASS__
]['permissioned_groups'] = [];
467 if (!empty(Civi
::$statics[__CLASS__
]['permissioned_groups'][$userCacheKey])) {
468 return Civi
::$statics[__CLASS__
]['permissioned_groups'][$userCacheKey];
471 if ($allGroups == NULL) {
472 $allGroups = CRM_Contact_BAO_Contact
::buildOptions('group_id', 'get');
475 $acls = CRM_ACL_BAO_Cache
::build($contactID);
479 $aclKeys = array_keys($acls);
480 $aclKeys = implode(',', $aclKeys);
482 $cacheKey = CRM_Utils_Cache
::cleanKey("$tableName-$aclKeys");
483 $cache = CRM_Utils_Cache
::singleton();
484 $ids = $cache->get($cacheKey);
488 SELECT a.operation, a.object_id
489 FROM civicrm_acl_cache c, civicrm_acl a
490 WHERE c.acl_id = a.id
492 AND a.object_table = %1
493 AND a.id IN ( $aclKeys )
494 GROUP BY a.operation,a.object_id
497 $params = [1 => [$tableName, 'String']];
498 $dao = CRM_Core_DAO
::executeQuery($query, $params);
499 while ($dao->fetch()) {
500 if ($dao->object_id
) {
501 if (self
::matchType($type, $dao->operation
)) {
502 $ids[] = $dao->object_id
;
506 // this user has got the permission for all objects of this type
507 // check if the type matches
508 if (self
::matchType($type, $dao->operation
)) {
509 foreach ($allGroups as $id => $dontCare) {
516 $cache->set($cacheKey, $ids);
520 if (empty($ids) && !empty($includedGroups) &&
521 is_array($includedGroups)
523 $ids = $includedGroups;
527 if (!empty($allGroups)) {
528 $groupWhere = " AND id IN (" . implode(',', array_keys($allGroups)) . ")";
530 // Contacts create hidden groups from search results. They should be able to retrieve their own.
531 $ownHiddenGroupsList = CRM_Core_DAO
::singleValueQuery("
532 SELECT GROUP_CONCAT(id) FROM civicrm_group WHERE is_hidden =1 AND created_id = $contactID
535 if ($ownHiddenGroupsList) {
536 $ownHiddenGroups = explode(',', $ownHiddenGroupsList);
537 $ids = array_merge((array) $ids, $ownHiddenGroups);
542 CRM_Utils_Hook
::aclGroup($type, $contactID, $tableName, $allGroups, $ids);
543 Civi
::$statics[__CLASS__
]['permissioned_groups'][$userCacheKey] = $ids;
553 protected static function matchType($type, $operation) {
555 switch ($operation) {
561 if ($type == CRM_ACL_API
::VIEW
) {
567 if ($type == CRM_ACL_API
::VIEW ||
$type == CRM_ACL_API
::EDIT
) {
573 if ($type == CRM_ACL_API
::CREATE
) {
579 if ($type == CRM_ACL_API
::DELETE
) {
585 if ($type == CRM_ACL_API
::SEARCH
) {
594 * Delete ACL records.
597 * ID of the ACL record to be deleted.
600 public static function del($aclId) {
601 // delete all entries from the acl cache
602 CRM_ACL_BAO_Cache
::resetCache();
604 $acl = new CRM_ACL_DAO_ACL();