-
Notifications
You must be signed in to change notification settings - Fork 21
/
oa_access.module
773 lines (690 loc) · 23.2 KB
/
oa_access.module
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
<?php
/**
* @file
* Code for the Open Atrium Access module.
*/
/**
* Path to the Group admin page.
*/
define('OA_ACCESS_GROUP_ADMIN_PATH', 'groups/oa_access');
/**
* Path to the Team admin pages.
*/
define('OA_ACCESS_TEAM_ADMIN_PATH', 'group/%/%/admin/oa_access');
/**
* Permission type -- Allows permission to be assigned to a Team.
*/
define('OA_ACCESS_TEAM_PERMISSION', 1);
/**
* Permission type -- Allows permission to be assigned to a Group.
*/
define('OA_ACCESS_GROUP_PERMISSION', 2);
/**
* Permission type -- Allows permission have an 'All' option.
*/
define('OA_ACCESS_ALLOW_OPTION_ALL', 4);
/**
* Permission type -- This permission will be set to 'All' by default for Groups.
*
* This implies OA_ACCESS_ALLOW_OPTION_ALL as well.
*/
define('OA_ACCESS_GROUP_DEFAULT_OPTION_ALL', 8 | OA_ACCESS_ALLOW_OPTION_ALL);
/**
* Permission type -- This permission will be set to 'All' by default for Teams.
*
* This implies OA_ACCESS_ALLOW_OPTION_ALL as well.
*/
define('OA_ACCESS_TEAM_DEFAULT_OPTION_ALL', 16 | OA_ACCESS_ALLOW_OPTION_ALL);
/**
* Permission type -- This permission will be set to 'All' by default for all.
*
* This combines OA_ACCESS_GROUP_DEFAULT_OPTION_ALL and
* OA_ACCESS_TEAM_DEFAULT_OPTION_ALL.
*/
define('OA_ACCESS_DEFAULT_OPTION_ALL', OA_ACCESS_GROUP_DEFAULT_OPTION_ALL | OA_ACCESS_TEAM_DEFAULT_OPTION_ALL);
/**
* Permission type -- The default permission type.
*
* Allows permission to be assigned to a Team or Group and doesn't allow an
* 'All' option. This is most useful for permissions where we are only removing
* permission, not adding it, for example, oa_workbench_access.
*
* @see oa_workbench_access
*/
define('OA_ACCESS_DEFAULT_PERMISSION', OA_ACCESS_TEAM_PERMISSION | OA_ACCESS_GROUP_PERMISSION);
/**
* Permission combine type -- Union of Group and Team permissions.
*
* If either the Group or Team grants permission, then the user has this
* permission.
*/
define('OA_ACCESS_COMBINE_UNION', 1);
/**
* Permission combine type -- Intersection of Group and Team permissions.
*
* Both the Group and Team must grant permission in order for this user to
* be granted permission.
*/
define('OA_ACCESS_COMBINE_INTERSECTION', 2);
/**
* Permission combine type -- Team permissions override.
*
* If both Group and Team permissions are set, then the Team permissions
* override the Group permissions.
*/
define('OA_ACCESS_COMBINE_TEAM_OVERRIDE', 3);
/**
* Permission combine type -- Team permissions override.
*
* If both Group and Team permissions are set, then the Group permissions
* override the Team permissions.
*/
define('OA_ACCESS_COMBINE_GROUP_OVERRIDE', 4);
/**
* Implements hook_menu().
*/
function oa_access_menu() {
$items = array();
$items[OA_ACCESS_GROUP_ADMIN_PATH] = array(
'title' => 'Group permissions',
'description' => 'Configure which Groups have which permissions.',
'access arguments' => array('administer oa_access permissions'),
'page callback' => 'drupal_get_form',
'page arguments' => array('oa_access_group_permissions_form'),
'file' => 'oa_access.admin.inc',
'type' => MENU_LOCAL_TASK,
);
if (module_exists('oa_teams')) {
$items[OA_ACCESS_TEAM_ADMIN_PATH] = array(
'title' => 'Team permissions',
'description' => 'Configure which Teams have which permissions.',
'access callback' => 'og_ui_user_access_group',
'access arguments' => array('administer oa_access permissions', 1, 2),
'page callback' => 'drupal_get_form',
'page arguments' => array('oa_access_team_permissions_form', 1, 2),
'file' => 'oa_access.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
}
return $items;
}
/**
* Implements hook_menu_alter().
*/
function oa_access_menu_alter(&$items) {
if (!isset($items['groups/list'])) {
$items['groups/list'] = array(
'title' => 'List',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
}
}
/**
* Implements hook_og_ui_get_group_admin().
*/
function oa_access_og_ui_get_group_admin($group_type, $gid) {
$items = array();
if (module_exists('oa_teams') && og_user_access($group_type, $gid, 'administer oa_access permissions')) {
$items['oa_access'] = array(
'title' => t('Team permissions'),
'description' => t('Configure which Teams have which permissions.'),
'href' => 'admin/oa_access',
);
}
return $items;
}
/**
* Implements hook_strongarm_alter().
*/
function oa_access_strongarm_alter(&$items) {
if (isset($items['contextual_tabs_extract'])) {
$items['contextual_tabs_extract']->value .= "\n" . implode("\n", array(
"groups:Group permissions,icon-user",
"List,icon-th-list",
));
}
}
/**
* Implements hook_permission().
*/
function oa_access_permission() {
return array(
'administer oa_access permissions' => array(
'title' => t('Administer Open Atrium Access permissions'),
'description' => t('Allows users to configure which Groups have which permissions.'),
),
);
}
/**
* Implements hook_og_permission().
*/
function oa_access_og_permission() {
return array(
'administer oa_access permissions' => array(
'title' => t('Administer Open Atrium Access permissions'),
'description' => t('Allows users to configure which Teams have which permissions.'),
'default role' => array(OG_ADMINISTRATOR_ROLE),
),
);
}
/**
* Implements hook_theme().
*/
function oa_access_theme() {
return array(
'oa_access_permissions_form' => array(
'render element' => 'permissions',
'file' => 'oa_access.theme.inc',
),
);
}
/**
* Implements hook_node_insert().
*/
function oa_access_node_insert($node) {
if ($node->type == 'oa_space') {
// Find all permissions which are type OA_ACCESS_DEFAULT_OPTION_ALL and
// OA_ACCESS_TEAM_PERMISSION and add the default 'All' option for those
// new spaces.
foreach (oa_access_get_permissions() as $name => $perm) {
if ($perm['type'] & OA_ACCESS_TEAM_PERMISSION && ($perm['type'] & OA_ACCESS_TEAM_DEFAULT_OPTION_ALL) == OA_ACCESS_TEAM_DEFAULT_OPTION_ALL) {
oa_access_initialize_permissions(array($name => $perm), array($node->nid));
}
}
}
}
/**
* Implements hook_node_delete().
*/
function oa_access_node_delete($node) {
db_delete('oa_access')
->condition('nid', $node->nid)
->execute();
}
/**
* Implements hook_modules_installed().
*
* Check each new module enabled to see if it defines some oa_access_permisions
* and if they are type OA_ACCESS_GROUP_DEFAULT_OPTION_ALL or
* OA_ACCESS_TEAM_DEFAULT_OPTION_ALL, then we set the 'All' permission for all
* existing Groups and Teams.
*/
function oa_access_modules_installed($modules) {
$permission_modules = array();
foreach ($modules as $module) {
$func = $module . '_oa_access_permission';
if (function_exists($module . '_oa_access_permission')) {
$permission_modules[] = $module;
}
}
if (!empty($permission_modules)) {
// Gather all the permissions we intend to initialize.
$permissions = array();
foreach (oa_access_get_permissions(TRUE) as $name => $perm) {
if (in_array($perm['module'], $permission_modules)) {
$permissions[$name] = $perm;
}
}
// Initialize them all at once (to avoid pulling groups multiple times).
oa_access_initialize_permissions($permissions);
}
}
/**
* Implements hook_modules_uninstalled().
*/
function oa_access_modules_uninstalled($modules) {
db_delete('oa_access')
->condition('module', $modules, 'IN')
->execute();
}
/**
* Get all permissions defined by implementing modules.
*
* @param boolean $reset
* (Optional) If set to TRUE, it will reset the static cache.
*
* @return array
* Associative array keyed with the permission name containing associative
* arrays with the following keys:
* - title: Human readable name of the permission.
* - description: Human readable description of the permission.
* - module: The machine name of the module which defines it.
* - type: Flags specifying if can be used for Groups or Teams or both.
*/
function oa_access_get_permissions($reset = FALSE) {
if ($reset) {
drupal_static_reset(__FUNCTION__);
}
$perms =& drupal_static(__FUNCTION__, NULL);
if (is_null($perms)) {
$perms = array();
foreach (module_implements('oa_access_permission') as $module) {
if ($result = module_invoke($module, 'oa_access_permission')) {
foreach ($result as $key => $perm) {
$perms[$key] = array_merge($perm, array(
'module' => $module,
));
// Set the default 'type' if not set.
if (empty($perms[$key]['type'])) {
$perms[$key]['type'] = OA_ACCESS_DEFAULT_PERMISSION;
}
elseif (!($perms[$key]['type'] & OA_ACCESS_GROUP_PERMISSION) && !($perms[$key]['type'] & OA_ACCESS_TEAM_PERMISSION)) {
$perms[$key]['type'] |= OA_ACCESS_DEFAULT_PERMISSION;
}
// Set the default 'combine' if not set.
if (empty($perms[$key]['combine'])) {
$perms[$key]['combine'] = OA_ACCESS_COMBINE_UNION;
}
}
}
}
drupal_alter('oa_access_permission', $perms);
}
return $perms;
}
/**
* Get a list of this user's Groups.
*
* @param object|NULL $user
* (Optional) The user to get groups for. If NULL, it will find groups for
* the currently logged in user.
*
* @return array
* An array of Group nids.
*/
function oa_access_user_groups($account = NULL) {
global $user;
if (is_null($account)) {
$account = $user;
}
$cache =& drupal_static(__FUNCTION__, array());
if (!isset($cache[$account->uid])) {
if (!isset($cache[$account->uid])) {
// First, get all the users oa_groups.
$query = db_select('og_membership', 'og');
$query->innerJoin('node', 'n', 'og.gid = n.nid');
$query
->fields('n', array('nid', 'type'))
->condition('og.group_type', 'node')
->condition('og.entity_type', 'user')
->condition('og.state', OG_STATE_ACTIVE)
->condition('n.type', 'oa_group')
->condition('og.etid', $account->uid);
$groups = array();
foreach ($query->execute() as $row) {
$groups[$row->nid] = $row->nid;
}
// All users are members of the magic 'All' group with an NID of 0.
$groups[0] = 0;
$cache[$account->uid] = $groups;
}
}
return $cache[$account->uid];
}
/**
* Get a list of this user's Teams.
*
* @param object|NULL $user
* (Optional) The user to get groups for. If NULL, it will find groups for
* the currently logged in user.
* @param integer $space_nid
* (Optional) The nid of a Space. If given, this will include the Teams
* from the given Space. If not given, then Teams won't be included.
*
* @return array
* An array of Team nids.
*/
function oa_access_user_teams($space_nid, $account = NULL) {
global $user;
if (!module_exists('oa_teams')) {
return array();
}
if (is_null($account)) {
$account = $user;
}
$cache =& drupal_static(__FUNCTION__, array());
if (!isset($cache[$space_nid][$account->uid])) {
$teams = array();
$valid_teams = oa_teams_get_teams_for_space($space_nid);
if (count($valid_teams) > 0) {
// Then, we get all the user's oa_teams, optionally filtering by valid
// teams for this particular Space.
$query = db_select('field_data_' . OA_TEAM_USERS_FIELD, 'f')
->fields('f', array('entity_id'))
->condition('entity_type', 'node')
->condition('entity_id', array_keys($valid_teams), 'IN')
->condition('deleted', 0)
->condition(OA_TEAM_USERS_FIELD . '_target_id', $account->uid);
foreach ($query->execute() as $row) {
$teams[$row->entity_id] = $row->entity_id;
}
}
// All Space members are members of the magic 'All' team with an NID
// that is equal to the Space's NID.
if (og_is_member('node', $space_nid, 'user', $account)) {
$teams[$space_nid] = $space_nid;
}
$cache[$space_nid][$account->uid] = $teams;
}
return $cache[$space_nid][$account->uid];
}
/**
* Gets all the permissions that a list of groups have.
*
* @param array $groups
* An array of nids of Groups or Teams.
*
* @return array
* An associative array keyed by group nid containing associative arrays
* keyed by the module and containing an array of permission names, for
* example:
* @code
* array('27' => array('mymodule' => array('a permission')))
* @endcode
* Which signifies that the group with nid 27 has 'a permission' from
* a module called 'mymodule'.
*
* @see oa_access_set_group_permissions()
* @see oa_access_user_groups()
* @see oa_access_user_teams()
*/
function oa_access_get_group_permissions($groups) {
$cache =& drupal_static(__FUNCTION__, array());
// First, go through the static cache and any groups we don't have data
// for to the $missing array.
$missing = array();
foreach ($groups as $gid) {
if (!isset($cache[$gid])) {
$missing[] = $gid;
}
}
// Next, query any of the missing groups from the database and add them to
// the static cache.
if (!empty($missing)) {
$query = db_select('oa_access', 'a')
->fields('a', array('nid', 'permission', 'module'))
->condition('a.nid', $missing, 'IN');
foreach ($query->execute() as $row) {
$cache[$row->nid][$row->module][] = $row->permission;
}
}
// Finally, add all the necessary permissions from the static cache and
// return it!
$return = array();
foreach ($groups as $gid) {
$return[$gid] = isset($cache[$gid]) ? $cache[$gid] : array();
}
return $return;
}
/**
* Changes the permissions for a set of groups.
*
* @param array $group_permissions
* An associatiive array keyed by Group or Team nid containing associative
* arrays keyed by module containing a list of permissions, for example:
* @code
* array('27' => array('mymodule' => array('a permission')))
* @endcode
* Which signifies that the group with nid 27 has 'a permission' from
* a module called 'mymodule'.
*
* @see oa_access_get_group_permissions()
*/
function oa_access_set_group_permissions($group_permissions) {
$cache =& drupal_static('oa_access_get_group_permissions', array());
// Delete current permissions for the given groups.
db_delete('oa_access')
->condition('nid', array_keys($group_permissions), 'IN')
->execute();
// Then build them back up!
foreach ($group_permissions as $nid => $modules) {
foreach ($modules as $module => $permissions) {
foreach ($permissions as $name) {
db_insert('oa_access')
->fields(array(
'nid' => $nid,
'permission' => $name,
'module' => $module,
))
->execute();
}
// Replace these entries in the cache, so that we don't have to pull them
// from the database at all!
$cache[$nid] = $modules;
}
}
}
/**
* Gets a combined list of the permissions that a list of groups can perform.
*
* This doesn't obey the permissions 'combine' property - it's just a straight
* forward union of all the permissions from each Group or Team.
*
* @param array
* An array of Group or Team nids.
*
* @return array
* @code
* array('one permission' => TRUE, 'an other permission' => TRUE)
* @endcode
* Which signifies that the combined permissions of the given groups
* include 'one permission' and 'an other permission'.
*
* @see oa_access_get_group_permissions()
* @see oa_access_user_groups()
* @see oa_access_user_teams()
*/
function oa_access_get_group_permissions_combined($groups) {
$return = array();
foreach (oa_access_get_group_permissions($groups) as $gid => $modules) {
foreach ($modules as $perms) {
foreach ($perms as $name) {
$return[$name] = TRUE;
}
}
}
return $return;
}
/**
* Helper function to determine if a permission is overridden by Group or Team.
*
* @param string $perm
* The permission name.
* @param integer $space_nid
* The nid of a Space, when checking if Teams override, or zero if checking
* if the Groups override.
*/
function _oa_access_is_overridden($perm, $space_nid) {
$groups =& drupal_static(__FUNCTION__, array());
if (!isset($groups[$space_nid])) {
if ($space_nid == 0) {
// Get an array of all the Group nids.
$groups[$space_nid] = array_keys(oa_core_get_all_groups());
}
else {
// Get an array of all the Team nids.
$groups[$space_nid] = array_keys(oa_teams_get_teams_for_space($space_nid));
}
// Add the magic 'All' option.
$groups[$space_nid][] = $space_nid;
}
$permissions = oa_access_get_group_permissions_combined($groups[$space_nid]);
return isset($permissions[$perm]);
}
/**
* Determines if the user has a permission.
*
* This is based on the user's membership in an Open Atrium Group or Team in
* the same way that user_access() is based on a user's membership in a role.
*
* @param object|integer|NULL $space
* The Space in which we want to perform the action. You can pass NULL if
* there is no Space active (this will mean that Team permissions won't be
* considered).
* @param string $permission
* The machine name of the permission we are checking.
* @param object|NULL $account
* (Optional) A user object representing the user to check. If NULL, it will
* check for the currently logged in user.
*
* @returns boolean
* TRUE if the user has permission; otherwise FALSE.
*/
function oa_access($space, $permission, $account = NULL) {
global $user;
if (is_null($account)) {
$account = $user;
}
// This function is most commonly called with the same $account but different
// $permissions's. So the layout of the static cache is optimized for that case.
//
// Also, when Teams come into play, access is dependent on the Space which the
// node belongs to. This is because permission gained by membership in a Team
// on Space A shouldn't give you that permission when editting content in
// Space B.
//
// So, the cache looks like this:
//
// $cache[$space_nid][$account->uid][$permission] = TRUE;
//
// However, if oa_teams is disabled, we'll use a constant for $space_nid to
// group everything togather for a modest performance boost.
//
$cache =& drupal_static(__FUNCTION__, array());
$space = !module_exists('oa_teams') ? 0 : ($space ? $space : 0);
$space_nid = is_object($space) ? $space->nid : $space;
if (!isset($cache[$space_nid][$account->uid])) {
// Get the Group and Team permissions.
$group_permissions = oa_access_get_group_permissions_combined(oa_access_user_groups($account));
$team_permissions = $space_nid ? oa_access_get_group_permissions_combined(oa_access_user_teams($space_nid, $account)) : array();
// Loop over the permissions, combining the Group and Team grants per the
// 'combine' property on the permission.
$perms = array();
foreach (oa_access_get_permissions() as $perm => $info) {
switch ($info['combine']) {
case OA_ACCESS_COMBINE_UNION:
$perms[$perm] = isset($group_permissions[$perm]) || isset($team_permissions[$perm]);
break;
case OA_ACCESS_COMBINE_INTERSECTION:
$perms[$perm] = isset($group_permissions[$perm]) && isset($team_permissions[$perm]);
break;
case OA_ACCESS_COMBINE_TEAM_OVERRIDE:
if ($space_nid && _oa_access_is_overridden($perm, $space_nid)) {
$perms[$perm] = isset($team_permissions[$perm]);
}
else {
$perms[$perm] = isset($group_permissions[$perm]) || isset($team_permissions[$perm]);
}
break;
case OA_ACCESS_COMBINE_GROUP_OVERRIDE:
if (_oa_access_is_overridden($perm, 0)) {
$perms[$perm] = isset($group_permissions[$perm]);
}
else {
$perms[$perm] = isset($group_permissions[$perm]) || isset($team_permissions[$perm]);
}
break;
}
}
$cache[$space_nid][$account->uid] = $perms;
}
return $cache[$space_nid][$account->uid][$permission];
}
/**
* Initializes new permissions that were added after module install.
*
* You should call this function when a new permission is added AFTER the
* initial install of your module. (Open Atrium Access will take care of
* initializing any permissions on install of your module for you.)
*
* When possible avoid calling this multiple times without the $groups argument.
* It's better to call it once for multiple permissions, because it will reduce
* the number of repeated database queries.
*
* At the moment, all this does is set the default permissions for permissions
* with a type of OA_ACCESS_GROUP_DEFAULT_OPTION_ALL or
* OA_ACCESS_TEAM_DEFAULT_OPTION_ALL. If you don't have any with those types,
* you don't have to ever call this function.
*
* @param string|object $perms
* The machine name of the new permission or an associative array containing
* the full permission array's keyed by permission machine name.
* @param array $groups
* (Optional) The nids of groups that were just added. If omitted, it will
* find all the appropriate groups in the system. This is primary for
* internal use.
*/
function oa_access_initialize_permissions($perms, array $groups = array()) {
if (is_string($perms)) {
$all_perms = oa_access_get_permissions(TRUE);
if (!isset($all_perms[$perms])) {
throw new Exception("Unknown permission: $perms");
}
$perms = array($perms => $all_perms[$perms]);
}
if (empty($groups)) {
$groups = db_select('node', 'n')
->fields('n', array('nid'))
->condition('type', 'oa_space')
->execute()
->fetchCol();
}
$updated = FALSE;
foreach ($perms as $name => $perm) {
$nids = array();
if ($perm['type'] & OA_ACCESS_TEAM_PERMISSION && ($perm['type'] & OA_ACCESS_TEAM_DEFAULT_OPTION_ALL) == OA_ACCESS_TEAM_DEFAULT_OPTION_ALL) {
$nids = $groups;
}
if ($perm['type'] & OA_ACCESS_GROUP_PERMISSION && ($perm['type'] & OA_ACCESS_GROUP_DEFAULT_OPTION_ALL) == OA_ACCESS_GROUP_DEFAULT_OPTION_ALL) {
// Add the '0' nid for the 'All' group.
$nids[] = 0;
}
if (count($nids) > 0) {
foreach ($nids as $nid) {
db_merge('oa_access')
->key(array(
'nid' => $nid,
'permission' => $name,
))
->fields(array(
'module' => $perm['module'],
))
->execute();
}
$updated = TRUE;
}
}
if ($updated) {
drupal_static_reset('oa_access_get_group_permissions');
}
}
/**
* Removes left over permissions that are no longer valid.
*
* @param string|NULL $module
* (Optional) If specified it will only attempt to cleanup permissions for
* the given module; otherwise it'll do them all.
*/
function oa_access_cleanup_permissions($module = NULL) {
$valid_permissions = oa_access_get_permissions();
$query = db_select('oa_access', 'a')
->distinct()
->fields('a', array('permission'));
if ($module) {
$query->condition('a.module', $module);
}
// Find all the permissions in the database which aren't declared by
// hook_oa_access_permission().
$invalid = array();
foreach ($query->execute() as $row) {
if (!isset($valid_permissions[$row->permission])) {
$invalid[] = $row->permission;
}
}
// And delete them!
if (!empty($invalid)) {
db_delete('oa_access')
->condition('permission', $invalid, 'IN')
->execute();
}
}