Repository navigation
Expand file tree
/
Copy pathAdminProjectAuthorizationResource.java
More file actions
860 lines (820 loc) · 37.2 KB
/
Copy pathAdminProjectAuthorizationResource.java
File metadata and controls
860 lines (820 loc) · 37.2 KB
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
/*******************************************************************************
* Copyright 2015 Defense Health Agency (DHA)
*
* If your use of this software does not include any GPLv2 components:
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ----------------------------------------------------------------------------
* If your use of this software includes any GPLv2 components:
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*******************************************************************************/
package prerna.semoss.web.services.local.auth;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
import jakarta.annotation.security.PermitAll;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Response;
import prerna.auth.AccessPermissionEnum;
import prerna.auth.AccessToken;
import prerna.auth.utils.SecurityAdminUtils;
import prerna.auth.utils.SecurityProjectUtils;
import prerna.auth.utils.reactors.admin.AdminMyProjectsReactor;
import prerna.engine.impl.SmssEditCopy;
import prerna.graph.utility.MsGraphUtility;
import prerna.io.connector.ms.MicrosoftGraphUserLookup;
import prerna.om.Insight;
import prerna.sablecc2.om.GenRowStruct;
import prerna.sablecc2.om.PixelDataType;
import prerna.sablecc2.om.ReactorKeysEnum;
import prerna.sablecc2.om.nounmeta.NounMetadata;
import prerna.semoss.web.services.local.ProjectResource;
import prerna.semoss.web.services.local.ResourceUtility;
import prerna.web.services.util.WebUtility;
@Path("/auth/admin/project")
@PermitAll
public class AdminProjectAuthorizationResource {
@Context
protected ServletContext context;
/**
* Get the projects the user has access to
*
* @param request
* @return
*/
@GET
@Produces("application/json")
@Path("getProjects")
public Response getProjectsGET(@Context HttpServletRequest request,
@QueryParam("projectId") List<String> projectFilter, @QueryParam("filterWord") String searchTerm,
@QueryParam("limit") Integer limit, @QueryParam("offset") Integer offset,
@QueryParam("metaKeys") List<String> metaKeys,
// @QueryParam("metaFilters") Map<String, Object> metaFilters,
@QueryParam("noMeta") Boolean noMeta, @QueryParam("userT") Boolean includeUserTracking,
@QueryParam("permissionUser") String permissionUser,
@QueryParam("effectivePermissions") List<Integer> effectivePermissions,
@QueryParam("createdBy") String createdBy, @QueryParam("createdByMe") Boolean createdByMe) {
String term = WebUtility.inputSQLSanitizer(searchTerm);
List<String> projects = WebUtility.inputSQLSanitizer(projectFilter);
List<String> keys = WebUtility.inputSQLSanitizer(metaKeys);
return ResourceUtility.respondAsAdmin(request, "list all projects", user -> {
AdminMyProjectsReactor reactor = new AdminMyProjectsReactor();
reactor.In();
Insight temp = new Insight();
temp.setUser(user);
reactor.setInsight(temp);
if (term != null) {
GenRowStruct struct = new GenRowStruct();
struct.add(new NounMetadata(term, PixelDataType.CONST_STRING));
reactor.getNounStore().addNoun(ReactorKeysEnum.FILTER_WORD.getKey(), struct);
}
if (limit != null) {
GenRowStruct struct = new GenRowStruct();
struct.add(new NounMetadata(limit, PixelDataType.CONST_INT));
reactor.getNounStore().addNoun(ReactorKeysEnum.LIMIT.getKey(), struct);
}
if (offset != null) {
GenRowStruct struct = new GenRowStruct();
struct.add(new NounMetadata(offset, PixelDataType.CONST_INT));
reactor.getNounStore().addNoun(ReactorKeysEnum.OFFSET.getKey(), struct);
}
if (projects != null && !projects.isEmpty()) {
GenRowStruct struct = new GenRowStruct();
for (String engine : projects) {
struct.add(new NounMetadata(engine, PixelDataType.CONST_STRING));
}
reactor.getNounStore().addNoun(ReactorKeysEnum.PROJECT.getKey(), struct);
}
if (keys != null && !keys.isEmpty()) {
GenRowStruct struct = new GenRowStruct();
for (String metaK : keys) {
struct.add(new NounMetadata(metaK, PixelDataType.CONST_STRING));
}
reactor.getNounStore().addNoun(ReactorKeysEnum.META_KEYS.getKey(), struct);
}
// if(metaFilters != null) {
// GenRowStruct struct = new GenRowStruct();
// struct.add(new NounMetadata(metaFilters, PixelDataType.MAP));
// reactor.getNounStore().addNoun(ReactorKeysEnum.META_FILTERS.getKey(), struct);
// }
if (noMeta != null) {
GenRowStruct struct = new GenRowStruct();
struct.add(new NounMetadata(noMeta, PixelDataType.BOOLEAN));
reactor.getNounStore().addNoun(ReactorKeysEnum.NO_META.getKey(), struct);
}
if (includeUserTracking != null) {
GenRowStruct struct = new GenRowStruct();
struct.add(new NounMetadata(includeUserTracking, PixelDataType.BOOLEAN));
reactor.getNounStore().addNoun(ReactorKeysEnum.INCLUDE_USERTRACKING_KEY.getKey(), struct);
}
if (permissionUser != null && !permissionUser.trim().isEmpty()) {
GenRowStruct struct = new GenRowStruct();
struct.add(new NounMetadata(permissionUser.trim(), PixelDataType.CONST_STRING));
reactor.getNounStore().addNoun("permissionUser", struct);
}
if (effectivePermissions != null && !effectivePermissions.isEmpty()) {
GenRowStruct struct = new GenRowStruct();
for (Integer permission : effectivePermissions) {
struct.add(new NounMetadata(permission, PixelDataType.CONST_INT));
}
reactor.getNounStore().addNoun("effectivePermissions", struct);
}
if (createdBy != null && !createdBy.trim().isEmpty()) {
GenRowStruct struct = new GenRowStruct();
for (Object creator : new Gson().fromJson(createdBy, List.class)) {
struct.add(new NounMetadata(creator, PixelDataType.MAP));
}
reactor.getNounStore().addNoun("createdBy", struct);
}
if (createdByMe != null) {
GenRowStruct struct = new GenRowStruct();
struct.add(new NounMetadata(createdByMe, PixelDataType.BOOLEAN));
reactor.getNounStore().addNoun("createdByMe", struct);
}
NounMetadata outputNoun = reactor.execute();
return outputNoun.getValue();
});
}
@POST
@Produces("application/json")
@Path("getProjects")
public Response getProjectsPOST(@Context HttpServletRequest request) {
return ResourceUtility.respondAsAdmin(request, "list all projects", user -> {
AdminMyProjectsReactor reactor = new AdminMyProjectsReactor();
reactor.In();
Insight temp = new Insight();
temp.setUser(user);
reactor.setInsight(temp);
Map<String, String[]> parameterMap = request.getParameterMap();
if (parameterMap.containsKey("filterWord") && parameterMap.get("filterWord") != null
&& parameterMap.get("filterWord").length > 0) {
GenRowStruct struct = new GenRowStruct();
struct.add(new NounMetadata(parameterMap.get("filterWord")[0], PixelDataType.CONST_STRING));
reactor.getNounStore().addNoun(ReactorKeysEnum.FILTER_WORD.getKey(), struct);
}
if (parameterMap.containsKey("limit") && parameterMap.get("limit") != null
&& parameterMap.get("limit").length > 0) {
GenRowStruct struct = new GenRowStruct();
struct.add(new NounMetadata(parameterMap.get("limit")[0], PixelDataType.CONST_INT));
reactor.getNounStore().addNoun(ReactorKeysEnum.LIMIT.getKey(), struct);
}
if (parameterMap.containsKey("offset") && parameterMap.get("offset") != null
&& parameterMap.get("offset").length > 0) {
GenRowStruct struct = new GenRowStruct();
struct.add(new NounMetadata(parameterMap.get("offset")[0], PixelDataType.CONST_INT));
reactor.getNounStore().addNoun(ReactorKeysEnum.OFFSET.getKey(), struct);
}
if (parameterMap.containsKey("projectId") && parameterMap.get("projectId") != null
&& parameterMap.get("projectId").length > 0) {
GenRowStruct struct = new GenRowStruct();
String[] projectFilter = parameterMap.get("projectId");
for (String project : projectFilter) {
struct.add(new NounMetadata(project, PixelDataType.CONST_STRING));
}
reactor.getNounStore().addNoun(ReactorKeysEnum.PROJECT.getKey(), struct);
}
if (parameterMap.containsKey("metaKeys") && parameterMap.get("metaKeys") != null
&& parameterMap.get("metaKeys").length > 0) {
GenRowStruct struct = new GenRowStruct();
String[] metaKeys = parameterMap.get("metaKeys");
for (String metaK : metaKeys) {
struct.add(new NounMetadata(metaK, PixelDataType.CONST_STRING));
}
reactor.getNounStore().addNoun(ReactorKeysEnum.META_KEYS.getKey(), struct);
}
if (parameterMap.containsKey("metaFilters") && parameterMap.get("metaFilters") != null
&& parameterMap.get("metaFilters").length > 0) {
Map<String, Object> metaFilters = new Gson().fromJson(parameterMap.get("metaFilters")[0], Map.class);
GenRowStruct struct = new GenRowStruct();
struct.add(new NounMetadata(metaFilters, PixelDataType.MAP));
reactor.getNounStore().addNoun(ReactorKeysEnum.META_FILTERS.getKey(), struct);
}
if (parameterMap.containsKey("noMeta") && parameterMap.get("noMeta") != null
&& parameterMap.get("noMeta").length > 0) {
GenRowStruct struct = new GenRowStruct();
struct.add(new NounMetadata(parameterMap.get("noMeta")[0], PixelDataType.BOOLEAN));
reactor.getNounStore().addNoun(ReactorKeysEnum.NO_META.getKey(), struct);
}
if (parameterMap.containsKey("userT") && parameterMap.get("userT") != null
&& parameterMap.get("userT").length > 0) {
GenRowStruct struct = new GenRowStruct();
struct.add(new NounMetadata(parameterMap.get("userT")[0], PixelDataType.BOOLEAN));
reactor.getNounStore().addNoun(ReactorKeysEnum.INCLUDE_USERTRACKING_KEY.getKey(), struct);
}
if (parameterMap.containsKey("permissionUser") && parameterMap.get("permissionUser") != null
&& parameterMap.get("permissionUser").length > 0) {
GenRowStruct struct = new GenRowStruct();
struct.add(new NounMetadata(parameterMap.get("permissionUser")[0].trim(), PixelDataType.CONST_STRING));
reactor.getNounStore().addNoun("permissionUser", struct);
}
if (parameterMap.containsKey("effectivePermissions") && parameterMap.get("effectivePermissions") != null
&& parameterMap.get("effectivePermissions").length > 0) {
GenRowStruct struct = new GenRowStruct();
for (String permission : parameterMap.get("effectivePermissions")) {
struct.add(new NounMetadata(Integer.valueOf(permission.trim()), PixelDataType.CONST_INT));
}
reactor.getNounStore().addNoun("effectivePermissions", struct);
}
if (parameterMap.containsKey("createdBy") && parameterMap.get("createdBy") != null
&& parameterMap.get("createdBy").length > 0) {
GenRowStruct struct = new GenRowStruct();
for (Object creator : new Gson().fromJson(parameterMap.get("createdBy")[0], List.class)) {
struct.add(new NounMetadata(creator, PixelDataType.MAP));
}
reactor.getNounStore().addNoun("createdBy", struct);
}
if (parameterMap.containsKey("createdByMe") && parameterMap.get("createdByMe") != null
&& parameterMap.get("createdByMe").length > 0) {
GenRowStruct struct = new GenRowStruct();
struct.add(new NounMetadata(Boolean.parseBoolean(parameterMap.get("createdByMe")[0].trim()),
PixelDataType.BOOLEAN));
reactor.getNounStore().addNoun("createdByMe", struct);
}
NounMetadata outputNoun = reactor.execute();
return outputNoun.getValue();
});
}
@GET
@Path("/getAllUserProjects")
@Produces("application/json")
public Response getAllUserProjects(@Context HttpServletRequest request, @QueryParam("userId") String userId,
@QueryParam("projectTypes") List<String> projectTypes, @QueryParam("searchTerm") String searchTerm,
@QueryParam("limit") long limit, @QueryParam("offset") long offset) {
String id = WebUtility.inputSQLSanitizer(userId);
String term = WebUtility.inputSQLSanitizer(searchTerm);
List<String> types = WebUtility.inputSanitizer(projectTypes);
return ResourceUtility.respondAsAdmin(request, "list the projects of user " + id,
SecurityAdminUtils::getInstance,
(user, adminUtils) -> adminUtils.getAllUserProjects(id, types, term, limit, offset));
}
@GET
@Path("/getUserProjectsNoCredentials")
@Produces("application/json")
public Response getUserProjectsNoCredentials(@Context HttpServletRequest request,
@QueryParam("userId") String userId, @QueryParam("projectTypes") List<String> projectTypes,
@QueryParam("searchTerm") String searchTerm, @QueryParam("limit") long limit,
@QueryParam("offset") long offset) {
String id = WebUtility.inputSQLSanitizer(userId);
String term = WebUtility.inputSQLSanitizer(searchTerm);
List<String> types = WebUtility.inputSanitizer(projectTypes);
return ResourceUtility.respondAsAdmin(request, "list the projects user " + id + " does not have access to",
SecurityAdminUtils::getInstance,
(user, adminUtils) -> adminUtils.getUserProjectsNoCredentials(id, types, term, limit, offset));
}
/**
* Saves new smss content for any project, in admin mode, and opens the project
* again with it. A changed {@code PROJECT_DISPLAY_NAME} is saved to the
* security database in admin mode too. The owner's endpoint is
* {@code /project-<projectId>/updateSmssFile}.
*
* @param request the request, whose session holds the admin
* @param form {@code projectId}, {@code smss}, the new content of the file,
* and {@code smssHash}, the hash that came with the copy edited
* @return 200 with the new {@code smssHash} when the content is saved, 400 with
* the reason when it cannot be, 403 for a user who is not an admin, and
* 409 with {@code errorCode} {@code SMSS_CHANGED} when the smss changed
* since that copy was read
*/
@POST
@Produces("application/json")
@Path("updateSmssFile")
public Response updateSmssFile(@Context HttpServletRequest request, MultivaluedMap<String, String> form) {
String requestedId = WebUtility.inputSanitizer(form.getFirst("projectId"));
String projectId = WebUtility.safePathSegment(requestedId);
String smss = form.getFirst("smss");
String smssHash = form.getFirst(SmssEditCopy.SMSS_HASH);
return ResourceUtility.respondAsAdmin(request, "update the smss of project " + requestedId,
SecurityAdminUtils::getInstance, (user, adminUtils) -> {
if (projectId == null || !SecurityProjectUtils.projectExists(projectId)) {
throw new IllegalArgumentException("Project " + requestedId + " does not exist");
}
return ProjectResource.saveSmssFile(user, adminUtils, projectId, smss, smssHash);
});
}
@POST
@Path("/grantAllProjects")
@Produces("application/json")
public Response grantAllProjects(@Context HttpServletRequest request, MultivaluedMap<String, String> form) {
String userId = WebUtility.inputSQLSanitizer(form.getFirst("userId"));
String permission = WebUtility.inputSQLSanitizer(form.getFirst("permission"));
boolean isAddNew = Boolean.parseBoolean(form.getFirst("isAddNew") + "");
return ResourceUtility.respondAsAdmin(request,
"grant all projects to user " + userId + " with permission " + permission,
SecurityAdminUtils::getInstance, (user, adminUtils) -> {
adminUtils.grantAllProjects(userId, AccessPermissionEnum.require(permission), isAddNew, user);
return success();
});
}
@POST
@Path("/grantNewUsersProjectAccess")
@Produces("application/json")
public Response grantNewUsersProjectAccess(@Context HttpServletRequest request,
MultivaluedMap<String, String> form) {
String permission = WebUtility.inputSQLSanitizer(form.getFirst("permission"));
String projectId = WebUtility.inputSQLSanitizer(form.getFirst("projectId"));
String endDate = null; // form.getFirst("endDate");
return ResourceUtility.respondAsAdmin(request,
"grant project " + projectId + " to new users with permission " + permission,
SecurityAdminUtils::getInstance, (user, adminUtils) -> {
String level = AccessPermissionEnum.require(permission);
adminUtils.grantNewUsersProjectAccess(projectId, level, user, endDate);
return success();
});
}
/**
* Get the project users and their permissions
*
* @param request
* @param form
* @return
*/
@GET
@Produces("application/json")
@Path("getProjectUsers")
public Response getProjectUsers(@Context HttpServletRequest request, @QueryParam("projectId") String projectId,
@QueryParam("userId") String userId, @QueryParam("searchTerm") String searchTerm,
@QueryParam("permission") String permission, @QueryParam("limit") long limit,
@QueryParam("offset") long offset) {
String project = WebUtility.inputSQLSanitizer(projectId);
String id = WebUtility.inputSQLSanitizer(userId);
String term = WebUtility.inputSQLSanitizer(searchTerm);
String level = WebUtility.inputSQLSanitizer(permission);
return ResourceUtility.respondAsAdmin(request, "list the users of project " + project,
SecurityAdminUtils::getInstance, (user, adminUtils) -> {
String searchParam = term != null ? term : id;
List<Map<String, Object>> members = adminUtils.getProjectUsers(project, searchParam, level, limit,
offset);
long totalMembers = adminUtils.getProjectUsersCount(project, searchParam, level);
Map<String, Object> ret = new HashMap<String, Object>();
ret.put("totalMembers", totalMembers);
ret.put("members", members);
return ret;
});
}
/**
* Add a user to an project
*
* @param request
* @param form
* @return
*/
@POST
@Produces("application/json")
@Path("addProjectUserPermission")
public Response addProjectUserPermission(@Context HttpServletRequest request, MultivaluedMap<String, String> form) {
String newUserId = WebUtility.inputSQLSanitizer(form.getFirst("id"));
String projectId = WebUtility.inputSQLSanitizer(form.getFirst("projectId"));
String permission = WebUtility.inputSQLSanitizer(form.getFirst("permission"));
String endDate = null; // form.getFirst("endDate");
return ResourceUtility.respondAsAdmin(request,
"add user " + newUserId + " to project " + projectId + " with permission " + permission,
SecurityAdminUtils::getInstance, (user, adminUtils) -> {
String level = AccessPermissionEnum.require(permission);
adminUtils.addProjectUser(newUserId, projectId, level, user, endDate);
return success();
});
}
/**
* Add all users to an project
*
* @param request
* @param form
* @return
*/
@POST
@Produces("application/json")
@Path("addAllUsers")
public Response addAllUsers(@Context HttpServletRequest request, MultivaluedMap<String, String> form) {
String projectId = WebUtility.inputSQLSanitizer(form.getFirst("projectId"));
String permission = WebUtility.inputSQLSanitizer(form.getFirst("permission"));
String endDate = null; // form.getFirst("endDate");
return ResourceUtility.respondAsAdmin(request,
"add all users to project " + projectId + " with permission " + permission,
SecurityAdminUtils::getInstance, (user, adminUtils) -> {
adminUtils.addAllProjectUsers(projectId, AccessPermissionEnum.require(permission), user, endDate);
return success();
});
}
/**
* Edit user permission for an project
*
* @param request
* @param form
* @return
*/
@POST
@Produces("application/json")
@Path("editProjectUserPermission")
public Response editProjectUserPermission(@Context HttpServletRequest request,
MultivaluedMap<String, String> form) {
String existingUserId = WebUtility.inputSQLSanitizer(form.getFirst("id"));
String projectId = WebUtility.inputSQLSanitizer(form.getFirst("projectId"));
String newPermission = WebUtility.inputSQLSanitizer(form.getFirst("permission"));
String endDate = null; // form.getFirst("endDate");
return ResourceUtility.respondAsAdmin(request,
"change the permission of user " + existingUserId + " on project " + projectId + " to " + newPermission,
SecurityAdminUtils::getInstance, (user, adminUtils) -> {
String level = AccessPermissionEnum.require(newPermission);
adminUtils.editProjectUserPermission(existingUserId, projectId, level, user, endDate);
return success();
});
}
/**
* Edit user permission for a project
*
* @param request
* @param form
* @return
*/
@POST
@Produces("application/json")
@Path("editProjectUserPermissions")
public Response editProjectUserPermissions(@Context HttpServletRequest request,
MultivaluedMap<String, String> form) {
String projectId = WebUtility.inputSQLSanitizer(form.getFirst("projectId"));
String endDate = null; // form.getFirst("endDate");
return ResourceUtility.respondAsAdmin(request, "edit the user permissions on project " + projectId,
SecurityAdminUtils::getInstance, (user, adminUtils) -> {
List<Map<String, String>> requests = new Gson().fromJson(form.getFirst("userpermissions"),
List.class);
AccessPermissionEnum.requireEach(requests);
adminUtils.editProjectUserPermissions(projectId, requests, user, endDate);
return success();
});
}
/**
* update all user's permission level to new permission level for an project
*
* @param request
* @param form
* @return
*/
@POST
@Produces("application/json")
@Path("updateProjectUserPermissions")
public Response updateProjectUserPermissions(@Context HttpServletRequest request,
MultivaluedMap<String, String> form) {
String projectId = WebUtility.inputSQLSanitizer(form.getFirst("projectId"));
String newPermission = WebUtility.inputSQLSanitizer(form.getFirst("permission"));
String endDate = null; // form.getFirst("endDate");
return ResourceUtility.respondAsAdmin(request,
"change the permission of every user on project " + projectId + " to " + newPermission,
SecurityAdminUtils::getInstance, (user, adminUtils) -> {
String level = AccessPermissionEnum.require(newPermission);
adminUtils.updateProjectUserPermissions(projectId, level, user, endDate);
return success();
});
}
/**
* Remove user permission for an project
*
* @param request
* @param form
* @return
*/
@POST
@Produces("application/json")
@Path("removeProjectUserPermission")
public Response removeProjectUserPermission(@Context HttpServletRequest request,
MultivaluedMap<String, String> form) {
String existingUserId = WebUtility.inputSQLSanitizer(form.getFirst("id"));
String projectId = WebUtility.inputSQLSanitizer(form.getFirst("projectId"));
return ResourceUtility.respondAsAdmin(request, "remove user " + existingUserId + " from project " + projectId,
SecurityAdminUtils::getInstance, (user, adminUtils) -> {
adminUtils.removeProjectUser(existingUserId, projectId);
return success();
});
}
/**
* Set whether a project can be cloned as a template by users who can view it.
*
* @param request current request
* @param form projectId and template boolean
* @return operation status
*/
@POST
@Produces("application/json")
@Path("setProjectTemplate")
public Response setProjectTemplate(@Context HttpServletRequest request, MultivaluedMap<String, String> form) {
String projectIdInput = form.getFirst("projectId");
String templateInput = form.getFirst("template");
String projectId = WebUtility.inputSQLSanitizer(projectIdInput);
boolean isTemplate = Boolean.parseBoolean(templateInput);
return ResourceUtility.respondAsAdmin(request,
"set the template status of project " + projectId + " to " + isTemplate,
SecurityAdminUtils::getInstance, (user, adminUtils) -> {
boolean validTemplate = "true".equalsIgnoreCase(templateInput)
|| "false".equalsIgnoreCase(templateInput);
if (projectIdInput == null || projectIdInput.trim().isEmpty() || !validTemplate) {
throw new IllegalArgumentException("projectId and a true or false template value are required");
}
adminUtils.setProjectTemplate(projectId, isTemplate);
return success();
});
}
/**
* Sets whether a project has a login page of its own in its portal, as an
* admin, so its portal is served to signed out visitors for them to sign in
* there.
*
* @param request the request
* @param form projectId, and customLogin as true or false
* @return whether the flag was saved
*/
@POST
@Produces("application/json")
@Path("setProjectCustomLogin")
public Response setProjectCustomLogin(@Context HttpServletRequest request, MultivaluedMap<String, String> form) {
String projectIdInput = form.getFirst("projectId");
String customLoginInput = form.getFirst("customLogin");
String projectId = WebUtility.inputSQLSanitizer(projectIdInput);
boolean hasCustomLogin = Boolean.parseBoolean(customLoginInput);
return ResourceUtility.respondAsAdmin(request, "set whether project " + projectId + " has its own login page",
SecurityAdminUtils::getInstance, (user, adminUtils) -> {
boolean validCustomLogin = "true".equalsIgnoreCase(customLoginInput)
|| "false".equalsIgnoreCase(customLoginInput);
if (projectIdInput == null || projectIdInput.trim().isEmpty() || !validCustomLogin) {
throw new IllegalArgumentException(
"projectId and a true or false customLogin value are required");
}
adminUtils.setProjectCustomLogin(projectId, hasCustomLogin);
return success();
});
}
@POST
@Produces("application/json")
@Path("setProjectGlobal")
public Response setProjectGlobal(@Context HttpServletRequest request, MultivaluedMap<String, String> form) {
String projectId = WebUtility.inputSQLSanitizer(form.getFirst("projectId"));
boolean isPublic = Boolean.parseBoolean(form.getFirst("public"));
return ResourceUtility.respondAsAdmin(request,
"make project " + projectId + (isPublic ? " public" : " private"), SecurityAdminUtils::getInstance,
(user, adminUtils) -> {
adminUtils.setProjectGlobal(projectId, isPublic);
return success();
});
}
/**
* Set the project as being discoverable for the entire semoss instance
*
* @param request
* @param form
* @return
*/
@POST
@Produces("application/json")
@Path("setProjectDiscoverable")
public Response setProjectDiscoverable(@Context HttpServletRequest request, MultivaluedMap<String, String> form) {
String projectId = WebUtility.inputSQLSanitizer(form.getFirst("projectId"));
boolean isDiscoverable = Boolean.parseBoolean(form.getFirst("discoverable"));
return ResourceUtility.respondAsAdmin(request,
"make project " + projectId + (isDiscoverable ? " discoverable" : " not discoverable"),
SecurityAdminUtils::getInstance, (user, adminUtils) -> {
adminUtils.setProjectDiscoverable(projectId, isDiscoverable);
return success();
});
}
/**
* Get users with no access to a given project
*
* @param request
* @param form
* @return
*/
@GET
@Produces("application/json")
@Path("getProjectUsersNoCredentials")
public Response getProjectUsersNoCredentials(@Context HttpServletRequest request,
@QueryParam("projectId") String projectId, @QueryParam("searchTerm") String searchTerm,
@QueryParam("limit") long limit, @QueryParam("offset") long offset,
@QueryParam(MicrosoftGraphUserLookup.LOOKUP_PARAM) String msGraphLookup) {
String project = WebUtility.inputSQLSanitizer(projectId);
String term = WebUtility.inputSQLSanitizer(searchTerm);
return ResourceUtility.respondAsAdmin(request, "search for people to add to project " + project,
SecurityAdminUtils::getInstance, (user, adminUtils) -> {
// when the directory is not used
// then we will look at our security db
if (!MicrosoftGraphUserLookup.useDirectory(msGraphLookup)) {
return adminUtils.getProjectUsersNoCredentials(project, term, limit, offset);
}
try {
return MsGraphUtility.getProjectUsers(request, user, project, term, limit, offset, true);
} catch (IllegalAccessException e) {
// not being signed in to Microsoft is a bad request, not a missing session
throw new IllegalArgumentException(e.getMessage(), e);
}
});
}
/**
* Admin approval of user access requests
*
* @param request
* @param form
* @return
*/
@POST
@Produces("application/json")
@Path("approveProjectUserAccessRequest")
public Response approveProjectUserAccessRequest(@Context HttpServletRequest request,
MultivaluedMap<String, String> form) {
String projectId = WebUtility.inputSQLSanitizer(form.getFirst("projectId"));
String endDate = null; // form.getFirst("endDate");
return ResourceUtility.respondAsAdmin(request, "approve the access requests to project " + projectId,
SecurityAdminUtils::getInstance, (user, adminUtils) -> {
// adding user permissions and updating user access requests in bulk
List<Map<String, Object>> requests = new Gson().fromJson(form.getFirst("requests"), List.class);
AccessToken token = user.getAccessToken(user.getPrimaryLogin());
String userId = token.getId();
String userType = token.getProvider().toString();
AccessPermissionEnum.requireEach(requests);
adminUtils.approveProjectUserAccessRequests(userId, userType, projectId, requests, endDate);
return success();
});
}
/**
* Admin deny of user access requests
*
* @param request
* @param form
* @return
*/
@POST
@Produces("application/json")
@Path("denyProjectUserAccessRequest")
public Response denyProjectUserAccessRequest(@Context HttpServletRequest request,
MultivaluedMap<String, String> form) {
String projectId = WebUtility.inputSQLSanitizer(form.getFirst("projectId"));
return ResourceUtility.respondAsAdmin(request, "deny the access requests to project " + projectId,
SecurityAdminUtils::getInstance, (user, adminUtils) -> {
// updating user access requests in bulk
List<String> requestids = new Gson().fromJson(form.getFirst("requestids"), List.class);
AccessToken token = user.getAccessToken(user.getPrimaryLogin());
String userId = token.getId();
String userType = token.getProvider().toString();
adminUtils.denyProjectUserAccessRequests(userId, userType, projectId, requestids);
return success();
});
}
/**
* Add user permissions in bulk to a project
*
* @param request
* @param form
* @return
*/
@POST
@Produces("application/json")
@Path("addProjectUserPermissions")
public Response addProjectUserPermissions(@Context HttpServletRequest request,
MultivaluedMap<String, String> form) {
String projectId = WebUtility.inputSQLSanitizer(form.getFirst("projectId"));
return ResourceUtility.respondAsAdmin(request, "add user permissions to project " + projectId,
SecurityAdminUtils::getInstance, (user, adminUtils) -> {
// adding user permissions in bulk
List<Map<String, String>> permission = new Gson().fromJson(form.getFirst("userpermissions"),
List.class);
AccessPermissionEnum.requireEach(permission);
// users picked from the Microsoft directory may not be in the security db yet
if (MicrosoftGraphUserLookup.isEnabled()) {
MicrosoftGraphUserLookup.addMissingUsers(user, permission);
}
adminUtils.addProjectUserPermissions(projectId, permission, user);
return success();
});
}
/**
* Remove user permissions for a project, in bulk
*
* @param request
* @param form
* @return
*/
@POST
@Produces("application/json")
@Path("removeProjectUserPermissions")
public Response removeProjectUserPermissions(@Context HttpServletRequest request,
MultivaluedMap<String, String> form) {
String projectId = WebUtility.inputSQLSanitizer(form.getFirst("projectId"));
return ResourceUtility.respondAsAdmin(request, "remove users from project " + projectId,
SecurityAdminUtils::getInstance, (user, adminUtils) -> {
Gson gson = new Gson();
List<String> ids = gson.fromJson(form.getFirst("ids"), List.class);
ids = WebUtility.inputSQLSanitizer(ids);
adminUtils.removeProjectUsers(ids, projectId);
return success();
});
}
/**
* @return the body of a change that was made
*/
private static Map<String, Object> success() {
Map<String, Object> ret = new HashMap<String, Object>();
ret.put("success", true);
return ret;
}
/**
* Lists the apps, agents and engines a project gives a permission to, in admin
* mode.
*
* @param request the request, whose session holds the admin
* @param projectId the project
* @return one entry for each app and engine, with its id, catalog type
* ({@code PROJECT} for an app or an agent), name and subtype, and its
* level and end date; 403 for a user who is not an admin
*/
@GET
@Produces("application/json")
@Path("getProjectGranteePermissions")
public Response getProjectGranteePermissions(@Context HttpServletRequest request,
@QueryParam("projectId") String projectId) {
String id = WebUtility.inputSanitizer(projectId);
return ResourceUtility.respondAsAdmin(request,
"list the apps and engines project " + id + " gives a permission to", SecurityAdminUtils::getInstance,
(user, adminUtils) -> adminUtils.getProjectGranteePermissions(id));
}
/**
* Gives an app or an agent a permission on any project, or changes the one it
* has, in admin mode. A project gives access to apps and agents only.
*
* @param request the request, whose session holds the admin
* @param form {@code projectId}, {@code granteeEngineId},
* {@code granteeEngineType} ({@code PROJECT}),
* {@code permission} (READ_ONLY or EDIT), and optionally
* {@code endDate}
* @return 200 when the permission is saved, 400 with the reason when it cannot
* be, and 403 for a user who is not an admin
*/
@POST
@Produces("application/json")
@Path("setProjectGranteePermission")
public Response setProjectGranteePermission(@Context HttpServletRequest request,
MultivaluedMap<String, String> form) {
String projectId = WebUtility.inputSanitizer(form.getFirst("projectId"));
String granteeEngineId = WebUtility.inputSanitizer(form.getFirst("granteeEngineId"));
String granteeEngineType = WebUtility.inputSanitizer(form.getFirst("granteeEngineType"));
String permission = WebUtility.inputSanitizer(form.getFirst("permission"));
String endDate = WebUtility.inputSanitizer(form.getFirst("endDate"));
String action = "give " + granteeEngineType + " " + granteeEngineId + " " + permission + " access to project "
+ projectId;
return ResourceUtility.respondAsAdmin(request, action, SecurityAdminUtils::getInstance, (user, adminUtils) -> {
String level = AccessPermissionEnum.require(permission);
adminUtils.setProjectGranteePermission(granteeEngineId, granteeEngineType, projectId, level, user, endDate);
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
return ret;
});
}
/**
* Takes away the permission a project gives an app, an agent or an engine, in
* admin mode.
*
* @param request the request, whose session holds the admin
* @param form {@code projectId}, {@code granteeEngineId} and
* {@code granteeEngineType}
* @return 200 when the permission is taken away, 400 with the reason when it
* cannot be, and 403 for a user who is not an admin
*/
@POST
@Produces("application/json")
@Path("removeProjectGranteePermission")
public Response removeProjectGranteePermission(@Context HttpServletRequest request,
MultivaluedMap<String, String> form) {
String projectId = WebUtility.inputSanitizer(form.getFirst("projectId"));
String granteeEngineId = WebUtility.inputSanitizer(form.getFirst("granteeEngineId"));
String granteeEngineType = WebUtility.inputSanitizer(form.getFirst("granteeEngineType"));
String action = "take away the access of " + granteeEngineType + " " + granteeEngineId + " to project "
+ projectId;
return ResourceUtility.respondAsAdmin(request, action, SecurityAdminUtils::getInstance, (user, adminUtils) -> {
adminUtils.removeProjectGranteePermission(granteeEngineId, granteeEngineType, projectId, user);
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
return ret;
});
}
}