From e89fb391fb284c766ec6ab022de592331aae639f Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 23 Oct 2020 18:36:14 +1300 Subject: [PATCH] Move filtering of unpermitted options for reports/ search select to financial acl extension --- CRM/Contribute/BAO/Query.php | 3 +- CRM/Financial/BAO/FinancialType.php | 6 +++ CRM/Report/Form/Contribute/Bookkeeping.php | 2 +- CRM/Report/Form/Contribute/Detail.php | 2 +- CRM/Report/Form/Contribute/Lybunt.php | 2 +- CRM/Report/Form/Contribute/Recur.php | 2 +- CRM/Report/Form/Contribute/Repeat.php | 2 +- CRM/Report/Form/Contribute/SoftCredit.php | 2 +- CRM/Report/Form/Contribute/Summary.php | 2 +- CRM/Report/Form/Contribute/Sybunt.php | 2 +- CRM/Report/Form/Contribute/TopDonor.php | 2 +- ext/financialacls/financialacls.php | 38 +++++++++++++++++++ .../Civi/Financialacls/OptionsTest.php | 26 +++++++++++++ 13 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 ext/financialacls/tests/phpunit/Civi/Financialacls/OptionsTest.php diff --git a/CRM/Contribute/BAO/Query.php b/CRM/Contribute/BAO/Query.php index f91512a0eaa3..5255b6d36ebf 100644 --- a/CRM/Contribute/BAO/Query.php +++ b/CRM/Contribute/BAO/Query.php @@ -931,9 +931,8 @@ public static function buildSearchForm(&$form) { ); // 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 diff --git a/CRM/Financial/BAO/FinancialType.php b/CRM/Financial/BAO/FinancialType.php index dd4d85dbe627..841952fba751 100644 --- a/CRM/Financial/BAO/FinancialType.php +++ b/CRM/Financial/BAO/FinancialType.php @@ -267,6 +267,12 @@ public static function getAllEnabledAvailableFinancialTypes($action = CRM_Core_A /** * 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 diff --git a/CRM/Report/Form/Contribute/Bookkeeping.php b/CRM/Report/Form/Contribute/Bookkeeping.php index 06f89ab8c959..c0bc36eacb0a 100644 --- a/CRM/Report/Form/Contribute/Bookkeeping.php +++ b/CRM/Report/Form/Contribute/Bookkeeping.php @@ -245,7 +245,7 @@ public function __construct() { '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' => [ diff --git a/CRM/Report/Form/Contribute/Detail.php b/CRM/Report/Form/Contribute/Detail.php index 14b5facbd9cb..4576423e1121 100644 --- a/CRM/Report/Form/Contribute/Detail.php +++ b/CRM/Report/Form/Contribute/Detail.php @@ -205,7 +205,7 @@ public function __construct() { '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' => [ diff --git a/CRM/Report/Form/Contribute/Lybunt.php b/CRM/Report/Form/Contribute/Lybunt.php index 101717609d06..97c0d8f9d321 100644 --- a/CRM/Report/Form/Contribute/Lybunt.php +++ b/CRM/Report/Form/Contribute/Lybunt.php @@ -205,7 +205,7 @@ public function __construct() { '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'), diff --git a/CRM/Report/Form/Contribute/Recur.php b/CRM/Report/Form/Contribute/Recur.php index 6f0109d2845e..c61fb4b76d7d 100644 --- a/CRM/Report/Form/Contribute/Recur.php +++ b/CRM/Report/Form/Contribute/Recur.php @@ -185,7 +185,7 @@ public function __construct() { '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' => [ diff --git a/CRM/Report/Form/Contribute/Repeat.php b/CRM/Report/Form/Contribute/Repeat.php index d5044a08e722..18bfd109bd0e 100644 --- a/CRM/Report/Form/Contribute/Repeat.php +++ b/CRM/Report/Form/Contribute/Repeat.php @@ -220,7 +220,7 @@ public function __construct() { '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'), diff --git a/CRM/Report/Form/Contribute/SoftCredit.php b/CRM/Report/Form/Contribute/SoftCredit.php index 8370e7420319..4d4277f6b6f7 100644 --- a/CRM/Report/Form/Contribute/SoftCredit.php +++ b/CRM/Report/Form/Contribute/SoftCredit.php @@ -206,7 +206,7 @@ public function __construct() { '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', diff --git a/CRM/Report/Form/Contribute/Summary.php b/CRM/Report/Form/Contribute/Summary.php index b7c40f5a199f..c73da90e77d4 100644 --- a/CRM/Report/Form/Contribute/Summary.php +++ b/CRM/Report/Form/Contribute/Summary.php @@ -171,7 +171,7 @@ public function __construct() { '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' => [ diff --git a/CRM/Report/Form/Contribute/Sybunt.php b/CRM/Report/Form/Contribute/Sybunt.php index 1b45af360eec..dc6b31898af1 100644 --- a/CRM/Report/Form/Contribute/Sybunt.php +++ b/CRM/Report/Form/Contribute/Sybunt.php @@ -209,7 +209,7 @@ public function __construct() { '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'), diff --git a/CRM/Report/Form/Contribute/TopDonor.php b/CRM/Report/Form/Contribute/TopDonor.php index 83d903950e66..7b6c14f51fb5 100644 --- a/CRM/Report/Form/Contribute/TopDonor.php +++ b/CRM/Report/Form/Contribute/TopDonor.php @@ -135,7 +135,7 @@ public function __construct() { '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'), diff --git a/ext/financialacls/financialacls.php b/ext/financialacls/financialacls.php index 0aa087dc79fb..c039ef1998f0 100644 --- a/ext/financialacls/financialacls.php +++ b/ext/financialacls/financialacls.php @@ -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 index 000000000000..faa39c791765 --- /dev/null +++ b/ext/financialacls/tests/phpunit/Civi/Financialacls/OptionsTest.php @@ -0,0 +1,26 @@ +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)); + } + +}