Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Financial type hook clean up and fix towards dev/core#2454 Extend financial acls view limitations to ContributionR… #19788

Merged
merged 2 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion CRM/Core/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,6 @@ public static function assembleBasicPermissions($all = FALSE, $descriptions = FA
// Add any permissions defined in hook_civicrm_permission implementations.
$module_permissions = $config->userPermissionClass->getAllModulePermissions($descriptions);
$permissions = array_merge($permissions, $module_permissions);
CRM_Financial_BAO_FinancialType::permissionedFinancialTypes($permissions, $descriptions);
return $permissions;
}

Expand Down
1 change: 1 addition & 0 deletions CRM/Financial/BAO/FinancialType.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ public static function getIncomeFinancialType() {
* @return bool
*/
public static function permissionedFinancialTypes(&$permissions, $descriptions) {
CRM_Core_Error::deprecatedFunctionWarning('not done via hook.');
if (!self::isACLFinancialTypeStatus()) {
return FALSE;
}
Expand Down
35 changes: 34 additions & 1 deletion ext/financialacls/financialacls.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ function financialacls_civicrm_selectWhereClause($entity, &$clauses) {
switch ($entity) {
case 'LineItem':
case 'MembershipType':
case 'ContributionRecur':
$types = [];
CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($types);
if ($types) {
Expand Down Expand Up @@ -253,6 +254,38 @@ function financialacls_civicrm_membershipTypeValues($form, &$membershipTypeValue
}
}

/**
* Add permissions.
*
* @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_permission/
*
* @param array $permissions
*/
function financialacls_civicrm_permission(&$permissions) {
if (!financialacls_is_acl_limiting_enabled()) {
return;
}
$actions = [
'add' => ts('add'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eileenmcnaughton just a small thing this probably should be E::ts rather than ts given we are in an extension

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I guess so - it works like that for core ext?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it does.

'view' => ts('view'),
'edit' => ts('edit'),
'delete' => ts('delete'),
];
$financialTypes = \CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'validate');
foreach ($financialTypes as $id => $type) {
foreach ($actions as $action => $action_ts) {
$permissions[$action . ' contributions of type ' . $type] = [
ts("CiviCRM: %1 contributions of type %2", [1 => $action_ts, 2 => $type]),
ts('%1 contributions of type %2', [1 => $action_ts, 2 => $type]),
];
}
}
$permissions['administer CiviCRM Financial Types'] = [
ts('CiviCRM: administer CiviCRM Financial Types'),
ts('Administer access to Financial Types'),
];
}

/**
* Remove unpermitted financial types from field Options in search context.
*
Expand All @@ -271,7 +304,7 @@ function financialacls_civicrm_fieldOptions($entity, $field, &$options, $params)
if (!financialacls_is_acl_limiting_enabled()) {
return;
}
if ($entity === 'Contribution' && $field === 'financial_type_id' && $params['context'] === 'search') {
if (in_array($entity, ['Contribution', 'ContributionRecur'], TRUE) && $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().
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,34 @@ public function testChangeFinancialTypeName(): void {
$this->assertEquals('Changing the name', substr($status[0]['text'], 0, 17));
}

/**
* Check method testPermissionedFinancialTypes()
*/
public function testPermissionedFinancialTypes(): void {
Civi::settings()->set('acl_financial_type', TRUE);
$permissions = \CRM_Core_Permission::basicPermissions(FALSE, TRUE);
$actions = [
'add' => ts('add'),
'view' => ts('view'),
'edit' => ts('edit'),
'delete' => ts('delete'),
];
$financialTypes = \CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'validate');
foreach ($financialTypes as $id => $type) {
foreach ($actions as $action => $action_ts) {
$this->assertEquals(
[
ts("CiviCRM: %1 contributions of type %2", [1 => $action_ts, 2 => $type]),
ts('%1 contributions of type %2', [1 => $action_ts, 2 => $type]),
],
$permissions[$action . ' contributions of type ' . $type]
);
}
}
$this->assertEquals([
ts('CiviCRM: administer CiviCRM Financial Types'),
ts('Administer access to Financial Types'),
], $permissions['administer CiviCRM Financial Types']);
}

}
30 changes: 0 additions & 30 deletions tests/phpunit/CRM/Financial/BAO/FinancialTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,36 +216,6 @@ public function testgetAvailableMembershipTypes() {
$this->assertEquals($expectedResult, $types, 'Verify that removing permission for a financial type restricts the available membership types');
}

/**
* Check method testPermissionedFinancialTypes()
*/
public function testPermissionedFinancialTypes() {
// First get all core permissions
$permissions = $checkPerms = CRM_Core_Permission::getCorePermissions();
$this->setACL();
CRM_Financial_BAO_FinancialType::permissionedFinancialTypes($permissions, TRUE);
$financialTypes = CRM_Contribute_PseudoConstant::financialType();
$actions = [
'add' => ts('add'),
'view' => ts('view'),
'edit' => ts('edit'),
'delete' => ts('delete'),
];
foreach ($financialTypes as $id => $type) {
foreach ($actions as $action => $action_ts) {
$checkPerms[$action . ' contributions of type ' . $type] = [
ts("CiviCRM: %1 contributions of type %2", [1 => $action_ts, 2 => $type]),
ts('%1 contributions of type %2', [1 => $action_ts, 2 => $type]),
];
}
}
$checkPerms['administer CiviCRM Financial Types'] = [
ts('CiviCRM: administer CiviCRM Financial Types'),
ts('Administer access to Financial Types'),
];
$this->assertEquals($permissions, $checkPerms, 'Verify that permissions for each financial type have been added');
}

/**
* Check method testcheckPermissionedLineItems()
*
Expand Down