From a864caa95f2391e50501ad0d8c5e517ec5c6a33a Mon Sep 17 00:00:00 2001 From: Civiware Solutions Date: Fri, 29 Jun 2018 01:50:47 +0530 Subject: [PATCH] added code to restrict restrictions --- batchexportperm.php | 77 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/batchexportperm.php b/batchexportperm.php index f85d681..a656f67 100644 --- a/batchexportperm.php +++ b/batchexportperm.php @@ -154,7 +154,7 @@ function batchexportperm_civicrm_permission(&$permissions) { */ function batchexportperm_civicrm_links($op, $objectName, $objectId, &$links, &$mask, &$values) { if ($op == 'contribution.selector.row' && $objectName == 'Contribution') { - if (!CRM_Core_Permission::check('edit exported contributions')) { + if (!_batchexportperm_civicrm_allow_contribution_to_edit($objectId)) { foreach ($links as $key => $link) { if ($link['name'] == 'Edit') { unset($links[$key]); @@ -163,3 +163,78 @@ function batchexportperm_civicrm_links($op, $objectName, $objectId, &$links, &$m } } } + +/** + * Implements hook_civicrm_pre(). + * + * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_pre + */ +function batchexportperm_civicrm_pre($op, $objectName, $id, &$params) { + if ($op == 'edit' && $objectName == 'Contribution') { + _batchexportperm_civicrm_check_permission($id); + } +} + +/** + * Implements hook_civicrm_apiWrappers(). + * + * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_apiWrappers + */ +function batchexportperm_civicrm_apiWrappers(&$wrappers, $apiRequest) { + if ($apiRequest['entity'] == 'Contribution' && $apiRequest['action'] == 'create') { + $contibutionId = CRM_Utils_Array::value('id', $apiRequest['params']); + _batchexportperm_civicrm_check_permission($contibutionId, 'exception'); + } +} + +/** + * Implements hook_civicrm_preProcess(). + * + * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_preProcess + */ +function batchexportperm_civicrm_preProcess($formName, &$form) { + if ($formName == 'CRM_Contribute_Form_Contribution' && $form->_action & CRM_Core_Action::UPDATE) { + $contributionId = $form->_id; + _batchexportperm_civicrm_check_permission($contributionId); + } +} + +/** + * Check if user has permission to update contribution. + * + * @param int $contributionId + * @param string $errorType + */ +function _batchexportperm_civicrm_check_permission($contributionId, $errorType = NULL) { + if (empty($contributionId) || _batchexportperm_civicrm_allow_contribution_to_edit($contributionId)) { + return FALSE; + } + $message = ts('You do not have the necessary permission to edit this contribution.'); + if ($errorType == 'exception') { + throw new API_Exception($message); + } + else { + CRM_Core_Error::statusBounce($message); + } +} + +/** + * Check if user has permission to update contribution. + * + * @param int $contributionId + */ +function _batchexportperm_civicrm_allow_contribution_to_edit($contributionId) { + $customFieldId = civicrm_api3('CustomField', 'getvalue', [ + 'return' => "id", + 'custom_group_id' => "Batch_Details", + 'name' => "Export_Date", + ]); + $exportDate = civicrm_api3('Contribution', 'getvalue', [ + 'return' => "custom_{$customFieldId}", + 'id' => $contributionId, + ]); + if (empty($exportDate) || CRM_Core_Permission::check('edit exported contributions')) { + return TRUE; + } + return FALSE; +}