-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFormResource.java
More file actions
371 lines (332 loc) · 13.6 KB
/
Copy pathFormResource.java
File metadata and controls
371 lines (332 loc) · 13.6 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
/*******************************************************************************
* 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.form;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import javax.naming.InvalidNameException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import jakarta.annotation.security.PermitAll;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Response;
import prerna.auth.AuthProvider;
import prerna.auth.User;
import prerna.ds.util.RdbmsQueryBuilder;
import prerna.engine.api.IDatabaseEngine;
import prerna.engine.api.IRawSelectWrapper;
import prerna.forms.AbstractFormBuilder;
import prerna.forms.FormBuilder;
import prerna.forms.FormFactory;
import prerna.masterdatabase.utility.MasterDatabaseUtility;
import prerna.rdf.engine.wrappers.WrapperManager;
import prerna.util.Constants;
import prerna.util.Utility;
import prerna.web.services.util.WebUtility;
@Path("/form")
@PermitAll
public class FormResource {
private static final Logger classLogger = LogManager.getLogger(FormResource.class);
private IDatabaseEngine formEngine;
@POST
@Path("/modifyUserAccess")
@Produces("application/json")
public Response modifyUserAccess(@Context HttpServletRequest request, MultivaluedMap<String, String> form) {
String cacId;
try {
cacId = getCacId(request);
} catch (IOException e) {
Map<String, String> err = new HashMap<String, String>();
err.put(Constants.ERROR_MESSAGE, e.getMessage());
return WebUtility.getResponse(err, 400);
}
try {
throwErrorIfNotAdmin(cacId);
} catch (IllegalAccessException e) {
classLogger.warn("User is trying to modify user access while not being an admin");
Map<String, String> err = new HashMap<String, String>();
err.put(Constants.ERROR_MESSAGE, e.getMessage());
return WebUtility.getResponse(err, 400);
}
String addOrRemove = WebUtility.inputSQLSanitizer(form.getFirst("addOrRemove"));
String userid = WebUtility.inputSQLSanitizer(form.getFirst("userid"));
String instancename = Utility.cleanString(WebUtility.inputSQLSanitizer(form.getFirst("instanceName")), true);
// this is only present if we are adding a user
String owner = WebUtility.inputSQLSanitizer(form.getFirst("ownerStatus"));
String query = null;
if (addOrRemove.equals("Remove")) {
if (instancename != null && !instancename.isEmpty() && !instancename.equals("null")
&& !instancename.equals("undefined")) {
query = "DELETE FROM FORMS_USER_ACCESS WHERE USER_ID = '"
+ RdbmsQueryBuilder.escapeForSQLStatement(userid) + "' AND INSTANCE_NAME = '"
+ RdbmsQueryBuilder.escapeForSQLStatement(instancename) + "';";
// log the operation
classLogger.info("User is removing user {} from having access to {}", userid, instancename);
} else {
// remove all of user
query = "DELETE FROM FORMS_USER_ACCESS WHERE USER_ID = '"
+ RdbmsQueryBuilder.escapeForSQLStatement(userid) + "';";
// log the operation
classLogger.info("User is removing all access for user {}", userid);
}
} else if (addOrRemove.equals("Add")) {
query = "INSERT INTO FORMS_USER_ACCESS (USER_ID, INSTANCE_NAME, IS_SYS_ADMIN) VALUES ('"
+ RdbmsQueryBuilder.escapeForSQLStatement(userid) + "','"
+ RdbmsQueryBuilder.escapeForSQLStatement(instancename) + "','"
+ RdbmsQueryBuilder.escapeForSQLStatement(owner) + "');";
// log the operation
classLogger.info("User is adding user {} to have access to {}", userid, instancename);
} else {
return WebUtility.getResponse("Error: need to specify Add or Remove", 400);
}
IDatabaseEngine formEngine = getEngine();
// execute the query
try {
formEngine.insertData(query);
// commit to engine
formEngine.commit();
} catch (Exception e) {
classLogger.error("Failed to insert form user access data and commit to the form engine", e);
return WebUtility.getResponse("An error occurred to update the user's access!", 400);
}
return WebUtility.getResponse("success", 200);
}
@POST
@Path("/renameInstance")
@Produces("application/json")
public Response renameInstance(@Context HttpServletRequest request, MultivaluedMap<String, String> form) {
String cacId;
try {
cacId = getCacId(request);
} catch (IOException e) {
Map<String, String> err = new HashMap<String, String>();
err.put(Constants.ERROR_MESSAGE, e.getMessage());
return WebUtility.getResponse(err, 400);
}
try {
throwErrorIfNotAdmin(cacId);
} catch (IllegalAccessException e) {
classLogger.warn("User is trying to rename an instance while not being an admin");
Map<String, String> err = new HashMap<String, String>();
err.put(Constants.ERROR_MESSAGE, e.getMessage());
return WebUtility.getResponse(err, 400);
}
String dbName = WebUtility.inputSQLSanitizer(form.getFirst("dbName"));
String origUri = WebUtility.inputSQLSanitizer(form.getFirst("originalUri"));
String newUri = WebUtility.inputSQLSanitizer(form.getFirst("newUri"));
boolean deleteInstanceBoolean = false;
if (form.getFirst("deleteInstanceBoolean") != null) {
deleteInstanceBoolean = Boolean.parseBoolean(form.getFirst("deleteInstanceBoolean"));
}
// log the operation
classLogger.info("User is renaming {} to {}", origUri, newUri);
IDatabaseEngine coreEngine = Utility.getDatabase(MasterDatabaseUtility.testDatabaseIdIfAlias(dbName));
AbstractFormBuilder formbuilder = FormFactory.getFormBuilder(coreEngine);
formbuilder.modifyInstanceValue(origUri, newUri, deleteInstanceBoolean);
return WebUtility.getResponse("success", 200);
}
@POST
@Path("/certifyInstance")
@Produces("application/json")
public Response certifyInstance(@Context HttpServletRequest request, MultivaluedMap<String, String> form) {
String cacId;
try {
cacId = getCacId(request);
} catch (IOException e) {
Map<String, String> err = new HashMap<String, String>();
err.put(Constants.ERROR_MESSAGE, e.getMessage());
return WebUtility.getResponse(err, 400);
}
String dbName = WebUtility.inputSQLSanitizer(form.getFirst("dbName"));
String instanceType = WebUtility.inputSQLSanitizer(form.getFirst("instanceType"));
String instanceName = WebUtility.inputSQLSanitizer(form.getFirst("instanceName"));
try {
throwErrorIfNotSysAdmin(cacId, instanceName);
} catch (IllegalAccessException e) {
classLogger.warn("User is trying to certify {} when he is not the system admin for the system",
instanceName);
Map<String, String> err = new HashMap<String, String>();
err.put(Constants.ERROR_MESSAGE, e.getMessage());
return WebUtility.getResponse(err, 400);
}
IDatabaseEngine coreEngine = Utility.getDatabase(MasterDatabaseUtility.testDatabaseIdIfAlias(dbName));
AbstractFormBuilder formbuilder = FormFactory.getFormBuilder(coreEngine);
formbuilder.setUser(cacId);
formbuilder.certifyInstance(instanceType, instanceName);
// log the operation
classLogger.info("User has certified {}", instanceName);
return WebUtility.getResponse("success", 200);
}
@POST
@Path("/getUserInstanceAuth")
@Produces("applicaiton/json")
public Response getUserInstanceAuth(@Context HttpServletRequest request) throws InvalidNameException {
/*
* Get the specific instances this user has access to
*/
String cacId;
try {
cacId = WebUtility.inputSQLSanitizer(getCacId(request));
} catch (IOException e) {
Map<String, String> err = new HashMap<String, String>();
err.put(Constants.ERROR_MESSAGE, e.getMessage());
return WebUtility.getResponse(err, 400);
}
Map<String, String> userAccessableInstances = new TreeMap<String, String>();
// map to store the valid instances for the given user
String query = "SELECT INSTANCE_NAME, IS_SYS_ADMIN FROM FORMS_USER_ACCESS WHERE USER_ID = '"
+ RdbmsQueryBuilder.escapeForSQLStatement(cacId) + "';";
IRawSelectWrapper wrapper = null;
try {
wrapper = WrapperManager.getInstance().getRawWrapper(getEngine(), query);
while (wrapper.hasNext()) {
Object[] values = wrapper.next().getValues();
userAccessableInstances.put(values[0].toString(), values[1].toString());
}
} catch (Exception e) {
classLogger.error("Failed to query the accessible form instances for user {}", cacId, e);
} finally {
if (wrapper != null) {
try {
wrapper.close();
} catch (IOException e) {
classLogger.error("Failed to close the result wrapper for the user instance access query", e);
}
}
}
Map<String, Object> returnData = new HashMap<String, Object>();
returnData.put("cac_id", cacId);
returnData.put("validInstances", userAccessableInstances);
return WebUtility.getResponse(returnData, 200);
}
/**
* Get the CAC ID for the user
*
* @param request
* @return
* @throws IOException
*/
private String getCacId(@Context HttpServletRequest request) throws IOException {
/*
* If you wanted to debug locally w/o a CAC, just return a string
*/
// return "mahkhalil";
String x509Id = null;
try {
HttpSession session = request.getSession(false);
User user = (User) session.getAttribute(Constants.SESSION_USER);
if (user.getAccessToken(AuthProvider.CAC) != null) {
x509Id = user.getAccessToken(AuthProvider.CAC).getId();
} else {
// if not CAC - we are using SMAL
x509Id = user.getAccessToken(AuthProvider.SAML).getId();
}
} catch (Exception e) {
throw new IOException("Could not identify user");
}
if (x509Id == null) {
throw new IOException("Could not identify user");
}
return x509Id;
}
/**
* Check that user is an admin
*
* @param cacId
* @throws IllegalAccessException
*/
private void throwErrorIfNotAdmin(String cacId) throws IllegalAccessException {
String isAdminQuery = "SELECT * FROM FORMS_USER_ACCESS " + "WHERE USER_ID='"
+ RdbmsQueryBuilder.escapeForSQLStatement(cacId) + "' " + "AND INSTANCE_NAME='ADMIN' " + "LIMIT 1;";
IRawSelectWrapper wrapper = null;
try {
wrapper = WrapperManager.getInstance().getRawWrapper(getEngine(), isAdminQuery);
if (!wrapper.hasNext()) {
throw new IllegalAccessException("User is not an admin and cannot perform this operation");
}
} catch (Exception e) {
classLogger.error("Failed to query whether user {} is an admin", cacId, e);
} finally {
if (wrapper != null) {
try {
wrapper.close();
} catch (IOException e) {
classLogger.error("Failed to close the result wrapper for the admin check query", e);
}
}
}
}
/**
* Check that user is an admin
*
* @param cacId
* @throws IllegalAccessException
*/
private void throwErrorIfNotSysAdmin(String cacId, String system) throws IllegalAccessException {
String isAdminQuery = "SELECT * FROM FORMS_USER_ACCESS " + "WHERE USER_ID='"
+ RdbmsQueryBuilder.escapeForSQLStatement(cacId) + "' " + "AND INSTANCE_NAME='"
+ RdbmsQueryBuilder.escapeForSQLStatement(system) + "' " + "AND IS_SYS_ADMIN=TRUE " + "LIMIT 1;";
IRawSelectWrapper wrapper = null;
try {
wrapper = WrapperManager.getInstance().getRawWrapper(getEngine(), isAdminQuery);
if (!wrapper.hasNext()) {
throw new IllegalAccessException("User is not an admin and cannot perform this operation");
}
} catch (Exception e) {
classLogger.error("Failed to query whether user {} is a system admin for system {}", cacId, system, e);
} finally {
if (wrapper != null) {
try {
wrapper.close();
} catch (IOException e) {
classLogger.error("Failed to close the result wrapper for the system admin check query", e);
}
}
}
}
/**
* Get the form engine Since this is a resource, we just need to make sure we
* load after DBLoader is done loading
*
* @return
*/
public IDatabaseEngine getEngine() {
if (formEngine == null) {
formEngine = Utility.getDatabase(FormBuilder.FORM_BUILDER_ENGINE_NAME);
AbstractFormBuilder.generateFormPermissionTable(formEngine);
}
return formEngine;
}
}