-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApiUserRights.php
1386 lines (1322 loc) · 54.2 KB
/
ApiUserRights.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
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
namespace YaleREDCap\ApiUserRights;
use DateTimeRC;
/**
* @property \ExternalModules\Framework $framework
* @see Framework
*/
require_once 'CsvImporter.php';
require_once 'RightsChecker.php';
class ApiUserRights extends \ExternalModules\AbstractExternalModule
{
/**
* All API methods
* @var array<int, array{
* area: string,
* langCodeArea: string,
* method: string,
* content: string,
* action: string,
* data: string|bool
* methodCode: string,
* methodCodeRC: string,
* langCode: string,
* langCodeInternal: string
* }>
*/
static array $methods = [
// Arms
[
"area" => "Arms",
"langCodeArea" => "api_97",
"method" => "Export Arms",
"content" => "arm",
"action" => "export",
"data" => "",
"methodCode" => "arm_export",
"methodCodeRC" => "exp_arms",
"langCode" => "api_63",
"langCodeInternal" => ""
],
[
"area" => "Arms",
"langCodeArea" => "api_97",
"method" => "Import Arms",
"content" => "arm",
"action" => "import",
"data" => "",
"methodCode" => "arm_import",
"methodCodeRC" => "imp_arms",
"langCode" => "api_82",
"langCodeInternal" => ""
],
[
"area" => "Arms",
"langCodeArea" => "api_97",
"method" => "Delete Arms",
"content" => "arm",
"action" => "delete",
"data" => "",
"methodCode" => "arm_delete",
"methodCodeRC" => "del_arms",
"langCode" => "api_82",
"langCodeInternal" => ""
],
// Data Access Groups
[
"area" => "Data Access Groups",
"langCodeArea" => "global_22",
"method" => "Export DAGs",
"content" => "dag",
"action" => "export",
"data" => "",
"methodCode" => "dag_export",
"methodCodeRC" => "exp_dags",
"langCode" => "api_152",
"langCodeInternal" => ""
],
[
"area" => "Data Access Groups",
"langCodeArea" => "global_22",
"method" => "Import DAGs",
"content" => "dag",
"action" => "import",
"data" => "",
"methodCode" => "dag_import",
"methodCodeRC" => "imp_dags",
"langCode" => "api_153",
"langCodeInternal" => ""
],
[
"area" => "Data Access Groups",
"langCodeArea" => "global_22",
"method" => "Delete DAGs",
"content" => "dag",
"action" => "delete",
"data" => "",
"methodCode" => "dag_delete",
"methodCodeRC" => "del_dags",
"langCode" => "api_154",
"langCodeInternal" => ""
],
[
"area" => "Data Access Groups",
"langCodeArea" => "global_22",
"method" => "Switch DAGs",
"content" => "dag",
"action" => "switch",
"data" => "",
"methodCode" => "dag_switch",
"methodCodeRC" => "switch_dag",
"langCode" => "api_186",
"langCodeInternal" => ""
],
[
"area" => "Data Access Groups",
"langCodeArea" => "global_22",
"method" => "Export User-DAG Assignment",
"content" => "userDagMapping",
"action" => "export",
"data" => "",
"methodCode" => "userDagMapping_export",
"methodCodeRC" => "exp_user_dag_maps",
"langCode" => "api_155",
"langCodeInternal" => ""
],
[
"area" => "Data Access Groups",
"langCodeArea" => "global_22",
"method" => "Import User-DAG Assignment",
"content" => "userDagMapping",
"action" => "import",
"data" => true,
"methodCode" => "userDagMapping_import",
"methodCodeRC" => "imp_user_dag_maps",
"langCode" => "api_156",
"langCodeInternal" => ""
],
// Events
[
"area" => "Events",
"langCodeArea" => "global_45",
"method" => "Export Events",
"content" => "event",
"action" => "export",
"data" => "",
"methodCode" => "event_export",
"methodCodeRC" => "exp_events",
"langCode" => "api_62",
"langCodeInternal" => ""
],
[
"area" => "Events",
"langCodeArea" => "global_45",
"method" => "Import Events",
"content" => "event",
"action" => "import",
"data" => "",
"methodCode" => "event_import",
"methodCodeRC" => "imp_events",
"langCode" => "api_84",
"langCodeInternal" => ""
],
[
"area" => "Events",
"langCodeArea" => "global_45",
"method" => "Delete Events",
"content" => "event",
"action" => "delete",
"data" => "",
"methodCode" => "event_delete",
"methodCodeRC" => "del_events",
"langCode" => "api_85",
"langCodeInternal" => ""
],
// Field Names
[
"area" => "Field Names",
"langCodeArea" => "api_98",
"method" => "Export List of Export Field Names",
"content" => "exportFieldNames",
"action" => "export",
"data" => "",
"methodCode" => "exportFieldNames_export",
"methodCodeRC" => "exp_field_names",
"langCode" => "api_52",
"langCodeInternal" => ""
],
// Files
[
"area" => "Files",
"langCodeArea" => "api_99",
"method" => "Export a File",
"content" => "file",
"action" => "export",
"data" => "",
"methodCode" => "file_export",
"methodCodeRC" => "exp_file",
"langCode" => "api_53",
"langCodeInternal" => ""
],
[
"area" => "Files",
"langCodeArea" => "",
"method" => "Import a File",
"content" => "file",
"action" => "import",
"data" => "",
"methodCode" => "file_import",
"methodCodeRC" => "imp_file",
"langCode" => "api_54",
"langCodeInternal" => ""
],
[
"area" => "Files",
"langCodeArea" => "",
"method" => "Delete a File",
"content" => "file",
"action" => "delete",
"data" => "",
"methodCode" => "file_delete",
"methodCodeRC" => "del_file",
"langCode" => "api_55",
"langCodeInternal" => ""
],
// File Repository
[
"area" => "File Repository",
"langCodeArea" => "app_04",
"method" => "Create a New Folder in the File Repository",
"content" => "fileRepository",
"action" => "createFolder",
"data" => "",
"methodCode" => "fileRepository_createFolder",
"methodCodeRC" => "create_folder_file_repo",
"langCode" => "api_209",
"langCodeInternal" => ""
],
[
"area" => "File Repository",
"langCodeArea" => "app_04",
"method" => "Export a List of Files/Folders from the File Repository",
"content" => "fileRepository",
"action" => "list",
"data" => "",
"methodCode" => "fileRepository_list",
"methodCodeRC" => "exp_list_file_repo",
"langCode" => "api_204",
"langCodeInternal" => ""
],
[
"area" => "File Repository",
"langCodeArea" => "app_04",
"method" => "Export a File from the File Repository",
"content" => "fileRepository",
"action" => "export",
"data" => "",
"methodCode" => "fileRepository_export",
"methodCodeRC" => "exp_file_repo",
"langCode" => "api_193",
"langCodeInternal" => ""
],
[
"area" => "File Repository",
"langCodeArea" => "app_04",
"method" => "Import a File into the File Repository",
"content" => "fileRepository",
"action" => "import",
"data" => "",
"methodCode" => "fileRepository_import",
"methodCodeRC" => "imp_file_repo",
"langCode" => "api_194",
"langCodeInternal" => ""
],
[
"area" => "File Repository",
"langCodeArea" => "app_04",
"method" => "Delete a File from the File Repository",
"content" => "fileRepository",
"action" => "delete",
"data" => "",
"methodCode" => "fileRepository_delete",
"methodCodeRC" => "del_file_repo",
"langCode" => "api_195",
"langCodeInternal" => ""
],
// Instruments
[
"area" => "Instruments",
"langCodeArea" => "global_110",
"method" => "Export Instruments (Data Entry Forms)",
"content" => "instrument",
"action" => "export",
"data" => "",
"methodCode" => "instrument_export",
"methodCodeRC" => "exp_instr",
"langCode" => "api_56",
"langCodeInternal" => ""
],
[
"area" => "Instruments",
"langCodeArea" => "global_110",
"method" => "Export PDF file of Instruments",
"content" => "pdf",
"action" => "export",
"data" => "",
"methodCode" => "pdf_export",
"methodCodeRC" => "exp_instr_pdf",
"langCode" => "api_57",
"langCodeInternal" => ""
],
[
"area" => "Instruments",
"langCodeArea" => "global_110",
"method" => "Export Instrument-Event Mappings",
"content" => "formEventMapping",
"action" => "export",
"data" => "",
"methodCode" => "formEventMapping_export",
"methodCodeRC" => "exp_inst_event_maps",
"langCode" => "api_64",
"langCodeInternal" => ""
],
[
"area" => "Instruments",
"langCodeArea" => "global_110",
"method" => "Import Instrument-Event Mappings",
"content" => "formEventMapping",
"action" => "import",
"data" => true,
"methodCode" => "formEventMapping_import",
"methodCodeRC" => "imp_inst_event_maps",
"langCode" => "api_81",
"langCodeInternal" => ""
],
// Logging
[
"area" => "Logging",
"langCodeArea" => "app_07",
"method" => "Export Logging",
"content" => "log",
"action" => "export",
"data" => "",
"methodCode" => "log_export",
"methodCodeRC" => "exp_logging",
"langCode" => "api_158",
"langCodeInternal" => ""
],
// Metadata
[
"area" => "Metadata",
"langCodeArea" => "api_100",
"method" => "Export Metadata (Data Dictionary)",
"content" => "metadata",
"action" => "export",
"data" => "",
"methodCode" => "metadata_export",
"methodCodeRC" => "exp_metadata",
"langCode" => "api_51",
"langCodeInternal" => ""
],
[
"area" => "Metadata",
"langCodeArea" => "api_100",
"method" => "Import Metadata (Data Dictionary)",
"content" => "metadata",
"action" => "import",
"data" => true,
"methodCode" => "metadata_import",
"methodCodeRC" => "imp_metadata",
"langCode" => "api_79",
"langCodeInternal" => ""
],
// Projects
/**[
"area" => "Projects",
"method" => "Create Project",
"content" => "project",
"action" => "import",
"data" => true
],*/
[
"area" => "Projects",
"langCodeArea" => "control_center_134",
"method" => "Import Project Info",
"content" => "project_settings",
"action" => "import",
"data" => "",
"methodCode" => "project_settings_import",
"methodCodeRC" => "imp_proj_sett",
"langCode" => "api_133",
"langCodeInternal" => ""
],
[
"area" => "Projects",
"langCodeArea" => "control_center_134",
"method" => "Export Project Info",
"content" => "project",
"action" => "export",
"data" => "",
"methodCode" => "project_export",
"methodCodeRC" => "exp_proj",
"langCode" => "api_66",
"langCodeInternal" => ""
],
[
"area" => "Projects",
"langCodeArea" => "control_center_134",
"method" => "Export Project XML",
"content" => "project_xml",
"action" => "export",
"data" => "",
"methodCode" => "project_xml_export",
"methodCodeRC" => "exp_proj_xml",
"langCode" => "api_docs_252",
"langCodeInternal" => ""
],
// Records
[
"area" => "Records",
"langCodeArea" => "dashboard_38",
"method" => "Export Records",
"content" => "record",
"action" => "export",
"data" => "",
"methodCode" => "record_export",
"methodCodeRC" => "exp_records",
"langCode" => "api_48",
"langCodeInternal" => ""
],
[
"area" => "Records",
"langCodeArea" => "dashboard_38",
"method" => "Import Records",
"content" => "record",
"action" => "import",
"data" => true,
"methodCode" => "record_import",
"methodCodeRC" => "imp_records",
"langCode" => "api_50",
"langCodeInternal" => ""
],
[
"area" => "Records",
"langCodeArea" => "dashboard_38",
"method" => "Delete Records",
"content" => "record",
"action" => "delete",
"data" => "",
"methodCode" => "record_delete",
"methodCodeRC" => "del_records",
"langCode" => "api_130",
"langCodeInternal" => ""
],
[
"area" => "Records",
"langCodeArea" => "dashboard_38",
"method" => "Rename Record",
"content" => "record",
"action" => "rename",
"data" => "",
"methodCode" => "record_rename",
"methodCodeRC" => "rename_record",
"langCode" => "api_190",
"langCodeInternal" => ""
],
[
"area" => "Records",
"langCodeArea" => "dashboard_38",
"method" => "Generate Next Record Name",
"content" => "generateNextRecordName",
"action" => "export",
"data" => "",
"methodCode" => "generateNextRecordName_export",
"methodCodeRC" => "exp_next_id",
"langCode" => "api_134",
"langCodeInternal" => ""
],
// Repeating Instruments and Events
[
"area" => "Repeating Instruments and Events",
"langCodeArea" => "rep_forms_events_01",
"method" => "Export Repeating Instruments and Events",
"content" => "repeatingFormsEvents",
"action" => "export",
"data" => "",
"methodCode" => "repeatingFormsEvents_export",
"methodCodeRC" => "exp_repeating_forms_events",
"langCode" => "rep_forms_events_02",
"langCodeInternal" => ""
],
[
"area" => "Repeating Instruments and Events",
"langCodeArea" => "rep_forms_events_01",
"method" => "Import Repeating Instruments and Events",
"content" => "repeatingFormsEvents",
"action" => "import",
"data" => true,
"methodCode" => "repeatingFormsEvents_import",
"methodCodeRC" => "imp_repeating_forms_events",
"langCode" => "rep_forms_events_05",
"langCodeInternal" => ""
],
// Reports
[
"area" => "Reports",
"langCodeArea" => "app_06",
"method" => "Export Reports",
"content" => "report",
"action" => "export",
"data" => "",
"methodCode" => "report_export",
"methodCodeRC" => "exp_reports",
"langCode" => "api_49",
"langCodeInternal" => ""
],
// REDCap
[
"area" => "REDCap",
"langCodeArea" => "api_101",
"method" => "Export REDCap Version",
"content" => "version",
"action" => "export",
"data" => "",
"methodCode" => "version_export",
"methodCodeRC" => "exp_rc_v",
"langCode" => "api_67",
"langCodeInternal" => ""
],
// Surveys
[
"area" => "Surveys",
"langCodeArea" => "dashboard_69",
"method" => "Export a Survey Link",
"content" => "surveyLink",
"action" => "export",
"data" => "",
"methodCode" => "surveyLink_export",
"methodCodeRC" => "exp_surv_link",
"langCode" => "api_58",
"langCodeInternal" => ""
],
[
"area" => "Surveys",
"langCodeArea" => "dashboard_69",
"method" => "Export Survey Participants",
"content" => "participantList",
"action" => "export",
"data" => "",
"methodCode" => "participantList_export",
"methodCodeRC" => "exp_surv_parts",
"langCode" => "api_61",
"langCodeInternal" => ""
],
[
"area" => "Surveys",
"langCodeArea" => "dashboard_69",
"method" => "Export a Survey Queue Link",
"content" => "surveyQueueLink",
"action" => "export",
"data" => "",
"methodCode" => "surveyQueueLink_export",
"methodCodeRC" => "exp_surv_queue_link",
"langCode" => "api_59",
"langCodeInternal" => ""
],
[
"area" => "Surveys",
"langCodeArea" => "dashboard_69",
"method" => "Export a Survey Return Code",
"content" => "surveyReturnCode",
"action" => "export",
"data" => "",
"methodCode" => "surveyReturnCode_export",
"methodCodeRC" => "exp_surv_ret_code",
"langCode" => "api_60",
"langCodeInternal" => ""
],
// Users & User Privileges
[
"area" => "Users & User Privileges",
"langCodeArea" => "api_docs_228",
"method" => "Export Users",
"content" => "user",
"action" => "export",
"data" => "",
"methodCode" => "user_export",
"methodCodeRC" => "exp_users",
"langCode" => "api_65",
"langCodeInternal" => ""
],
[
"area" => "Users & User Privileges",
"langCodeArea" => "api_docs_228",
"method" => "Import Users",
"content" => "user",
"action" => "import",
"data" => true,
"methodCode" => "user_import",
"methodCodeRC" => "imp_users",
"langCode" => "api_80",
"langCodeInternal" => ""
],
[
"area" => "Users & User Privileges",
"langCodeArea" => "api_docs_228",
"method" => "Delete Users",
"content" => "user",
"action" => "delete",
"data" => "",
"methodCode" => "user_delete",
"methodCodeRC" => "del_users",
"langCode" => "api_159",
"langCodeInternal" => ""
],
// User Roles
[
"area" => "User Roles",
"langCodeArea" => "api_162",
"method" => "Export User Roles",
"content" => "userRole",
"action" => "export",
"data" => "",
"methodCode" => "userRole_export",
"methodCodeRC" => "exp_user_roles",
"langCode" => "api_163",
"langCodeInternal" => ""
],
[
"area" => "User Roles",
"langCodeArea" => "api_162",
"method" => "Import User Roles",
"content" => "userRole",
"action" => "import",
"data" => true,
"methodCode" => "userRole_import",
"methodCodeRC" => "imp_user_roles",
"langCode" => "api_164",
"langCodeInternal" => ""
],
[
"area" => "User Roles",
"langCodeArea" => "api_162",
"method" => "Delete User Roles",
"content" => "userRole",
"action" => "delete",
"data" => "",
"methodCode" => "userRole_delete",
"methodCodeRC" => "del_user_roles",
"langCode" => "api_165",
"langCodeInternal" => ""
],
[
"area" => "User Roles",
"langCodeArea" => "api_162",
"method" => "Export User-Role Assignment",
"content" => "userRoleMapping",
"action" => "export",
"data" => "",
"methodCode" => "userRoleMapping_export",
"methodCodeRC" => "exp_user_role_maps",
"langCode" => "api_172",
"langCodeInternal" => ""
],
[
"area" => "User Roles",
"langCodeArea" => "api_162",
"method" => "Import User-Role Assignment",
"content" => "userRoleMapping",
"action" => "import",
"data" => true,
"methodCode" => "userRoleMapping_import",
"methodCodeRC" => "imp_user_role_maps",
"langCode" => "api_173",
"langCodeInternal" => ""
]
];
/**
*
* All valid content values for REDCap API
* @var array
*/
static array $content = [
'record',
'metadata',
'file',
'fileRepository',
'filesize',
'fileinfo',
'repeatingFormsEvents',
'instrument',
'event',
'arm',
'user',
'project_settings',
'report',
'authkey',
'version',
'pdf',
'surveyLink',
'surveyQueueLink',
'surveyReturnCode',
'participantList',
'exportFieldNames',
'appRightsCheck',
'formEventMapping',
'fieldValidation',
'attachment',
'project',
'generateNextRecordName',
'project_xml',
'dag',
'userDagMapping',
'log',
'tableau',
'mycap',
'userRole',
'userRoleMapping'
];
/**
* All content values actually used in standard API methods
* @var array
*/
static array $contentUsed = [
'record',
'metadata',
'file',
'fileRepository',
'repeatingFormsEvents',
'instrument',
'event',
'arm',
'user',
'project_settings',
'report',
'version',
'pdf',
'surveyLink',
'surveyQueueLink',
'surveyReturnCode',
'participantList',
'exportFieldNames',
'formEventMapping',
'project',
'generateNextRecordName',
'project_xml',
'dag',
'userDagMapping',
'log',
'userRole',
'userRoleMapping'
];
// public function redcap_module_api_before($project_id, $content, $action)
// {
// $this->framework->log('API Before', [
// 'project_id' => $project_id,
// 'project_id2' => $this->framework->getProjectId(),
// 'project_id3' => $this->framework->getProject()->getProjectId(),
// 'user_id' => $this->framework->getUser()->getUsername(),
// 'content' => $content,
// 'action' => $action,
// ]);
// ob_start(function ($str) {
// $this->framework->log('API After', [ 'response' => $str ]);
// return $str;
// }, 0, PHP_OUTPUT_HANDLER_FLUSHABLE);
// //$this->framework->exitAfterHook();
// }
public function redcap_module_ajax($action, $payload, $project_id, $record, $instrument, $event_id, $repeat_instance, $survey_hash, $response_id, $survey_queue_hash, $page, $page_full, $user_id, $group_id)
{
try {
$user = $this->framework->getUser();
$rights = $user->getRights();
if ( ($rights['user_rights'] ?? '') != '1' ) {
return null;
}
if ( $action === 'getApiUserRights' ) {
$headerInfo = $this->getTableHeader();
$users = $this->getAllUsers($project_id);
return [
'methodOrder' => $headerInfo['methodOrder'],
'methodCodes' => $headerInfo['methodCodes'],
'users' => $users
];
} elseif ( $action === 'saveApiUserRights' ) {
$userToSet = $payload['user'] ?? '';
$rights = $payload['rights'] ?? [];
$rightsChecker = new RightsChecker($this, $userToSet, $rights);
if ( $rightsChecker->isValid() ) {
$this->saveRights($userToSet, $rights, $project_id);
return true;
} else {
return false;
}
} elseif ( $action === 'importRightsCsv' ) {
$importer = new CsvImporter($this, $payload['data'] ?? '', $project_id);
$importer->parseCsvString();
$contentsValid = $importer->contentsValid();
if ( $contentsValid !== true ) {
return [
'status' => 'error',
'data' => [
'errors' => $importer->errorMessages,
'badUsers' => $importer->badUsers,
'badMethods' => $importer->badMethods
]
];
}
if ( filter_var($payload['confirm'], FILTER_VALIDATE_BOOL) ) {
$this->saveSnapshot($project_id);
$this->logEvent('API User Rights Imported');
return [
'status' => 'ok',
'data' => $importer->import()
];
} else {
return [
'status' => 'ok',
'data' => $importer->getUpdateTable()
];
}
} elseif ( $action === 'saveApiUserRightsSnapshot' ) {
try {
$this->saveSnapshot($project_id);
return [ "success" => true, "tstext" => $this->getLastSnapshotText() ];
} catch ( \Throwable $e ) {
$this->framework->log('Error saving snapshot', [ 'error' => $e->getMessage() ]);
return [ "success" => false ];
}
} elseif ( $action === 'getSnapshotsInfo' ) {
try {
$snapshots = $this->getSnapshotsInfo($project_id);
return [ "success" => true, "snapshots" => $snapshots ];
} catch ( \Throwable $e ) {
$this->framework->log('Error getting snapshots', [ 'error' => $e->getMessage() ]);
return [ "success" => false ];
}
} elseif ( $action === 'downloadSnapshot' ) {
$snapshotId = $payload['snapshotId'] ?? '';
$data = $this->downloadSnapshot($snapshotId, $project_id);
$this->logEvent('API User Rights Snapshot Downloaded', [ 'Snapshot Timestamp' => $data['ts'] ]);
return [ "success" => true, "snapshot" => $data['snapshot'], "tsFormatted" => $data['tsFormatted'] ];
}
} catch ( \Throwable $e ) {
$this->framework->log('Ajax error', [ 'error' => $e->getMessage() ]);
}
}
public function redcap_module_configuration_settings($project_id, $settings)
{
try {
if ( empty($project_id) ) {
$settingName = 'default-rights-json-system';
$currentDefaultRightsString = $this->framework->getSystemSetting($settingName) ?? '{}';
$defaultRightsTitle = "Define the set of default API User Rights for the system";
$defaultRightsDesc = "These rights will be applied to all users who have not been assigned specific rights. These can be overridden in each project.";
} else {
$settingName = 'default-rights-json-project';
$currentDefaultRightsString = $this->framework->getProjectSetting($settingName, $project_id) ?? '{}';
$defaultRightsTitle = "Define the set of default API User Rights for this project";
$defaultRightsDesc = "These rights will be applied to all users who have not been assigned specific rights in this project.";
}
$currentDefaultRights = json_decode($currentDefaultRightsString, true);
$headerInfo = $this->getTableHeader();
$odd = true;
$nameData = "<tr id='default-rights-field'>
<td colspan='3'>
<p><span class='font-weight-bold'>" . $defaultRightsTitle . "</span><br>" . $defaultRightsDesc . "</p>
<div class='row row-cols-1 row-cols-sm-2 row-cols-lg-3'>";
foreach ( $headerInfo['sections'] as $section => $methods ) {
$cardClass = $odd ? 'odd' : 'even';
$odd = !$odd;
$nameData .= "<div class='col card-container g-2'><div class='card h-100 " . $cardClass . "'><div class='card-body'><h5 class='card-title'>" . $section . "</h5>";
foreach ( $methods as $method ) {
$name = $method["content"] . "_" . $method['action'];
$checked = ($currentDefaultRights[$name] ?? '') == '1' ? 'checked' : '';
$nameData .= "<div class='form-check'><input class='form-check-input default-api-rights-input' type='checkbox' id='" . $name . "' name='" . $name . "' value='' " . $checked . "><label class='form-check-label' for='" . $name . "'>" . $method['method'] . "</label></div>";
}
$nameData .= "</div></div></div>";
}
$nameData .= "</div></td></tr>";
$scriptData = "<script>\$('tr[field=\'" . $settingName . "\']').after(\$(`" . $nameData . "`));
$('tr[field=\'" . $settingName . "\']').hide();
$('input.default-api-rights-input').change(saveDefaultRights);
function saveDefaultRights() {
const rights = {};
$('input.default-api-rights-input').each(function() {
rights[this.id] = this.checked ? 1 : 0;
});
var rightsJson = JSON.stringify(rights);
$('input[name=\'" . $settingName . "\']').val(rightsJson);
}
</script><style> div.card.odd {background-color: #eee;} div.card.even {background-color: #fcfef5;}</style>";
$settings[] = [
'key' => $settingName,
'name' => $scriptData,
'type' => 'json'
];
} catch ( \Throwable $e ) {
$this->framework->log('Error adding default rights config', [ 'error' => $e->getMessage() ]);
} finally {
return $settings;
}
}
public function redcap_module_link_check_display($project_id, $link)
{
$user = $this->framework->getUser();
$rights = $user->getRights();
if ( ($rights['user_rights'] ?? '') != '1' && !$user->isSuperUser() ) {
return null;
}
return $link;
}
public function redcap_every_page_before_render($project_id = null) : void
{
// Only run on the pages we're interested in
if ( !defined('PAGE') || !isset($_SERVER) ) {
return;
}
if (
($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST' ||
(PAGE ?? '') !== 'api/index.php'
) {
return;
}
try {
$post = $this->framework->escape($_POST) ?? [];
// If content is not valid, let REDCap handle it
$contentValid = $this->validateContent($post);
if ( !$contentValid ) {
return;
}
$user = $this->getApiUser($post);
// Invalid token, let REDCap handle it
if ( empty($user) || empty($user['username'] || empty($user['$project_id'])) ) {
return;
}
$username = $user['username'];
$pid = $user['project_id'];
// Module is not enabled for this project, don't do anything
if ( !in_array($pid, $this->getProjectsWithModuleEnabledIncludingByDefault() ?? []) ) {
return;
}
// Check for exceptions - situations where we want to allow the request to proceed
// even if the method is not defined in the API
$exempt = $this->checkForExceptions($post, $user);
// This is an exempt method, let REDCap handle it
if ( $exempt ) {
return;
}
// Try to determine the API method
$method = $this->determineApiMethod($post);
// Not a valid API method and not exempt, you shall not pass
if ( !$method || empty($method) ) {
http_response_code(400);
header('Content-Type: application/json');
// TODO: Send in the requested return format instead of always json
echo json_encode([ 'error' => 'Invalid request.' ]);
$this->framework->exitAfterHook();
return;
}
// Check if the user has permission to use this method
$methodAllowed = $this->isMethodAllowed($method, $username, $pid);
if ( !$methodAllowed ) {
$this->framework->log('API Method Not Allowed', [ 'user' => $username, 'project_id' => $pid, 'methodCode' => $method['methodCode'] ?? '', 'method' => $method['method'] ?? '' ]);
http_response_code(403);
header('Content-Type: application/json');
echo json_encode([ 'error' => 'You do not have permissions to use this API method.' ]);
$this->framework->exitAfterHook();
return;
}
} catch ( \Throwable $e ) {
$this->framework->log('error', [ 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]);
}
}
public function determineApiMethod(array $data)
{