3 require_once 'financialacls.civix.php';
5 use CRM_Financialacls_ExtensionUtil
as E
;
9 * Implements hook_civicrm_config().
11 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config/
13 function financialacls_civicrm_config(&$config) {
14 _financialacls_civix_civicrm_config($config);
18 * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
20 function financialacls_civicrm_container($container) {
21 $dispatcherDefn = $container->getDefinition('dispatcher');
22 $container->addResource(new \Symfony\Component\Config\
Resource\
FileResource(__FILE__
));
23 $dispatcherDefn->addMethodCall('addListener', ['civi.api4.authorizeRecord::Contribution', '_financialacls_civi_api4_authorizeContribution']);
27 * Implements hook_civicrm_xmlMenu().
29 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_xmlMenu
31 function financialacls_civicrm_xmlMenu(&$files) {
32 _financialacls_civix_civicrm_xmlMenu($files);
36 * Implements hook_civicrm_install().
38 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install
40 function financialacls_civicrm_install() {
41 _financialacls_civix_civicrm_install();
45 * Implements hook_civicrm_postInstall().
47 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postInstall
49 function financialacls_civicrm_postInstall() {
50 _financialacls_civix_civicrm_postInstall();
54 * Implements hook_civicrm_uninstall().
56 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_uninstall
58 function financialacls_civicrm_uninstall() {
59 _financialacls_civix_civicrm_uninstall();
63 * Implements hook_civicrm_enable().
65 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable
67 function financialacls_civicrm_enable() {
68 _financialacls_civix_civicrm_enable();
72 * Implements hook_civicrm_disable().
74 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_disable
76 function financialacls_civicrm_disable() {
77 _financialacls_civix_civicrm_disable();
81 * Implements hook_civicrm_upgrade().
83 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_upgrade
85 function financialacls_civicrm_upgrade($op, CRM_Queue_Queue
$queue = NULL) {
86 return _financialacls_civix_civicrm_upgrade($op, $queue);
90 * Implements hook_civicrm_managed().
92 * Generate a list of entities to create/deactivate/delete when this module
93 * is installed, disabled, uninstalled.
95 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_managed
97 function financialacls_civicrm_managed(&$entities) {
98 _financialacls_civix_civicrm_managed($entities);
102 * Implements hook_civicrm_caseTypes().
104 * Generate a list of case-types.
106 * Note: This hook only runs in CiviCRM 4.4+.
108 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_caseTypes
110 function financialacls_civicrm_caseTypes(&$caseTypes) {
111 _financialacls_civix_civicrm_caseTypes($caseTypes);
115 * Implements hook_civicrm_angularModules().
117 * Generate a list of Angular modules.
119 * Note: This hook only runs in CiviCRM 4.5+. It may
120 * use features only available in v4.6+.
122 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_angularModules
124 function financialacls_civicrm_angularModules(&$angularModules) {
125 _financialacls_civix_civicrm_angularModules($angularModules);
129 * Implements hook_civicrm_alterSettingsFolders().
131 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_alterSettingsFolders
133 function financialacls_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
134 _financialacls_civix_civicrm_alterSettingsFolders($metaDataFolders);
138 * Implements hook_civicrm_entityTypes().
140 * Declare entity types provided by this module.
142 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_entityTypes
144 function financialacls_civicrm_entityTypes(&$entityTypes) {
145 _financialacls_civix_civicrm_entityTypes($entityTypes);
149 * Implements hook_civicrm_thems().
151 function financialacls_civicrm_themes(&$themes) {
152 _financialacls_civix_civicrm_themes($themes);
156 * Intervene to prevent deletion, where permissions block it.
159 * @param string $objectName
160 * @param int|null $id
161 * @param array $params
163 * @throws \API_Exception
164 * @throws \CRM_Core_Exception
166 function financialacls_civicrm_pre($op, $objectName, $id, &$params) {
167 if (!financialacls_is_acl_limiting_enabled()) {
170 if ($objectName === 'LineItem' && !empty($params['check_permissions'])) {
171 $operationMap = ['delete' => CRM_Core_Action
::DELETE
, 'edit' => CRM_Core_Action
::UPDATE
, 'create' => CRM_Core_Action
::ADD
];
172 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($types, $operationMap[$op]);
173 if (empty($params['financial_type_id'])) {
174 $params['financial_type_id'] = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_LineItem', $params['id'], 'financial_type_id');
176 if (!array_key_exists($params['financial_type_id'], $types)) {
177 throw new API_Exception('You do not have permission to ' . $op . ' this line item');
180 if ($objectName === 'FinancialType' && !empty($params['id']) && !empty($params['name'])) {
181 $prevName = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialType', $params['id']);
182 if ($prevName !== $params['name']) {
183 CRM_Core_Session
::setStatus(ts("Changing the name of a Financial Type will result in losing the current permissions associated with that Financial Type.
184 Before making this change you should likely note the existing permissions at Administer > Users and Permissions > Permissions (Access Control),
185 then clicking the Access Control link for your Content Management System, then noting down the permissions for 'CiviCRM: {financial type name} view', etc.
186 Then after making the change of name, reset the permissions to the way they were."), ts('Warning'), 'warning');
192 * Implements hook_civicrm_selectWhereClause().
194 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_selectWhereClause
196 function financialacls_civicrm_selectWhereClause($entity, &$clauses) {
197 if (!financialacls_is_acl_limiting_enabled()) {
203 case 'MembershipType':
204 case 'ContributionRecur':
206 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($types);
208 $clauses['financial_type_id'] = 'IN (' . implode(',', array_keys($types)) . ')';
211 $clauses['financial_type_id'] = '= 0';
220 * Remove unpermitted options.
222 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_buildAmount
224 * @param string $component
225 * @param \CRM_Core_Form $form
226 * @param array $feeBlock
228 function financialacls_civicrm_buildAmount($component, $form, &$feeBlock) {
229 if (!financialacls_is_acl_limiting_enabled()) {
233 foreach ($feeBlock as $key => $value) {
234 foreach ($value['options'] as $k => $options) {
235 if (!CRM_Core_Permission
::check('add contributions of type ' . CRM_Contribute_PseudoConstant
::financialType($options['financial_type_id']))) {
236 unset($feeBlock[$key]['options'][$k]);
239 if (empty($feeBlock[$key]['options'])) {
240 unset($feeBlock[$key]);
246 * Remove unpermitted membership types from selection availability..
248 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_membershipTypeValues
250 * @param \CRM_Core_Form $form
251 * @param array $membershipTypeValues
253 function financialacls_civicrm_membershipTypeValues($form, &$membershipTypeValues) {
254 if (!financialacls_is_acl_limiting_enabled()) {
257 $financialTypes = NULL;
258 $financialTypes = CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes, CRM_Core_Action
::ADD
);
259 foreach ($membershipTypeValues as $id => $type) {
260 if (!isset($financialTypes[$type['financial_type_id']])) {
261 unset($membershipTypeValues[$id]);
269 * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_permission/
271 * @param array $permissions
273 function financialacls_civicrm_permission(&$permissions) {
274 if (!financialacls_is_acl_limiting_enabled()) {
279 'view' => ts('view'),
280 'edit' => ts('edit'),
281 'delete' => ts('delete'),
283 $financialTypes = \CRM_Contribute_BAO_Contribution
::buildOptions('financial_type_id', 'validate');
284 foreach ($financialTypes as $id => $type) {
285 foreach ($actions as $action => $action_ts) {
286 $permissions[$action . ' contributions of type ' . $type] = [
287 ts("CiviCRM: %1 contributions of type %2", [1 => $action_ts, 2 => $type]),
288 ts('%1 contributions of type %2', [1 => $action_ts, 2 => $type]),
292 $permissions['administer CiviCRM Financial Types'] = [
293 ts('CiviCRM: administer CiviCRM Financial Types'),
294 ts('Administer access to Financial Types'),
299 * Listener for 'civi.api4.authorizeRecord::Contribution'
301 * @param \Civi\Api4\Event\AuthorizeRecordEvent $e
302 * @throws \CRM_Core_Exception
304 function _financialacls_civi_api4_authorizeContribution(\Civi\Api4\Event\AuthorizeRecordEvent
$e) {
305 if (!financialacls_is_acl_limiting_enabled()) {
308 if ($e->getEntityName() === 'Contribution') {
309 $contributionID = $e->getRecord()['id'] ??
NULL;
310 $financialTypeID = $e->getRecord()['financial_type_id'] ?? CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionID, 'financial_type_id');
311 if (!CRM_Core_Permission
::check(_financialacls_getRequiredPermission($financialTypeID, $e->getActionName()), $e->getUserID())) {
312 $e->setAuthorized(FALSE);
314 if ($e->getActionName() === 'delete') {
315 // First check contribution financial type
316 // Now check permissioned line items & permissioned contribution
317 if (!CRM_Financial_BAO_FinancialType
::checkPermissionedLineItems($contributionID, 'delete', FALSE, $e->getUserID())
319 $e->setAuthorized(FALSE);
326 * Get the permission required to perform this action on this financial type.
328 * @param int $financialTypeID
329 * @param string $action
333 function _financialacls_getRequiredPermission(int $financialTypeID, string $action): string {
334 $financialType = CRM_Core_PseudoConstant
::getName('CRM_Contribute_DAO_Contribution', 'financial_type_id', $financialTypeID);
338 'delete' => 'delete',
340 return $actionMap[$action] . ' contributions of type ' . $financialType;
344 * Remove unpermitted financial types from field Options in search context.
346 * Search context is described as
347 * 'search' => "search: searchable options are returned; labels are translated.",
348 * So this is appropriate to removing the options from search screens.
350 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_fieldOptions
352 * @param string $entity
353 * @param string $field
354 * @param array $options
355 * @param array $params
357 function financialacls_civicrm_fieldOptions($entity, $field, &$options, $params) {
358 if (!financialacls_is_acl_limiting_enabled()) {
361 if (in_array($entity, ['Contribution', 'ContributionRecur'], TRUE) && $field === 'financial_type_id' && $params['context'] === 'search') {
362 $action = CRM_Core_Action
::VIEW
;
363 // At this stage we are only considering the view action. Code from
364 // CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes().
366 CRM_Core_Action
::VIEW
=> 'view',
367 CRM_Core_Action
::UPDATE
=> 'edit',
368 CRM_Core_Action
::ADD
=> 'add',
369 CRM_Core_Action
::DELETE
=> 'delete',
371 $cacheKey = 'available_types_' . $action;
372 if (!isset(\Civi
::$statics['CRM_Financial_BAO_FinancialType'][$cacheKey])) {
373 foreach ($options as $finTypeId => $type) {
374 if (!CRM_Core_Permission
::check($actions[$action] . ' contributions of type ' . $type)) {
375 unset($options[$finTypeId]);
378 \Civi
::$statics['CRM_Financial_BAO_FinancialType'][$cacheKey] = $options;
380 $options = \Civi
::$statics['CRM_Financial_BAO_FinancialType'][$cacheKey];
385 * Is financial acl limiting enabled.
387 * Once this extension is detangled enough to be optional this will go
388 * and the status of the extension rather than the setting will dictate.
392 function financialacls_is_acl_limiting_enabled(): bool {
393 return (bool) Civi
::settings()->get('acl_financial_type');
396 // --- Functions below this ship commented out. Uncomment as required. ---
399 * Implements hook_civicrm_preProcess().
401 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_preProcess
403 //function financialacls_civicrm_preProcess($formName, &$form) {
408 * Implements hook_civicrm_navigationMenu().
410 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_navigationMenu
412 //function financialacls_civicrm_navigationMenu(&$menu) {
413 // _financialacls_civix_insert_navigation_menu($menu, 'Mailings', array(
414 // 'label' => E::ts('New subliminal message'),
415 // 'name' => 'mailing_subliminal_message',
416 // 'url' => 'civicrm/mailing/subliminal',
417 // 'permission' => 'access CiviMail',
418 // 'operator' => 'OR',
421 // _financialacls_civix_navigationMenu($menu);