Repository navigation
Expand file tree
/
Copy pathProjectAuthorizationResource.java
More file actions
917 lines (872 loc) · 38.1 KB
/
Copy pathProjectAuthorizationResource.java
File metadata and controls
917 lines (872 loc) · 38.1 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
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
/*******************************************************************************
* 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 com.google.gson.GsonBuilder;
import com.google.gson.ToNumberPolicy;
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.User;
import prerna.auth.access.ModelUsageLimit;
import prerna.auth.utils.AbstractSecurityUtils;
import prerna.auth.utils.SecurityAdminUtils;
import prerna.auth.utils.SecurityModelUsageLimitUtils;
import prerna.auth.utils.SecurityProjectUtils;
import prerna.graph.utility.MsGraphUtility;
import prerna.io.connector.ms.MicrosoftGraphUserLookup;
import prerna.om.Insight;
import prerna.reactor.project.MyProjectsReactor;
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.ResourceUtility;
import prerna.web.services.util.WebUtility;
@Path("/auth/project")
@PermitAll
public class ProjectAuthorizationResource {
private static final String ADMINS_ONLY = "This functionality is limited to only admins";
/**
* The field for a model usage limit, as JSON, empty for no limit defined. A
* propagation without it keeps the limit of a person whose access to a model
* changes.
*/
private static final String USAGE_LIMIT = "usageLimit";
/** Reads JSON rows, keeping whole numbers whole */
private static final Gson ROW_GSON = new GsonBuilder().setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
.create();
@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("onlyFavorites") Boolean favoritesOnly, @QueryParam("metaKeys") List<String> metaKeys,
// @QueryParam("metaFilters") Map<String, Object> metaFilters,
@QueryParam("noMeta") Boolean noMeta, @QueryParam("userT") Boolean includeUserTracking) {
String term = WebUtility.inputSQLSanitizer(searchTerm);
List<String> projects = WebUtility.inputSanitizer(projectFilter);
List<String> keys = WebUtility.inputSanitizer(metaKeys);
return ResourceUtility.respond(request, "list the projects they can access", user -> {
MyProjectsReactor reactor = new MyProjectsReactor();
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 (favoritesOnly != null) {
GenRowStruct struct = new GenRowStruct();
struct.add(new NounMetadata(favoritesOnly, PixelDataType.BOOLEAN));
reactor.getNounStore().addNoun(ReactorKeysEnum.ONLY_FAVORITES.getKey(), struct);
}
if (projects != null && !projects.isEmpty()) {
GenRowStruct struct = new GenRowStruct();
for (String project : projects) {
struct.add(new NounMetadata(project, 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);
}
return reactor.execute().getValue();
});
}
@POST
@Produces("application/json")
@Path("getProjects")
public Response getProjectsPOST(@Context HttpServletRequest request) {
return ResourceUtility.respond(request, "list the projects they can access", user -> {
MyProjectsReactor reactor = new MyProjectsReactor();
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(WebUtility.jsonSanitizer(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);
}
return reactor.execute().getValue();
});
}
/**
* Get the user app permission level
*
* @param request
* @param form
* @return
*/
@GET
@Produces("application/json")
@Path("getUserProjectPermission")
public Response getUserProjectPermission(@Context HttpServletRequest request,
@QueryParam("projectId") String projectId, @QueryParam("searchTerm") String searchTerm) {
String project = WebUtility.inputSanitizer(projectId);
String action = "read their permission on project " + project;
return ResourceUtility.respond(request, action, user -> {
String permission = SecurityProjectUtils.getActualUserProjectPermission(user, project);
if (permission == null) {
// are you discoverable?
if (SecurityProjectUtils.projectIsDiscoverable(project)) {
permission = "DISCOVERABLE";
} else {
throw new IllegalAccessException("User does not have access to this project");
}
}
Map<String, String> ret = new HashMap<>();
ret.put("permission", permission);
return ret;
});
}
/**
* 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.inputSanitizer(projectId);
String member = WebUtility.inputSQLSanitizer(userId);
String term = WebUtility.inputSQLSanitizer(searchTerm);
String level = WebUtility.inputSanitizer(permission);
String searchParam = term != null ? term : member;
return ResourceUtility.respond(request, "list the users of project " + project, user -> {
List<Map<String, Object>> members = SecurityProjectUtils.getProjectUsers(user, project, searchParam, level,
limit, offset);
long totalMembers = SecurityProjectUtils.getProjectUsersCount(user, project, searchParam, level);
Map<String, Object> ret = new HashMap<>();
ret.put("totalMembers", totalMembers);
ret.put("members", members);
return ret;
});
}
/**
* Add a user to an app
*
* @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.inputSanitizer(form.getFirst("projectId"));
String permission = WebUtility.inputSanitizer(form.getFirst("permission"));
String endDate = WebUtility.inputSanitizer(form.getFirst("endDate"));
return ResourceUtility.respond(request, "add user " + newUserId + " to project " + projectId, user -> {
if (AbstractSecurityUtils.adminOnlyProjectAddAccess(projectId) && !SecurityAdminUtils.userIsAdmin(user)) {
throw new IllegalAccessException(ADMINS_ONLY);
}
String level = AccessPermissionEnum.require(permission);
SecurityProjectUtils.addProjectUser(user, newUserId, projectId, level, endDate);
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
return ret;
});
}
/**
* Propagate project dependent permissions. The person's usage limit on the
* models the project depends on is sent as {@code usageLimit} (JSON, empty for
* no limit defined). Without it, a person added to a model gets no limit and a
* person whose access to a model changes keeps their limit. Only a model's
* owners set a limit that differs from the current one.
*
* @param request
* @param form
* @return
*/
@POST
@Produces("application/json")
@Path("propagateProjectDependencyPermission")
public Response propagateProjectDependencyPermission(@Context HttpServletRequest request,
MultivaluedMap<String, String> form) {
String newUserId = WebUtility.inputSQLSanitizer(form.getFirst("id"));
String newUserType = WebUtility.inputSanitizer(form.getFirst("type"));
String projectId = WebUtility.inputSanitizer(form.getFirst("projectId"));
String requestedPermission = WebUtility.inputSanitizer(form.getFirst("permission"));
String endDate = WebUtility.inputSanitizer(form.getFirst("endDate"));
boolean usageLimitSent = form.containsKey(USAGE_LIMIT);
String usageLimit = form.getFirst(USAGE_LIMIT);
return ResourceUtility.respond(request, "give user " + newUserId + " access to project " + projectId, user -> {
// Determine if admin right are required to add users and, if so, if requester
// has those rights.
if (AbstractSecurityUtils.adminOnlyProjectAddAccess(projectId) && !SecurityAdminUtils.userIsAdmin(user)) {
throw new IllegalAccessException(ADMINS_ONLY);
}
requireProjectEditor(user, projectId);
String level = AccessPermissionEnum.require(requestedPermission);
if (usageLimitSent) {
return SecurityProjectUtils.propagateProjectPermission(user, projectId, newUserId, newUserType, level,
endDate, SecurityModelUsageLimitUtils.readJson(usageLimit));
}
return SecurityProjectUtils.propagateProjectPermission(user, projectId, newUserId, newUserType, level,
endDate);
});
}
/**
* Propagate project dependent permissions, in bulk. Each row of
* {@code userpermissions} can carry the person's usage limit on the models the
* project depends on as {@code usageLimit}. Without it, a person whose access
* to a model changes keeps their limit.
*
* @param request
* @param form
* @return
*/
@POST
@Produces("application/json")
@Path("propagateProjectDependencyPermissions")
public Response propagateProjectDependencyPermissions(@Context HttpServletRequest request,
MultivaluedMap<String, String> form) {
String projectId = WebUtility.inputSanitizer(form.getFirst("projectId"));
String endDate = WebUtility.inputSanitizer(form.getFirst("endDate"));
return ResourceUtility.respond(request, "give users access to project " + projectId, user -> {
List<Map<String, Object>> requests = ROW_GSON.fromJson(form.getFirst("userpermissions"), List.class);
// Determine if admin right are required to add users and, if so, if requester
// has those rights.
if (AbstractSecurityUtils.adminOnlyProjectAddAccess(projectId) && !SecurityAdminUtils.userIsAdmin(user)) {
throw new IllegalAccessException(ADMINS_ONLY);
}
requireProjectEditor(user, projectId);
// every row is checked before the first one is propagated
AccessPermissionEnum.requireEach(requests);
Map<String, Object> userRet = new HashMap<>();
for (Map<String, Object> userRequest : requests) {
String newUserId = textOf(userRequest.get("userid"));
String newUserType = textOf(userRequest.get("type"));
String requestedPermission = textOf(userRequest.get("permission"));
Map<String, Object> responses;
if (userRequest.containsKey(USAGE_LIMIT)) {
responses = SecurityProjectUtils.propagateProjectPermission(user, projectId, newUserId, newUserType,
requestedPermission, endDate, readUsageLimit(userRequest.get(USAGE_LIMIT)));
} else {
responses = SecurityProjectUtils.propagateProjectPermission(user, projectId, newUserId, newUserType,
requestedPermission, endDate);
}
userRet.put(newUserId, responses);
}
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
ret.put("users", userRet);
return ret;
});
}
/**
* Edit user permission for a 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 existingUserType = WebUtility.inputSQLSanitizer(form.getFirst("type"));
String projectId = WebUtility.inputSanitizer(form.getFirst("projectId"));
String newPermission = WebUtility.inputSanitizer(form.getFirst("permission"));
String endDate = WebUtility.inputSanitizer(form.getFirst("endDate"));
return ResourceUtility.respond(request, "edit user " + existingUserId + " on project " + projectId, user -> {
if (AbstractSecurityUtils.adminOnlyProjectAddAccess(projectId) && !SecurityAdminUtils.userIsAdmin(user)) {
throw new IllegalAccessException(ADMINS_ONLY);
}
SecurityProjectUtils.editProjectUserPermission(user, existingUserId, existingUserType, projectId,
AccessPermissionEnum.require(newPermission), endDate);
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
return ret;
});
}
/**
* Edit user permission for project, in bulk
*
* @param request
* @param form
* @return
*/
@POST
@Produces("application/json")
@Path("editProjectUserPermissions")
public Response editProjectUserPermissions(@Context HttpServletRequest request,
MultivaluedMap<String, String> form) {
String projectId = WebUtility.inputSanitizer(form.getFirst("projectId"));
String endDate = WebUtility.inputSanitizer(form.getFirst("endDate"));
return ResourceUtility.respond(request, "edit user permissions on project " + projectId, user -> {
if (AbstractSecurityUtils.adminOnlyProjectAddAccess(projectId) && !SecurityAdminUtils.userIsAdmin(user)) {
throw new IllegalAccessException(ADMINS_ONLY);
}
List<Map<String, String>> requests = new Gson().fromJson(form.getFirst("userpermissions"), List.class);
AccessPermissionEnum.requireEach(requests);
SecurityProjectUtils.editProjectUserPermissions(user, projectId, requests, endDate);
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
return ret;
});
}
/**
* Remove user permission for an app
*
* @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.inputSanitizer(form.getFirst("projectId"));
String action = "remove user " + existingUserId + " from project " + projectId;
return ResourceUtility.respond(request, action, user -> {
if (AbstractSecurityUtils.adminOnlyProjectAddAccess(projectId) && !SecurityAdminUtils.userIsAdmin(user)) {
throw new IllegalAccessException(ADMINS_ONLY);
}
SecurityProjectUtils.removeProjectUser(user, existingUserId, projectId);
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
return ret;
});
}
/**
* 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.inputSanitizer(projectIdInput);
boolean isTemplate = Boolean.parseBoolean(templateInput);
return ResourceUtility.respond(request, "set whether project " + projectId + " is a template", user -> {
if (projectIdInput == null || projectIdInput.trim().isEmpty() || templateInput == null
|| !("true".equalsIgnoreCase(templateInput) || "false".equalsIgnoreCase(templateInput))) {
throw new IllegalArgumentException("projectId and a true or false template value are required");
}
SecurityProjectUtils.setProjectTemplate(user, projectId, isTemplate);
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
return ret;
});
}
/**
* Sets whether a project has a login page of its own in its portal, so its
* portal is served to signed out visitors for them to sign in there. Only the
* project's owners can change it.
*
* @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.inputSanitizer(projectIdInput);
boolean hasCustomLogin = Boolean.parseBoolean(customLoginInput);
return ResourceUtility.respond(request, "set whether project " + projectId + " has its own login page",
user -> {
if (projectIdInput == null || projectIdInput.trim().isEmpty()
|| !("true".equalsIgnoreCase(customLoginInput)
|| "false".equalsIgnoreCase(customLoginInput))) {
throw new IllegalArgumentException(
"projectId and a true or false customLogin value are required");
}
SecurityProjectUtils.setProjectCustomLogin(user, projectId, hasCustomLogin);
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
return ret;
});
}
/**
* Get the app as being global (read only) for the entire semoss instance
*
* @param request
* @param form
* @return
*/
@POST
@Produces("application/json")
@Path("setProjectGlobal")
public Response setProjectGlobal(@Context HttpServletRequest request, MultivaluedMap<String, String> form) {
String projectId = WebUtility.inputSanitizer(form.getFirst("projectId"));
boolean isPublic = Boolean.parseBoolean(form.getFirst("public"));
String action = "make project " + projectId + (isPublic ? " public" : " private");
return ResourceUtility.respond(request, action, user -> {
if (AbstractSecurityUtils.adminOnlyProjectSetPublic(projectId) && !SecurityAdminUtils.userIsAdmin(user)) {
throw new IllegalAccessException(ADMINS_ONLY);
}
SecurityProjectUtils.setProjectGlobal(user, projectId, isPublic);
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
return ret;
});
}
/**
* 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.inputSanitizer(form.getFirst("projectId"));
boolean isDiscoverable = Boolean.parseBoolean(form.getFirst("discoverable"));
String action = "make project " + projectId + (isDiscoverable ? " discoverable" : " not discoverable");
return ResourceUtility.respond(request, action, user -> {
if (AbstractSecurityUtils.adminOnlyProjectSetDiscoverable(projectId)
&& !SecurityAdminUtils.userIsAdmin(user)) {
throw new IllegalAccessException(ADMINS_ONLY);
}
SecurityProjectUtils.setProjectDiscoverable(user, projectId, isDiscoverable);
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
return ret;
});
}
/**
* Set the app visibility for the user to be seen
*
* @param request
* @param form
* @return
*/
@POST
@Produces("application/json")
@Path("setProjectVisibility")
public Response setProjectVisibility(@Context HttpServletRequest request, MultivaluedMap<String, String> form) {
String projectId = WebUtility.inputSanitizer(form.getFirst("projectId"));
boolean visible = Boolean.parseBoolean(form.getFirst("visibility"));
String action = (visible ? "show" : "hide") + " project " + projectId;
return ResourceUtility.respond(request, action, user -> {
SecurityProjectUtils.setProjectVisibility(user, projectId, visible);
return true;
});
}
/**
* Set the app as favorited by the user
*
* @param request
* @param form
* @return
*/
@POST
@Produces("application/json")
@Path("setProjectFavorite")
public Response setProjectFavorite(@Context HttpServletRequest request, MultivaluedMap<String, String> form) {
String projectId = WebUtility.inputSanitizer(form.getFirst("projectId"));
boolean isFavorite = Boolean.parseBoolean(form.getFirst("isFavorite"));
String action = (isFavorite ? "favorite" : "unfavorite") + " project " + projectId;
return ResourceUtility.respond(request, action, user -> {
SecurityProjectUtils.setProjectFavorite(user, projectId, isFavorite);
return true;
});
}
/**
* Get users with no access to a given app
*
* @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.inputSanitizer(projectId);
String term = WebUtility.inputSQLSanitizer(searchTerm);
return ResourceUtility.respond(request, "find people to add to project " + project, user -> {
// when the directory is not used
// then we will look at our security db
if (!MicrosoftGraphUserLookup.useDirectory(msGraphLookup)) {
return SecurityProjectUtils.getProjectUsersNoCredentials(user, project, term, limit, offset);
}
return MsGraphUtility.getProjectUsers(request, user, project, term, limit, offset, false);
});
}
/**
* 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.inputSanitizer(form.getFirst("projectId"));
String endDate = WebUtility.inputSanitizer(form.getFirst("endDate"));
return ResourceUtility.respond(request, "approve access requests for project " + projectId, user -> {
if (AbstractSecurityUtils.adminOnlyProjectAddAccess(projectId) && !SecurityAdminUtils.userIsAdmin(user)) {
throw new IllegalAccessException(ADMINS_ONLY);
}
// adding user permissions and updating user access requests in bulk
List<Map<String, String>> requests = new Gson().fromJson(form.getFirst("requests"), List.class);
AccessPermissionEnum.requireEach(requests);
SecurityProjectUtils.approveProjectUserAccessRequests(user, projectId, requests, endDate);
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
return ret;
});
}
/**
* 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.inputSanitizer(form.getFirst("projectId"));
return ResourceUtility.respond(request, "deny access requests for project " + projectId, user -> {
if (AbstractSecurityUtils.adminOnlyProjectAddAccess(projectId) && !SecurityAdminUtils.userIsAdmin(user)) {
throw new IllegalAccessException(ADMINS_ONLY);
}
// updating user access requests in bulk
List<String> requestids = new Gson().fromJson(form.getFirst("requestids"), List.class);
requestids = WebUtility.inputSQLSanitizer(requestids);
SecurityProjectUtils.denyProjectUserAccessRequests(user, projectId, requestids);
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
return ret;
});
}
/**
* 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.inputSanitizer(form.getFirst("projectId"));
String endDate = WebUtility.inputSanitizer(form.getFirst("endDate"));
return ResourceUtility.respond(request, "add users to project " + projectId, user -> {
if (AbstractSecurityUtils.adminOnlyProjectAddAccess(projectId) && !SecurityAdminUtils.userIsAdmin(user)) {
throw new IllegalAccessException(ADMINS_ONLY);
}
// adding user permissions in bulk
List<Map<String, String>> permission = AccessPermissionEnum
.requireEach(new Gson().fromJson(form.getFirst("userpermissions"), List.class));
// users picked from the Microsoft directory may not be in the security db yet
if (MicrosoftGraphUserLookup.isEnabled()) {
MicrosoftGraphUserLookup.addMissingUsers(user, permission);
}
SecurityProjectUtils.addProjectUserPermissions(user, projectId, permission, endDate);
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
return ret;
});
}
/**
* Remove user permissions for an 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.inputSanitizer(form.getFirst("projectId"));
return ResourceUtility.respond(request, "remove users from project " + projectId, user -> {
List<String> ids = new Gson().fromJson(form.getFirst("ids"), List.class);
if (AbstractSecurityUtils.adminOnlyProjectAddAccess(projectId) && !SecurityAdminUtils.userIsAdmin(user)) {
throw new IllegalAccessException(ADMINS_ONLY);
}
SecurityProjectUtils.removeProjectUsers(user, ids, projectId);
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
return ret;
});
}
/**
* Reads a row's {@code usageLimit}, sent as a JSON object or as JSON text.
*
* @param value the value sent, which may be null
* @return the limit, or null for no limit defined
* @throws IllegalArgumentException when the value is not a limit
*/
private static ModelUsageLimit readUsageLimit(Object value) {
if (value == null || value instanceof String) {
return SecurityModelUsageLimitUtils.readJson((String) value);
}
return SecurityModelUsageLimitUtils.readJson(ROW_GSON.toJson(value));
}
/**
* Reads a value from one of the posted user permission requests as text.
*
* @param value a row's value, which may be null
* @return the value as text, or null
*/
private static String textOf(Object value) {
return value == null ? null : value.toString();
}
/**
* Lists the apps, agents and engines a project gives a permission to. An app's
* custom reactors, an agent's runs, or an engine's own code, use the permission
* in place of the signed in user's own. The project's editors and owners list
* them.
*
* @param request the request, whose session holds the user
* @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 cannot edit the project
*/
@GET
@Produces("application/json")
@Path("getProjectGranteePermissions")
public Response getProjectGranteePermissions(@Context HttpServletRequest request,
@QueryParam("projectId") String projectId) {
String id = WebUtility.inputSanitizer(projectId);
return ResourceUtility.respond(request, "list the apps and engines project " + id + " gives a permission to",
user -> SecurityProjectUtils.getProjectGranteePermissions(user, id));
}
/**
* Gives an app or an agent a permission on a project, or changes the one it
* has, so an app's custom reactors or an agent's runs can use the project for
* people who have no access to it. A project gives access to apps and agents
* only. Only the project's owners can.
*
* @param request the request, whose session holds the user
* @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 owner of the project
*/
@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.respond(request, action, user -> {
String level = AccessPermissionEnum.require(permission);
SecurityProjectUtils.setProjectGranteePermission(user, granteeEngineId, granteeEngineType, projectId, level,
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. Only
* the project's owners can.
*
* @param request the request, whose session holds the user
* @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 owner of the project
*/
@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.respond(request, action, user -> {
SecurityProjectUtils.removeProjectGranteePermission(user, granteeEngineId, granteeEngineType, projectId);
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
return ret;
});
}
/**
* Refuses a user who cannot edit the project. Propagating a project's
* permissions gives people access to everything it depends on, so it is limited
* to the project's editors.
*
* @param user the signed in user
* @param projectId the project
* @throws IllegalAccessException when the user cannot edit the project
*/
private static void requireProjectEditor(User user, String projectId) throws IllegalAccessException {
if (!SecurityProjectUtils.userCanEditProject(user, projectId)) {
throw new IllegalAccessException(
"Project " + projectId + " does not exist or user does not have access to edit it");
}
}
}