Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,59 @@ public Response removeProjectUserPermission(@Context HttpServletRequest request,
return WebUtility.getResponse(ret, 200);
}

/**
* 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");
if (projectIdInput == null || projectIdInput.trim().isEmpty() || templateInput == null
|| !("true".equalsIgnoreCase(templateInput) || "false".equalsIgnoreCase(templateInput))) {
Map<String, String> errorMap = new HashMap<>();
errorMap.put(Constants.ERROR_MESSAGE, "projectId and a true or false template value are required");
return WebUtility.getResponse(errorMap, 400);
}

String projectId = WebUtility.inputSQLSanitizer(projectIdInput);
boolean isTemplate = Boolean.parseBoolean(templateInput);
SecurityAdminUtils adminUtils;
try {
User user = ResourceUtility.getUser(request);
adminUtils = performAdminCheck(request, user);
} catch (IllegalAccessException e) {
classLogger.warn("User attempted to update project {} template status without admin access", projectId);
Map<String, String> errorMap = new HashMap<>();
errorMap.put(Constants.ERROR_MESSAGE, e.getMessage());
return WebUtility.getResponse(errorMap, 401);
}

try {
adminUtils.setProjectTemplate(projectId, isTemplate);
} catch (IllegalArgumentException e) {
classLogger.error("Failed to update template status for project {}", projectId, e);
Map<String, String> errorMap = new HashMap<>();
errorMap.put(Constants.ERROR_MESSAGE, e.getMessage());
return WebUtility.getResponse(errorMap, 400);
} catch (Exception e) {
classLogger.error("Unexpected failure updating template status for project {}", projectId, e);
Map<String, String> errorMap = new HashMap<>();
errorMap.put(Constants.ERROR_MESSAGE, "An unexpected error happened. Please try again.");
return WebUtility.getResponse(errorMap, 500);
}

classLogger.info("Admin has set project {} template status to {}", projectId, isTemplate);
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
return WebUtility.getResponse(ret, 200);
}

@POST
@Produces("application/json")
@Path("setProjectGlobal")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,63 @@ public Response removeProjectUserPermission(@Context HttpServletRequest request,
return WebUtility.getResponse(ret, 200);
}

/**
* 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) {
User user;
try {
user = ResourceUtility.getUser(request);
} catch (IllegalAccessException e) {
classLogger.error("Invalid user session trying to update a project template setting", e);
Map<String, String> errorMap = new HashMap<>();
errorMap.put(Constants.ERROR_MESSAGE, "User session is invalid");
return WebUtility.getResponse(errorMap, 401);
}

String projectIdInput = form.getFirst("projectId");
String templateInput = form.getFirst("template");
if (projectIdInput == null || projectIdInput.trim().isEmpty() || templateInput == null
|| !("true".equalsIgnoreCase(templateInput) || "false".equalsIgnoreCase(templateInput))) {
Map<String, String> errorMap = new HashMap<>();
errorMap.put(Constants.ERROR_MESSAGE, "projectId and a true or false template value are required");
return WebUtility.getResponse(errorMap, 400);
}

String projectId = WebUtility.inputSanitizer(projectIdInput);
boolean isTemplate = Boolean.parseBoolean(templateInput);
try {
SecurityProjectUtils.setProjectTemplate(user, projectId, isTemplate);
} catch (IllegalAccessException e) {
classLogger.warn("User attempted to update project {} template status without owner access", projectId);
Map<String, String> errorMap = new HashMap<>();
errorMap.put(Constants.ERROR_MESSAGE, e.getMessage());
return WebUtility.getResponse(errorMap, 400);
} catch (IllegalArgumentException e) {
classLogger.error("Failed to update template status for project {}", projectId, e);
Map<String, String> errorMap = new HashMap<>();
errorMap.put(Constants.ERROR_MESSAGE, e.getMessage());
return WebUtility.getResponse(errorMap, 400);
} catch (Exception e) {
classLogger.error("Unexpected failure updating template status for project {}", projectId, e);
Map<String, String> errorMap = new HashMap<>();
errorMap.put(Constants.ERROR_MESSAGE, "An unexpected error happened. Please try again.");
return WebUtility.getResponse(errorMap, 500);
}

classLogger.info("User has set project {} template status to {}", projectId, isTemplate);
Map<String, Object> ret = new HashMap<>();
ret.put("success", true);
return WebUtility.getResponse(ret, 200);
}

/**
* Get the app as being global (read only) for the entire semoss instance
*
Expand Down
Loading