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 | ||
353ffa53 | 163 | $acl = self::getTableName(); |
6a488035 | 164 | $contact = CRM_Contact_BAO_Contact::getTableName(); |
6a488035 | 165 | |
20095057 | 166 | $query = " SELECT acl.* |
2ee636aa | 167 | FROM $acl acl |
168 | WHERE acl.entity_table = '$contact' | |
169 | AND acl.entity_id = $contact_id"; | |
6a488035 TO |
170 | |
171 | $rule->query($query); | |
172 | ||
173 | while ($rule->fetch()) { | |
174 | $results[$rule->id] = $rule->toArray(); | |
175 | } | |
176 | ||
4c8054c4 | 177 | $results += self::getACLRoles($contact_id); |
6a488035 TO |
178 | |
179 | return $results; | |
180 | } | |
181 | ||
182 | /** | |
d2e5d2ce | 183 | * Get all of the ACLs through ACL groups. |
6a488035 | 184 | * |
b758c7d5 TO |
185 | * @param int $contact_id |
186 | * ID of a contact to search for. | |
6a488035 | 187 | * |
a6c01b45 CW |
188 | * @return array |
189 | * Array of assoc. arrays of ACL rules | |
03149bb2 | 190 | * |
191 | * @throws \CRM_Core_Exception | |
6a488035 | 192 | */ |
9bd90abd | 193 | protected static function getACLRoles($contact_id = NULL) { |
6a488035 | 194 | $contact_id = CRM_Utils_Type::escape($contact_id, 'Integer'); |
6a488035 TO |
195 | |
196 | $rule = new CRM_ACL_BAO_ACL(); | |
197 | ||
353ffa53 | 198 | $contact = CRM_Contact_BAO_Contact::getTableName(); |
6a488035 | 199 | |
99f37ee2 | 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'))) . ')']; | |
6a488035 | 202 | |
b0854786 | 203 | if (!empty($contact_id)) { |
99f37ee2 | 204 | $where[] = " acl.entity_table = '$contact' AND acl.is_active = 1 AND acl.entity_id = $contact_id"; |
6a488035 TO |
205 | } |
206 | ||
cf0d1c08 | 207 | $results = []; |
6a488035 | 208 | |
99f37ee2 | 209 | $rule->query($query . ' WHERE ' . implode(' AND ', $where)); |
6a488035 TO |
210 | |
211 | while ($rule->fetch()) { | |
a5611c8e | 212 | $results[$rule->id] = $rule->toArray(); |
6a488035 TO |
213 | } |
214 | ||
215 | return $results; | |
216 | } | |
217 | ||
218 | /** | |
d2e5d2ce | 219 | * Get all ACLs granted to a contact through all group memberships. |
6a488035 | 220 | * |
b758c7d5 TO |
221 | * @param int $contact_id |
222 | * The contact's ID. | |
223 | * @param bool $aclRoles | |
224 | * Include ACL Roles?. | |
6a488035 | 225 | * |
a6c01b45 CW |
226 | * @return array |
227 | * Assoc array of ACL rules | |
03149bb2 | 228 | * @throws \CRM_Core_Exception |
6a488035 | 229 | */ |
9bd90abd | 230 | protected static function getGroupACLs($contact_id, $aclRoles = FALSE) { |
6a488035 TO |
231 | $contact_id = CRM_Utils_Type::escape($contact_id, 'Integer'); |
232 | ||
233 | $rule = new CRM_ACL_BAO_ACL(); | |
234 | ||
353ffa53 TO |
235 | $acl = self::getTableName(); |
236 | $c2g = CRM_Contact_BAO_GroupContact::getTableName(); | |
237 | $group = CRM_Contact_BAO_Group::getTableName(); | |
cf0d1c08 | 238 | $results = []; |
6a488035 TO |
239 | |
240 | if ($contact_id) { | |
241 | $query = " | |
20095057 | 242 | SELECT acl.* |
243 | FROM $acl acl | |
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'"; | |
6a488035 TO |
249 | |
250 | $rule->query($query); | |
251 | ||
252 | while ($rule->fetch()) { | |
79380078 | 253 | $results[$rule->id] = $rule->toArray(); |
6a488035 TO |
254 | } |
255 | } | |
256 | ||
257 | if ($aclRoles) { | |
258 | $results += self::getGroupACLRoles($contact_id); | |
259 | } | |
260 | ||
261 | return $results; | |
262 | } | |
263 | ||
264 | /** | |
d2e5d2ce | 265 | * Get all of the ACLs for a contact through ACL groups owned by Contact. |
6a488035 TO |
266 | * groups. |
267 | * | |
b758c7d5 TO |
268 | * @param int $contact_id |
269 | * ID of a contact to search for. | |
6a488035 | 270 | * |
a6c01b45 CW |
271 | * @return array |
272 | * Array of assoc. arrays of ACL rules | |
03149bb2 | 273 | * @throws \CRM_Core_Exception |
6a488035 | 274 | */ |
9bd90abd | 275 | protected static function getGroupACLRoles($contact_id) { |
6a488035 TO |
276 | $contact_id = CRM_Utils_Type::escape($contact_id, 'Integer'); |
277 | ||
278 | $rule = new CRM_ACL_BAO_ACL(); | |
279 | ||
280 | $acl = self::getTableName(); | |
281 | $aclRole = 'civicrm_acl_role'; | |
282 | ||
6a488035 | 283 | $aclER = CRM_ACL_DAO_EntityRole::getTableName(); |
353ffa53 | 284 | $c2g = CRM_Contact_BAO_GroupContact::getTableName(); |
6a488035 | 285 | |
20095057 | 286 | $query = " SELECT acl.* |
287 | FROM $acl acl | |
6a488035 TO |
288 | INNER JOIN civicrm_option_group og |
289 | ON og.name = 'acl_role' | |
290 | INNER JOIN civicrm_option_value ov | |
20095057 | 291 | ON acl.entity_table = '$aclRole' |
6a488035 | 292 | AND ov.option_group_id = og.id |
20095057 | 293 | AND acl.entity_id = ov.value |
6a488035 TO |
294 | AND ov.is_active = 1 |
295 | INNER JOIN $aclER | |
20095057 | 296 | ON $aclER.acl_role_id = acl.entity_id |
6a488035 TO |
297 | AND $aclER.is_active = 1 |
298 | INNER JOIN $c2g | |
299 | ON $aclER.entity_id = $c2g.group_id | |
300 | AND $aclER.entity_table = 'civicrm_group' | |
20095057 | 301 | WHERE acl.entity_table = '$aclRole' |
302 | AND acl.is_active = 1 | |
6a488035 TO |
303 | AND $c2g.contact_id = $contact_id |
304 | AND $c2g.status = 'Added'"; | |
305 | ||
cf0d1c08 | 306 | $results = []; |
6a488035 TO |
307 | |
308 | $rule->query($query); | |
309 | ||
310 | while ($rule->fetch()) { | |
39eb89f4 | 311 | $results[$rule->id] = $rule->toArray(); |
6a488035 TO |
312 | } |
313 | ||
314 | // also get all acls for "Any Role" case | |
315 | // and authenticated User Role if present | |
316 | $roles = "0"; | |
317 | $session = CRM_Core_Session::singleton(); | |
318 | if ($session->get('ufID') > 0) { | |
319 | $roles .= ",2"; | |
320 | } | |
321 | ||
322 | $query = " | |
20095057 | 323 | SELECT acl.* |
324 | FROM $acl acl | |
325 | WHERE acl.entity_id IN ( $roles ) | |
326 | AND acl.entity_table = 'civicrm_acl_role' | |
6a488035 TO |
327 | "; |
328 | ||
329 | $rule->query($query); | |
330 | while ($rule->fetch()) { | |
331 | $results[$rule->id] = $rule->toArray(); | |
332 | } | |
333 | ||
334 | return $results; | |
335 | } | |
336 | ||
337 | /** | |
338 | * Get all ACLs owned by a given contact, including domain and group-level. | |
339 | * | |
b758c7d5 TO |
340 | * @param int $contact_id |
341 | * The contact ID. | |
6a488035 | 342 | * |
a6c01b45 CW |
343 | * @return array |
344 | * Assoc array of ACL rules | |
03149bb2 | 345 | * |
346 | * @throws \CRM_Core_Exception | |
6a488035 | 347 | */ |
03149bb2 | 348 | public static function getAllByContact($contact_id) { |
cf0d1c08 | 349 | $result = []; |
6a488035 TO |
350 | |
351 | /* First, the contact-specific ACLs, including ACL Roles */ | |
2ee636aa | 352 | if ($contact_id) { |
353 | $result += self::getACLs((int) $contact_id); | |
354 | } | |
6a488035 TO |
355 | |
356 | /* Then, all ACLs granted through group membership */ | |
357 | $result += self::getGroupACLs($contact_id, TRUE); | |
358 | ||
359 | return $result; | |
360 | } | |
361 | ||
28518c90 | 362 | /** |
c490a46a | 363 | * @param array $params |
28518c90 EM |
364 | * |
365 | * @return CRM_ACL_DAO_ACL | |
366 | */ | |
03149bb2 | 367 | public static function create($params) { |
6a488035 TO |
368 | $dao = new CRM_ACL_DAO_ACL(); |
369 | $dao->copyValues($params); | |
370 | $dao->save(); | |
1fe97a01 | 371 | return $dao; |
6a488035 TO |
372 | } |
373 | ||
28518c90 | 374 | /** |
c490a46a | 375 | * @param array $params |
03149bb2 | 376 | * @param array $defaults |
28518c90 | 377 | */ |
00be9182 | 378 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
379 | CRM_Core_DAO::commonRetrieve('CRM_ACL_DAO_ACL', $params, $defaults); |
380 | } | |
381 | ||
382 | /** | |
fe482240 | 383 | * Update the is_active flag in the db. |
6a488035 | 384 | * |
b758c7d5 TO |
385 | * @param int $id |
386 | * Id of the database record. | |
387 | * @param bool $is_active | |
388 | * Value we want to set the is_active field. | |
6a488035 | 389 | * |
8a4fede3 | 390 | * @return bool |
391 | * true if we found and updated the object, else false | |
6a488035 | 392 | */ |
00be9182 | 393 | public static function setIsActive($id, $is_active) { |
9cdf85c1 | 394 | Civi::cache('fields')->flush(); |
5e601882 SL |
395 | // reset ACL and system caches. |
396 | CRM_Core_BAO_Cache::resetCaches(); | |
6a488035 TO |
397 | |
398 | return CRM_Core_DAO::setFieldValue('CRM_ACL_DAO_ACL', $id, 'is_active', $is_active); | |
399 | } | |
400 | ||
28518c90 EM |
401 | /** |
402 | * @param $str | |
100fef9d | 403 | * @param int $contactID |
28518c90 EM |
404 | * |
405 | * @return bool | |
406 | */ | |
00be9182 | 407 | public static function check($str, $contactID) { |
6a488035 TO |
408 | |
409 | $acls = CRM_ACL_BAO_Cache::build($contactID); | |
410 | ||
411 | $aclKeys = array_keys($acls); | |
412 | $aclKeys = implode(',', $aclKeys); | |
413 | ||
414 | if (empty($aclKeys)) { | |
415 | return FALSE; | |
416 | } | |
417 | ||
6a488035 TO |
418 | $query = " |
419 | SELECT count( a.id ) | |
420 | FROM civicrm_acl_cache c, civicrm_acl a | |
421 | WHERE c.acl_id = a.id | |
422 | AND a.is_active = 1 | |
423 | AND a.object_table = %1 | |
424 | AND a.id IN ( $aclKeys ) | |
425 | "; | |
cf0d1c08 | 426 | $params = [1 => [$str, 'String']]; |
6a488035 TO |
427 | |
428 | $count = CRM_Core_DAO::singleValueQuery($query, $params); | |
429 | return ($count) ? TRUE : FALSE; | |
430 | } | |
431 | ||
28518c90 EM |
432 | /** |
433 | * @param $type | |
434 | * @param $tables | |
435 | * @param $whereTables | |
100fef9d | 436 | * @param int $contactID |
28518c90 EM |
437 | * |
438 | * @return null|string | |
439 | */ | |
199761b4 | 440 | public static function whereClause($type, &$tables, &$whereTables, $contactID = NULL) { |
6a488035 | 441 | $acls = CRM_ACL_BAO_Cache::build($contactID); |
6a488035 TO |
442 | |
443 | $whereClause = NULL; | |
cf0d1c08 | 444 | $clauses = []; |
6a488035 TO |
445 | |
446 | if (!empty($acls)) { | |
447 | $aclKeys = array_keys($acls); | |
448 | $aclKeys = implode(',', $aclKeys); | |
449 | ||
450 | $query = " | |
451 | SELECT a.operation, a.object_id | |
452 | FROM civicrm_acl_cache c, civicrm_acl a | |
453 | WHERE c.acl_id = a.id | |
454 | AND a.is_active = 1 | |
455 | AND a.object_table = 'civicrm_saved_search' | |
456 | AND a.id IN ( $aclKeys ) | |
457 | ORDER BY a.object_id | |
458 | "; | |
459 | ||
460 | $dao = CRM_Core_DAO::executeQuery($query); | |
461 | ||
462 | // do an or of all the where clauses u see | |
cf0d1c08 | 463 | $ids = []; |
6a488035 TO |
464 | while ($dao->fetch()) { |
465 | // make sure operation matches the type TODO | |
466 | if (self::matchType($type, $dao->operation)) { | |
467 | if (!$dao->object_id) { | |
cf0d1c08 | 468 | $ids = []; |
6a488035 TO |
469 | $whereClause = ' ( 1 ) '; |
470 | break; | |
471 | } | |
472 | $ids[] = $dao->object_id; | |
473 | } | |
474 | } | |
475 | ||
476 | if (!empty($ids)) { | |
477 | $ids = implode(',', $ids); | |
478 | $query = " | |
479 | SELECT g.* | |
480 | FROM civicrm_group g | |
481 | WHERE g.id IN ( $ids ) | |
482 | AND g.is_active = 1 | |
483 | "; | |
353ffa53 | 484 | $dao = CRM_Core_DAO::executeQuery($query); |
1bcdee33 | 485 | $groupIDs = []; |
486 | $groupContactCacheClause = FALSE; | |
6a488035 | 487 | while ($dao->fetch()) { |
1bcdee33 | 488 | $groupIDs[] = $dao->id; |
6a488035 | 489 | |
1d902030 | 490 | if (($dao->saved_search_id || $dao->children || $dao->parents)) { |
491 | if ($dao->cache_date == NULL) { | |
492 | CRM_Contact_BAO_GroupContactCache::load($dao); | |
493 | } | |
1bcdee33 | 494 | $groupContactCacheClause = " UNION SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id IN (" . implode(', ', $groupIDs) . ")"; |
6a488035 | 495 | } |
6a488035 | 496 | |
6a488035 TO |
497 | } |
498 | ||
1bcdee33 | 499 | if ($groupIDs) { |
500 | $clauses[] = "( | |
501 | `contact_a`.id IN ( | |
502 | SELECT contact_id FROM civicrm_group_contact WHERE group_id IN (" . implode(', ', $groupIDs) . ") AND status = 'Added' | |
503 | $groupContactCacheClause | |
504 | ) | |
505 | )"; | |
6a488035 TO |
506 | } |
507 | } | |
508 | } | |
509 | ||
510 | if (!empty($clauses)) { | |
511 | $whereClause = ' ( ' . implode(' OR ', $clauses) . ' ) '; | |
512 | } | |
513 | ||
514 | // call the hook to get additional whereClauses | |
515 | CRM_Utils_Hook::aclWhereClause($type, $tables, $whereTables, $contactID, $whereClause); | |
516 | ||
199761b4 | 517 | if (empty($whereClause)) { |
6a488035 TO |
518 | $whereClause = ' ( 0 ) '; |
519 | } | |
520 | ||
521 | return $whereClause; | |
522 | } | |
523 | ||
28518c90 | 524 | /** |
c490a46a | 525 | * @param int $type |
100fef9d | 526 | * @param int $contactID |
28518c90 EM |
527 | * @param string $tableName |
528 | * @param null $allGroups | |
529 | * @param null $includedGroups | |
530 | * | |
531 | * @return array | |
532 | */ | |
e6a83034 TO |
533 | public static function group( |
534 | $type, | |
100b0ec6 TO |
535 | $contactID = NULL, |
536 | $tableName = 'civicrm_saved_search', | |
537 | $allGroups = NULL, | |
6a488035 TO |
538 | $includedGroups = NULL |
539 | ) { | |
e3ad0182 | 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'])) { | |
cf0d1c08 | 542 | Civi::$statics[__CLASS__]['permissioned_groups'] = []; |
e3ad0182 | 543 | } |
544 | if (!empty(Civi::$statics[__CLASS__]['permissioned_groups'][$userCacheKey])) { | |
545 | return Civi::$statics[__CLASS__]['permissioned_groups'][$userCacheKey]; | |
546 | } | |
6a488035 | 547 | |
8aace6af | 548 | if ($allGroups == NULL) { |
887688b9 | 549 | $allGroups = CRM_Contact_BAO_Contact::buildOptions('group_id', NULL, ['onlyActive' => FALSE]); |
8aace6af JG |
550 | } |
551 | ||
6a488035 TO |
552 | $acls = CRM_ACL_BAO_Cache::build($contactID); |
553 | ||
cf0d1c08 | 554 | $ids = []; |
6a488035 TO |
555 | if (!empty($acls)) { |
556 | $aclKeys = array_keys($acls); | |
557 | $aclKeys = implode(',', $aclKeys); | |
558 | ||
77f080cb | 559 | $cacheKey = CRM_Utils_Cache::cleanKey("$tableName-$aclKeys"); |
557f8c17 ARW |
560 | $cache = CRM_Utils_Cache::singleton(); |
561 | $ids = $cache->get($cacheKey); | |
562 | if (!$ids) { | |
cf0d1c08 | 563 | $ids = []; |
557f8c17 | 564 | $query = " |
6a488035 TO |
565 | SELECT a.operation, a.object_id |
566 | FROM civicrm_acl_cache c, civicrm_acl a | |
567 | WHERE c.acl_id = a.id | |
568 | AND a.is_active = 1 | |
569 | AND a.object_table = %1 | |
570 | AND a.id IN ( $aclKeys ) | |
571 | GROUP BY a.operation,a.object_id | |
572 | ORDER BY a.object_id | |
573 | "; | |
cf0d1c08 | 574 | $params = [1 => [$tableName, 'String']]; |
557f8c17 ARW |
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; | |
580 | } | |
6a488035 | 581 | } |
557f8c17 ARW |
582 | else { |
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) { | |
587 | $ids[] = $id; | |
588 | } | |
6a488035 | 589 | } |
557f8c17 | 590 | break; |
6a488035 | 591 | } |
6a488035 | 592 | } |
557f8c17 | 593 | $cache->set($cacheKey, $ids); |
6a488035 TO |
594 | } |
595 | } | |
596 | ||
b1f4e637 RN |
597 | if (empty($ids) && !empty($includedGroups) && |
598 | is_array($includedGroups) | |
599 | ) { | |
600 | $ids = $includedGroups; | |
601 | } | |
e3ad0182 | 602 | if ($contactID) { |
54d93c06 | 603 | $groupWhere = ''; |
604 | if (!empty($allGroups)) { | |
605 | $groupWhere = " AND id IN (" . implode(',', array_keys($allGroups)) . ")"; | |
606 | } | |
e3ad0182 | 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 | |
54d93c06 | 610 | $groupWhere |
e3ad0182 | 611 | "); |
612 | if ($ownHiddenGroupsList) { | |
613 | $ownHiddenGroups = explode(',', $ownHiddenGroupsList); | |
54d93c06 | 614 | $ids = array_merge((array) $ids, $ownHiddenGroups); |
e3ad0182 | 615 | } |
616 | ||
617 | } | |
b1f4e637 | 618 | |
6a488035 | 619 | CRM_Utils_Hook::aclGroup($type, $contactID, $tableName, $allGroups, $ids); |
e3ad0182 | 620 | Civi::$statics[__CLASS__]['permissioned_groups'][$userCacheKey] = $ids; |
6a488035 TO |
621 | return $ids; |
622 | } | |
623 | ||
28518c90 | 624 | /** |
c490a46a | 625 | * @param int $type |
28518c90 EM |
626 | * @param $operation |
627 | * | |
628 | * @return bool | |
629 | */ | |
9bd90abd | 630 | protected static function matchType($type, $operation) { |
6a488035 TO |
631 | $typeCheck = FALSE; |
632 | switch ($operation) { | |
633 | case 'All': | |
634 | $typeCheck = TRUE; | |
635 | break; | |
636 | ||
637 | case 'View': | |
638 | if ($type == CRM_ACL_API::VIEW) { | |
639 | $typeCheck = TRUE; | |
640 | } | |
641 | break; | |
642 | ||
643 | case 'Edit': | |
644 | if ($type == CRM_ACL_API::VIEW || $type == CRM_ACL_API::EDIT) { | |
645 | $typeCheck = TRUE; | |
646 | } | |
647 | break; | |
648 | ||
649 | case 'Create': | |
650 | if ($type == CRM_ACL_API::CREATE) { | |
651 | $typeCheck = TRUE; | |
652 | } | |
653 | break; | |
654 | ||
655 | case 'Delete': | |
656 | if ($type == CRM_ACL_API::DELETE) { | |
657 | $typeCheck = TRUE; | |
658 | } | |
659 | break; | |
660 | ||
661 | case 'Search': | |
662 | if ($type == CRM_ACL_API::SEARCH) { | |
663 | $typeCheck = TRUE; | |
664 | } | |
665 | break; | |
666 | } | |
667 | return $typeCheck; | |
668 | } | |
669 | ||
670 | /** | |
d2e5d2ce | 671 | * Delete ACL records. |
6a488035 | 672 | * |
b758c7d5 TO |
673 | * @param int $aclId |
674 | * ID of the ACL record to be deleted. | |
6a488035 | 675 | * |
6a488035 | 676 | */ |
00be9182 | 677 | public static function del($aclId) { |
6a488035 TO |
678 | // delete all entries from the acl cache |
679 | CRM_ACL_BAO_Cache::resetCache(); | |
680 | ||
681 | $acl = new CRM_ACL_DAO_ACL(); | |
682 | $acl->id = $aclId; | |
683 | $acl->delete(); | |
684 | } | |
96025800 | 685 | |
6a488035 | 686 | } |