forked from eileenmcnaughton/org.civicrm.multisite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multisite.php
627 lines (575 loc) · 19 KB
/
multisite.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
<?php
require_once 'multisite.civix.php';
/**
* Implements hook_civicrm_config().
*/
function multisite_civicrm_config(&$config) {
_multisite_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_xmlMenu().
*/
function multisite_civicrm_xmlMenu(&$files) {
_multisite_civix_civicrm_xmlMenu($files);
}
/**
* Implements hook_civicrm_install().
*/
function multisite_civicrm_install() {
return _multisite_civix_civicrm_install();
}
/**
* Implements hook_civicrm_uninstall().
*/
function multisite_civicrm_uninstall() {
return _multisite_civix_civicrm_uninstall();
}
/**
* Implements hook_civicrm_enable().
*/
function multisite_civicrm_enable() {
return _multisite_civix_civicrm_enable();
}
/**
* Implements hook_civicrm_disable().
*/
function multisite_civicrm_disable() {
return _multisite_civix_civicrm_disable();
}
/**
* Implements hook_civicrm_upgrade().
*/
function multisite_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _multisite_civix_civicrm_upgrade($op, $queue);
}
/**
* Implements hook_civicrm_managed().
*/
function multisite_civicrm_managed(&$entities) {
_multisite_civix_civicrm_managed($entities);
}
/**
* Implements hook_civicrm_validate_form().
*
* Make parents optional for administrators when
* organization id is set
*
* @param string $formName - Name of the form being validated, you will typically switch off this value.
* @param array $fields - Array of name value pairs for all 'POST'ed form values
* @param array $files - Array of file properties as sent by PHP POST protocol
* @param object $form - Reference to the civicrm form object. This is useful if you want to retrieve any values that we've constructed in the form
* @param array $errors - Reference to the errors array. All errors will be added to this array
* Returns TRUE if form validates successfully, otherwise array with input field names as keys and error message strings as values
*/
function multisite_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {
if ((!isset($fields['organization_id']) && !empty($form->_entityId))) {
try{
//$fields['group_organization']
$fields['group_organization'] = civicrm_api3('group_organization', 'getvalue', array('group_id' => $form->_entityId, 'return' => 'id'));
$form->setElementError('parents', NULL);
}
catch (Exception $e) {
}
}
if (!empty($fields['organization_id']) || !empty($fields['group_organization'])) {
$form->setElementError('parents', NULL);
}
}
/**
* Implements hook civicrm_pre().
*
* @param string $op
* @param string $objectName
* @param int $id
* @param array $params
*/
function multisite_civicrm_pre($op, $objectName, $id, &$params) {
// allow setting of org instead of parent
if ($objectName == 'Group') {
if (empty($params['parents'])) {
// if parents left empty we need to fill organization_id (if not filled)
// and set no parent. We don't want Civi doing this on our behalf
// as we assume admin users can make sensible choices on nesting
// & the default should be the org link
$params['no_parent'] = 1;
}
if (empty($params['organization_id'])) {
$params['organization_id'] = _multisite_get_domain_organization(TRUE);
}
}
}
/**
* Implements hook_civicrm_post().
*
* Current implementation assumes shared user table for all sites -
* a more sophisticated version will be able to cope with a combination of shared user tables
* and separate user tables
*
* @param string $op
* @param string $objectName
* @param int $objectId
* @param object $objectRef
*/
function multisite_civicrm_post($op, $objectName, $objectId, &$objectRef) {
if ($op == 'edit' && $objectName == 'UFMatch') {
static $updating = FALSE;
if ($updating) {
// prevent recursion
return;
}
$updating = TRUE;
$ufs = civicrm_api('uf_match', 'get', array(
'version' => 3,
'contact_id' => $objectRef->contact_id,
'uf_id' => $objectRef->uf_id,
'id' => array(
'!=' => $objectRef->id,
),
));
foreach ($ufs['values'] as $ufMatch) {
civicrm_api('UFMatch', 'create', array(
'version' => 3,
'id' => $ufMatch['id'],
'uf_name' => $objectRef->uf_name,
));
}
}
}
/**
* Implements ACLGroup hook().
*
* aclGroup function returns a list of groups which are either children of the
* domain group id or connected to the same organisation as the domain Group ID
*
* @param string $type
* @param int $contactID
* @param string $tableName
* @param array $allGroups
* @param array $currentGroups
*/
function multisite_civicrm_aclGroup($type, $contactID, $tableName, &$allGroups, &$currentGroups) {
// only process saved search
if ($tableName != 'civicrm_saved_search') {
return;
}
$isEnabled = civicrm_api('setting', 'getvalue', array(
'version' => 3,
'name' => 'is_enabled',
'group' => 'Multi Site Preferences')
);
$groupID = _multisite_get_domain_group();
// If multisite is not enabled, or if a domain group is not selected, then we default to all groups allowed
if (!$isEnabled || !$groupID) {
$currentGroups = array_flip($allGroups);
return;
}
if (!CRM_Core_Permission::check('list all groups in domain') && !_multisite_add_permissions($type)) {
return;
}
$currentGroups = _multisite_get_all_child_groups($groupID, FALSE);
$currentGroups = array_merge($currentGroups, _multisite_get_domain_groups($groupID));
$disabledGroups = array();
$disabled = civicrm_api3('group', 'get', array(
'is_active' => 0,
'check_permissions' => FALSE,
'return' => 'id',
'sequential' => 1,
'options' => array('limit' => 0)));
foreach ($disabled['values'] as $group) {
$disabledGroups[] = $group['id'];
}
if (!empty($allGroups)) {
//all groups is empty if we really mean all groups but if a filter like 'is_disabled' is already applied
// it is populated, ajax calls from Manage Groups will leave empty but calls from New Mailing pass in a filtered list
$originalCurrentGroups = $currentGroups;
$currentGroups = array_intersect($currentGroups, array_flip($allGroups));
$currentGroups = array_merge($currentGroups, array_intersect($originalCurrentGroups, $disabledGroups));
}
}
/**
* Implements selectWhereClause hook().
*
* selectWhereClause restricts group selection to those which are either
* children of the domain group id or connected to the same organisation as the
* domain Group ID
*
* @param string $entity
* @param array $clauses
*/
function multisite_civicrm_selectWhereClause($entity, &$clauses) {
// Only process groups, only without "view all contacts" permission.
if ($entity != 'Group' || !(_multisite_is_permission()) || CRM_Core_Permission::check('view all contacts')) {
return;
}
$isEnabled = civicrm_api('setting', 'getvalue', array(
'version' => 3,
'name' => 'is_enabled',
'group' => 'Multi Site Preferences',
));
$groupID = _multisite_get_domain_group();
// If multisite is not enabled, or if a domain group is not selected, then we default to all groups allowed
if (!$isEnabled || !$groupID) {
return;
}
if (!CRM_Core_Permission::check('list all groups in domain') && !_multisite_add_permissions($type)) {
return;
}
$currentGroups = _multisite_get_all_child_groups($groupID, FALSE);
$currentGroups = array_merge($currentGroups, _multisite_get_domain_groups($groupID));
$groups_list = array_unique($currentGroups);
// Don't crash if there's no groups....
if (empty($groups_list)) {
$groups_list = array(0);
}
$clauses['id'][] = 'IN (' . implode(',', $groups_list) . ')';
}
/**
*
* @param string $type
* @param array $tables tables to be included in query
* @param array $whereTables tables required for where portion of query
* @param int $contactID contact for whom where clause is being composed
* @param string $where Where clause The completed clause will look like
* (multisiteGroupTable.group_id IN ("1,2,3,4,5") AND multisiteGroupTable.status IN ('Added') AND contact_a.is_deleted = 0)
* where the child groups are groups the contact is potentially a member of
*
*/
function multisite_civicrm_aclWhereClause($type, &$tables, &$whereTables, &$contactID, &$where) {
if (!$contactID) {
return;
}
if (!_multisite_add_permissions($type)) {
return;
}
$groupID = _multisite_get_domain_group();
if (!$groupID) {
return;
}
$childOrganizations = _multisite_get_all_child_groups($groupID);
if (!empty($childOrganizations)) {
$groupTable = 'civicrm_group_contact';
$groupTableAlias = 'multisiteGroupTable';
$tables[$groupTableAlias] = $whereTables[$groupTableAlias] = "
LEFT JOIN {$groupTable} $groupTableAlias ON contact_a.id = {$groupTableAlias}.contact_id
";
$deletedContactClause = CRM_Core_Permission::check('access deleted contacts') ? '' : 'AND contact_a.is_deleted = 0';
$where = "(multisiteGroupTable.group_id IN (" . implode(',', $childOrganizations) . ") AND {$groupTableAlias}.status IN ('Added') $deletedContactClause)";
}
}
/**
* Add site specific tabs.
*
* @param array $tabs
* @param int $contactID
*
* @throws \CiviCRM_API3_Exception
*/
function multisite_civicrm_tabs(&$tabs, $contactID) {
$enabled = civicrm_api3('setting', 'getvalue', array('group' => 'Multi Site Preferences', 'name' => 'multisite_custom_tabs_restricted'));
if (!$enabled) {
return;
}
$tabs_visible = civicrm_api3('setting', 'getvalue', array('group' => 'Multi Site Preferences', 'name' => 'multisite_custom_tabs_enabled'));
foreach ($tabs as $id => $tab) {
if (stristr($tab['id'], 'custom_')) {
$tab_id = str_replace('custom_', '', $tab['id']);
if (!in_array($tab_id, $tabs_visible)) {
unset($tabs[$id]);
}
}
}
}
/**
* invoke permissions hook
* note that permissions hook is now permission hook
* @param array $permissions
*/
function multisite_civicrm_permissions(&$permissions) {
multisite_civicrm_permission($permissions);
}
/**
* invoke permissions hook
* @param array $permissions
*/
function multisite_civicrm_permission(&$permissions) {
$prefix = ts('CiviCRM Multisite') . ': ';
$permissions = $permissions + array(
'view all contacts in domain' => $prefix . ts('view all contacts in domain'),
'edit all contacts in domain' => $prefix . ts('edit all contacts in domain'),
'list all groups in domain' => $prefix . ts('list all groups in domain'),
);
}
/**
* Implements hook_civicrm_permission_check().
*/
function multisite_civicrm_permission_check($permission, &$granted) {
$isEnabled = civicrm_api('setting', 'getvalue', array(
'version' => 3,
'name' => 'is_enabled',
'group' => 'Multi Site Preferences')
);
if ($isEnabled == 0) {
// Multisite ACLs are not enabled, so 'view all contacts in domain' cascades to 'view all contacts'
// and the same is true for 'edit all contacts' - cf. CRM-19256
if ($permission == 'view all contacts' && CRM_Core_Permission::check('view all contacts in domain')) {
$granted = TRUE;
}
elseif ($permission == 'edit all contacts' && CRM_Core_Permission::check('edit all contacts in domain')) {
$granted = TRUE;
}
}
}
/**
* Implements hook_civicrm_alterSettingsFolders().
*/
function multisite_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
_multisite_civix_civicrm_alterSettingsFolders($metaDataFolders);
}
/**
* Get all groups that are children of the parent group
* (iterate through all levels)
*
* @param int $groupID
* @param bool $includeParent
* @return array:child groups
*/
function _multisite_get_all_child_groups($groupID, $includeParent = TRUE) {
static $_cache = array();
if (!array_key_exists($groupID, $_cache)) {
$childGroups = &CRM_Core_BAO_Cache::getItem('descendant groups for an org', $groupID);
if (empty($childGroups)) {
$childGroups = array();
$query = "
SELECT children
FROM civicrm_group
WHERE children IS NOT NULL
AND id IN ";
if (!is_array($groupID)) {
$groupIDs = array(
$groupID,
);
}
while (!empty($groupIDs)) {
$groupIDString = implode(',', $groupIDs);
$realQuery = $query . " ( $groupIDString )";
$dao = CRM_Core_DAO::executeQuery($realQuery);
$groupIDs = array();
while ($dao->fetch()) {
if ($dao->children) {
$childIDs = explode(',', $dao->children);
foreach ($childIDs as $childID) {
if (!array_key_exists($childID, $childGroups)) {
$childGroups[$childID] = 1;
$groupIDs[] = $childID;
}
}
}
}
}
CRM_Core_BAO_Cache::setItem($childGroups, 'descendant groups for an org', $groupID);
}
$_cache[$groupID] = $childGroups;
}
if ($includeParent || CRM_Core_Permission::check('administer Multiple Organizations')) {
return array_keys(array(
$groupID => 1,
) + $_cache[$groupID]);
}
else {
return array_keys($_cache[$groupID]);
}
}
/**
* Get groups linked to the domain via the group organization
* being shared with the domain group
* @return NULL|int $groupID
*/
function _multisite_get_domain_groups($groupID) {
$sql = " SELECT o2.group_id as group_id
FROM civicrm_group_organization o
INNER JOIN civicrm_group_organization o2 ON o.organization_id = o2.organization_id
AND o.group_id = $groupID AND o2.group_id <> $groupID
";
$dao = CRM_Core_DAO::executeQuery($sql);
$groups = array();
while ($dao->fetch()) {
$groups[] = (int) $dao->group_id;
}
return $groups;
}
/**
*
* @return NULL|int $groupID
*/
function _multisite_get_domain_group($permission = 1) {
$groupID = CRM_Core_BAO_Domain::getGroupId();
if (empty($groupID) || !is_numeric($groupID)) {
/* domain group not defined - we could let people know but
* it is acceptable for some domains not to be in the multisite
* so should probably check enabled before we spring an error
*/
return NULL;
}
// We will check for the possibility of the acl_enabled setting being deliberately set to 0
if ($permission) {
$aclsEnabled = civicrm_api('setting', 'getvalue', array(
'version' => 3,
'name' => 'multisite_acl_enabled',
'group' => 'Multi Site Preferences')
);
if (is_numeric($aclsEnabled) && !$aclsEnabled) {
return NULL;
}
}
return $groupID;
}
/**
* get organization of domain group
*/
function _multisite_get_domain_organization($permission = TRUE) {
$groupID = _multisite_get_domain_group($permission);
if (empty($groupID)) {
return FALSE;
}
return civicrm_api('group_organization', 'getvalue', array(
'version' => 3,
'group_id' => $groupID,
'return' => 'organization_id',
));
}
/**
* Should we be adding ACLs in this instance.
*
* If we don't add them the user will not be able to see anything.
* We check if the install has the permissions
* hook implemented correctly & if so only allow view & edit based on those.
*
* Otherwise all users get these permissions added (4.2 vs 4.3 / other CMS issues)
*
* @param int $type type of operation
*
* @return bool
*/
function _multisite_add_permissions($type) {
$hookclass = 'CRM_Utils_Hook';
if (!method_exists($hookclass, 'permissions') && !method_exists($hookclass, 'permission')) {
// ie. unpatched 4.2 so we can't check for extra declared permissions
// & default to applying this to all
return TRUE;
}
if ($type == 'group') {
// @fixme only handling we have for this at the moment
return TRUE;
}
// extra check to make sure that hook is properly implemented
// if not we won't check for it. NB view all contacts in domain is enough checking
$declaredPermissions = CRM_Core_Permission::basicPermissions();
if (!array_key_exists('view all contacts in domain', $declaredPermissions)) {
return TRUE;
}
if (CRM_ACL_BAO_ACL::matchType($type, 'View') && CRM_Core_Permission::check('view all contacts in domain')) {
return TRUE;
}
if (CRM_ACL_BAO_ACL::matchType($type, 'Edit') && CRM_Core_Permission::check('edit all contacts in domain')) {
return TRUE;
}
return FALSE;
}
/**
* Implements buildForm hook().
*
* http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_buildForm
* @param string $formName
* @param object $form reference to the form object
*/
function multisite_civicrm_buildForm($formName, &$form) {
if ($formName == 'CRM_Group_Form_Edit' || $formName == 'CRM_Contact_Form_Task_SaveSearch') {
_multisite_alter_form_crm_group_form_edit($formName, $form);
}
}
/**
* Called from buildForm hook.
*
* http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_buildForm
*
* @param string $formName
* @param object $form reference to the form object
*/
function _multisite_alter_form_crm_group_form_edit($formName, &$form) {
if (isset($form->_defaultValues['parents'])) {
$parentOrgs = civicrm_api('group_organization', 'get', array(
'version' => 3,
'group_id' => $form->_defaultValues['parents'],
'return' => 'organization_id',
'sequential' => 1,
));
if ($parentOrgs['count'] == 1) {
$groupOrg = $parentOrgs['values'][0]['organization_id'];
$defaults['organization_id'] = $groupOrg;
$defaults['organization'] = civicrm_api('contact', 'getvalue', array(
'version' => 3,
'id' => $groupOrg,
'return' => 'display_name',
));
$defaults['parents'] = "";
$form->setDefaults($defaults);
}
}
unset($form->_required[2]);
unset($form->_rules['parents']);
}
/**
* Implements hook_civicrm_alterAPIPermissions().
*
* http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterAPIPermissions
* @param string $entity
* @param string $action
* @param array &$params
* @param array &$permissions
*/
function multisite_civicrm_alterAPIPermissions($entity, $action, &$params, &$permissions) {
_multisite_is_permission(TRUE);
$domain_id = CRM_Core_Config::domainID();
if ($domain_id !== 1) {
$entities = array('address', 'email', 'phone', 'website', 'im', 'loc_block',
'entity_tag', 'relationship', 'group_contact',
);
foreach ($entities as $entity) {
$permissions[$entity]['default'] = array(
'access CiviCRM',
'edit all contacts in domain',
);
$permissions[$entity]['get'] = array('access CiviCRM');
}
$permissions['relationship']['delete'] = array(
'access CiviCRM',
'edit all contacts in domain',
);
$permissions['contact']['update'] = array(
'access CiviCRM',
'edit all contacts in domain',
);
}
}
/**
* Are we checking if we are in multisite permission
* @param bool $check
* @return bool
*/
function _multisite_is_permission($check = NULL) {
static $checking = FALSE;
if (isset($check)) {
$checking = $check;
}
return $checking;
}
/**
* Add in mailing API wrapper
* @param array $wrappers
* @param array $apiRequest
*/
function multisite_civicrm_apiWrappers(&$wrappers, $apiRequest) {
if ($apiRequest['entity'] == 'Mailing' && $apiRequest['action'] == 'getlist') {
$wrappers[] = new CRM_Multisite_MailingWrapper();
}
}