Repository navigation
Expand file tree
/
Copy pathEngineAuthorizationResource.java
More file actions
764 lines (729 loc) · 32 KB
/
Copy pathEngineAuthorizationResource.java
File metadata and controls
764 lines (729 loc) · 32 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
/*******************************************************************************
* 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.utils.AbstractSecurityUtils;
import prerna.auth.utils.SecurityAdminUtils;
import prerna.auth.utils.SecurityEngineUtils;
import prerna.auth.utils.SecurityModelUsageLimitUtils;
import prerna.graph.utility.MsGraphUtility;
import prerna.io.connector.ms.MicrosoftGraphUserLookup;
import prerna.om.Insight;
import prerna.reactor.security.MyEnginesReactor;
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/engine")
@PermitAll
public class EngineAuthorizationResource {
private static final String ADMINS_ONLY = "This functionality is limited to only admins";
/**
* The form field for a model usage limit, as JSON, empty for no limit defined.
* An edit without it keeps the person's limit.
*/
private static final String USAGE_LIMIT = "usageLimit";
@Context
protected ServletContext context;
/**
* Get the engines the user has access to
*
* @param request
* @return
*/
@GET
@Produces("application/json")
@Path("getEngines")
public Response getEngines(@Context HttpServletRequest request, @QueryParam("engineId") List<String> engineFilter,
@QueryParam("engineTypes") List<String> engineTypes, @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> engines = WebUtility.inputSanitizer(engineFilter);
List<String> types = WebUtility.inputSanitizer(engineTypes);
List<String> keys = WebUtility.inputSanitizer(metaKeys);
return ResourceUtility.respond(request, "list the engines they can access", user -> {
MyEnginesReactor reactor = new MyEnginesReactor();
reactor.In();
Insight temp = new Insight();
temp.setUser(user);
reactor.setInsight(temp);
String filterWord = WebUtility.inputSQLSanitizer(term);
if (filterWord != null) {
GenRowStruct struct = new GenRowStruct();
struct.add(new NounMetadata(filterWord, 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 (engines != null && !engines.isEmpty()) {
GenRowStruct struct = new GenRowStruct();
for (String engine : engines) {
struct.add(new NounMetadata(engine, PixelDataType.CONST_STRING));
}
reactor.getNounStore().addNoun(ReactorKeysEnum.ENGINE.getKey(), struct);
}
if (types != null && !types.isEmpty()) {
GenRowStruct struct = new GenRowStruct();
for (String eType : types) {
struct.add(new NounMetadata(eType, PixelDataType.CONST_STRING));
}
reactor.getNounStore().addNoun(ReactorKeysEnum.ENGINE_TYPE.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("getEngines")
public Response getEnginesPOST(@Context HttpServletRequest request) {
return ResourceUtility.respond(request, "list the engines they can access", user -> {
MyEnginesReactor reactor = new MyEnginesReactor();
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("engineId") && parameterMap.get("engineId") != null
&& parameterMap.get("engineId").length > 0) {
GenRowStruct struct = new GenRowStruct();
String[] engineFilter = parameterMap.get("engineId");
for (String engine : engineFilter) {
struct.add(new NounMetadata(WebUtility.inputSanitizer(engine), PixelDataType.CONST_STRING));
}
reactor.getNounStore().addNoun(ReactorKeysEnum.ENGINE.getKey(), struct);
}
if (parameterMap.containsKey("engineTypes") && parameterMap.get("engineTypes") != null
&& parameterMap.get("engineTypes").length > 0) {
GenRowStruct struct = new GenRowStruct();
String[] engineTypes = parameterMap.get("engineTypes");
for (String eType : engineTypes) {
struct.add(new NounMetadata(WebUtility.inputSanitizer(eType), PixelDataType.CONST_STRING));
}
reactor.getNounStore().addNoun(ReactorKeysEnum.ENGINE_TYPE.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(WebUtility.inputSanitizer(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 engine permission level
*
* @param request
* @param form
* @return
*/
@GET
@Produces("application/json")
@Path("getUserEnginePermission")
public Response getUserEnginePermission(@Context HttpServletRequest request,
@QueryParam("engineId") String engineId) {
String engine = WebUtility.inputSanitizer(engineId);
String action = "read their permission on engine " + engine;
return ResourceUtility.respond(request, action, user -> {
String permission = SecurityEngineUtils.getActualUserEnginePermission(user, engine);
if (permission == null) {
// are you discoverable?
if (SecurityEngineUtils.engineIsDiscoverable(engine)) {
permission = "DISCOVERABLE";
} else {
throw new IllegalAccessException("User does not have access to this engine");
}
}
Map<String, String> ret = new HashMap<>();
ret.put("permission", permission);
return ret;
});
}
/**
* Get the engine users and their permissions. On a model, each row has its
* {@code usage_limit}.
*
* @param request
* @param form
* @return
*/
@GET
@Produces("application/json")
@Path("getEngineUsers")
public Response getEngineUsers(@Context HttpServletRequest request, @QueryParam("engineId") String engineId,
@QueryParam("userId") String userId, @QueryParam("searchTerm") String searchTerm,
@QueryParam("permission") String permission, @QueryParam("limit") long limit,
@QueryParam("offset") long offset) {
String engine = WebUtility.inputSanitizer(engineId);
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 engine " + engine, user -> {
List<Map<String, Object>> members = SecurityEngineUtils.getEngineUsers(user, engine, searchParam, level,
limit, offset);
long totalMembers = SecurityEngineUtils.getEngineUsersCount(user, engine, searchParam, level);
Map<String, Object> ret = new HashMap<>();
ret.put("totalMembers", totalMembers);
ret.put("members", members);
return ret;
});
}
/**
* Add a user to an engine. A model's usage limit is sent as {@code usageLimit}
* (JSON, empty for no limit defined); without it the person gets no limit. Only
* the model's owners give a limit.
*
* @param request
* @param form
* @return
*/
@POST
@Produces("application/json")
@Path("addEngineUserPermission")
public Response addEngineUserPermission(@Context HttpServletRequest request, MultivaluedMap<String, String> form) {
String engineId = WebUtility.inputSanitizer(form.getFirst("engineId"));
String newUserId = WebUtility.inputSQLSanitizer(form.getFirst("id"));
String permission = WebUtility.inputSanitizer(form.getFirst("permission"));
String endDate = null; // form.getFirst("endDate");
String usageLimit = form.getFirst(USAGE_LIMIT);
return ResourceUtility.respond(request, "add user " + newUserId + " to engine " + engineId, user -> {
if (AbstractSecurityUtils.adminOnlyEngineAddAccess(engineId) && !SecurityAdminUtils.userIsAdmin(user)) {
throw new IllegalAccessException(ADMINS_ONLY);
}
String level = AccessPermissionEnum.require(permission);
SecurityEngineUtils.addEngineUser(user, newUserId, engineId, level, endDate,
SecurityModelUsageLimitUtils.readJson(usageLimit));
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
return ret;
});
}
/**
* Add user permissions in bulk to a engine. Each row of {@code userpermissions}
* can carry a model's usage limit as {@code usageLimit}, which only the model's
* owners give; a row without it gives no limit.
*
* @param request
* @param form
* @return
*/
@POST
@Produces("application/json")
@Path("addEngineUserPermissions")
public Response addEngineUserPermissions(@Context HttpServletRequest request, MultivaluedMap<String, String> form) {
String engineId = WebUtility.inputSanitizer(form.getFirst("engineId"));
return ResourceUtility.respond(request, "add users to engine " + engineId, user -> {
if (AbstractSecurityUtils.adminOnlyEngineAddAccess(engineId) && !SecurityAdminUtils.userIsAdmin(user)) {
throw new IllegalAccessException(ADMINS_ONLY);
}
// adding user permissions in bulk
List<Map<String, Object>> 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);
}
// now add the permission
SecurityEngineUtils.addEngineUserPermissions(user, engineId, permission);
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
return ret;
});
}
/**
* Edit user permission for an engine. A model's usage limit is sent as
* {@code usageLimit} (JSON, empty for no limit defined); a form without it,
* such as a change of level only, leaves the limit as it is. Only the model's
* owners change a limit.
*
* @param request
* @param form
* @return
*/
@POST
@Produces("application/json")
@Path("editEngineUserPermission")
public Response editEngineUserPermission(@Context HttpServletRequest request, MultivaluedMap<String, String> form) {
String engineId = WebUtility.inputSanitizer(form.getFirst("engineId"));
String existingUserId = WebUtility.inputSQLSanitizer(form.getFirst("id"));
String existingUserType = WebUtility.inputSQLSanitizer(form.getFirst("type"));
String newPermission = WebUtility.inputSanitizer(form.getFirst("permission"));
String endDate = null; // form.getFirst("endDate");
boolean usageLimitSent = form.containsKey(USAGE_LIMIT);
String usageLimit = form.getFirst(USAGE_LIMIT);
return ResourceUtility.respond(request, "edit user " + existingUserId + " on engine " + engineId, user -> {
if (AbstractSecurityUtils.adminOnlyEngineAddAccess(engineId) && !SecurityAdminUtils.userIsAdmin(user)) {
throw new IllegalAccessException(ADMINS_ONLY);
}
String level = AccessPermissionEnum.require(newPermission);
if (usageLimitSent) {
SecurityEngineUtils.editEngineUserPermission(user, existingUserId, existingUserType, engineId, level,
endDate, SecurityModelUsageLimitUtils.readJson(usageLimit));
} else {
SecurityEngineUtils.editEngineUserPermission(user, existingUserId, existingUserType, engineId, level,
endDate);
}
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
return ret;
});
}
/**
* Edit user permission for an engine, in bulk. Each row of
* {@code userpermissions} can carry a model's usage limit as
* {@code usageLimit}, which only the model's owners change; a row without it
* keeps the person's limit, and a row whose {@code usageLimit} is null removes
* it.
*
* @param request
* @param form
* @return
*/
@POST
@Produces("application/json")
@Path("editEngineUserPermissions")
public Response editEngineUserPermissions(@Context HttpServletRequest request,
MultivaluedMap<String, String> form) {
String engineId = WebUtility.inputSanitizer(form.getFirst("engineId"));
return ResourceUtility.respond(request, "edit user permissions on engine " + engineId, user -> {
if (AbstractSecurityUtils.adminOnlyEngineAddAccess(engineId) && !SecurityAdminUtils.userIsAdmin(user)) {
throw new IllegalAccessException(ADMINS_ONLY);
}
List<Map<String, Object>> requests = new Gson().fromJson(form.getFirst("userpermissions"), List.class);
SecurityEngineUtils.editEngineUserPermissions(user, engineId, AccessPermissionEnum.requireEach(requests));
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
return ret;
});
}
/**
* Remove user permission for an engine
*
* @param request
* @param form
* @return
*/
@POST
@Produces("application/json")
@Path("removeEngineUserPermission")
public Response removeEngineUserPermission(@Context HttpServletRequest request,
MultivaluedMap<String, String> form) {
String existingUserId = WebUtility.inputSQLSanitizer(form.getFirst("id"));
String engineId = WebUtility.inputSanitizer(form.getFirst("engineId"));
String action = "remove user " + existingUserId + " from engine " + engineId;
return ResourceUtility.respond(request, action, user -> {
if (AbstractSecurityUtils.adminOnlyEngineAddAccess(engineId) && !SecurityAdminUtils.userIsAdmin(user)) {
throw new IllegalAccessException(ADMINS_ONLY);
}
SecurityEngineUtils.removeEngineUser(user, existingUserId, engineId);
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
return ret;
});
}
/**
* Remove user permissions for an engine, in bulk
*
* @param request
* @param form
* @return
*/
@POST
@Produces("application/json")
@Path("removeEngineUserPermissions")
public Response removeEngineUserPermissions(@Context HttpServletRequest request,
MultivaluedMap<String, String> form) {
String engineId = WebUtility.inputSanitizer(form.getFirst("engineId"));
return ResourceUtility.respond(request, "remove users from engine " + engineId, user -> {
List<String> ids = new Gson().fromJson(form.getFirst("ids"), List.class);
ids = WebUtility.inputSQLSanitizer(ids);
if (AbstractSecurityUtils.adminOnlyEngineAddAccess(engineId) && !SecurityAdminUtils.userIsAdmin(user)) {
throw new IllegalAccessException(ADMINS_ONLY);
}
SecurityEngineUtils.removeEngineUsers(user, ids, engineId);
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
return ret;
});
}
/**
* Lists the apps and engines an engine gives a permission to. An app's custom
* reactors, or another engine's own code, use the permission in place of the
* signed in user's own. The engine's editors and owners list them.
*
* @param request the request, whose session holds the user
* @param engineId the engine
* @return one entry for each app or engine, with its id, catalog type
* ({@code PROJECT} for an app) and name, its level and end date and, on
* a model, its App Limit or Engine Limit; 403 for a user who cannot
* edit the engine
*/
@GET
@Produces("application/json")
@Path("getEngineGranteePermissions")
public Response getEngineGranteePermissions(@Context HttpServletRequest request,
@QueryParam("engineId") String engineId) {
String id = WebUtility.inputSanitizer(engineId);
return ResourceUtility.respond(request, "list the apps and engines engine " + id + " gives a permission to",
user -> SecurityEngineUtils.getEngineGranteePermissions(user, id));
}
/**
* Gives an app or another engine a permission on an engine, or changes the one
* it has, so the app's custom reactors or the other engine's own code can use
* the engine for people who have no access to it. Only the engine's owners
* can. On a model the App Limit or Engine Limit is sent as {@code usageLimit}
* (JSON, empty for no limit defined); without it the grantee keeps the limit
* it has.
*
* @param request the request, whose session holds the user
* @param form {@code engineId}, {@code granteeEngineId},
* {@code granteeEngineType} (the grantee's catalog type,
* {@code PROJECT} for an app), {@code permission} (READ_ONLY or
* EDIT), and optionally {@code endDate} and {@code usageLimit}
* @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 engine
*/
@POST
@Produces("application/json")
@Path("setEngineGranteePermission")
public Response setEngineGranteePermission(@Context HttpServletRequest request,
MultivaluedMap<String, String> form) {
String engineId = WebUtility.inputSanitizer(form.getFirst("engineId"));
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"));
boolean givesUsageLimit = form.containsKey(USAGE_LIMIT);
String usageLimit = form.getFirst(USAGE_LIMIT);
String action = "give " + granteeEngineType + " " + granteeEngineId + " " + permission
+ " access to engine " + engineId;
return ResourceUtility.respond(request, action, user -> {
String level = AccessPermissionEnum.require(permission);
if (givesUsageLimit) {
SecurityEngineUtils.setEngineGranteePermission(user, granteeEngineId, granteeEngineType, engineId,
level, endDate, SecurityModelUsageLimitUtils.readJson(usageLimit));
} else {
SecurityEngineUtils.setEngineGranteePermission(user, granteeEngineId, granteeEngineType, engineId,
level, endDate);
}
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
return ret;
});
}
/**
* Takes away the permission an engine gives an app or another engine. Only the
* engine's owners can. On a model the grantee's App Limit or Engine Limit goes
* with it.
*
* @param request the request, whose session holds the user
* @param form {@code engineId}, {@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 engine
*/
@POST
@Produces("application/json")
@Path("removeEngineGranteePermission")
public Response removeEngineGranteePermission(@Context HttpServletRequest request,
MultivaluedMap<String, String> form) {
String engineId = WebUtility.inputSanitizer(form.getFirst("engineId"));
String granteeEngineId = WebUtility.inputSanitizer(form.getFirst("granteeEngineId"));
String granteeEngineType = WebUtility.inputSanitizer(form.getFirst("granteeEngineType"));
String action = "take away the access of " + granteeEngineType + " " + granteeEngineId + " to engine "
+ engineId;
return ResourceUtility.respond(request, action, user -> {
SecurityEngineUtils.removeEngineGranteePermission(user, granteeEngineId, granteeEngineType, engineId);
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
return ret;
});
}
/**
* Set the engine as being global (read only) for the entire semoss instance
*
* @param request
* @param form
* @return
*/
@POST
@Produces("application/json")
@Path("setEngineGlobal")
public Response setEngineGlobal(@Context HttpServletRequest request, MultivaluedMap<String, String> form) {
String engineId = WebUtility.inputSanitizer(form.getFirst("engineId"));
boolean isPublic = Boolean.parseBoolean(form.getFirst("public"));
String action = "make engine " + engineId + (isPublic ? " public" : " private");
return ResourceUtility.respond(request, action, user -> {
if (AbstractSecurityUtils.adminOnlyEngineSetPublic(engineId) && !SecurityAdminUtils.userIsAdmin(user)) {
throw new IllegalAccessException(ADMINS_ONLY);
}
SecurityEngineUtils.setEngineGlobal(user, engineId, isPublic);
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
return ret;
});
}
/**
* Set the engine as being discoverable for the entire semoss instance
*
* @param request
* @param form
* @return
*/
@POST
@Produces("application/json")
@Path("setEngineDiscoverable")
public Response setEngineDiscoverable(@Context HttpServletRequest request, MultivaluedMap<String, String> form) {
String engineId = WebUtility.inputSanitizer(form.getFirst("engineId"));
boolean isDiscoverable = Boolean.parseBoolean(form.getFirst("discoverable"));
String action = "make engine " + engineId + (isDiscoverable ? " discoverable" : " not discoverable");
return ResourceUtility.respond(request, action, user -> {
if (AbstractSecurityUtils.adminOnlyEngineSetDiscoverable(engineId)
&& !SecurityAdminUtils.userIsAdmin(user)) {
throw new IllegalAccessException(ADMINS_ONLY);
}
SecurityEngineUtils.setEngineDiscoverable(user, engineId, isDiscoverable);
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
return ret;
});
}
/**
* Set the engine visibility for the user to be seen
*
* @param request
* @param form
* @return
*/
@POST
@Produces("application/json")
@Path("setEngineVisibility")
public Response setEngineVisibility(@Context HttpServletRequest request, MultivaluedMap<String, String> form) {
String engineId = WebUtility.inputSanitizer(form.getFirst("engineId"));
boolean visible = Boolean.parseBoolean(form.getFirst("visibility"));
String action = (visible ? "show" : "hide") + " engine " + engineId;
return ResourceUtility.respond(request, action, user -> {
SecurityEngineUtils.setEngineVisibility(user, engineId, visible);
return true;
});
}
/**
* Set the engine as favorited by the user
*
* @param request
* @param form
* @return
*/
@POST
@Produces("application/json")
@Path("setEngineFavorite")
public Response setEngineFavorite(@Context HttpServletRequest request, MultivaluedMap<String, String> form) {
String engineId = WebUtility.inputSanitizer(form.getFirst("engineId"));
boolean isFavorite = Boolean.parseBoolean(form.getFirst("isFavorite"));
String action = (isFavorite ? "favorite" : "unfavorite") + " engine " + engineId;
return ResourceUtility.respond(request, action, user -> {
SecurityEngineUtils.setEngineFavorite(user, engineId, isFavorite);
return true;
});
}
/**
* Get users with no access to a given engine
*
* @param request
* @param form
* @return
*/
@GET
@Produces("application/json")
@Path("getEngineUsersNoCredentials")
public Response getEngineUsersNoCredentials(@Context HttpServletRequest request,
@QueryParam("engineId") String engineId, @QueryParam("searchTerm") String searchTerm,
@QueryParam("limit") long limit, @QueryParam("offset") long offset,
@QueryParam(MicrosoftGraphUserLookup.LOOKUP_PARAM) String msGraphLookup) {
String engine = WebUtility.inputSanitizer(engineId);
String term = WebUtility.inputSQLSanitizer(searchTerm);
return ResourceUtility.respond(request, "find people to add to engine " + engine, user -> {
// when the directory is not used
// then we will look at our security db
if (!MicrosoftGraphUserLookup.useDirectory(msGraphLookup)) {
return SecurityEngineUtils.getEngineUsersNoCredentials(user, engine, term, limit, offset);
}
return MsGraphUtility.getEngineUsers(request, user, engine, term, limit, offset, false);
});
}
/**
* approval of user access requests
*
* @param request
* @param form
* @return
*/
@POST
@Produces("application/json")
@Path("approveEngineUserAccessRequest")
public Response approveEngineUserAccessRequest(@Context HttpServletRequest request,
MultivaluedMap<String, String> form) {
String engineId = WebUtility.inputSanitizer(form.getFirst("engineId"));
String endDate = null; // form.getFirst("endDate");
return ResourceUtility.respond(request, "approve access requests for engine " + engineId, user -> {
if (AbstractSecurityUtils.adminOnlyEngineAddAccess(engineId) && !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);
SecurityEngineUtils.approveEngineUserAccessRequests(user, engineId, 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("denyEngineUserAccessRequest")
public Response denyEngineUserAccessRequest(@Context HttpServletRequest request,
MultivaluedMap<String, String> form) {
String engineId = WebUtility.inputSanitizer(form.getFirst("engineId"));
return ResourceUtility.respond(request, "deny access requests for engine " + engineId, user -> {
if (AbstractSecurityUtils.adminOnlyEngineAddAccess(engineId) && !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);
SecurityEngineUtils.denyEngineUserAccessRequests(user, engineId, requestIds);
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
return ret;
});
}
}