use of org.cerberus.crud.service.ILogEventService in project cerberus-source by cerberustesting.
the class UpdateMyUser method doPost.
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO create class Validator to validate all parameter from page
PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
String charset = request.getCharacterEncoding();
String login = request.getUserPrincipal().getName();
String column = request.getParameter("column");
String value = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("value"), "", charset);
response.setContentType("application/json");
JSONObject jsonResponse = new JSONObject();
LOG.debug("value : " + value + " column : " + column + " login : " + login);
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
IUserService userService = appContext.getBean(UserService.class);
User myUser;
try {
try {
myUser = userService.findUserByKey(login);
switch(column) {
case "name":
myUser.setName(value);
break;
case "team":
myUser.setTeam(value);
break;
case "defaultSystem":
myUser.setDefaultSystem(value);
request.getSession().setAttribute("MySystem", value);
break;
case "email":
myUser.setEmail(value);
break;
case "language":
myUser.setLanguage(value);
request.getSession().setAttribute("MyLang", value);
break;
case "userPreferences":
myUser.setUserPreferences(value);
break;
}
userService.updateUser(myUser);
/**
* Adding Log entry.
*/
ILogEventService logEventService = appContext.getBean(LogEventService.class);
logEventService.createForPrivateCalls("/UpdateMyUser", "UPDATE", "Updated user : " + login, request);
jsonResponse.put("messageType", MessageEventEnum.GENERIC_OK.getCodeString());
jsonResponse.put("message", MessageEventEnum.GENERIC_OK.getDescription());
} catch (CerberusException ex) {
jsonResponse.put("messageType", MessageEventEnum.GENERIC_ERROR.getCodeString());
jsonResponse.put("message", ex.getMessageError().getDescription());
}
} catch (JSONException e) {
LOG.warn(e);
// returns a default error message with the json format that is able to be parsed by the client-side
response.setContentType("application/json");
response.getWriter().print(AnswerUtil.createGenericErrorAnswer());
}
response.getWriter().print(jsonResponse.toString());
}
use of org.cerberus.crud.service.ILogEventService in project cerberus-source by cerberustesting.
the class DeleteUser method processRequest.
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, CerberusException, JSONException {
JSONObject jsonResponse = new JSONObject();
Answer ans = new Answer();
MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
ans.setResultMessage(msg);
PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
String charset = request.getCharacterEncoding();
String login = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("login"), "", charset);
boolean userHasPermissions = request.isUserInRole("Administrator");
/**
* Checking all constrains before calling the services.
*/
if (StringUtil.isNullOrEmpty(login)) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "User").replace("%OPERATION%", "Delete").replace("%REASON%", "User name is missing!"));
ans.setResultMessage(msg);
} else if (!userHasPermissions) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "User").replace("%OPERATION%", "Delete").replace("%REASON%", "You don't have the right to do that"));
ans.setResultMessage(msg);
} else {
/**
* All data seems cleans so we can call the services.
*/
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
IUserService userService = appContext.getBean(IUserService.class);
AnswerItem resp = userService.readByKey(login);
if (resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
if (resp.getItem() != null) {
ans = userService.delete((User) resp.getItem());
if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
/**
* Object updated. Adding Log entry.
*/
ILogEventService logEventService = appContext.getBean(LogEventService.class);
logEventService.createForPrivateCalls("/DeleteUser", "DELETE", "Delete User : ['" + login + "']", request);
}
} else {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "User").replace("%OPERATION%", "Delete").replace("%REASON%", "User not found"));
ans.setResultMessage(msg);
}
}
}
/**
* Formating and returning the json result.
*/
jsonResponse.put("messageType", ans.getResultMessage().getMessage().getCodeString());
jsonResponse.put("message", ans.getResultMessage().getDescription());
response.getWriter().print(jsonResponse);
response.getWriter().flush();
}
use of org.cerberus.crud.service.ILogEventService in project cerberus-source by cerberustesting.
the class UpdateMyUserSystem method processRequest.
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String login = request.getUserPrincipal().getName();
String value = request.getParameter("value").replace("'", "");
LOG.info("value : " + value + " login : " + login);
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
IUserService userService = appContext.getBean(UserService.class);
User myUser;
try {
myUser = userService.findUserByKey(login);
myUser.setDefaultSystem(value);
request.getSession().setAttribute("MySystem", value);
try {
userService.updateUser(myUser);
/**
* Adding Log entry.
*/
ILogEventService logEventService = appContext.getBean(LogEventService.class);
logEventService.createForPrivateCalls("/UpdateMyUserSystem", "UPDATE", "Updated user : " + login, request);
response.getWriter().print(value);
} catch (CerberusException ex) {
response.getWriter().print(ex.getMessageError().getDescription());
}
} catch (CerberusException ex) {
response.getWriter().print(ex.getMessageError().getDescription());
}
}
Aggregations