Add userId and conditions to hook_civicrm_selectWhereClause signature
authorcolemanw <coleman@civicrm.org>
Mon, 18 Sep 2023 23:08:19 +0000 (19:08 -0400)
committercolemanw <coleman@civicrm.org>
Wed, 20 Sep 2023 11:30:24 +0000 (07:30 -0400)
18 files changed:
CRM/Activity/BAO/Activity.php
CRM/Case/BAO/Case.php
CRM/Case/BAO/CaseContact.php
CRM/Contact/BAO/Contact.php
CRM/Contact/BAO/Group.php
CRM/Contact/BAO/RelationshipCache.php
CRM/Contribute/BAO/ContributionSoft.php
CRM/Core/BAO/CustomValue.php
CRM/Core/BAO/File.php
CRM/Core/BAO/Note.php
CRM/Core/BAO/UFJoin.php
CRM/Core/BAO/UFMatch.php
CRM/Core/BAO/UserJob.php
CRM/Core/DAO.php
CRM/Price/BAO/LineItem.php
CRM/Queue/BAO/Queue.php
CRM/Utils/Hook.php
ext/oauth-client/CRM/OAuth/BAO/OAuthContactToken.php

index 821034f9a5e421aa895e9df9abf011f3e457d2c0..5d74041ed6fedefa1a36499adbc6571c88caa947 100644 (file)
@@ -802,10 +802,11 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
 
   /**
    * @param string|null $entityName
+   * @param int|null $userId
    * @param array $conditions
    * @inheritDoc
    */
-  public function addSelectWhereClause(string $entityName = NULL, array $conditions = []): array {
+  public function addSelectWhereClause(string $entityName = NULL, int $userId = NULL, array $conditions = []): array {
     $clauses = [];
     $permittedActivityTypeIDs = self::getPermittedActivityTypes();
     if (!empty($conditions['activity_type_id'])) {
@@ -827,7 +828,7 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
       $contactClause = implode(' AND contact_id ', $contactClause);
       $clauses['id'][] = "IN (SELECT activity_id FROM civicrm_activity_contact WHERE contact_id $contactClause)";
     }
-    CRM_Utils_Hook::selectWhereClause($this, $clauses);
+    CRM_Utils_Hook::selectWhereClause($this, $clauses, $userId, $conditions);
     return $clauses;
   }
 
index 8ffae031abeb63fa73fdbb73abc74a2f1ec5a9ff..d8aa69d2e82b2ae0fa1b66ee8a006cf154d490e7 100644 (file)
@@ -3014,10 +3014,11 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')';
 
   /**
    * @param string|null $entityName
+   * @param int|null $userId
    * @param array $conditions
    * @inheritDoc
    */
-  public function addSelectWhereClause(string $entityName = NULL, array $conditions = []): array {
+  public function addSelectWhereClause(string $entityName = NULL, int $userId = NULL, array $conditions = []): array {
     // We always return an array with these keys, even if they are empty,
     // because this tells the query builder that we have considered these fields for acls
     $clauses = [
@@ -3041,7 +3042,7 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')';
         )
       )";
     }
-    CRM_Utils_Hook::selectWhereClause($this, $clauses);
+    CRM_Utils_Hook::selectWhereClause($this, $clauses, $userId, $conditions);
     return $clauses;
   }
 
index 5cdd8aa6d35a5958fcb1300252452fc2eb5de201..ba3cccd9ed34aa237b8e8630ad0719b4ec7d9fc1 100644 (file)
@@ -72,10 +72,11 @@ class CRM_Case_BAO_CaseContact extends CRM_Case_DAO_CaseContact implements \Civi
 
   /**
    * @param string|null $entityName
+   * @param int|null $userId
    * @param array $conditions
    * @inheritDoc
    */
-  public function addSelectWhereClause(string $entityName = NULL, array $conditions = []): array {
+  public function addSelectWhereClause(string $entityName = NULL, int $userId = NULL, array $conditions = []): array {
     return [
       // Reuse case acls
       'case_id' => CRM_Utils_SQL::mergeSubquery('Case'),
index 7fcef7ca859c97f9cb2f86a3b3a8c862d0333385..b9b83e95d81992b0a0dbac22b12fb2978609c747 100644 (file)
@@ -3410,17 +3410,18 @@ LEFT JOIN civicrm_address ON ( civicrm_address.contact_id = civicrm_contact.id )
 
   /**
    * @param string|null $entityName
+   * @param int|null $userId
    * @param array $conditions
    * @inheritDoc
    */
-  public function addSelectWhereClause(string $entityName = NULL, array $conditions = []): array {
+  public function addSelectWhereClause(string $entityName = NULL, int $userId = NULL, array $conditions = []): array {
     // We always return an array with these keys, even if they are empty,
     // because this tells the query builder that we have considered these fields for acls
     $clauses = [
       'id' => (array) CRM_Contact_BAO_Contact_Permission::cacheSubquery(),
       'is_deleted' => CRM_Core_Permission::check('access deleted contacts') ? [] : ['!= 1'],
     ];
-    CRM_Utils_Hook::selectWhereClause($this, $clauses);
+    CRM_Utils_Hook::selectWhereClause($this, $clauses, $userId, $conditions);
     return $clauses;
   }
 
index cf25ab4c65a451addfdddf13a469a84c786718d0..3a5b8b7b8196ce234697bd21f31efe2d99001c63 100644 (file)
@@ -311,17 +311,18 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
 
   /**
    * @param string|null $entityName
+   * @param int|null $userId
    * @param array $conditions
    * @inheritDoc
    */
-  public function addSelectWhereClause(string $entityName = NULL, array $conditions = []): array {
+  public function addSelectWhereClause(string $entityName = NULL, int $userId = NULL, array $conditions = []): array {
     $clauses = [];
     if (!CRM_Core_Permission::check([['edit all contacts', 'view all contacts']])) {
       $allowedGroups = CRM_Core_Permission::group(NULL, FALSE);
       $groupsIn = $allowedGroups ? implode(',', array_keys($allowedGroups)) : '0';
       $clauses['id'][] = "IN ($groupsIn)";
     }
-    CRM_Utils_Hook::selectWhereClause($this, $clauses);
+    CRM_Utils_Hook::selectWhereClause($this, $clauses, $userId, $conditions);
     return $clauses;
   }
 
index 37bace2c9d088b4cd7530e3acafed2fb436b8bed..cc3e2d89a57752221920e9daa9a9b93c6f585663 100644 (file)
@@ -157,17 +157,18 @@ class CRM_Contact_BAO_RelationshipCache extends CRM_Contact_DAO_RelationshipCach
 
   /**
    * @param string|null $entityName
+   * @param int|null $userId
    * @param array $conditions
    * @return array
    */
-  public function addSelectWhereClause(string $entityName = NULL, array $conditions = []): array {
+  public function addSelectWhereClause(string $entityName = NULL, int $userId = NULL, array $conditions = []): array {
     // Permission for this entity depends on access to the two related contacts.
     $contactClause = CRM_Utils_SQL::mergeSubquery('Contact');
     $clauses = [
       'near_contact_id' => $contactClause,
       'far_contact_id' => $contactClause,
     ];
-    CRM_Utils_Hook::selectWhereClause($this, $clauses);
+    CRM_Utils_Hook::selectWhereClause($this, $clauses, $userId, $conditions);
     return $clauses;
   }
 
index d5203ea5b0092f610754cdaa0107ac12e1cae073..797d3dce9bf58178dd8a939b5a6c77a73461bd04 100644 (file)
@@ -647,12 +647,13 @@ class CRM_Contribute_BAO_ContributionSoft extends CRM_Contribute_DAO_Contributio
 
   /**
    * @param string|null $entityName
+   * @param int|null $userId
    * @param array $conditions
    * @inheritDoc
    */
-  public function addSelectWhereClause(string $entityName = NULL, array $conditions = []): array {
+  public function addSelectWhereClause(string $entityName = NULL, int $userId = NULL, array $conditions = []): array {
     $clauses['contribution_id'] = CRM_Utils_SQL::mergeSubquery('Contribution');
-    CRM_Utils_Hook::selectWhereClause($this, $clauses);
+    CRM_Utils_Hook::selectWhereClause($this, $clauses, $userId, $conditions);
     return $clauses;
   }
 
index c225665baac7a6d02812d746986de44f962720ba..f801e1296fb80f42624d168249b8931ca54efe01 100644 (file)
@@ -213,10 +213,11 @@ class CRM_Core_BAO_CustomValue extends CRM_Core_DAO {
   /**
    * ACL clause for an APIv4 custom pseudo-entity (aka multi-record custom group extending Contact).
    * @param string|null $entityName
+   * @param int|null $userId
    * @param array $conditions
    * @return array
    */
-  public function addSelectWhereClause(string $entityName = NULL, array $conditions = []): array {
+  public function addSelectWhereClause(string $entityName = NULL, int $userId = NULL, array $conditions = []): array {
     // To-date, custom-value-based entities are only supported for contacts.
     // If this changes, $entityName variable contains the name of this custom group,
     // and could be used to lookup the type of entity this custom group joins to.
index 555f15d00c054c0053dc06a6c4b2d2c40830f2e0..d0ad6492a389ba4c02c59e798672e849cb907455 100644 (file)
@@ -836,25 +836,26 @@ HEREDOC;
 
   /**
    * @param string|null $entityName
+   * @param int|null $userId
    * @param array $conditions
    * @inheritDoc
    */
-  public function addSelectWhereClause(string $entityName = NULL, array $conditions = []): array {
+  public function addSelectWhereClause(string $entityName = NULL, int $userId = NULL, array $conditions = []): array {
     // TODO: This seemded like a good idea... piggybacking off the ACL clause of EntityFile
     // however that's too restrictive because entityFile ACLs are limited to just attachments,
     // so this would prevent access to other file fields (e.g. custom fields)
     // Disabling this function for now by calling the parent instead.
-    return parent::addSelectWhereClause($entityName, $conditions);
-    $clauses = [
-      'id' => [],
-    ];
-    // File ACLs are driven by the EntityFile table
-    $entityFileClause = CRM_Core_DAO_EntityFile::getDynamicFkAclClauses();
-    if ($entityFileClause) {
-      $clauses['id'] = 'IN (SELECT file_id FROM `civicrm_entity_file` WHERE (' . implode(') OR (', $entityFileClause) . '))';
-    }
-    CRM_Utils_Hook::selectWhereClause($this, $clauses);
-    return $clauses;
+    return parent::addSelectWhereClause('File', $userId, $conditions);
+    //  $clauses = [
+    //    'id' => [],
+    //  ];
+    //  // File ACLs are driven by the EntityFile table
+    //  $entityFileClause = CRM_Core_DAO_EntityFile::getDynamicFkAclClauses();
+    //  if ($entityFileClause) {
+    //    $clauses['id'] = 'IN (SELECT file_id FROM `civicrm_entity_file` WHERE (' . implode(') OR (', $entityFileClause) . '))';
+    //  }
+    //  CRM_Utils_Hook::selectWhereClause($this, $clauses, $userId, $conditions);
+    //  return $clauses;
   }
 
   /**
index ffd6e3246c2e7797c4e9a6f3ac7d3936ee54a443..46e7a9052c6f0144853f4bbef84e5f0c950499ed 100644 (file)
@@ -529,7 +529,7 @@ WHERE participant.contact_id = %1 AND  note.entity_table = 'civicrm_participant'
     }
   }
 
-  public function addSelectWhereClause(string $entityName = NULL, array $conditions = []): array {
+  public function addSelectWhereClause(string $entityName = NULL, int $userId = NULL, array $conditions = []): array {
     $clauses = [];
     $relatedClauses = self::getDynamicFkAclClauses('entity_table', 'entity_id', $conditions['entity_table'] ?? NULL);
     if ($relatedClauses) {
@@ -546,7 +546,7 @@ WHERE participant.contact_id = %1 AND  note.entity_table = 'civicrm_participant'
         ],
       ];
     }
-    CRM_Utils_Hook::selectWhereClause($this, $clauses);
+    CRM_Utils_Hook::selectWhereClause($this, $clauses, $userId, $conditions);
     return $clauses;
   }
 
index 422e6d2e451a9279bf510ea65d75066ecfd0136b..b9344ea232693a8c84dc112aff8e533fafa24283 100644 (file)
@@ -179,12 +179,13 @@ class CRM_Core_BAO_UFJoin extends CRM_Core_DAO_UFJoin {
    * Override base method which assumes permissions should be based on entity_table.
    *
    * @param string|null $entityName
+   * @param int|null $userId
    * @param array $conditions
    * @return array
    */
-  public function addSelectWhereClause(string $entityName = NULL, array $conditions = []): array {
+  public function addSelectWhereClause(string $entityName = NULL, int $userId = NULL, array $conditions = []): array {
     $clauses = [];
-    CRM_Utils_Hook::selectWhereClause($this, $clauses);
+    CRM_Utils_Hook::selectWhereClause($this, $clauses, $userId, $conditions);
     return $clauses;
   }
 
index 3cffbc226060d448cff8c416a5d936e47a016cbf..5a2406b5c17365a3c45e5ff24bc6176d8ab120a2 100644 (file)
@@ -617,13 +617,14 @@ AND    domain_id    = %4
 
   /**
    * @param string|null $entityName
+   * @param int|null $userId
    * @param array $conditions
    * @inheritDoc
    */
-  public function addSelectWhereClause(string $entityName = NULL, array $conditions = []): array {
+  public function addSelectWhereClause(string $entityName = NULL, int $userId = NULL, array $conditions = []): array {
     // Prevent default behavior of joining ACLs onto the contact_id field.
     $clauses = [];
-    CRM_Utils_Hook::selectWhereClause($this, $clauses);
+    CRM_Utils_Hook::selectWhereClause($this, $clauses, $userId, $conditions);
     return $clauses;
   }
 
index 7e99bf6f75cea52bdffa1d702857b883a50c2060..a6ba558f8391f30e250b85d0afb29a9a695ee83f 100644 (file)
@@ -148,17 +148,18 @@ class CRM_Core_BAO_UserJob extends CRM_Core_DAO_UserJob implements HookInterface
    * 'view all contacts' etc.
    *
    * @param string|null $entityName
+   * @param int|null $userId
    * @param array $conditions
    * @inheritDoc
    */
-  public function addSelectWhereClause(string $entityName = NULL, array $conditions = []): array {
+  public function addSelectWhereClause(string $entityName = NULL, int $userId = NULL, array $conditions = []): array {
     $clauses = [];
     if (!\CRM_Core_Permission::check('administer queues')) {
       // @todo - the is_template should really be prefixed. We need to add support
       // for that in the compiler & then this would be `{table}.is_template`
       $clauses['created_id'] = '= ' . (int) CRM_Core_Session::getLoggedInContactID() . ' OR is_template = 1';
     }
-    CRM_Utils_Hook::selectWhereClause($this, $clauses);
+    CRM_Utils_Hook::selectWhereClause($this, $clauses, $userId, $conditions);
     return $clauses;
   }
 
index 289ede21b5fa6795aa79a1e4ac8bade4887c0950..300cc120864a58ea425585c4e184bbcda2ff1c11 100644 (file)
@@ -3076,13 +3076,17 @@ SELECT contact_id
    *   Name of the entity being queried (for normal BAO files implementing this method, this variable is redundant
    *   as there is a 1-1 relationship between most entities and most BAOs. However the variable is passed in to support
    *   dynamic entities such as ECK).
+   * @param int|null $userId
+   *   Contact id of the current user.
+   *   This param is more aspirational than functional for now. Someday the API may support checking permissions
+   *   for contacts other than the current user, but at present this is always NULL which defaults to the current user.
    * @param array $conditions
    *   Contains field/value pairs gleaned from the WHERE clause or ON clause
    *   (depending on how the entity was added to the query).
    *   Can be used for optimization/deduping of clauses.
    * @return array
    */
-  public function addSelectWhereClause(string $entityName = NULL, array $conditions = []): array {
+  public function addSelectWhereClause(string $entityName = NULL, int $userId = NULL, array $conditions = []): array {
     $clauses = [];
     $fields = $this::getSupportedFields();
     foreach ($fields as $fieldName => $field) {
@@ -3153,8 +3157,12 @@ SELECT contact_id
    *
    * With acls from related entities + additional clauses from hook_civicrm_selectWhereClause
    *
-   * @param string $tableAlias
-   * @param string $entityName
+   * DO NOT OVERRIDE THIS FUNCTION
+   *
+   * @TODO: ADD `final` keyword to function signature
+   *
+   * @param string|null $tableAlias
+   * @param string|null $entityName
    * @param array $conditions
    *   Values from WHERE or ON clause
    * @return array
@@ -3165,7 +3173,7 @@ SELECT contact_id
     $entityName = $entityName ?? CRM_Core_DAO_AllCoreTables::getBriefName($bao::class);
     $finalClauses = [];
     $fields = static::getSupportedFields();
-    $selectWhereClauses = $bao->addSelectWhereClause($entityName, $conditions);
+    $selectWhereClauses = $bao->addSelectWhereClause($entityName, NULL, $conditions);
     foreach ($selectWhereClauses as $fieldName => $fieldClauses) {
       $finalClauses[$fieldName] = NULL;
       if ($fieldClauses) {
index fbc37be9fa0f16b726c930ec0d43a75e4a551efa..4d4069054a5e7cf66bd3392a76cc5c33a8ca7e87 100644 (file)
@@ -1280,12 +1280,13 @@ WHERE li.contribution_id = %1";
    * and membership tables just seem too non-performant.
    *
    * @param string|null $entityName
+   * @param int|null $userId
    * @param array $conditions
    * @inheritDoc
    */
-  public function addSelectWhereClause(string $entityName = NULL, array $conditions = []): array {
+  public function addSelectWhereClause(string $entityName = NULL, int $userId = NULL, array $conditions = []): array {
     $clauses['contribution_id'] = CRM_Utils_SQL::mergeSubquery('Contribution');
-    CRM_Utils_Hook::selectWhereClause($this, $clauses);
+    CRM_Utils_Hook::selectWhereClause($this, $clauses, $userId, $conditions);
     return $clauses;
   }
 
index af942b9a90595dc5b6ab0b70e54755d8423757f4..d608767ceb6c4925e5d6058b01b1544801c0a2ae 100644 (file)
  */
 class CRM_Queue_BAO_Queue extends CRM_Queue_DAO_Queue implements \Civi\Core\HookInterface {
 
-  public function addSelectWhereClause(string $entityName = NULL, array $conditions = []): array {
+  public function addSelectWhereClause(string $entityName = NULL, int $userId = NULL, array $conditions = []): array {
     $clauses = [];
     if (!\CRM_Core_Permission::check('administer queues')) {
       $cid = (int) CRM_Core_Session::getLoggedInContactID();
       $clauses['id'] = "IN (SELECT queue_id FROM `civicrm_user_job` WHERE created_id = $cid)";
     }
-    CRM_Utils_Hook::selectWhereClause($this, $clauses);
+    CRM_Utils_Hook::selectWhereClause($this, $clauses, $userId, $conditions);
     return $clauses;
   }
 
index 4d5703a5463f968071ffe1b5791001035b93463e..f6f96425d6cf04ab12b584cad8776b8563498fed 100644 (file)
@@ -627,13 +627,18 @@ abstract class CRM_Utils_Hook {
   /**
    * @param string|CRM_Core_DAO $entity
    * @param array $clauses
-   * @return mixed
+   * @param int|null $userId
+   *   User contact id. NULL == current user.
+   * @param array $conditions
+   *   Values from WHERE or ON clause
    */
-  public static function selectWhereClause($entity, &$clauses) {
+  public static function selectWhereClause($entity, array &$clauses, int $userId = NULL, array $conditions = []): void {
     $entityName = is_object($entity) ? CRM_Core_DAO_AllCoreTables::getBriefName($entity::class) : $entity;
     $null = NULL;
-    return self::singleton()->invoke(['entity', 'clauses'], $entityName, $clauses,
-      $null, $null, $null, $null,
+    $userId = $userId ?? (int) CRM_Core_Session::getLoggedInContactID();
+    self::singleton()->invoke(['entity', 'clauses', 'userId', 'conditions'],
+      $entityName, $clauses, $userId, $conditions,
+      $null, $null,
       'civicrm_selectWhereClause'
     );
   }
index e9f633ea7468966affaae1498d45eeeea8ac7c18..46401c6d1f7471315bfc0f502d02b6e78bd57a7a 100644 (file)
@@ -98,10 +98,11 @@ class CRM_OAuth_BAO_OAuthContactToken extends CRM_OAuth_DAO_OAuthContactToken {
 
   /**
    * @param string|null $entityName
+   * @param int|null $userId
    * @param array $conditions
    * @inheritDoc
    */
-  public function addSelectWhereClause(string $entityName = NULL, array $conditions = []): array {
+  public function addSelectWhereClause(string $entityName = NULL, int $userId = NULL, array $conditions = []): array {
     $clauses = [];
     $loggedInContactID = CRM_Core_Session::getLoggedInContactID();
 
@@ -117,7 +118,7 @@ class CRM_OAuth_BAO_OAuthContactToken extends CRM_OAuth_DAO_OAuthContactToken {
     else {
       $clauses['contact_id'][] = "= -1";
     }
-    CRM_Utils_Hook::selectWhereClause($this, $clauses);
+    CRM_Utils_Hook::selectWhereClause($this, $clauses, $userId, $conditions);
     return $clauses;
   }