Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
19 | * Access Control List | |
20 | */ | |
21 | class CRM_ACL_BAO_ACL extends CRM_ACL_DAO_ACL { | |
db01bf2f | 22 | /** |
23 | * @var string | |
24 | */ | |
683bf891 SL |
25 | public static $_entityTable = NULL; |
26 | public static $_objectTable = NULL; | |
27 | public static $_operation = NULL; | |
6a488035 | 28 | |
683bf891 | 29 | public static $_fieldKeys = NULL; |
6a488035 | 30 | |
28518c90 | 31 | /** |
3819f101 | 32 | * Get ACL entity table. |
33 | * | |
28518c90 EM |
34 | * @return array|null |
35 | */ | |
00be9182 | 36 | public static function entityTable() { |
9bd90abd | 37 | CRM_Core_Error::deprecatedFunctionWarning('unused function to be removed'); |
6a488035 | 38 | if (!self::$_entityTable) { |
cf0d1c08 | 39 | self::$_entityTable = [ |
6a488035 TO |
40 | 'civicrm_contact' => ts('Contact'), |
41 | 'civicrm_acl_role' => ts('ACL Role'), | |
cf0d1c08 | 42 | ]; |
6a488035 TO |
43 | } |
44 | return self::$_entityTable; | |
45 | } | |
46 | ||
28518c90 EM |
47 | /** |
48 | * @return array|null | |
49 | */ | |
00be9182 | 50 | public static function objectTable() { |
9bd90abd | 51 | CRM_Core_Error::deprecatedFunctionWarning('unused function to be removed'); |
6a488035 | 52 | if (!self::$_objectTable) { |
cf0d1c08 | 53 | self::$_objectTable = [ |
6a488035 TO |
54 | 'civicrm_contact' => ts('Contact'), |
55 | 'civicrm_group' => ts('Group'), | |
56 | 'civicrm_saved_search' => ts('Contact Group'), | |
6a488035 | 57 | 'civicrm_admin' => ts('Import'), |
cf0d1c08 | 58 | ]; |
6a488035 TO |
59 | } |
60 | return self::$_objectTable; | |
61 | } | |
62 | ||
28518c90 | 63 | /** |
9bd90abd | 64 | * Available operations for pseudoconstant. |
65 | * | |
66 | * @return array | |
28518c90 | 67 | */ |
00be9182 | 68 | public static function operation() { |
6a488035 | 69 | if (!self::$_operation) { |
cf0d1c08 | 70 | self::$_operation = [ |
6a488035 TO |
71 | 'View' => ts('View'), |
72 | 'Edit' => ts('Edit'), | |
73 | 'Create' => ts('Create'), | |
74 | 'Delete' => ts('Delete'), | |
75 | 'Search' => ts('Search'), | |
76 | 'All' => ts('All'), | |
cf0d1c08 | 77 | ]; |
6a488035 TO |
78 | } |
79 | return self::$_operation; | |
80 | } | |
81 | ||
6a488035 TO |
82 | /** |
83 | * Given a table and id pair, return the filter clause | |
84 | * | |
b758c7d5 TO |
85 | * @param string $table |
86 | * The table owning the object. | |
87 | * @param int $id | |
88 | * The ID of the object. | |
89 | * @param array $tables | |
90 | * Tables that will be needed in the FROM. | |
6a488035 | 91 | * |
72b3a70c CW |
92 | * @return string|null |
93 | * WHERE-style clause to filter results, | |
16b10e64 | 94 | * or null if $table or $id is null |
03149bb2 | 95 | * |
96 | * @throws \CRM_Core_Exception | |
6a488035 TO |
97 | */ |
98 | public static function getClause($table, $id, &$tables) { | |
9bd90abd | 99 | CRM_Core_Error::deprecatedFunctionWarning('unused function to be removed'); |
353ffa53 TO |
100 | $table = CRM_Utils_Type::escape($table, 'String'); |
101 | $id = CRM_Utils_Type::escape($id, 'Integer'); | |
cf0d1c08 | 102 | $whereTables = []; |
6a488035 TO |
103 | |
104 | $ssTable = CRM_Contact_BAO_SavedSearch::getTableName(); | |
105 | ||
106 | if (empty($table)) { | |
107 | return NULL; | |
108 | } | |
109 | elseif ($table == $ssTable) { | |
110 | return CRM_Contact_BAO_SavedSearch::whereClause($id, $tables, $whereTables); | |
111 | } | |
112 | elseif (!empty($id)) { | |
113 | $tables[$table] = TRUE; | |
114 | return "$table.id = $id"; | |
115 | } | |
116 | return NULL; | |
117 | } | |
118 | ||
119 | /** | |
120 | * Construct an associative array of an ACL rule's properties | |
121 | * | |
b758c7d5 TO |
122 | * @param string $format |
123 | * Sprintf format for array. | |
124 | * @param bool $hideEmpty | |
125 | * Only return elements that have a value set. | |
6a488035 | 126 | * |
a6c01b45 CW |
127 | * @return array |
128 | * Assoc. array of the ACL rule's properties | |
6a488035 | 129 | */ |
100b0ec6 | 130 | public function toArray($format = '%s', $hideEmpty = FALSE) { |
cf0d1c08 | 131 | $result = []; |
6a488035 TO |
132 | |
133 | if (!self::$_fieldKeys) { | |
134 | $fields = CRM_ACL_DAO_ACL::fields(); | |
135 | self::$_fieldKeys = array_keys($fields); | |
136 | } | |
137 | ||
138 | foreach (self::$_fieldKeys as $field) { | |
139 | $result[$field] = $this->$field; | |
140 | } | |
141 | return $result; | |
142 | } | |
143 | ||
144 | /** | |
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. | |
149 | * | |
b758c7d5 TO |
150 | * @param int $contact_id |
151 | * ID of a contact to search for. | |
6a488035 | 152 | * |
a6c01b45 CW |
153 | * @return array |
154 | * Array of assoc. arrays of ACL rules | |
03149bb2 | 155 | * |
156 | * @throws \CRM_Core_Exception | |
6a488035 | 157 | */ |
2ee636aa | 158 | protected static function getACLs(int $contact_id) { |
cf0d1c08 | 159 | $results = []; |
6a488035 | 160 | |
6a488035 TO |
161 | $rule = new CRM_ACL_BAO_ACL(); |
162 | ||
6a488035 | 163 | $contact = CRM_Contact_BAO_Contact::getTableName(); |
6a488035 | 164 | |
20095057 | 165 | $query = " SELECT acl.* |
d340e675 | 166 | FROM civicrm_acl acl |
2ee636aa | 167 | WHERE acl.entity_table = '$contact' |
168 | AND acl.entity_id = $contact_id"; | |
6a488035 TO |
169 | |
170 | $rule->query($query); | |
171 | ||
172 | while ($rule->fetch()) { | |
173 | $results[$rule->id] = $rule->toArray(); | |
174 | } | |
175 | ||
4c8054c4 | 176 | $results += self::getACLRoles($contact_id); |
6a488035 TO |
177 | |
178 | return $results; | |
179 | } | |
180 | ||
181 | /** | |
d2e5d2ce | 182 | * Get all of the ACLs through ACL groups. |
6a488035 | 183 | * |
b758c7d5 TO |
184 | * @param int $contact_id |
185 | * ID of a contact to search for. | |
6a488035 | 186 | * |
a6c01b45 CW |
187 | * @return array |
188 | * Array of assoc. arrays of ACL rules | |
03149bb2 | 189 | * |
190 | * @throws \CRM_Core_Exception | |
6a488035 | 191 | */ |
9bd90abd | 192 | protected static function getACLRoles($contact_id = NULL) { |
6a488035 | 193 | $contact_id = CRM_Utils_Type::escape($contact_id, 'Integer'); |
6a488035 TO |
194 | |
195 | $rule = new CRM_ACL_BAO_ACL(); | |
196 | ||
353ffa53 | 197 | $contact = CRM_Contact_BAO_Contact::getTableName(); |
6a488035 | 198 | |
99f37ee2 | 199 | $query = 'SELECT acl.* FROM civicrm_acl acl'; |
200 | $where = ['acl.entity_table = "civicrm_acl_role" AND acl.entity_id IN (' . implode(',', array_keys(CRM_Core_OptionGroup::values('acl_role'))) . ')']; | |
6a488035 | 201 | |
b0854786 | 202 | if (!empty($contact_id)) { |
99f37ee2 | 203 | $where[] = " acl.entity_table = '$contact' AND acl.is_active = 1 AND acl.entity_id = $contact_id"; |
6a488035 TO |
204 | } |
205 | ||
cf0d1c08 | 206 | $results = []; |
6a488035 | 207 | |
99f37ee2 | 208 | $rule->query($query . ' WHERE ' . implode(' AND ', $where)); |
6a488035 TO |
209 | |
210 | while ($rule->fetch()) { | |
a5611c8e | 211 | $results[$rule->id] = $rule->toArray(); |
6a488035 TO |
212 | } |
213 | ||
214 | return $results; | |
215 | } | |
216 | ||
217 | /** | |
d2e5d2ce | 218 | * Get all ACLs granted to a contact through all group memberships. |
6a488035 | 219 | * |
b758c7d5 TO |
220 | * @param int $contact_id |
221 | * The contact's ID. | |
222 | * @param bool $aclRoles | |
223 | * Include ACL Roles?. | |
6a488035 | 224 | * |
a6c01b45 CW |
225 | * @return array |
226 | * Assoc array of ACL rules | |
03149bb2 | 227 | * @throws \CRM_Core_Exception |
6a488035 | 228 | */ |
9bd90abd | 229 | protected static function getGroupACLs($contact_id, $aclRoles = FALSE) { |
6a488035 TO |
230 | $contact_id = CRM_Utils_Type::escape($contact_id, 'Integer'); |
231 | ||
232 | $rule = new CRM_ACL_BAO_ACL(); | |
233 | ||
353ffa53 TO |
234 | $c2g = CRM_Contact_BAO_GroupContact::getTableName(); |
235 | $group = CRM_Contact_BAO_Group::getTableName(); | |
cf0d1c08 | 236 | $results = []; |
6a488035 TO |
237 | |
238 | if ($contact_id) { | |
239 | $query = " | |
20095057 | 240 | SELECT acl.* |
d340e675 | 241 | FROM civicrm_acl acl |
20095057 | 242 | INNER JOIN $c2g group_contact |
243 | ON acl.entity_id = group_contact.group_id | |
244 | WHERE acl.entity_table = '$group' | |
245 | AND group_contact.contact_id = $contact_id | |
246 | AND group_contact.status = 'Added'"; | |
6a488035 TO |
247 | |
248 | $rule->query($query); | |
249 | ||
250 | while ($rule->fetch()) { | |
79380078 | 251 | $results[$rule->id] = $rule->toArray(); |
6a488035 TO |
252 | } |
253 | } | |
254 | ||
255 | if ($aclRoles) { | |
256 | $results += self::getGroupACLRoles($contact_id); | |
257 | } | |
258 | ||
259 | return $results; | |
260 | } | |
261 | ||
262 | /** | |
d2e5d2ce | 263 | * Get all of the ACLs for a contact through ACL groups owned by Contact. |
6a488035 TO |
264 | * groups. |
265 | * | |
b758c7d5 TO |
266 | * @param int $contact_id |
267 | * ID of a contact to search for. | |
6a488035 | 268 | * |
a6c01b45 CW |
269 | * @return array |
270 | * Array of assoc. arrays of ACL rules | |
03149bb2 | 271 | * @throws \CRM_Core_Exception |
6a488035 | 272 | */ |
9bd90abd | 273 | protected static function getGroupACLRoles($contact_id) { |
6a488035 TO |
274 | $contact_id = CRM_Utils_Type::escape($contact_id, 'Integer'); |
275 | ||
276 | $rule = new CRM_ACL_BAO_ACL(); | |
277 | ||
6a488035 TO |
278 | $aclRole = 'civicrm_acl_role'; |
279 | ||
6a488035 | 280 | $aclER = CRM_ACL_DAO_EntityRole::getTableName(); |
353ffa53 | 281 | $c2g = CRM_Contact_BAO_GroupContact::getTableName(); |
6a488035 | 282 | |
20095057 | 283 | $query = " SELECT acl.* |
d340e675 | 284 | FROM civicrm_acl acl |
6a488035 TO |
285 | INNER JOIN civicrm_option_group og |
286 | ON og.name = 'acl_role' | |
287 | INNER JOIN civicrm_option_value ov | |
20095057 | 288 | ON acl.entity_table = '$aclRole' |
6a488035 | 289 | AND ov.option_group_id = og.id |
20095057 | 290 | AND acl.entity_id = ov.value |
6a488035 TO |
291 | AND ov.is_active = 1 |
292 | INNER JOIN $aclER | |
20095057 | 293 | ON $aclER.acl_role_id = acl.entity_id |
6a488035 TO |
294 | AND $aclER.is_active = 1 |
295 | INNER JOIN $c2g | |
296 | ON $aclER.entity_id = $c2g.group_id | |
297 | AND $aclER.entity_table = 'civicrm_group' | |
20095057 | 298 | WHERE acl.entity_table = '$aclRole' |
299 | AND acl.is_active = 1 | |
6a488035 TO |
300 | AND $c2g.contact_id = $contact_id |
301 | AND $c2g.status = 'Added'"; | |
302 | ||
cf0d1c08 | 303 | $results = []; |
6a488035 TO |
304 | |
305 | $rule->query($query); | |
306 | ||
307 | while ($rule->fetch()) { | |
39eb89f4 | 308 | $results[$rule->id] = $rule->toArray(); |
6a488035 TO |
309 | } |
310 | ||
311 | // also get all acls for "Any Role" case | |
312 | // and authenticated User Role if present | |
313 | $roles = "0"; | |
314 | $session = CRM_Core_Session::singleton(); | |
315 | if ($session->get('ufID') > 0) { | |
316 | $roles .= ",2"; | |
317 | } | |
318 | ||
319 | $query = " | |
20095057 | 320 | SELECT acl.* |
d340e675 | 321 | FROM civicrm_acl acl |
20095057 | 322 | WHERE acl.entity_id IN ( $roles ) |
323 | AND acl.entity_table = 'civicrm_acl_role' | |
6a488035 TO |
324 | "; |
325 | ||
326 | $rule->query($query); | |
327 | while ($rule->fetch()) { | |
328 | $results[$rule->id] = $rule->toArray(); | |
329 | } | |
330 | ||
331 | return $results; | |
332 | } | |
333 | ||
334 | /** | |
335 | * Get all ACLs owned by a given contact, including domain and group-level. | |
336 | * | |
b758c7d5 TO |
337 | * @param int $contact_id |
338 | * The contact ID. | |
6a488035 | 339 | * |
a6c01b45 CW |
340 | * @return array |
341 | * Assoc array of ACL rules | |
03149bb2 | 342 | * |
343 | * @throws \CRM_Core_Exception | |
6a488035 | 344 | */ |
03149bb2 | 345 | public static function getAllByContact($contact_id) { |
cf0d1c08 | 346 | $result = []; |
6a488035 TO |
347 | |
348 | /* First, the contact-specific ACLs, including ACL Roles */ | |
2ee636aa | 349 | if ($contact_id) { |
350 | $result += self::getACLs((int) $contact_id); | |
351 | } | |
6a488035 TO |
352 | |
353 | /* Then, all ACLs granted through group membership */ | |
354 | $result += self::getGroupACLs($contact_id, TRUE); | |
355 | ||
356 | return $result; | |
357 | } | |
358 | ||
28518c90 | 359 | /** |
c490a46a | 360 | * @param array $params |
28518c90 EM |
361 | * |
362 | * @return CRM_ACL_DAO_ACL | |
363 | */ | |
03149bb2 | 364 | public static function create($params) { |
6a488035 TO |
365 | $dao = new CRM_ACL_DAO_ACL(); |
366 | $dao->copyValues($params); | |
367 | $dao->save(); | |
1fe97a01 | 368 | return $dao; |
6a488035 TO |
369 | } |
370 | ||
28518c90 | 371 | /** |
c490a46a | 372 | * @param array $params |
03149bb2 | 373 | * @param array $defaults |
28518c90 | 374 | */ |
00be9182 | 375 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
376 | CRM_Core_DAO::commonRetrieve('CRM_ACL_DAO_ACL', $params, $defaults); |
377 | } | |
378 | ||
379 | /** | |
fe482240 | 380 | * Update the is_active flag in the db. |
6a488035 | 381 | * |
b758c7d5 TO |
382 | * @param int $id |
383 | * Id of the database record. | |
384 | * @param bool $is_active | |
385 | * Value we want to set the is_active field. | |
6a488035 | 386 | * |
8a4fede3 | 387 | * @return bool |
388 | * true if we found and updated the object, else false | |
6a488035 | 389 | */ |
00be9182 | 390 | public static function setIsActive($id, $is_active) { |
9cdf85c1 | 391 | Civi::cache('fields')->flush(); |
5e601882 SL |
392 | // reset ACL and system caches. |
393 | CRM_Core_BAO_Cache::resetCaches(); | |
6a488035 TO |
394 | |
395 | return CRM_Core_DAO::setFieldValue('CRM_ACL_DAO_ACL', $id, 'is_active', $is_active); | |
396 | } | |
397 | ||
28518c90 EM |
398 | /** |
399 | * @param $str | |
100fef9d | 400 | * @param int $contactID |
28518c90 EM |
401 | * |
402 | * @return bool | |
403 | */ | |
00be9182 | 404 | public static function check($str, $contactID) { |
6a488035 TO |
405 | |
406 | $acls = CRM_ACL_BAO_Cache::build($contactID); | |
407 | ||
408 | $aclKeys = array_keys($acls); | |
409 | $aclKeys = implode(',', $aclKeys); | |
410 | ||
411 | if (empty($aclKeys)) { | |
412 | return FALSE; | |
413 | } | |
414 | ||
6a488035 TO |
415 | $query = " |
416 | SELECT count( a.id ) | |
417 | FROM civicrm_acl_cache c, civicrm_acl a | |
418 | WHERE c.acl_id = a.id | |
419 | AND a.is_active = 1 | |
420 | AND a.object_table = %1 | |
421 | AND a.id IN ( $aclKeys ) | |
422 | "; | |
cf0d1c08 | 423 | $params = [1 => [$str, 'String']]; |
6a488035 TO |
424 | |
425 | $count = CRM_Core_DAO::singleValueQuery($query, $params); | |
1699214f | 426 | return (bool) $count; |
6a488035 TO |
427 | } |
428 | ||
28518c90 EM |
429 | /** |
430 | * @param $type | |
431 | * @param $tables | |
432 | * @param $whereTables | |
100fef9d | 433 | * @param int $contactID |
28518c90 EM |
434 | * |
435 | * @return null|string | |
436 | */ | |
199761b4 | 437 | public static function whereClause($type, &$tables, &$whereTables, $contactID = NULL) { |
6a488035 | 438 | $acls = CRM_ACL_BAO_Cache::build($contactID); |
6a488035 TO |
439 | |
440 | $whereClause = NULL; | |
cf0d1c08 | 441 | $clauses = []; |
6a488035 TO |
442 | |
443 | if (!empty($acls)) { | |
444 | $aclKeys = array_keys($acls); | |
445 | $aclKeys = implode(',', $aclKeys); | |
446 | ||
447 | $query = " | |
448 | SELECT a.operation, a.object_id | |
449 | FROM civicrm_acl_cache c, civicrm_acl a | |
450 | WHERE c.acl_id = a.id | |
451 | AND a.is_active = 1 | |
452 | AND a.object_table = 'civicrm_saved_search' | |
453 | AND a.id IN ( $aclKeys ) | |
454 | ORDER BY a.object_id | |
455 | "; | |
456 | ||
457 | $dao = CRM_Core_DAO::executeQuery($query); | |
458 | ||
459 | // do an or of all the where clauses u see | |
cf0d1c08 | 460 | $ids = []; |
6a488035 TO |
461 | while ($dao->fetch()) { |
462 | // make sure operation matches the type TODO | |
463 | if (self::matchType($type, $dao->operation)) { | |
464 | if (!$dao->object_id) { | |
cf0d1c08 | 465 | $ids = []; |
6a488035 TO |
466 | $whereClause = ' ( 1 ) '; |
467 | break; | |
468 | } | |
469 | $ids[] = $dao->object_id; | |
470 | } | |
471 | } | |
472 | ||
473 | if (!empty($ids)) { | |
474 | $ids = implode(',', $ids); | |
475 | $query = " | |
476 | SELECT g.* | |
477 | FROM civicrm_group g | |
478 | WHERE g.id IN ( $ids ) | |
479 | AND g.is_active = 1 | |
480 | "; | |
353ffa53 | 481 | $dao = CRM_Core_DAO::executeQuery($query); |
1bcdee33 | 482 | $groupIDs = []; |
483 | $groupContactCacheClause = FALSE; | |
6a488035 | 484 | while ($dao->fetch()) { |
1bcdee33 | 485 | $groupIDs[] = $dao->id; |
6a488035 | 486 | |
1d902030 | 487 | if (($dao->saved_search_id || $dao->children || $dao->parents)) { |
488 | if ($dao->cache_date == NULL) { | |
489 | CRM_Contact_BAO_GroupContactCache::load($dao); | |
490 | } | |
1bcdee33 | 491 | $groupContactCacheClause = " UNION SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id IN (" . implode(', ', $groupIDs) . ")"; |
6a488035 | 492 | } |
6a488035 | 493 | |
6a488035 TO |
494 | } |
495 | ||
1bcdee33 | 496 | if ($groupIDs) { |
497 | $clauses[] = "( | |
498 | `contact_a`.id IN ( | |
499 | SELECT contact_id FROM civicrm_group_contact WHERE group_id IN (" . implode(', ', $groupIDs) . ") AND status = 'Added' | |
500 | $groupContactCacheClause | |
501 | ) | |
502 | )"; | |
6a488035 TO |
503 | } |
504 | } | |
505 | } | |
506 | ||
507 | if (!empty($clauses)) { | |
508 | $whereClause = ' ( ' . implode(' OR ', $clauses) . ' ) '; | |
509 | } | |
510 | ||
511 | // call the hook to get additional whereClauses | |
512 | CRM_Utils_Hook::aclWhereClause($type, $tables, $whereTables, $contactID, $whereClause); | |
513 | ||
199761b4 | 514 | if (empty($whereClause)) { |
6a488035 TO |
515 | $whereClause = ' ( 0 ) '; |
516 | } | |
517 | ||
518 | return $whereClause; | |
519 | } | |
520 | ||
28518c90 | 521 | /** |
c490a46a | 522 | * @param int $type |
100fef9d | 523 | * @param int $contactID |
28518c90 EM |
524 | * @param string $tableName |
525 | * @param null $allGroups | |
526 | * @param null $includedGroups | |
527 | * | |
528 | * @return array | |
529 | */ | |
e6a83034 TO |
530 | public static function group( |
531 | $type, | |
100b0ec6 TO |
532 | $contactID = NULL, |
533 | $tableName = 'civicrm_saved_search', | |
534 | $allGroups = NULL, | |
6a488035 TO |
535 | $includedGroups = NULL |
536 | ) { | |
e3ad0182 | 537 | $userCacheKey = "{$contactID}_{$type}_{$tableName}_" . CRM_Core_Config::domainID() . '_' . md5(implode(',', array_merge((array) $allGroups, (array) $includedGroups))); |
538 | if (empty(Civi::$statics[__CLASS__]['permissioned_groups'])) { | |
cf0d1c08 | 539 | Civi::$statics[__CLASS__]['permissioned_groups'] = []; |
e3ad0182 | 540 | } |
541 | if (!empty(Civi::$statics[__CLASS__]['permissioned_groups'][$userCacheKey])) { | |
542 | return Civi::$statics[__CLASS__]['permissioned_groups'][$userCacheKey]; | |
543 | } | |
6a488035 | 544 | |
8aace6af | 545 | if ($allGroups == NULL) { |
887688b9 | 546 | $allGroups = CRM_Contact_BAO_Contact::buildOptions('group_id', NULL, ['onlyActive' => FALSE]); |
8aace6af JG |
547 | } |
548 | ||
6a488035 TO |
549 | $acls = CRM_ACL_BAO_Cache::build($contactID); |
550 | ||
cf0d1c08 | 551 | $ids = []; |
6a488035 TO |
552 | if (!empty($acls)) { |
553 | $aclKeys = array_keys($acls); | |
554 | $aclKeys = implode(',', $aclKeys); | |
555 | ||
77f080cb | 556 | $cacheKey = CRM_Utils_Cache::cleanKey("$tableName-$aclKeys"); |
557f8c17 ARW |
557 | $cache = CRM_Utils_Cache::singleton(); |
558 | $ids = $cache->get($cacheKey); | |
559 | if (!$ids) { | |
cf0d1c08 | 560 | $ids = []; |
557f8c17 | 561 | $query = " |
6a488035 TO |
562 | SELECT a.operation, a.object_id |
563 | FROM civicrm_acl_cache c, civicrm_acl a | |
564 | WHERE c.acl_id = a.id | |
565 | AND a.is_active = 1 | |
566 | AND a.object_table = %1 | |
567 | AND a.id IN ( $aclKeys ) | |
568 | GROUP BY a.operation,a.object_id | |
569 | ORDER BY a.object_id | |
570 | "; | |
cf0d1c08 | 571 | $params = [1 => [$tableName, 'String']]; |
557f8c17 ARW |
572 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
573 | while ($dao->fetch()) { | |
574 | if ($dao->object_id) { | |
575 | if (self::matchType($type, $dao->operation)) { | |
576 | $ids[] = $dao->object_id; | |
577 | } | |
6a488035 | 578 | } |
557f8c17 ARW |
579 | else { |
580 | // this user has got the permission for all objects of this type | |
581 | // check if the type matches | |
582 | if (self::matchType($type, $dao->operation)) { | |
583 | foreach ($allGroups as $id => $dontCare) { | |
584 | $ids[] = $id; | |
585 | } | |
6a488035 | 586 | } |
557f8c17 | 587 | break; |
6a488035 | 588 | } |
6a488035 | 589 | } |
557f8c17 | 590 | $cache->set($cacheKey, $ids); |
6a488035 TO |
591 | } |
592 | } | |
593 | ||
b1f4e637 RN |
594 | if (empty($ids) && !empty($includedGroups) && |
595 | is_array($includedGroups) | |
596 | ) { | |
597 | $ids = $includedGroups; | |
598 | } | |
e3ad0182 | 599 | if ($contactID) { |
54d93c06 | 600 | $groupWhere = ''; |
601 | if (!empty($allGroups)) { | |
602 | $groupWhere = " AND id IN (" . implode(',', array_keys($allGroups)) . ")"; | |
603 | } | |
e3ad0182 | 604 | // Contacts create hidden groups from search results. They should be able to retrieve their own. |
605 | $ownHiddenGroupsList = CRM_Core_DAO::singleValueQuery(" | |
606 | SELECT GROUP_CONCAT(id) FROM civicrm_group WHERE is_hidden =1 AND created_id = $contactID | |
54d93c06 | 607 | $groupWhere |
e3ad0182 | 608 | "); |
609 | if ($ownHiddenGroupsList) { | |
610 | $ownHiddenGroups = explode(',', $ownHiddenGroupsList); | |
54d93c06 | 611 | $ids = array_merge((array) $ids, $ownHiddenGroups); |
e3ad0182 | 612 | } |
613 | ||
614 | } | |
b1f4e637 | 615 | |
6a488035 | 616 | CRM_Utils_Hook::aclGroup($type, $contactID, $tableName, $allGroups, $ids); |
e3ad0182 | 617 | Civi::$statics[__CLASS__]['permissioned_groups'][$userCacheKey] = $ids; |
6a488035 TO |
618 | return $ids; |
619 | } | |
620 | ||
28518c90 | 621 | /** |
c490a46a | 622 | * @param int $type |
28518c90 EM |
623 | * @param $operation |
624 | * | |
625 | * @return bool | |
626 | */ | |
9bd90abd | 627 | protected static function matchType($type, $operation) { |
6a488035 TO |
628 | $typeCheck = FALSE; |
629 | switch ($operation) { | |
630 | case 'All': | |
631 | $typeCheck = TRUE; | |
632 | break; | |
633 | ||
634 | case 'View': | |
635 | if ($type == CRM_ACL_API::VIEW) { | |
636 | $typeCheck = TRUE; | |
637 | } | |
638 | break; | |
639 | ||
640 | case 'Edit': | |
641 | if ($type == CRM_ACL_API::VIEW || $type == CRM_ACL_API::EDIT) { | |
642 | $typeCheck = TRUE; | |
643 | } | |
644 | break; | |
645 | ||
646 | case 'Create': | |
647 | if ($type == CRM_ACL_API::CREATE) { | |
648 | $typeCheck = TRUE; | |
649 | } | |
650 | break; | |
651 | ||
652 | case 'Delete': | |
653 | if ($type == CRM_ACL_API::DELETE) { | |
654 | $typeCheck = TRUE; | |
655 | } | |
656 | break; | |
657 | ||
658 | case 'Search': | |
659 | if ($type == CRM_ACL_API::SEARCH) { | |
660 | $typeCheck = TRUE; | |
661 | } | |
662 | break; | |
663 | } | |
664 | return $typeCheck; | |
665 | } | |
666 | ||
667 | /** | |
d2e5d2ce | 668 | * Delete ACL records. |
6a488035 | 669 | * |
b758c7d5 TO |
670 | * @param int $aclId |
671 | * ID of the ACL record to be deleted. | |
6a488035 | 672 | * |
6a488035 | 673 | */ |
00be9182 | 674 | public static function del($aclId) { |
6a488035 TO |
675 | // delete all entries from the acl cache |
676 | CRM_ACL_BAO_Cache::resetCache(); | |
677 | ||
678 | $acl = new CRM_ACL_DAO_ACL(); | |
679 | $acl->id = $aclId; | |
680 | $acl->delete(); | |
681 | } | |
96025800 | 682 | |
6a488035 | 683 | } |