/**
* @param string $contactAlias
- * @param int $contactID
*
* @return array
*/
- public static function cacheClause($contactAlias = 'contact_a', $contactID = NULL) {
+ public static function cacheClause($contactAlias = 'contact_a') {
if (CRM_Core_Permission::check('view all contacts') ||
CRM_Core_Permission::check('edit all contacts')
) {
}
}
- $session = CRM_Core_Session::singleton();
- $contactID = $session->get('userID');
- if (!$contactID) {
- $contactID = 0;
- }
- $contactID = CRM_Utils_Type::escape($contactID, 'Integer');
-
+ $contactID = (int) CRM_Core_Session::getLoggedInContactID();
self::cache($contactID);
if (is_array($contactAlias) && !empty($contactAlias)) {
return array($fromClause, $whereClase);
}
+ /**
+ * Generate acl subquery that can be placed in the WHERE clause of a query or the ON clause of a JOIN
+ *
+ * @param string $contactIdField
+ * Full "table_name.field_name" for the field containing a contact id
+ * @return string
+ */
+ public static function cacheSubquery($contactIdField) {
+ $clauses = array();
+ if (!CRM_Core_Permission::check(array(array('view all contacts', 'edit all contacts')))) {
+ $contactID = (int) CRM_Core_Session::getLoggedInContactID();
+ self::cache($contactID);
+ $clauses[] = "$contactIdField IN (SELECT contact_id FROM civicrm_acl_contact_cache WHERE user_id = $contactID)";
+ }
+ if (!CRM_Core_Permission::check('access deleted contacts')) {
+ $clauses[] = "$contactIdField NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1)";
+ }
+ return $clauses ? implode(' AND ', $clauses) : '1';
+ }
+
/**
* Get the permission base on its relationship.
*
$this->query->limit($this->options['limit'], $this->options['offset']);
}
+ // ACLs
+ $this->addAclClause();
+
$result_entities = array();
$result_dao = \CRM_Core_DAO::executeQuery($this->query->toSQL());
return \Civi::service('civi_api_kernel')->runAuthorize($entity, 'get', $params);
}
+ /**
+ * If this entity has a `contact_id` field, add appropriate acl clause
+ */
+ private function addAclClause() {
+ if (in_array('contact_id', $this->entityFieldNames)) {
+ $clause = \CRM_Contact_BAO_Contact_Permission::cacheSubquery('a.contact_id');
+ if ($clause !== '1') {
+ $this->query->where($clause);
+ }
+ }
+ }
+
}