use of org.cerberus.crud.service.IUserService in project cerberus-source by cerberustesting.
the class GetUsers method doPost.
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String echo = request.getParameter("sEcho");
// data that will be shown in the table
JSONArray data = new JSONArray();
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
IUserService userService = appContext.getBean(UserService.class);
IUserSystemService userSystemService = appContext.getBean(IUserSystemService.class);
IUserGroupService userGroupService = appContext.getBean(UserGroupService.class);
try {
JSONObject jsonResponse = new JSONObject();
try {
for (User myUser : userService.findallUser()) {
JSONObject u = new JSONObject();
u.put("login", myUser.getLogin());
u.put("name", myUser.getName());
u.put("team", myUser.getTeam());
u.put("defaultSystem", myUser.getDefaultSystem());
u.put("request", myUser.getRequest());
u.put("email", myUser.getEmail());
JSONArray groups = new JSONArray();
for (UserGroup group : userGroupService.findGroupByKey(myUser.getLogin())) {
groups.put(group.getGroup());
}
u.put("group", groups);
JSONArray systems = new JSONArray();
for (UserSystem sys : userSystemService.findUserSystemByUser(myUser.getLogin())) {
systems.put(sys.getSystem());
}
u.put("system", systems);
data.put(u);
}
} catch (CerberusException ex) {
response.setContentType("text/html");
response.getWriter().print(ex.getMessageError().getDescription());
}
jsonResponse.put("aaData", data);
jsonResponse.put("sEcho", echo);
jsonResponse.put("iTotalRecords", data.length());
jsonResponse.put("iTotalDisplayRecords", data.length());
response.setContentType("application/json");
response.getWriter().print(jsonResponse.toString());
} catch (JSONException e) {
LOG.warn(e);
response.setContentType("text/html");
response.getWriter().print(e.getMessage());
}
}
use of org.cerberus.crud.service.IUserService in project cerberus-source by cerberustesting.
the class ReadMyUser 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 {
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
IUserService userService = appContext.getBean(UserService.class);
IUserSystemService userSystemService = appContext.getBean(IUserSystemService.class);
IUserGroupService userGroupService = appContext.getBean(UserGroupService.class);
response.setContentType("application/json");
response.setCharacterEncoding("utf8");
JSONObject data = new JSONObject();
try {
String user = request.getUserPrincipal().getName();
User myUser = userService.findUserByKey(user);
data.put("login", myUser.getLogin());
data.put("name", myUser.getName());
data.put("team", myUser.getTeam());
data.put("defaultSystem", myUser.getDefaultSystem());
data.put("request", myUser.getRequest());
data.put("email", myUser.getEmail());
data.put("language", myUser.getLanguage());
data.put("robotHost", myUser.getRobotHost());
data.put("robotPort", myUser.getRobotPort());
data.put("robotPlatform", myUser.getRobotPort());
data.put("robotBrowser", myUser.getRobotPort());
data.put("robotVersion", myUser.getRobotPort());
data.put("robot", myUser.getRobot());
data.put("reportingFavorite", myUser.getReportingFavorite());
data.put("userPreferences", myUser.getUserPreferences());
JSONArray groups = new JSONArray();
for (UserGroup group : userGroupService.findGroupByKey(myUser.getLogin())) {
groups.put(group.getGroup());
}
data.put("group", groups);
JSONArray systems = new JSONArray();
for (UserSystem sys : userSystemService.findUserSystemByUser(myUser.getLogin())) {
systems.put(sys.getSystem());
}
data.put("system", systems);
HttpSession session = request.getSession();
session.setAttribute("MySystem", myUser.getDefaultSystem());
session.setAttribute("MyLang", myUser.getLanguage());
} catch (CerberusException ex) {
response.getWriter().print(ex.getMessageError().getDescription());
} catch (JSONException ex) {
LOG.warn(ex);
} catch (NullPointerException ex) {
response.sendRedirect("./Login.jsp");
}
response.getWriter().print(data.toString());
}
use of org.cerberus.crud.service.IUserService 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.IUserService 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.IUserService 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