Skip to content
Snippets Groups Projects
Unverified Commit 83db8c00 authored by colemanw's avatar colemanw Committed by GitHub
Browse files

Merge pull request #20769 from eileenmcnaughton/acl_test

#2115 Move financial acl code out of v3 Contribution create
parents eb9d477c 0552d667
No related branches found
No related tags found
No related merge requests found
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
* @package CiviCRM_APIv3 * @package CiviCRM_APIv3
*/ */
use Civi\Api4\Contribution;
/** /**
* Add or update a Contribution. * Add or update a Contribution.
* *
...@@ -40,18 +42,14 @@ function civicrm_api3_contribution_create($params) { ...@@ -40,18 +42,14 @@ function civicrm_api3_contribution_create($params) {
} }
$params['skipCleanMoney'] = TRUE; $params['skipCleanMoney'] = TRUE;
if (!empty($params['check_permissions']) && CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()) { if (!empty($params['check_permissions'])) {
if (empty($params['id'])) { // Check acls on this entity. Note that we pass in financial type id, if we have it
$op = CRM_Core_Action::ADD; // since we know this is checked by acls. In v4 we do something more generic.
} if (!Contribution::checkAccess()
else { ->setAction(empty($params['id']) ? 'create' : 'update')
if (empty($params['financial_type_id'])) { ->addValue('id', $params['id'] ?? NULL)
$params['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $params['id'], 'financial_type_id'); ->addValue('financial_type_id', $params['financial_type_id'] ?? NULL)
} ->execute()->first()['access']) {
$op = CRM_Core_Action::UPDATE;
}
CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($types, $op);
if (!array_key_exists($params['financial_type_id'], $types)) {
throw new API_Exception('You do not have permission to create this contribution'); throw new API_Exception('You do not have permission to create this contribution');
} }
} }
......
...@@ -305,19 +305,41 @@ function _financialacls_civi_api4_authorizeContribution(\Civi\Api4\Event\Authori ...@@ -305,19 +305,41 @@ function _financialacls_civi_api4_authorizeContribution(\Civi\Api4\Event\Authori
if (!financialacls_is_acl_limiting_enabled()) { if (!financialacls_is_acl_limiting_enabled()) {
return; return;
} }
if ($e->getActionName() === 'delete' && $e->getEntityName() === 'Contribution') { if ($e->getEntityName() === 'Contribution') {
$contributionID = $e->getRecord()['id']; $contributionID = $e->getRecord()['id'] ?? NULL;
// First check contribution financial type $financialTypeID = $e->getRecord()['financial_type_id'] ?? CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionID, 'financial_type_id');
$financialType = CRM_Core_PseudoConstant::getName('CRM_Contribute_DAO_Contribution', 'financial_type_id', CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionID, 'financial_type_id')); if (!CRM_Core_Permission::check(_financialacls_getRequiredPermission($financialTypeID, $e->getActionName()), $e->getUserID())) {
// Now check permissioned line items & permissioned contribution
if (!CRM_Core_Permission::check('delete contributions of type ' . $financialType, $e->getUserID()) ||
!CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($contributionID, 'delete', FALSE, $e->getUserID())
) {
$e->setAuthorized(FALSE); $e->setAuthorized(FALSE);
} }
if ($e->getActionName() === 'delete') {
// First check contribution financial type
// Now check permissioned line items & permissioned contribution
if (!CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($contributionID, 'delete', FALSE, $e->getUserID())
) {
$e->setAuthorized(FALSE);
}
}
} }
} }
/**
* Get the permission required to perform this action on this financial type.
*
* @param int $financialTypeID
* @param string $action
*
* @return string
*/
function _financialacls_getRequiredPermission(int $financialTypeID, string $action): string {
$financialType = CRM_Core_PseudoConstant::getName('CRM_Contribute_DAO_Contribution', 'financial_type_id', $financialTypeID);
$actionMap = [
'create' => 'add',
'update' => 'edit',
'delete' => 'delete',
];
return $actionMap[$action] . ' contributions of type ' . $financialType;
}
/** /**
* Remove unpermitted financial types from field Options in search context. * Remove unpermitted financial types from field Options in search context.
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment