}
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);
/**
* 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') {
+ $types = array_keys(self::getAllEnabledAvailableFinancialTypes());
+ if (empty($types)) {
+ $types = [0];
+ }
+ $clauses[] = ' civicrm_contribution.financial_type_id IN (' . implode(',', $types) . ')';
}
- 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);
}
/**
/**
* 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);
$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 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 IN (1,4,2)', $whereClause);
+ }
+
}