Skip to content

Commit

Permalink
Merge pull request #22495 from eileenmcnaughton/fin2
Browse files Browse the repository at this point in the history
Move permission checks from Query & BAO to financialacl extension
  • Loading branch information
colemanw authored Jan 14, 2022
2 parents be0c852 + 00d50ac commit 31f306c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 72 deletions.
6 changes: 5 additions & 1 deletion CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,11 @@ public function initialize($apiEntity = NULL) {
}
if (isset($component) && !$this->_skipPermission) {
// Unit test coverage in api_v3_FinancialTypeACLTest::testGetACLContribution.
CRM_Financial_BAO_FinancialType::buildPermissionedClause($this->_whereClause, $component);
$clauses = CRM_Financial_BAO_FinancialType::buildPermissionedClause($component);
if (!empty($this->_whereClause) && !empty($clauses)) {
$this->_whereClause .= ' AND ';
}
$this->_whereClause .= $clauses;
}

$this->_fromClause = self::fromClause($this->_tables, NULL, NULL, $this->_primaryLocation, $this->_mode, $apiEntity);
Expand Down
29 changes: 1 addition & 28 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -1157,34 +1157,7 @@ protected static function disconnectPledgePaymentsIfCancelled(int $pledgePayment
}

/**
* @inheritDoc
*/
public function addSelectWhereClause() {
$whereClauses = parent::addSelectWhereClause();
if ($whereClauses !== []) {
// In this case permisssions have been applied & we assume the
// financialaclreport is applying these
// https://github.com/JMAConsulting/biz.jmaconsulting.financialaclreport/blob/master/financialaclreport.php#L107
return $whereClauses;
}

if (!CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()) {
return $whereClauses;
}
$types = CRM_Financial_BAO_FinancialType::getAllEnabledAvailableFinancialTypes();
if (empty($types)) {
$whereClauses['financial_type_id'] = 'IN (0)';
}
else {
$whereClauses['financial_type_id'] = [
'IN (' . implode(',', array_keys($types)) . ')',
];
}
return $whereClauses;
}

/**
* @param null $status
* @param string $status
* @param null $startDate
* @param null $endDate
*
Expand Down
33 changes: 14 additions & 19 deletions CRM/Financial/BAO/FinancialType.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,35 +343,30 @@ public static function addACLClausesToWhereClauses(&$whereClauses) {
/**
* Function to build a permissioned sql where clause based on available financial types.
*
* @param array $whereClauses
* (reference ) an array of clauses
* @param string $component
* the type of component
* @param string $alias
* the alias to use
*
* @return string $clauses
*/
public static function buildPermissionedClause(&$whereClauses, $component = NULL, $alias = NULL) {
public static function buildPermissionedClause(string $component): string {
$clauses = [];
// @todo the relevant addSelectWhere clause should be called.
if (!self::isACLFinancialTypeStatus()) {
return FALSE;
return '';
}
if ($component == 'contribution') {
$types = self::getAllEnabledAvailableFinancialTypes();
$column = "financial_type_id";
if ($component === 'contribution') {
$clauses = CRM_Contribute_BAO_Contribution::getSelectWhereClause();
}
if ($component == 'membership') {
if ($component === 'membership') {
self::getAvailableMembershipTypes($types, CRM_Core_Action::VIEW);
$column = "membership_type_id";
}
if (!empty($whereClauses)) {
$whereClauses .= ' AND ';
}
if (empty($types)) {
$whereClauses .= " civicrm_{$component}.{$column} IN (0)";
return;
$types = array_keys($types);
if (empty($types)) {
$types = [0];
}
$clauses[] = ' civicrm_membership.membership_type_id IN (' . implode(',', $types) . ')';

}
$whereClauses .= " civicrm_{$component}.{$column} IN (" . implode(',', array_keys($types)) . ")";
return implode(' AND ', $clauses);
}

/**
Expand Down
1 change: 1 addition & 0 deletions ext/financialacls/financialacls.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ function financialacls_civicrm_selectWhereClause($entity, &$clauses) {
case 'LineItem':
case 'MembershipType':
case 'ContributionRecur':
case 'Contribution':
$clauses['financial_type_id'] = _financialacls_civicrm_get_type_clause();
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class FinancialTypeTest extends BaseTestClass {
/**
* Test that a message is put in session when changing the name of a
* financial type.
*
* @throws \CRM_Core_Exception
*/
public function testChangeFinancialTypeName(): void {
Civi::settings()->set('acl_financial_type', TRUE);
Expand Down Expand Up @@ -73,4 +71,25 @@ public function testGetIncomeFinancialType(): void {
$this->assertEquals([1 => 'Donation'], $type);
}

/**
* Check method test buildPermissionedClause()
*/
public function testBuildPermissionedClause(): void {
Civi::settings()->set('acl_financial_type', 1);
$this->setPermissions([
'view contributions of type Donation',
'view contributions of type Member Dues',
]);
$whereClause = \CRM_Financial_BAO_FinancialType::buildPermissionedClause('contribution');
$this->assertEquals('(`civicrm_contribution`.`financial_type_id` IS NULL OR (`civicrm_contribution`.`financial_type_id` IN (1,2)))', $whereClause);
$this->setPermissions([
'view contributions of type Donation',
'view contributions of type Member Dues',
'view contributions of type Event Fee',
]);

$whereClause = \CRM_Financial_BAO_FinancialType::buildPermissionedClause('contribution');
$this->assertEquals('(`civicrm_contribution`.`financial_type_id` IS NULL OR (`civicrm_contribution`.`financial_type_id` IN (1,4,2)))', $whereClause);
}

}
22 changes: 0 additions & 22 deletions tests/phpunit/CRM/Financial/BAO/FinancialTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,26 +302,4 @@ public function testCheckPermissionedLineItems() {
$this->assertEquals($perm, TRUE, 'Verify that lineitems now have permission.');
}

/**
* Check method testisACLFinancialTypeStatus()
*/
public function testBuildPermissionedClause() {
$this->setACL();
$this->setPermissions([
'view contributions of type Donation',
'view contributions of type Member Dues',
]);
CRM_Financial_BAO_FinancialType::buildPermissionedClause($whereClause, 'contribution');
$this->assertEquals($whereClause, ' civicrm_contribution.financial_type_id IN (1,2)');
$this->setPermissions([
'view contributions of type Donation',
'view contributions of type Member Dues',
'view contributions of type Event Fee',
]);
$whereClause = NULL;

CRM_Financial_BAO_FinancialType::buildPermissionedClause($whereClause, 'contribution');
$this->assertEquals($whereClause, ' civicrm_contribution.financial_type_id IN (1,4,2)');
}

}

0 comments on commit 31f306c

Please sign in to comment.