CRM-15854 - MailingAB API - Restrict to 'access CiviMail'
[civicrm-core.git] / CRM / Core / DAO / permissions.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Decide what permissions to check for an api call
30 * The contact must have all of the returned permissions for the api call to be allowed
31 *
32 * @param $entity : (str) api entity
33 * @param $action : (str) api action
34 * @param $params : (array) api params
35 *
36 * @return array
37 * Array of permissions to check for this entity-action combo
38 */
39 function _civicrm_api3_permissions($entity, $action, &$params) {
40 // FIXME: Lowercase entity_names are nonstandard but difficult to fix here
41 // because this function invokes hook_civicrm_alterAPIPermissions
42 $entity = _civicrm_api_get_entity_name_from_camel($entity);
43
44 /**
45 * @var array of permissions
46 *
47 * For each entity, we declare an array of permissions required for each action
48 * The action is the array key, possible values:
49 * * create: applies to create (with no id in params)
50 * * update: applies to update, setvalue, create (with id in params)
51 * * get: applies to getcount, getsingle, getvalue and other gets
52 * * delete: applies to delete, replace
53 * * meta: applies to getfields, getoptions, getspec
54 * * default: catch-all for anything not declared
55 *
56 * Note: some APIs declare other actions as well
57 */
58 $permissions = array();
59
60 // These are the default permissions - if any entity does not declare permissions for a given action,
61 // (or the entity does not declare permissions at all) - then the action will be used from here
62 $permissions['default'] = array(
63 // applies to getfields, getoptions, etc.
64 'meta' => array('access CiviCRM'),
65 // catch-all, applies to create, get, delete, etc.
66 // If an entity declares it's own 'default' action it will override this one
67 'default' => array('administer CiviCRM'),
68 );
69
70 // Note: Additional permissions in DynamicFKAuthorization
71 $permissions['attachment'] = array(
72 'default' => array(
73 array('access CiviCRM', 'access AJAX API'),
74 ),
75 );
76
77 // Contact permissions
78 $permissions['contact'] = array(
79 'create' => array(
80 'access CiviCRM',
81 'add contacts',
82 ),
83 'delete' => array(
84 'access CiviCRM',
85 'delete contacts',
86 ),
87 // managed by query object
88 'get' => array(),
89 'update' => array(
90 'access CiviCRM',
91 'edit all contacts',
92 ),
93 'getquick' => array(
94 array('access CiviCRM', 'access AJAX API'),
95 ),
96 );
97
98 // Contact-related data permissions.
99 // CRM-14094 - Users can edit and delete contact-related objects using inline edit with 'edit all contacts' permission
100 $permissions['address'] = array(
101 'get' => array(
102 'access CiviCRM',
103 'view all contacts',
104 ),
105 'default' => array(
106 'access CiviCRM',
107 'edit all contacts',
108 ),
109 );
110 $permissions['email'] = $permissions['address'];
111 $permissions['phone'] = $permissions['address'];
112 $permissions['website'] = $permissions['address'];
113 $permissions['im'] = $permissions['address'];
114 $permissions['loc_block'] = $permissions['address'];
115 $permissions['entity_tag'] = $permissions['address'];
116 $permissions['note'] = $permissions['address'];
117
118 //relationship permissions
119 $permissions['relationship'] = array(
120 'get' => array(
121 'access CiviCRM',
122 'view all contacts',
123 ),
124 'delete' => array(
125 'access CiviCRM',
126 'delete contacts',
127 ),
128 'default' => array(
129 'access CiviCRM',
130 'edit all contacts',
131 ),
132 );
133
134 // Activity permissions
135 $permissions['activity'] = array(
136 'delete' => array(
137 'access CiviCRM',
138 'delete activities',
139 ),
140 'default' => array(
141 'access CiviCRM',
142 'view all activities',
143 ),
144 );
145
146 // Case permissions
147 $permissions['case'] = array(
148 'create' => array(
149 'access CiviCRM',
150 'add cases',
151 ),
152 'delete' => array(
153 'access CiviCRM',
154 'delete in CiviCase',
155 ),
156 'default' => array(
157 'access CiviCRM',
158 'access all cases and activities',
159 ),
160 );
161
162 // Financial permissions
163 $permissions['contribution'] = array(
164 'get' => array(
165 'access CiviCRM',
166 'access CiviContribute',
167 ),
168 'delete' => array(
169 'access CiviCRM',
170 'access CiviContribute',
171 'delete in CiviContribute',
172 ),
173 'completetransaction' => array(
174 'edit contributions',
175 ),
176 'default' => array(
177 'access CiviCRM',
178 'access CiviContribute',
179 'edit contributions',
180 ),
181 );
182 $permissions['line_item'] = $permissions['contribution'];
183
184 // Custom field permissions
185 $permissions['custom_field'] = array(
186 'default' => array(
187 'administer CiviCRM',
188 'access all custom data',
189 ),
190 );
191 $permissions['custom_group'] = $permissions['custom_field'];
192
193 // Event permissions
194 $permissions['event'] = array(
195 'create' => array(
196 'access CiviCRM',
197 'access CiviEvent',
198 'edit all events',
199 ),
200 'delete' => array(
201 'access CiviCRM',
202 'access CiviEvent',
203 'delete in CiviEvent',
204 ),
205 'get' => array(
206 'access CiviCRM',
207 'access CiviEvent',
208 'view event info',
209 ),
210 'update' => array(
211 'access CiviCRM',
212 'access CiviEvent',
213 'edit all events',
214 ),
215 );
216
217 // File permissions
218 $permissions['file'] = array(
219 'default' => array(
220 'access CiviCRM',
221 'access uploaded files',
222 ),
223 );
224 $permissions['files_by_entity'] = $permissions['file'];
225
226 // Group permissions
227 $permissions['group'] = array(
228 'get' => array(
229 'access CiviCRM',
230 ),
231 'default' => array(
232 'access CiviCRM',
233 'edit groups',
234 ),
235 );
236 $permissions['group_contact'] = $permissions['group'];
237 $permissions['group_nesting'] = $permissions['group'];
238 $permissions['group_organization'] = $permissions['group'];
239
240 // CiviMail Permissions
241 $civiMailBasePerms = array(
242 // To get/preview/update, one must have least one of these perms:
243 // Mailing API implementations enforce nuances of create/approve/schedule permissions.
244 'access CiviMail',
245 'create mailings',
246 'schedule mailings',
247 'approve mailings',
248 );
249 $permissions['mailing'] = array(
250 'get' => array(
251 'access CiviCRM',
252 $civiMailBasePerms,
253 ),
254 'delete' => array(
255 'access CiviCRM',
256 $civiMailBasePerms,
257 'delete in CiviMail',
258 ),
259 'submit' => array(
260 'access CiviCRM',
261 array('access CiviMail', 'schedule mailings'),
262 ),
263 'default' => array(
264 'access CiviCRM',
265 $civiMailBasePerms,
266 ),
267 );
268 $permissions['mailing_group'] = $permissions['mailing'];
269 $permissions['mailing_job'] = $permissions['mailing'];
270 $permissions['mailing_recipients'] = $permissions['mailing'];
271
272 $permissions['mailing_a_b'] = array(
273 'get' => array(
274 'access CiviCRM',
275 'access CiviMail',
276 ),
277 'delete' => array(
278 'access CiviCRM',
279 'access CiviMail',
280 'delete in CiviMail',
281 ),
282 'submit' => array(
283 'access CiviCRM',
284 array('access CiviMail', 'schedule mailings'),
285 ),
286 'default' => array(
287 'access CiviCRM',
288 'access CiviMail',
289 ),
290 );
291
292 // Membership permissions
293 $permissions['membership'] = array(
294 'get' => array(
295 'access CiviCRM',
296 'access CiviMember',
297 ),
298 'delete' => array(
299 'access CiviCRM',
300 'access CiviMember',
301 'delete in CiviMember',
302 ),
303 'default' => array(
304 'access CiviCRM',
305 'access CiviMember',
306 'edit memberships',
307 ),
308 );
309 $permissions['membership_status'] = $permissions['membership'];
310 $permissions['membership_type'] = $permissions['membership'];
311 $permissions['membership_payment'] = array(
312 'create' => array(
313 'access CiviCRM',
314 'access CiviMember',
315 'edit memberships',
316 'access CiviContribute',
317 'edit contributions',
318 ),
319 'delete' => array(
320 'access CiviCRM',
321 'access CiviMember',
322 'delete in CiviMember',
323 'access CiviContribute',
324 'delete in CiviContribute',
325 ),
326 'get' => array(
327 'access CiviCRM',
328 'access CiviMember',
329 'access CiviContribute',
330 ),
331 'update' => array(
332 'access CiviCRM',
333 'access CiviMember',
334 'edit memberships',
335 'access CiviContribute',
336 'edit contributions',
337 ),
338 );
339
340 // Participant permissions
341 $permissions['participant'] = array(
342 'create' => array(
343 'access CiviCRM',
344 'access CiviEvent',
345 'register for events',
346 ),
347 'delete' => array(
348 'access CiviCRM',
349 'access CiviEvent',
350 'edit event participants',
351 ),
352 'get' => array(
353 'access CiviCRM',
354 'access CiviEvent',
355 'view event participants',
356 ),
357 'update' => array(
358 'access CiviCRM',
359 'access CiviEvent',
360 'edit event participants',
361 ),
362 );
363 $permissions['participant_payment'] = array(
364 'create' => array(
365 'access CiviCRM',
366 'access CiviEvent',
367 'register for events',
368 'access CiviContribute',
369 'edit contributions',
370 ),
371 'delete' => array(
372 'access CiviCRM',
373 'access CiviEvent',
374 'edit event participants',
375 'access CiviContribute',
376 'delete in CiviContribute',
377 ),
378 'get' => array(
379 'access CiviCRM',
380 'access CiviEvent',
381 'view event participants',
382 'access CiviContribute',
383 ),
384 'update' => array(
385 'access CiviCRM',
386 'access CiviEvent',
387 'edit event participants',
388 'access CiviContribute',
389 'edit contributions',
390 ),
391 );
392
393 // Pledge permissions
394 $permissions['pledge'] = array(
395 'create' => array(
396 'access CiviCRM',
397 'access CiviPledge',
398 'edit pledges',
399 ),
400 'delete' => array(
401 'access CiviCRM',
402 'access CiviPledge',
403 'delete in CiviPledge',
404 ),
405 'get' => array(
406 'access CiviCRM',
407 'access CiviPledge',
408 ),
409 'update' => array(
410 'access CiviCRM',
411 'access CiviPledge',
412 'edit pledges',
413 ),
414 );
415 $permissions['pledge_payment'] = array(
416 'create' => array(
417 'access CiviCRM',
418 'access CiviPledge',
419 'edit pledges',
420 'access CiviContribute',
421 'edit contributions',
422 ),
423 'delete' => array(
424 'access CiviCRM',
425 'access CiviPledge',
426 'delete in CiviPledge',
427 'access CiviContribute',
428 'delete in CiviContribute',
429 ),
430 'get' => array(
431 'access CiviCRM',
432 'access CiviPledge',
433 'access CiviContribute',
434 ),
435 'update' => array(
436 'access CiviCRM',
437 'access CiviPledge',
438 'edit pledges',
439 'access CiviContribute',
440 'edit contributions',
441 ),
442 );
443
444 // Profile permissions
445 $permissions['profile'] = array(
446 'get' => array(), // the profile will take care of this
447 );
448
449 $permissions['uf_group'] = array(
450 'get' => array(
451 'access CiviCRM',
452 ),
453 );
454 $permissions['uf_field'] = $permissions['uf_group'];
455 $permissions['option_value'] = $permissions['uf_group'];
456 $permissions['option_group'] = $permissions['option_value'];
457
458 // Translate 'create' action to 'update' if id is set
459 if ($action == 'create' && (!empty($params['id']) || !empty($params[$entity . '_id']))) {
460 $action = 'update';
461 }
462
463 // let third parties modify the permissions
464 CRM_Utils_Hook::alterAPIPermissions($entity, $action, $params, $permissions);
465
466 // Merge permissions for this entity with the defaults
467 $perm = CRM_Utils_Array::value($entity, $permissions, array()) + $permissions['default'];
468
469 // Return exact match if permission for this action has been declared
470 if (isset($perm[$action])) {
471 return $perm[$action];
472 }
473
474 // Translate specific actions into their generic equivalents
475 $snippet = substr($action, 0, 3);
476 if ($action == 'replace' || $snippet == 'del') {
477 // 'Replace' is a combination of get+create+update+delete; however, the permissions
478 // on each of those will be tested separately at runtime. This is just a sniff-test
479 // based on the heuristic that 'delete' tends to be the most closesly guarded
480 // of the necessary permissions.
481 $action = 'delete';
482 }
483 elseif ($action == 'setvalue' || $snippet == 'upd') {
484 $action = 'update';
485 }
486 elseif ($action == 'getfields' || $action == 'getspec' || $action == 'getoptions') {
487 $action = 'meta';
488 }
489 elseif ($snippet == 'get') {
490 $action = 'get';
491 }
492 return isset($perm[$action]) ? $perm[$action] : $perm['default'];
493 }
494
495 # FIXME: not sure how to permission the following API 3 calls:
496 # contribution_transact (make online contributions)
497 # entity_tag_display
498 # group_contact_pending
499 # group_contact_update_status
500 # mailing_event_bounce
501 # mailing_event_click
502 # mailing_event_confirm
503 # mailing_event_forward
504 # mailing_event_open
505 # mailing_event_reply
506 # mailing_group_event_domain_unsubscribe
507 # mailing_group_event_resubscribe
508 # mailing_group_event_subscribe
509 # mailing_group_event_unsubscribe
510 # membership_status_calc
511 # survey_respondant_count