Move filtering of unpermitted options for reports/ search select to financial acl...
authoreileen <emcnaughton@wikimedia.org>
Fri, 23 Oct 2020 05:36:14 +0000 (18:36 +1300)
committereileen <emcnaughton@wikimedia.org>
Tue, 27 Oct 2020 22:59:50 +0000 (11:59 +1300)
13 files changed:
CRM/Contribute/BAO/Query.php
CRM/Financial/BAO/FinancialType.php
CRM/Report/Form/Contribute/Bookkeeping.php
CRM/Report/Form/Contribute/Detail.php
CRM/Report/Form/Contribute/Lybunt.php
CRM/Report/Form/Contribute/Recur.php
CRM/Report/Form/Contribute/Repeat.php
CRM/Report/Form/Contribute/SoftCredit.php
CRM/Report/Form/Contribute/Summary.php
CRM/Report/Form/Contribute/Sybunt.php
CRM/Report/Form/Contribute/TopDonor.php
ext/financialacls/financialacls.php
ext/financialacls/tests/phpunit/Civi/Financialacls/OptionsTest.php [new file with mode: 0644]

index f91512a0eaa3131945c6f6ec63456238e380f4fe..5255b6d36ebf783df925a43dda4c2fa5e6b36f82 100644 (file)
@@ -931,9 +931,8 @@ class CRM_Contribute_BAO_Query extends CRM_Core_BAO_Query {
     );
 
     // CRM-13848
-    CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, CRM_Core_Action::VIEW);
     $form->addSelect('financial_type_id',
-      ['entity' => 'contribution', 'multiple' => 'multiple', 'context' => 'search', 'options' => $financialTypes]
+      ['entity' => 'contribution', 'multiple' => 'multiple', 'context' => 'search', 'options' => CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'search')]
     );
 
     // use contribution_payment_instrument_id instead of payment_instrument_id
index dd4d85dbe627bb167bef09b5ce73974b63a2741b..841952fba751fa8ce72ae95541677adf053dc195 100644 (file)
@@ -267,6 +267,12 @@ class CRM_Financial_BAO_FinancialType extends CRM_Financial_DAO_FinancialType {
   /**
    * Get available Financial Types.
    *
+   * This logic is being moved into the financialacls extension.
+   *
+   * Rather than call this function consider using
+   *
+   * $types = \CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'search');
+   *
    * @param array $financialTypes
    *   (reference ) an array of financial types
    * @param int|string $action
index 06f89ab8c959ab10f3b892059bb996e391281fd2..c0bc36eacb0a6ff69dd1739fc000f1cd9b348f6f 100644 (file)
@@ -245,7 +245,7 @@ class CRM_Report_Form_Contribute_Bookkeeping extends CRM_Report_Form {
             'title' => ts('Financial Type'),
             'type' => CRM_Utils_Type::T_INT,
             'operatorType' => CRM_Report_Form::OP_MULTISELECT,
-            'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(),
+            'options' => CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'search'),
           ],
         ],
         'order_bys' => [
index 14b5facbd9cbc964a44b6de8ae196972034162ce..4576423e11211e3af0d2a70665637e3c2ca32d3b 100644 (file)
@@ -205,7 +205,7 @@ class CRM_Report_Form_Contribute_Detail extends CRM_Report_Form {
             'financial_type_id' => [
               'title' => ts('Financial Type'),
               'operatorType' => CRM_Report_Form::OP_MULTISELECT,
-              'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(),
+              'options' => CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'search'),
               'type' => CRM_Utils_Type::T_INT,
             ],
             'contribution_page_id' => [
index 101717609d06fdfb51fea784f4f5df79d3162ad4..97c0d8f9d32120a153a236997b7f484622e4c060 100644 (file)
@@ -205,7 +205,7 @@ class CRM_Report_Form_Contribute_Lybunt extends CRM_Report_Form {
             'title' => ts('Financial Type'),
             'type' => CRM_Utils_Type::T_INT,
             'operatorType' => CRM_Report_Form::OP_MULTISELECT,
-            'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(),
+            'options' => CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'search'),
           ],
           'contribution_status_id' => [
             'title' => ts('Contribution Status'),
index 6f0109d2845e0b1d43b4f5470b5b1ed69b4a87ce..c61fb4b76d7d8aa43b94c146e29ca8abc9a88cdc 100644 (file)
@@ -185,7 +185,7 @@ class CRM_Report_Form_Contribute_Recur extends CRM_Report_Form {
           'financial_type_id' => [
             'title' => ts('Financial Type'),
             'operatorType' => CRM_Report_Form::OP_MULTISELECT,
-            'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(),
+            'options' => CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'search'),
             'type' => CRM_Utils_Type::T_INT,
           ],
           'frequency_unit' => [
index d5044a08e7224be21d81fbe12d8a178cc712fb59..18bfd109bd0eb29a7c85581fb2fbbaf5f7ba5365 100644 (file)
@@ -220,7 +220,7 @@ class CRM_Report_Form_Contribute_Repeat extends CRM_Report_Form {
             'title' => ts('Financial Type'),
             'type' => CRM_Utils_Type::T_INT,
             'operatorType' => CRM_Report_Form::OP_MULTISELECT,
-            'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(),
+            'options' => CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'search'),
           ),
           'contribution_status_id' => array(
             'title' => ts('Contribution Status'),
index 8370e7420319e0a7a4e0e014024f62a8447a0803..4d4277f6b6f7e29b16db0e22d55a1bbd902932d7 100644 (file)
@@ -206,7 +206,7 @@ class CRM_Report_Form_Contribute_SoftCredit extends CRM_Report_Form {
             'title' => ts('Financial Type'),
             'type' => CRM_Utils_Type::T_INT,
             'operatorType' => CRM_Report_Form::OP_MULTISELECT,
-            'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(),
+            'options' => CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'search'),
           ],
         ],
         'grouping' => 'softcredit-fields',
index b7c40f5a199f7c4d43e885766dc2b516660016bd..c73da90e77d44734ea3c68d345b9dd1b0045b326 100644 (file)
@@ -171,7 +171,7 @@ class CRM_Report_Form_Contribute_Summary extends CRM_Report_Form {
           'financial_type_id' => [
             'title' => ts('Financial Type'),
             'operatorType' => CRM_Report_Form::OP_MULTISELECT,
-            'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(),
+            'options' => CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'search'),
             'type' => CRM_Utils_Type::T_INT,
           ],
           'contribution_page_id' => [
index 1b45af360eec4a46d86f50d1f4f72f64716f032a..dc6b31898af1f79233c50746c613254238880968 100644 (file)
@@ -209,7 +209,7 @@ class CRM_Report_Form_Contribute_Sybunt extends CRM_Report_Form {
             'title' => ts('Financial Type'),
             'type' => CRM_Utils_Type::T_INT,
             'operatorType' => CRM_Report_Form::OP_MULTISELECT,
-            'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(),
+            'options' => CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'search'),
           ],
           'contribution_status_id' => [
             'title' => ts('Contribution Status'),
index 83d903950e66f7f694567f1cfa0a26a9c5c9dd18..7b6c14f51fb520903d3681c7357f874fabd775a2 100644 (file)
@@ -135,7 +135,7 @@ class CRM_Report_Form_Contribute_TopDonor extends CRM_Report_Form {
             'title' => ts('Financial Type'),
             'type' => CRM_Utils_Type::T_INT,
             'operatorType' => CRM_Report_Form::OP_MULTISELECT,
-            'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(),
+            'options' => CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'search'),
           ],
           'contribution_status_id' => [
             'title' => ts('Contribution Status'),
index 0aa087dc79fbdb81d745be82c6aae7a49567d7f5..c039ef1998f005ea1017a53063f79f69c24af753 100644 (file)
@@ -232,6 +232,44 @@ function financialacls_civicrm_membershipTypeValues($form, &$membershipTypeValue
   }
 }
 
+/**
+ * Remove unpermitted financial types from field Options in search context.
+ *
+ * Search context is described as
+ * 'search' => "search: searchable options are returned; labels are translated.",
+ * So this is appropriate to removing the options from search screens.
+ *
+ * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_fieldOptions
+ *
+ * @param string $entity
+ * @param string $field
+ * @param array $options
+ * @param array $params
+ */
+function financialacls_civicrm_fieldOptions($entity, $field, &$options, $params) {
+  if ($entity === 'Contribution' && $field === 'financial_type_id' && $params['context'] === 'search') {
+    $action = CRM_Core_Action::VIEW;
+    // At this stage we are only considering the view action. Code from
+    // CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes().
+    $actions = [
+      CRM_Core_Action::VIEW => 'view',
+      CRM_Core_Action::UPDATE => 'edit',
+      CRM_Core_Action::ADD => 'add',
+      CRM_Core_Action::DELETE => 'delete',
+    ];
+    $cacheKey = 'available_types_' . $action;
+    if (!isset(\Civi::$statics['CRM_Financial_BAO_FinancialType'][$cacheKey])) {
+      foreach ($options as $finTypeId => $type) {
+        if (!CRM_Core_Permission::check($actions[$action] . ' contributions of type ' . $type)) {
+          unset($options[$finTypeId]);
+        }
+      }
+      \Civi::$statics['CRM_Financial_BAO_FinancialType'][$cacheKey] = $options;
+    }
+    $options = \Civi::$statics['CRM_Financial_BAO_FinancialType'][$cacheKey];
+  }
+}
+
 // --- Functions below this ship commented out. Uncomment as required. ---
 
 /**
diff --git a/ext/financialacls/tests/phpunit/Civi/Financialacls/OptionsTest.php b/ext/financialacls/tests/phpunit/Civi/Financialacls/OptionsTest.php
new file mode 100644 (file)
index 0000000..1f9ab60
--- /dev/null
@@ -0,0 +1,24 @@
+<?php
+
+namespace Civi\Financialacls;
+
+// I fought the Autoloader and the autoloader won.
+require_once 'BaseTestClass.php';
+
+/**
+ * @group headless
+ */
+class OptionsTest extends BaseTestClass {
+
+  /**
+   * Test buildMembershipTypes.
+   */
+  public function testBuildOptions() {
+    $this->setupLoggedInUserWithLimitedFinancialTypeAccess();
+    $options = \CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes();
+    $this->assertEquals(['Donation'], array_merge($options));
+    $builtOptions = \CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'search');
+    $this->assertEquals(['Donation'], array_merge($builtOptions));
+  }
+
+}