Add test for existing Auth Code flow behavior
[civicrm-core.git] / ext / financialacls / financialacls.php
CommitLineData
e7339d59 1<?php
2
3require_once 'financialacls.civix.php';
4// phpcs:disable
5use CRM_Financialacls_ExtensionUtil as E;
6// phpcs:enable
7
8/**
9 * Implements hook_civicrm_config().
10 *
11 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config/
12 */
13function financialacls_civicrm_config(&$config) {
14 _financialacls_civix_civicrm_config($config);
15}
16
17/**
18 * Implements hook_civicrm_xmlMenu().
19 *
20 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_xmlMenu
21 */
22function financialacls_civicrm_xmlMenu(&$files) {
23 _financialacls_civix_civicrm_xmlMenu($files);
24}
25
26/**
27 * Implements hook_civicrm_install().
28 *
29 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install
30 */
31function financialacls_civicrm_install() {
32 _financialacls_civix_civicrm_install();
33}
34
35/**
36 * Implements hook_civicrm_postInstall().
37 *
38 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postInstall
39 */
40function financialacls_civicrm_postInstall() {
41 _financialacls_civix_civicrm_postInstall();
42}
43
44/**
45 * Implements hook_civicrm_uninstall().
46 *
47 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_uninstall
48 */
49function financialacls_civicrm_uninstall() {
50 _financialacls_civix_civicrm_uninstall();
51}
52
53/**
54 * Implements hook_civicrm_enable().
55 *
56 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable
57 */
58function financialacls_civicrm_enable() {
59 _financialacls_civix_civicrm_enable();
60}
61
62/**
63 * Implements hook_civicrm_disable().
64 *
65 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_disable
66 */
67function financialacls_civicrm_disable() {
68 _financialacls_civix_civicrm_disable();
69}
70
71/**
72 * Implements hook_civicrm_upgrade().
73 *
74 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_upgrade
75 */
76function financialacls_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
77 return _financialacls_civix_civicrm_upgrade($op, $queue);
78}
79
80/**
81 * Implements hook_civicrm_managed().
82 *
83 * Generate a list of entities to create/deactivate/delete when this module
84 * is installed, disabled, uninstalled.
85 *
86 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_managed
87 */
88function financialacls_civicrm_managed(&$entities) {
89 _financialacls_civix_civicrm_managed($entities);
90}
91
92/**
93 * Implements hook_civicrm_caseTypes().
94 *
95 * Generate a list of case-types.
96 *
97 * Note: This hook only runs in CiviCRM 4.4+.
98 *
99 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_caseTypes
100 */
101function financialacls_civicrm_caseTypes(&$caseTypes) {
102 _financialacls_civix_civicrm_caseTypes($caseTypes);
103}
104
105/**
106 * Implements hook_civicrm_angularModules().
107 *
108 * Generate a list of Angular modules.
109 *
110 * Note: This hook only runs in CiviCRM 4.5+. It may
111 * use features only available in v4.6+.
112 *
113 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_angularModules
114 */
115function financialacls_civicrm_angularModules(&$angularModules) {
116 _financialacls_civix_civicrm_angularModules($angularModules);
117}
118
119/**
120 * Implements hook_civicrm_alterSettingsFolders().
121 *
122 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_alterSettingsFolders
123 */
124function financialacls_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
125 _financialacls_civix_civicrm_alterSettingsFolders($metaDataFolders);
126}
127
128/**
129 * Implements hook_civicrm_entityTypes().
130 *
131 * Declare entity types provided by this module.
132 *
133 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_entityTypes
134 */
135function financialacls_civicrm_entityTypes(&$entityTypes) {
136 _financialacls_civix_civicrm_entityTypes($entityTypes);
137}
138
139/**
140 * Implements hook_civicrm_thems().
141 */
142function financialacls_civicrm_themes(&$themes) {
143 _financialacls_civix_civicrm_themes($themes);
144}
145
b82fb202 146/**
147 * Intervene to prevent deletion, where permissions block it.
148 *
aeee327d 149 * @param string $op
b82fb202 150 * @param string $objectName
151 * @param int|null $id
152 * @param array $params
153 *
154 * @throws \API_Exception
155 * @throws \CRM_Core_Exception
156 */
157function financialacls_civicrm_pre($op, $objectName, $id, &$params) {
34509ae3 158 if (!financialacls_is_acl_limiting_enabled()) {
159 return;
160 }
aeee327d 161 if ($objectName === 'LineItem' && !empty($params['check_permissions'])) {
34509ae3 162 $operationMap = ['delete' => CRM_Core_Action::DELETE, 'edit' => CRM_Core_Action::UPDATE, 'create' => CRM_Core_Action::ADD];
163 CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($types, $operationMap[$op]);
164 if (empty($params['financial_type_id'])) {
165 $params['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_LineItem', $params['id'], 'financial_type_id');
166 }
167 if (!in_array($params['financial_type_id'], array_keys($types))) {
168 throw new API_Exception('You do not have permission to ' . $op . ' this line item');
b82fb202 169 }
170 }
d646594f 171 if ($objectName === 'FinancialType' && !empty($params['id']) && !empty($params['name'])) {
172 $prevName = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $params['id']);
173 if ($prevName !== $params['name']) {
174 CRM_Core_Session::setStatus(ts("Changing the name of a Financial Type will result in losing the current permissions associated with that Financial Type.
175 Before making this change you should likely note the existing permissions at Administer > Users and Permissions > Permissions (Access Control),
176 then clicking the Access Control link for your Content Management System, then noting down the permissions for 'CiviCRM: {financial type name} view', etc.
177 Then after making the change of name, reset the permissions to the way they were."), ts('Warning'), 'warning');
178 }
179 }
b82fb202 180}
181
28188a1e 182/**
183 * Implements hook_civicrm_selectWhereClause().
184 *
185 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_selectWhereClause
186 */
187function financialacls_civicrm_selectWhereClause($entity, &$clauses) {
34509ae3 188 if (!financialacls_is_acl_limiting_enabled()) {
189 return;
190 }
2dc76e8d
MW
191
192 switch ($entity) {
193 case 'LineItem':
194 case 'MembershipType':
f4a72e8d 195 case 'ContributionRecur':
2dc76e8d
MW
196 $types = [];
197 CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($types);
198 if ($types) {
199 $clauses['financial_type_id'] = 'IN (' . implode(',', array_keys($types)) . ')';
200 }
201 else {
202 $clauses['financial_type_id'] = '= 0';
203 }
204 break;
205
28188a1e 206 }
207
208}
209
07d89c14 210/**
34509ae3 211 * Remove unpermitted options.
07d89c14 212 *
213 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_buildAmount
214 *
215 * @param string $component
216 * @param \CRM_Core_Form $form
217 * @param array $feeBlock
218 */
219function financialacls_civicrm_buildAmount($component, $form, &$feeBlock) {
34509ae3 220 if (!financialacls_is_acl_limiting_enabled()) {
221 return;
222 }
223
224 foreach ($feeBlock as $key => $value) {
225 foreach ($value['options'] as $k => $options) {
226 if (!CRM_Core_Permission::check('add contributions of type ' . CRM_Contribute_PseudoConstant::financialType($options['financial_type_id']))) {
227 unset($feeBlock[$key]['options'][$k]);
07d89c14 228 }
229 }
34509ae3 230 if (empty($feeBlock[$key]['options'])) {
231 unset($feeBlock[$key]);
232 }
07d89c14 233 }
234}
235
e9eed3db 236/**
237 * Remove unpermitted membership types from selection availability..
238 *
239 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_membershipTypeValues
240 *
241 * @param \CRM_Core_Form $form
242 * @param array $membershipTypeValues
243 */
244function financialacls_civicrm_membershipTypeValues($form, &$membershipTypeValues) {
34509ae3 245 if (!financialacls_is_acl_limiting_enabled()) {
246 return;
247 }
e9eed3db 248 $financialTypes = NULL;
249 $financialTypes = CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, CRM_Core_Action::ADD);
250 foreach ($membershipTypeValues as $id => $type) {
251 if (!isset($financialTypes[$type['financial_type_id']])) {
252 unset($membershipTypeValues[$id]);
253 }
254 }
255}
256
4e0bf3e8 257/**
258 * Add permissions.
259 *
260 * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_permission/
261 *
262 * @param array $permissions
263 */
264function financialacls_civicrm_permission(&$permissions) {
265 if (!financialacls_is_acl_limiting_enabled()) {
266 return;
267 }
268 $actions = [
269 'add' => ts('add'),
270 'view' => ts('view'),
271 'edit' => ts('edit'),
272 'delete' => ts('delete'),
273 ];
274 $financialTypes = \CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'validate');
275 foreach ($financialTypes as $id => $type) {
276 foreach ($actions as $action => $action_ts) {
277 $permissions[$action . ' contributions of type ' . $type] = [
278 ts("CiviCRM: %1 contributions of type %2", [1 => $action_ts, 2 => $type]),
279 ts('%1 contributions of type %2', [1 => $action_ts, 2 => $type]),
280 ];
281 }
282 }
283 $permissions['administer CiviCRM Financial Types'] = [
284 ts('CiviCRM: administer CiviCRM Financial Types'),
285 ts('Administer access to Financial Types'),
286 ];
287}
288
51d1f926 289/**
290 * Remove unpermitted financial types from field Options in search context.
291 *
292 * Search context is described as
293 * 'search' => "search: searchable options are returned; labels are translated.",
294 * So this is appropriate to removing the options from search screens.
295 *
296 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_fieldOptions
297 *
298 * @param string $entity
299 * @param string $field
300 * @param array $options
301 * @param array $params
302 */
303function financialacls_civicrm_fieldOptions($entity, $field, &$options, $params) {
34509ae3 304 if (!financialacls_is_acl_limiting_enabled()) {
305 return;
306 }
f4a72e8d 307 if (in_array($entity, ['Contribution', 'ContributionRecur'], TRUE) && $field === 'financial_type_id' && $params['context'] === 'search') {
51d1f926 308 $action = CRM_Core_Action::VIEW;
309 // At this stage we are only considering the view action. Code from
310 // CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes().
311 $actions = [
312 CRM_Core_Action::VIEW => 'view',
313 CRM_Core_Action::UPDATE => 'edit',
314 CRM_Core_Action::ADD => 'add',
315 CRM_Core_Action::DELETE => 'delete',
316 ];
317 $cacheKey = 'available_types_' . $action;
318 if (!isset(\Civi::$statics['CRM_Financial_BAO_FinancialType'][$cacheKey])) {
319 foreach ($options as $finTypeId => $type) {
320 if (!CRM_Core_Permission::check($actions[$action] . ' contributions of type ' . $type)) {
321 unset($options[$finTypeId]);
322 }
323 }
324 \Civi::$statics['CRM_Financial_BAO_FinancialType'][$cacheKey] = $options;
325 }
326 $options = \Civi::$statics['CRM_Financial_BAO_FinancialType'][$cacheKey];
327 }
328}
329
34509ae3 330/**
331 * Is financial acl limiting enabled.
332 *
333 * Once this extension is detangled enough to be optional this will go
334 * and the status of the extension rather than the setting will dictate.
335 *
336 * @return bool
337 */
d646594f 338function financialacls_is_acl_limiting_enabled(): bool {
34509ae3 339 return (bool) Civi::settings()->get('acl_financial_type');
340}
341
e7339d59 342// --- Functions below this ship commented out. Uncomment as required. ---
343
344/**
345 * Implements hook_civicrm_preProcess().
346 *
347 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_preProcess
348 */
349//function financialacls_civicrm_preProcess($formName, &$form) {
350//
351//}
352
353/**
354 * Implements hook_civicrm_navigationMenu().
355 *
356 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_navigationMenu
357 */
358//function financialacls_civicrm_navigationMenu(&$menu) {
359// _financialacls_civix_insert_navigation_menu($menu, 'Mailings', array(
360// 'label' => E::ts('New subliminal message'),
361// 'name' => 'mailing_subliminal_message',
362// 'url' => 'civicrm/mailing/subliminal',
363// 'permission' => 'access CiviMail',
364// 'operator' => 'OR',
365// 'separator' => 0,
366// ));
367// _financialacls_civix_navigationMenu($menu);
368//}