/
nodeaccess.module
1179 lines (1079 loc) · 33 KB
/
nodeaccess.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
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Provide per node/content type access control
*/
/**
* Implements hook_config_info().
*/
function nodeaccess_config_info() {
$prefixes['nodeaccess.settings'] = array(
'label' => t('Nodeaccess Settings'),
'group' => t('Configuration'),
);
return $prefixes;
}
/**
* Implements hook_menu().
*
* @return array
*/
function nodeaccess_menu() {
$items = array();
$items['admin/config/people/nodeaccess'] = array(
'title' => 'Nodeaccess',
'description' => 'Change default settings for the Nodeaccess module.',
'page arguments' => array('nodeaccess_admin_form'),
'page callback' => 'backdrop_get_form',
'file' => 'nodeaccess.admin.inc',
'access arguments' => array('administer nodeaccess'),
);
$items['node/%node/grant'] = array(
'title' => 'Grant',
'page callback' => 'backdrop_get_form',
'page arguments' => array('nodeaccess_grants_form', 1),
'access callback' => 'nodeaccess_access',
'access arguments' => array('grant', 1),
'weight' => 5,
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Implements hook_admin_paths().
*
* @return array
*/
function nodeaccess_admin_paths() {
$paths = array(
'node/*/grant' => TRUE,
'admin/config/people/nodeaccess' => TRUE,
);
return $paths;
}
/**
* Implements hook_permission().
*
* @return array
*/
function nodeaccess_permission() {
return array(
'administer nodeaccess' => array(
'title' => t('Administer Nodeaccess'),
),
'grant node permissions' => array(
'title' => t('Grant Node Permissions'),
),
'grant editable node permissions' => array(
'title' => t('Grant node edit permissions'),
),
'grant deletable node permissions' => array(
'title' => t('Grant node delete permissions'),
),
'grant own node permissions' => array(
'title' => t('Grant own node permissions'),
),
);
}
/**
* Implements hook_node_access().
*
* @param $node
* @param $op
* @param $account
*
* @return string
*/
function nodeaccess_node_access($node, $op, $account) {
$anonymous_rid = nodeaccess_get_role_id_from_machine(BACKDROP_ANONYMOUS_ROLE);
switch ($op) {
case 'update':
case 'delete':
if (!isset($account->uid)) {
global $user;
$account = $user;
}
// If the node belongs to a deleted user.
if ($account->uid == 0 && $node->uid == 0) {
$grants = nodeaccess_get_grants($node);
// We check if the role has particular access to this node.
// If anonymous has rights to this node, we allow them.
if ((!empty($grants['rid'][$anonymous_rid]['grant_update']) && $op == 'update') ||
(!empty($grants['rid'][$anonymous_rid]['grant_delete']) && $op == 'delete')) {
return NODE_ACCESS_ALLOW;
}
return NODE_ACCESS_DENY;
}
break;
default:
break;
}
}
/**
* Determine access to Grant tab.
*
* @param $op
* @param $node
* @param $account
*
* @return bool
* Whether the user has access to the grant tab.
*/
function nodeaccess_access($op, $node, $account = NULL) {
// Load in our configurations
$config = config('nodeaccess.settings');
global $user;
if (empty($node)) {
return FALSE;
}
// If no user object is supplied, the access check is for the current user.
if (empty($account)) {
$account = $user;
}
$allowed_types = $config->get('types');
if ($op == 'grant') {
if ($node->nid && isset($allowed_types[$node->type]) && !empty($allowed_types[$node->type]) &&
(user_access('grant node permissions', $account) ||
(user_access('grant editable node permissions', $account) && node_access('update', $node, $account)) ||
(user_access('grant deletable node permissions', $account) && node_access('delete', $node, $account)) ||
(user_access('grant own node permissions', $account) && ($account->uid == $node->uid)))) {
return TRUE;
}
}
return FALSE;
}
/**
* Implements hook_theme().
*
* @param $existing
* @param $type
* @param $theme
* @param $path
*
* @return array
*/
function nodeaccess_theme($existing, $type, $theme, $path) {
return array(
'nodeaccess_admin_form_types' => array(
'render element' => 'form',
),
'nodeaccess_grants_form' => array(
'render element' => 'form',
),
);
}
/**
* Menu callback. Draws the grant tab.
*
* @param $form
* @param $form_state
* @param $node
*
* @return mixed
*/
function nodeaccess_grants_form($form, &$form_state, $node) {
if (isset($form_state['values'])) {
$form_values = $form_state['values'];
}
if (!isset($form_values)) {
$form_values = array();
$grants = nodeaccess_get_grants($node);
$form_values['rid'] = isset($grants['rid']) ? $grants['rid'] : array();
$form_values['uid'] = isset($grants['uid']) ? $grants['uid'] : array();
}
elseif ($form_values['keys']) {
$name = preg_replace('!\*+!', '%', $form_values['keys']);
$query = db_select('users', 'u')
->fields('u', array('uid', 'name'))
->condition('name', $name, "LIKE");
if (isset($form_values['uid']) && count($form_values['uid'])) {
$uid = array_keys($form_values['uid']);
$query->condition('uid', $uid, 'NOT IN');
}
$authenticated_rid = nodeaccess_get_role_id_from_machine(BACKDROP_AUTHENTICATED_ROLE);
$result = $query->execute();
while ($account = $result->fetch()) {
$form_values['uid'][$account->uid] = array(
'name' => $account->name,
'keep' => 1,
'grant_view' => isset($form_values['rid'][$authenticated_rid]['grant_view']) ?
$form_values['rid'][$authenticated_rid]['grant_view'] : 0,
'grant_update' => isset($form_values['rid'][$authenticated_rid]['grant_update']) ?
$form_values['rid'][$authenticated_rid]['grant_update'] : 0,
'grant_delete' => isset($form_values['rid'][$authenticated_rid]['grant_delete']) ?
$form_values['rid'][$authenticated_rid]['grant_delete'] : 0,
);
}
}
if (!isset($form_values['rid'])) {
$form_values['rid'] = array();
}
if (!isset($form_values['uid'])) {
$form_values['uid'] = array();
}
$roles = $form_values['rid'];
$users = $form_values['uid'];
$form['nid'] = array(
'#type' => 'hidden',
'#value' => $node->nid,
);
// Open our Backdrop configuration for the module and read in
// the settings.
$config = config('nodeaccess.settings');
$allowed_roles = $config->get('allowed_roles');
$allowed_grants = $config->get('allowed_grants');
$role_map = $config->get('role_map');
// If $preserve is TRUE, the fields the user is not allowed to view or
// edit are included in the form as hidden fields to preserve them.
$preserve = $config->get('preserve', TRUE);
// Roles table.
if (is_array($roles)) {
$form['rid'] = array('#tree' => TRUE);
foreach ($roles as $key => $field) {
// $key = nodeaccess_get_role_id_from_machine($key);
if (!empty($allowed_roles[$key])) {
$form['rid'][$key]['name'] = array(
'#type' => 'hidden',
'#value' => $field['name'],
);
if ($allowed_grants['view']) {
$form['rid'][$key]['grant_view'] = array(
'#type' => 'checkbox',
'#default_value' => $field['grant_view'],
);
}
elseif ($preserve) {
$form['rid'][$key]['grant_view'] = array(
'#type' => 'hidden',
'#value' => (int) $field['grant_view'],
);
}
if ($allowed_grants['edit']) {
$form['rid'][$key]['grant_update'] = array(
'#type' => 'checkbox',
'#default_value' => $field['grant_update'],
);
}
elseif (!empty($preserve)) {
$form['rid'][$key]['grant_update'] = array(
'#type' => 'hidden',
'#value' => (int) $field['grant_update'],
);
}
if ($allowed_grants['delete']) {
$form['rid'][$key]['grant_delete'] = array(
'#type' => 'checkbox',
'#default_value' => $field['grant_delete'],
);
}
elseif ($preserve) {
$form['rid'][$key]['grant_delete'] = array(
'#type' => 'hidden',
'#value' => (int) $field['grant_delete'],
);
}
}
elseif ($preserve) {
$form['rid'][$key]['name'] = array(
'#type' => 'hidden',
'#value' => (int) $field['name'],
);
$form['rid'][$key]['grant_view'] = array(
'#type' => 'hidden',
'#value' => (int) $field['grant_view'],
);
$form['rid'][$key]['grant_update'] = array(
'#type' => 'hidden',
'#value' => (int) $field['grant_update'],
);
$form['rid'][$key]['grant_delete'] = array(
'#type' => 'hidden',
'#value' => (int) $field['grant_delete'],
);
}
}
}
// Users table.
if (is_array($users)) {
$form['uid'] = array('#tree' => TRUE);
foreach ($users as $key => $field) {
$form['uid'][$key]['name'] = array(
'#type' => 'hidden',
'#value' => $field['name'],
);
$form['uid'][$key]['keep'] = array(
'#type' => 'checkbox',
'#default_value' => $field['keep'],
);
if ($allowed_grants['view']) {
$form['uid'][$key]['grant_view'] = array(
'#type' => 'checkbox',
'#default_value' => $field['grant_view'],
);
}
elseif ($preserve) {
$form['uid'][$key]['grant_view'] = array(
'#type' => 'hidden',
'#value' => (int) $field['grant_view'],
);
}
if ($allowed_grants['edit']) {
$form['uid'][$key]['grant_update'] = array(
'#type' => 'checkbox',
'#default_value' => $field['grant_update'],
);
}
elseif ($preserve) {
$form['uid'][$key]['grant_update'] = array(
'#type' => 'hidden',
'#value' => (int) $field['grant_update'],
);
}
if ($allowed_grants['delete']) {
$form['uid'][$key]['grant_delete'] = array(
'#type' => 'checkbox',
'#default_value' => $field['grant_delete'],
);
}
elseif ($preserve) {
$form['uid'][$key]['grant_delete'] = array(
'#type' => 'hidden',
'#value' => (int) $field['grant_delete'],
);
}
}
}
// Autocomplete returns errors if users don't have access to profiles.
if (user_access('access user profiles')) {
$form['keys'] = array(
'#type' => 'textfield',
'#default_value' => isset($form_values['keys']) ? $form_values['keys'] : '',
'#size' => 40,
'#autocomplete_path' => 'user/autocomplete',
);
}
else {
$form['keys'] = array(
'#type' => 'textfield',
'#default_value' => isset($form_values['keys'])? $form_values['keys'] : '',
'#size' => 40,
);
}
$form['search'] = array(
'#type' => 'submit',
'#value' => t('Search'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save Grants'),
);
return $form;
}
/**
* Validate function for nodeaccess_grants_form.
*
* @param array $form
* @param array &$form_state
*/
function nodeaccess_grants_form_validate($form, &$form_state) {
$form_values = &$form_state['values'];
// Delete unkept users.
if (isset($form_values['uid']) && is_array($form_values['uid'])) {
foreach ($form_values['uid'] as $uid => $row) {
if (!$row['keep']) {
unset($form_values['uid'][$uid]);
}
}
}
if (!isset($form_values['uid'])) {
unset($form_values['uid']);
}
}
/**
* Submit function for nodeaccess_grants_form.
*
* @param array $form
* @param array &$form_state
*/
function nodeaccess_grants_form_submit($form, &$form_state) {
if ($form_state['clicked_button']['#id'] == 'edit-search') {
$form_state['rebuild'] = TRUE;
$form_state['storage']['values'] = $form_state['values'];
}
else {
unset($form_state['rebuild']);
_nodeaccess_grants_form_submit($form, $form_state);
backdrop_set_message(t('Grants saved.'));
}
}
/**
* Private function to submit the per-node grants table.
*
* @param array $form
* @param array &$form_state
*/
function _nodeaccess_grants_form_submit($form, &$form_state) {
$form_values = &$form_state['values'];
$grants = array();
$nid = $form_values['nid'];
$node = node_load($nid);
foreach (array('uid', 'rid') as $type) {
$realm = 'nodeaccess_' . $type;
if (isset($form_values[$type]) && is_array($form_values[$type])) {
foreach ($form_values[$type] as $gid => $line) {
$grant = array(
'gid' => $gid,
'realm' => $realm,
'grant_view' => empty($line['grant_view']) ? 0 : 1,
'grant_update' => empty($line['grant_update']) ? 0 : 1,
'grant_delete' => empty($line['grant_delete']) ? 0 : 1,
);
$grants[] = $grant;
}
}
}
nodeaccess_set_grants($node, $grants);
}
/**
* Theme function for nodeaccess_grants_form.
*
* @param array $vars
*
* @return string
*/
function theme_nodeaccess_grants_form($vars) {
// Load in our configurations
$config = config('nodeaccess.settings');
$role_map = $config->get('role_map');
$output = '';
$form = $vars['form'];
$rows = array();
$allowed_roles = $config->get('allowed_roles');
$allowed_grants = $config->get('allowed_grants');
// Retrieve role names for columns.
$roles = user_roles();
// Roles table.
$roles = element_children($form['rid']);
if (count($roles) && count($allowed_roles)) {
$header = array();
$header[] = t('Role');
if ($allowed_grants['view']) {
$header[] = t('View');
}
if ($allowed_grants['edit']) {
$header[] = t('Edit');
}
if ($allowed_grants['delete']) {
$header[] = t('Delete');
}
foreach ($roles as $key => $role) {
if (!empty($allowed_roles[$role])) {
$row = array();
$row[] = $role_map[$role]['name'] . backdrop_render($form['rid'][$role]['name']);
if ($allowed_grants['view']) {
$row[] = backdrop_render($form['rid'][$role]['grant_view']);
}
if ($allowed_grants['edit']) {
$row[] = backdrop_render($form['rid'][$role]['grant_update']);
}
if ($allowed_grants['delete']) {
$row[] = backdrop_render($form['rid'][$role]['grant_delete']);
}
$rows[] = $row;
}
}
$output .= theme('table', array('header' => $header, 'rows' => $rows));
}
// Change search button to "add user" to clarify what the button does.
$form['search']['#value'] = t('Add User');
// Search form.
$output .= '<p><div class="search-form">';
$output .= '<strong>' . t('User specific grants. Enter names to search for users:') . '</strong><br />';
$output .= t('If a user has no grants, they will be removed from the table below. This is because denies are implicit. You cannot deny access to a user if their role has access. Neither Backdrop or Drupal work that way.');
$output .= '<div class="container-inline">';
$output .= backdrop_render($form['keys']);
$output .= backdrop_render($form['search']);
$output .= '</div></div></p>';
// Users table.
unset($rows);
$users = element_children($form['uid']);
if (count($users) > 0) {
$header = array();
$rows = array();
$header[] = t('User');
$header[] = t('Keep?');
if ($allowed_grants['view']) {
$header[] = t('View');
}
if ($allowed_grants['edit']) {
$header[] = t('Edit');
}
if ($allowed_grants['delete']) {
$header[] = t('Delete');
}
foreach ($users as $key) {
$row = array();
$row[] = $form['uid'][$key]['name']['#value'];
$row[] = backdrop_render($form['uid'][$key]['keep']);
if ($allowed_grants['view']) {
$row[] = backdrop_render($form['uid'][$key]['grant_view']);
}
if ($allowed_grants['edit']) {
$row[] = backdrop_render($form['uid'][$key]['grant_update']);
}
if ($allowed_grants['delete']) {
$row[] = backdrop_render($form['uid'][$key]['grant_delete']);
}
$rows[] = $row;
}
$output .= theme('table', array('header' => $header, 'rows' => $rows));
}
else {
$header = array(t('There are no user specific grants for this node.'));
$rows = array();
$output .= theme('table', array('header' => $header, 'rows' => $rows));
}
$output .= backdrop_render_children($form);
return $output;
}
/**
* Implements hook_node_grants().
*
* @param Object $account
* @parap string $op
*
* @return array
*/
function nodeaccess_node_grants($account, $op) {
$roles = array();
foreach ($account->roles as $key => $role) {
$roles[] = nodeaccess_get_role_id_from_machine($role);
}
return array(
'nodeaccess_rid' => $roles,
'nodeaccess_uid' => array($account->uid),
'nodeaccess_author' => array($account->uid),
);
}
/**
* Implements hook_node_update().
*
* @param Object $node
*/
function nodeaccess_node_update($node) {
// Load in our configurations
$config = config('nodeaccess.settings');
if (module_exists('user_reference')) {
$fields = $config->get('nodeaccess_' . $node->type . '_user_reference');
foreach (array_keys($fields) as $field_name) {
if (isset($node->$field_name)) {
$old_node = node_load($node->nid);
// Delete the old user as it's changed.
if ($node->$field_name != $old_node->$field_name) {
nodeaccess_delete_user_reference($old_node);
nodeaccess_insert_user_reference($node);
}
break;
}
}
}
// Done, author permissions are not written into nodeaccess.
}
/**
* Implements hook_node_delete().
*
* @param Object $node
*/
function nodeaccess_node_delete($node) {
// Deleting node, delete related permissions.
nodeaccess_delete_grants($node);
}
/**
* Implements hook_node_access_records().
*
* @param Object $node
*
* @return array|NULL
*/
function nodeaccess_node_access_records($node) {
// Load in our configurations
$config = config('nodeaccess.settings');
if (nodeaccess_disabling() || !$node->status) {
return NULL;
}
// Need to find out if node has own grants or whether to use defaults.
$default = $config->get('nodeaccess_' . $node->type);
// Setup default keys that are required by node_access_write_grants().
$grant_defaults = array(
'gid' => 0,
'realm' => 'nodeaccess_rid',
'grant_view' => 0,
'grant_update' => 0,
'grant_delete' => 0,
'priority' => $config->get('priority'),
);
$query = db_select('nodeaccess', 'n')
->fields('n', array('gid', 'realm', 'grant_view', 'grant_update', 'grant_delete'))
->condition('nid', $node->nid, '=');
$result = $query->execute();
if (!$result->rowCount()) {
// Node has no own grants, use defaults.
foreach ($default as $key => $role) {
$gid = nodeaccess_get_role_id_from_machine($key);
$default[$key]['gid'] = $gid;
}
$grants = $default;
}
else {
// Node has own grants, use them.
$grants = array();
while ($row = $result->fetchAssoc()) {
$grants[] = $row;
}
}
// Apply author grants.
$author_prefs = $config->get('authors');
// Array is pre-populated with grant values.
if (!empty($author_prefs[$node->type])) {
$grant = $author_prefs[$node->type];
}
else {
$grant = array(
'grant_view' => 0,
'grant_update' => 0,
'grant_delete' => 0,
);
}
$grant['gid'] = $node->uid;
$grant['realm'] = 'nodeaccess_author';
// Include author grant even with all values FALSE, it may be
// needed to overwrite an older value.
$grants[] = $grant;
foreach ($grants as $id => $grant) {
// Merge missing default grant keys.
$grants[$id] = $grants[$id] + $grant_defaults;
}
return $grants;
}
/**
* Mark module to be in process of disabling.
*
* Prevents entries being saved to node_access while module is being disabled.
*
* @param mixed $set
* A boolean set or NULL to not change status.
*
* @return bool
*/
function nodeaccess_disabling($set = NULL) {
static $disabling = FALSE;
if ($set !== NULL) {
$disabling = $set;
}
return $disabling;
}
/**
* Implements hook_node_type_delete().
*
* @param Object $info
*/
function nodeaccess_node_type_delete($info) {
// Load in our configurations
$config = config('nodeaccess.settings');
// Node type is being deleted, delete its preferences.
$authors = $config->get('authors');
$types = $config->get('types');
$config->clear('nodeaccess_' . $info->type);
unset($authors[$info->type]);
unset($types[$info->type]);
$config->set('authors', $authors);
$config->set('types', $types);
$config->save();
}
/**
* Implements hook_node_type_update().
*
* @param Object $info
*/
function nodeaccess_node_type_update($info) {
// Load in our configurations
$config = config('nodeaccess.settings');
// Node type has changed, move preferences to new type.
if (!empty($info->old_type) && $info->old_type != $info->type) {
$setting = $config->get('nodeaccess_' . $info->old_type, array());
$config->clear('nodeaccess_' . $info->old_type);
$config->set('nodeaccess_' . $info->type, $setting);
$author_prefs = $config->get('authors', array());
$author_prefs[$info->type] = array(
'grant_view' => $author_prefs[$info->old_type]['grant_view'],
'grant_update' => $author_prefs[$info->old_type]['grant_update'],
'grant_delete' => $author_prefs[$info->old_type]['grant_delete'],
);
unset($author_prefs[$info->old_type]);
$config->set('authors', $author_prefs);
$config->save();
}
}
/**
* Implements hook_node_type_insert().
*
* @param Object $info
*/
function nodeaccess_node_type_insert($info) {
// Load in our configurations
$config = config('nodeaccess.settings');
$anonymous_rid = nodeaccess_get_role_id_from_machine(BACKDROP_ANONYMOUS_ROLE);
$authenticated_rid = nodeaccess_get_role_id_from_machine(BACKDROP_AUTHENTICATED_ROLE);
$role_perms = array();
$roles = array(
$anonymous_rid => BACKDROP_ANONYMOUS_ROLE,
$authenticated_rid => BACKDROP_AUTHENTICATED_ROLE,
);
// New node type, default to our default content type permissions.
foreach ($roles as $rid => $machine) {
$permissions = user_role_permissions(array($machine));
$role_perms[$rid]['access content'] = in_array('access content', $permissions);
}
$grants[] = array(
'gid' => $anonymous_rid,
'realm' => 'nodeaccess_rid',
'grant_view' => $role_perms[$anonymous_rid]['access content'],
'grant_update' => 0,
'grant_delete' => 0,
);
$grants[] = array(
'gid' => $authenticated_rid,
'realm' => 'nodeaccess_rid',
'grant_view' => $role_perms[$authenticated_rid]['access content'],
'grant_update' => 0,
'grant_delete' => 0,
);
$config->set('nodeaccess_' . $info->type, $grants);
// Add permissions for author.
$author_prefs = $config->get('authors');
$author_prefs[$info->type] = array(
'grant_view' => 0,
'grant_update' => 0,
'grant_delete' => 0,
);
$config->set('authors', $author_prefs);
$config->save();
node_access_needs_rebuild(TRUE);
}
/**
* Insert user_reference grants from a node.
*
* @param Object $node
*/
function nodeaccess_insert_user_reference($node) {
// Load in our configurations
$config = config('nodeaccess.settings');
$form_values = nodeaccess_get_grants($node);
// Now, append or overwrite the uid with what was specified in the user
// reference field.
$fields = $config->get('nodeaccess_' . $node->type . '_user_reference');
foreach ($fields as $field_name => $field) {
$user_uids = field_get_items('node', $node, $field_name);
$user_references = user_load_multiple($user_uids);
// Add each of the referenced users a form value.
foreach ($user_references as $user) {
$form_values['uid'][$user->uid] = array(
'name' => $user->name,
'keep' => 1,
'grant_view' => $field['grant_view'],
'grant_update' => $field['grant_update'],
'grant_delete' => $field['grant_delete'],
);
}
}
// Only do the changes if there are users to save.
if (count($form_values['uid']) > 0) {
$form_values['nid'] = $node->nid;
$form_state = array('values' => $form_values);
_nodeaccess_grants_form_submit(NULL, $form_state);
}
}
/**
* Delete all userreference user grants from a node.
*
* @param Object $node
*/
function nodeaccess_delete_user_reference($node) {
// Load in our configurations
$config = config('nodeaccess.settings');
$form_values = nodeaccess_get_grants($node);
// Now, append or overwrite the uid with what was specified in the user
// reference field.
$fields = $config->get('nodeaccess_' . $node->type . '_user_reference');
foreach ($fields as $field_name => $field) {
$user_uids = field_get_items('node', $node, $field_name);
$user_references = user_load_multiple($user_uids);
foreach ($user_references as $user) {
unset($form_values['uid'][$user->uid]);
}
}
$form_values['nid'] = $node->nid;
$form_state = array('values' => $form_values);
_nodeaccess_grants_form_submit(NULL, $form_state);
}
/**
* Return the grants applied to a node object used for Grant form.
*
* @param Object $node
*
* @return array
* An array of grants with keys 'rid' for roles and 'uid' for users.
*/
function nodeaccess_get_grants($node) {
$grants = array();
// Load grants by roles.
$roles = user_roles();
foreach ($roles as $machine => $name) {
$key = nodeaccess_get_role_id_from_machine($machine);
$query = db_select('nodeaccess', 'na');
// AND na.realm = :realm AND na.nid = :nid', array(':realm' => 'nodeaccess_rid', ':nid' => $node->nid));
$query->fields('na', array('grant_view', 'grant_update', 'grant_delete'));
$query->condition('na.realm', 'nodeaccess_rid', '=');
$query->condition('na.nid', $node->nid, '=');
$query->condition('na.gid', $key, '=');
$result = $query->execute();
$grant = $result->fetchObject();
if (!empty($grant)) {
$grants['rid'][$key] = array(
'name' => empty($name) ? '' : check_plain($name),
'grant_view' => (bool) $grant->grant_view,
'grant_update' => (bool) $grant->grant_update,
'grant_delete' => (bool) $grant->grant_delete,
);
}
else {
$grant = nodeaccess_get_default_grants($node->type, $machine);
$grants['rid'][$key] = array(
'name' => empty($name) ? '' : check_plain($name),
'grant_view' => (bool) $grant->grant_view,
'grant_update' => (bool) $grant->grant_update,
'grant_delete' => (bool) $grant->grant_delete,
);
}
}
// Load grants by users
$query = db_select('node_access', 'na');
$query->join('users', 'u', 'u.uid = na.gid');
$query->fields('u', array('uid', 'name'));
$query->fields('na', array('grant_view', 'grant_update', 'grant_delete'));
$query->condition('na.nid', $node->nid, '=');
$query->condition('na.realm', 'nodeaccess_uid', '=');
$query->orderBy('name');
$result = $query->execute();
while ($account = $result->fetch()) {
$grants['uid'][$account->uid] = array(
'name' => $account->name,
'keep' => TRUE,
'grant_view' => (bool) $account->grant_view,
'grant_update' => (bool) $account->grant_update,
'grant_delete' => (bool) $account->grant_delete,
);
}
return $grants;
}
/**
* Set all grants for a node to nodeaccess table and acquire them.
*
* @param $node
* node object the grants are being applied to.
* @param array $grants
* array of grants as defined (@see nodeaccess_save_grant).
*/
function nodeaccess_set_grants($node, $grants = array()) {
// Allow other modules to edit all grants for array.
backdrop_alter('nodeaccess_grants', $grants, $node);
nodeaccess_delete_grants($node);
if (count($grants)) {
foreach ($grants as $grant) {
// Allow other modules to edit single grants.
backdrop_alter('nodeaccess_grant', $grant, $node);
nodeaccess_save_grant($node, $grant);
}
}
node_access_acquire_grants($node);
}
/**
* Delete all grants from nodeaccess table for this node.
*
* @param $node
* node object whose grants are being revoked.
*/
function nodeaccess_delete_grants($node) {
try {
db_delete('nodeaccess')
->condition('nid', $node->nid)