use of org.cerberus.crud.factory.IFactoryUserSystem in project cerberus-source by cerberustesting.
the class UpdateUser method doPost.
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, IndexOutOfBoundsException {
// TODO create class Validator to validate all parameter from page
JSONObject jsonResponse = new JSONObject();
MessageEvent msg1 = new MessageEvent(MessageEventEnum.GENERIC_OK);
Answer ans = new Answer();
Answer finalAnswer = new Answer(msg1);
MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
ans.setResultMessage(msg);
String id = request.getParameter("id");
String login = request.getParameter("login");
String name = request.getParameter("name");
String email = request.getParameter("email");
String team = request.getParameter("team");
String systems = request.getParameter("systems");
String requests = request.getParameter("request");
String groups = request.getParameter("groups");
String defaultSystem = request.getParameter("defaultSystem");
if (StringUtil.isNullOrEmpty(login) || StringUtil.isNullOrEmpty(id)) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "User").replace("%OPERATION%", "Update").replace("%REASON%", "User login is missing."));
ans.setResultMessage(msg);
} else {
LOG.info("Updating user " + login);
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
IUserService userService = appContext.getBean(UserService.class);
IUserGroupService userGroupService = appContext.getBean(UserGroupService.class);
IFactoryUserSystem userSystemFactory = appContext.getBean(IFactoryUserSystem.class);
IUserSystemService userSystemService = appContext.getBean(IUserSystemService.class);
IFactoryUserGroup factoryGroup = new FactoryUserGroup();
User myUser;
List<UserGroup> newGroups = null;
List<UserSystem> newSystems = null;
try {
myUser = userService.findUserByKey(id);
List<String> listGroup = new ArrayList<String>();
JSONArray GroupArray = new JSONArray(request.getParameter("groups"));
for (int i = 0; i < GroupArray.length(); i++) {
listGroup.add(GroupArray.getString(i));
}
newGroups = new ArrayList<UserGroup>();
for (String group : listGroup) {
newGroups.add(factoryGroup.create(group));
}
myUser.setLogin(login);
myUser.setName(name);
myUser.setTeam(team);
newSystems = new ArrayList<UserSystem>();
JSONArray SystemArray = new JSONArray(request.getParameter("systems"));
List<String> listSystem = new ArrayList<String>();
for (int i = 0; i < SystemArray.length(); i++) {
listSystem.add(SystemArray.getString(i));
}
for (String system : listSystem) {
newSystems.add(userSystemFactory.create(login, system));
}
myUser.setDefaultSystem(defaultSystem);
myUser.setRequest(requests);
myUser.setEmail(email);
try {
ans = userService.update(myUser);
AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
/**
* Update was successful. Adding Log entry.
*/
ILogEventService logEventService = appContext.getBean(LogEventService.class);
logEventService.createForPrivateCalls("/UpdateUser", "UPDATE", "Updated user : " + login, request);
if (!newGroups.isEmpty()) {
userGroupService.updateUserGroups(myUser, newGroups);
/**
* Adding Log entry.
*/
logEventService = appContext.getBean(LogEventService.class);
logEventService.createForPrivateCalls("/UpdateUser", "UPDATE", "Updated user groups : " + login, request);
}
if (!newSystems.isEmpty()) {
request.getSession().setAttribute("MySystem", newSystems.get(0).getSystem());
userSystemService.updateUserSystems(myUser, newSystems);
/**
* Adding Log entry.
*/
logEventService = appContext.getBean(LogEventService.class);
logEventService.createForPrivateCalls("/UpdateUser", "UPDATE", "Updated user system : " + login, request);
}
}
/**
* Adding Log entry.
*/
finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
jsonResponse.put("messageType", finalAnswer.getResultMessage().getMessage().getCodeString());
jsonResponse.put("message", finalAnswer.getResultMessage().getDescription());
response.getWriter().print(jsonResponse);
} catch (CerberusException ex) {
response.getWriter().print(ex.getMessageError().getDescription());
}
} catch (CerberusException ex) {
response.getWriter().print(ex.getMessageError().getDescription());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
use of org.cerberus.crud.factory.IFactoryUserSystem in project cerberus-source by cerberustesting.
the class CreateUser 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 {
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
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();
IParameterService parameterService = appContext.getBean(ParameterService.class);
IEmailService emailService = appContext.getBean(IEmailService.class);
String system = "";
String password = parameterService.findParameterByKey("cerberus_accountcreation_defaultpassword", system).getValue();
String newPassword = ParameterParserUtil.parseStringParam(request.getParameter("newPassword"), "Y");
String login = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("login"), "", charset);
String email = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("email"), "", charset);
String defaultSystem = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("defaultSystem"), "", charset);
String name = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("name"), "", charset);
String team = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("team"), "", charset);
JSONArray JSONSystems = new JSONArray(ParameterParserUtil.parseStringParam(request.getParameter("systems"), null));
JSONArray JSONGroups = new JSONArray(ParameterParserUtil.parseStringParam(request.getParameter("groups"), null));
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%", "Create").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%", "Create").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.
*/
IUserService userService = appContext.getBean(IUserService.class);
IFactoryUser factoryUser = appContext.getBean(IFactoryUser.class);
IFactoryUserGroup factoryGroup = new FactoryUserGroup();
IFactoryUserSystem userSystemFactory = appContext.getBean(IFactoryUserSystem.class);
IUserGroupService userGroupService = appContext.getBean(UserGroupService.class);
IUserSystemService userSystemService = appContext.getBean(IUserSystemService.class);
LinkedList<UserGroup> newGroups = new LinkedList<>();
for (int i = 0; i < JSONGroups.length(); i++) {
newGroups.add(factoryGroup.create(login, JSONGroups.getString(i)));
}
LinkedList<UserSystem> newSystems = new LinkedList<>();
for (int i = 0; i < JSONSystems.length(); i++) {
newSystems.add(userSystemFactory.create(login, JSONSystems.getString(i)));
}
User userData = factoryUser.create(0, login, password, "", newPassword, name, team, "en", "", "", "", "", "", "", "", defaultSystem, email, null, null);
ans = userService.create(userData);
if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
/**
* Send Email to explain how to connect Cerberus if
* activateNotification is set to Y
*/
String sendNotification = parameterService.findParameterByKey("cerberus_notification_accountcreation_activatenotification", system).getValue();
if (sendNotification.equalsIgnoreCase("Y")) {
Answer msgSent = new Answer(emailService.generateAndSendAccountCreationEmail(userData));
ans = AnswerUtil.agregateAnswer(ans, msgSent);
}
/**
* Object updated. Adding Log entry.
*/
ILogEventService logEventService = appContext.getBean(LogEventService.class);
logEventService.createForPrivateCalls("/CreateUser", "CREATE", "Create User : ['" + login + "']", request);
ans = AnswerUtil.agregateAnswer(ans, userGroupService.updateGroupsByUser(userData, newGroups));
ans = AnswerUtil.agregateAnswer(ans, userSystemService.updateSystemsByUser(userData, newSystems));
}
}
/**
* 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();
}
Aggregations