Search in sources :

Example 1 with User

use of org.cerberus.crud.entity.User 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();
        }
    }
}
Also used : User(org.cerberus.crud.entity.User) UserSystem(org.cerberus.crud.entity.UserSystem) IFactoryUserSystem(org.cerberus.crud.factory.IFactoryUserSystem) MessageEvent(org.cerberus.engine.entity.MessageEvent) ArrayList(java.util.ArrayList) ILogEventService(org.cerberus.crud.service.ILogEventService) LogEventService(org.cerberus.crud.service.impl.LogEventService) IUserGroupService(org.cerberus.crud.service.IUserGroupService) FactoryUserGroup(org.cerberus.crud.factory.impl.FactoryUserGroup) IFactoryUserGroup(org.cerberus.crud.factory.IFactoryUserGroup) UserGroup(org.cerberus.crud.entity.UserGroup) ApplicationContext(org.springframework.context.ApplicationContext) IFactoryUserSystem(org.cerberus.crud.factory.IFactoryUserSystem) ILogEventService(org.cerberus.crud.service.ILogEventService) IUserSystemService(org.cerberus.crud.service.IUserSystemService) CerberusException(org.cerberus.exception.CerberusException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) FactoryUserGroup(org.cerberus.crud.factory.impl.FactoryUserGroup) IFactoryUserGroup(org.cerberus.crud.factory.IFactoryUserGroup) Answer(org.cerberus.util.answer.Answer) JSONObject(org.json.JSONObject) IUserService(org.cerberus.crud.service.IUserService) IFactoryUserGroup(org.cerberus.crud.factory.IFactoryUserGroup)

Example 2 with User

use of org.cerberus.crud.entity.User in project cerberus-source by cerberustesting.

the class ChangeUserPassword method doPost.

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String login = request.getParameter("login");
    String currentPassword = request.getParameter("currentPassword");
    String newPassword = request.getParameter("newPassword");
    String confirmPassword = request.getParameter("confirmPassword");
    String resetPasswordToken = request.getParameter("resetPasswordToken");
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    IUserService userService = appContext.getBean(UserService.class);
    User myUser;
    try {
        JSONObject jsonResponse = new JSONObject();
        try {
            myUser = userService.findUserByKey(login);
            AnswerItem ansPassword = userService.updateUserPassword(myUser, currentPassword, newPassword, confirmPassword, resetPasswordToken);
            jsonResponse.put("messageType", ansPassword.getResultMessage().getMessage().getCodeString());
            jsonResponse.put("message", ansPassword.getResultMessage().getDescription());
        } catch (CerberusException ex1) {
            // TODO:FN this need to be refactored //findUserByKey should return answer
            jsonResponse.put("messageType", "KO");
            jsonResponse.put("message", ex1.toString());
        }
        response.setContentType("application/json");
        response.getWriter().print(jsonResponse.toString());
    } 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());
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) CerberusException(org.cerberus.exception.CerberusException) User(org.cerberus.crud.entity.User) JSONObject(org.json.JSONObject) IUserService(org.cerberus.crud.service.IUserService) JSONException(org.json.JSONException) AnswerItem(org.cerberus.util.answer.AnswerItem)

Example 3 with User

use of org.cerberus.crud.entity.User in project cerberus-source by cerberustesting.

the class ChangeUserPasswordAdmin method doPost.

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String login = request.getParameter("login");
    String newPassword = request.getParameter("newPassword");
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    IUserService userService = appContext.getBean(UserService.class);
    User myUser;
    try {
        JSONObject jsonResponse = new JSONObject();
        try {
            myUser = userService.findUserByKey(login);
            AnswerItem ansPassword = userService.updateUserPasswordAdmin(myUser, newPassword);
            jsonResponse.put("messageType", ansPassword.getResultMessage().getMessage().getCodeString());
            jsonResponse.put("message", ansPassword.getResultMessage().getDescription());
        } catch (CerberusException ex1) {
            // TODO:FN this need to be refactored //findUserByKey should return answer
            jsonResponse.put("messageType", "KO");
            jsonResponse.put("message", ex1.toString());
        }
        response.setContentType("application/json");
        response.getWriter().print(jsonResponse.toString());
    } 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());
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) CerberusException(org.cerberus.exception.CerberusException) User(org.cerberus.crud.entity.User) JSONObject(org.json.JSONObject) IUserService(org.cerberus.crud.service.IUserService) JSONException(org.json.JSONException) AnswerItem(org.cerberus.util.answer.AnswerItem)

Example 4 with User

use of org.cerberus.crud.entity.User in project cerberus-source by cerberustesting.

the class ForgotPassword 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 {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        IUserService userService = appContext.getBean(UserService.class);
        IEmailService emailService = appContext.getBean(IEmailService.class);
        IParameterService parameterService = appContext.getBean(ParameterService.class);
        String system = "";
        JSONObject jsonResponse = new JSONObject();
        String login = ParameterParserUtil.parseStringParam(request.getParameter("login"), "");
        /**
         * Check if notification parameter is set to Y. If not, return an
         * error
         */
        String sendNotification = parameterService.findParameterByKey("cerberus_notification_accountcreation_activatenotification", system).getValue();
        if (!sendNotification.equalsIgnoreCase("Y")) {
            jsonResponse.put("messageType", "Error");
            jsonResponse.put("message", "This functionality is not activated. Please contact your Cerberus Administrator.");
            response.getWriter().print(jsonResponse);
            response.getWriter().flush();
            return;
        }
        /**
         * If email not found in database, send error message
         */
        AnswerItem ai = userService.readByKey(login);
        User user = (User) ai.getItem();
        if (user == null) {
            jsonResponse.put("messageType", "Error");
            jsonResponse.put("message", "Login submitted is unknown !");
            response.getWriter().print(jsonResponse);
            response.getWriter().flush();
            return;
        }
        /**
         * Update user setting a new value in requestresetpassword
         */
        userService.requestResetPassword(user);
        /**
         * Send an email with the hash as a parameter
         */
        Answer mailSent = new Answer(emailService.generateAndSendForgotPasswordEmail(user));
        if (!mailSent.isCodeStringEquals("OK")) {
            jsonResponse.put("messageType", "Error");
            jsonResponse.put("message", "An error occured sending the notification. Detail : " + mailSent.getMessageDescription());
            response.getWriter().print(jsonResponse);
            response.getWriter().flush();
            return;
        }
        /**
         * Adding Log entry.
         */
        ILogEventService logEventService = appContext.getBean(ILogEventService.class);
        logEventService.createForPrivateCalls("/ForgotPassword", "CREATE", "User : " + login + " asked for password recovery", request);
        /**
         * Build Response Message
         */
        jsonResponse.put("messageType", "OK");
        jsonResponse.put("message", "An e-mail has been sent to the mailbox " + user.getEmail() + ".");
        response.getWriter().print(jsonResponse);
        response.getWriter().flush();
    } catch (CerberusException myexception) {
        response.getWriter().print(myexception.getMessageError().getDescription());
    } catch (JSONException ex) {
        LOG.warn(ex);
        response.setContentType("application/json");
        response.getWriter().print(AnswerUtil.createGenericErrorAnswer());
    }
}
Also used : CerberusException(org.cerberus.exception.CerberusException) User(org.cerberus.crud.entity.User) JSONException(org.json.JSONException) IParameterService(org.cerberus.crud.service.IParameterService) AnswerItem(org.cerberus.util.answer.AnswerItem) Answer(org.cerberus.util.answer.Answer) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) IUserService(org.cerberus.crud.service.IUserService) ILogEventService(org.cerberus.crud.service.ILogEventService) IEmailService(org.cerberus.service.email.IEmailService) PrintWriter(java.io.PrintWriter)

Example 5 with User

use of org.cerberus.crud.entity.User in project cerberus-source by cerberustesting.

the class ReadUser method findUserList.

// </editor-fold>
private AnswerItem findUserList(ApplicationContext appContext, HttpServletRequest request, HttpServletResponse response) throws JSONException {
    AnswerItem item = new AnswerItem();
    JSONObject jsonResponse = new JSONObject();
    userService = appContext.getBean(UserService.class);
    int startPosition = Integer.valueOf(ParameterParserUtil.parseStringParam(request.getParameter("iDisplayStart"), "0"));
    int length = Integer.valueOf(ParameterParserUtil.parseStringParam(request.getParameter("iDisplayLength"), "0"));
    /*int sEcho  = Integer.valueOf(request.getParameter("sEcho"));*/
    String searchParameter = ParameterParserUtil.parseStringParam(request.getParameter("sSearch"), "");
    int columnToSortParameter = Integer.parseInt(ParameterParserUtil.parseStringParam(request.getParameter("iSortCol_0"), "1"));
    String sColumns = ParameterParserUtil.parseStringParam(request.getParameter("sColumns"), "userID,login,name");
    String[] columnToSort = sColumns.split(",");
    String columnName = columnToSort[columnToSortParameter];
    String sort = ParameterParserUtil.parseStringParam(request.getParameter("sSortDir_0"), "asc");
    List<String> individualLike = new ArrayList(Arrays.asList(ParameterParserUtil.parseStringParam(request.getParameter("sLike"), "").split(",")));
    Map<String, List<String>> individualSearch = new HashMap<>();
    for (int a = 0; a < columnToSort.length; a++) {
        if (null != request.getParameter("sSearch_" + a) && !request.getParameter("sSearch_" + a).isEmpty()) {
            List<String> search = new ArrayList(Arrays.asList(request.getParameter("sSearch_" + a).split(",")));
            if (individualLike.contains(columnToSort[a])) {
                individualSearch.put(columnToSort[a] + ":like", search);
            } else {
                individualSearch.put(columnToSort[a], search);
            }
        }
    }
    AnswerList resp = userService.readByCriteria(startPosition, length, columnName, sort, searchParameter, individualSearch);
    JSONArray jsonArray = new JSONArray();
    boolean userHasPermissions = request.isUserInRole("IntegratorRO");
    if (resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
        // the service was able to perform the query, then we should get all values
        for (User user : (List<User>) resp.getDataList()) {
            JSONObject res = convertUserToJSONObject(user);
            if (request.getParameter("systems") != null) {
                IUserSystemService userSystemService = appContext.getBean(IUserSystemService.class);
                AnswerList a = userSystemService.readByUser(user.getLogin());
                if (a.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && a.getDataList() != null) {
                    JSONArray JSONsystems = new JSONArray();
                    List<UserSystem> systems = a.getDataList();
                    for (UserSystem u : systems) {
                        JSONsystems.put(convertUserSystemToJSONObject(u));
                    }
                    res.put("systems", JSONsystems);
                }
            }
            if (request.getParameter("groups") != null) {
                IUserGroupService userGroupService = appContext.getBean(UserGroupService.class);
                AnswerList a = userGroupService.readByUser(user.getLogin());
                if (a.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && a.getDataList() != null) {
                    JSONArray JSONgroups = new JSONArray();
                    List<UserGroup> groups = a.getDataList();
                    for (UserGroup u : groups) {
                        JSONgroups.put(convertUserGroupToJSONObject(u));
                    }
                    res.put("groups", JSONgroups);
                }
            }
            jsonArray.put(res);
        }
    }
    jsonResponse.put("hasPermissions", userHasPermissions);
    jsonResponse.put("contentTable", jsonArray);
    jsonResponse.put("iTotalRecords", resp.getTotalRows());
    jsonResponse.put("iTotalDisplayRecords", resp.getTotalRows());
    item.setItem(jsonResponse);
    item.setResultMessage(resp.getResultMessage());
    return item;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) User(org.cerberus.crud.entity.User) UserSystem(org.cerberus.crud.entity.UserSystem) IUserService(org.cerberus.crud.service.IUserService) UserService(org.cerberus.crud.service.impl.UserService) JSONArray(org.json.JSONArray) AnswerItem(org.cerberus.util.answer.AnswerItem) IUserGroupService(org.cerberus.crud.service.IUserGroupService) UserGroup(org.cerberus.crud.entity.UserGroup) JSONObject(org.json.JSONObject) AnswerList(org.cerberus.util.answer.AnswerList) IUserSystemService(org.cerberus.crud.service.IUserSystemService)

Aggregations

User (org.cerberus.crud.entity.User)34 IUserService (org.cerberus.crud.service.IUserService)16 ApplicationContext (org.springframework.context.ApplicationContext)14 JSONObject (org.json.JSONObject)13 IFactoryUser (org.cerberus.crud.factory.IFactoryUser)11 CerberusException (org.cerberus.exception.CerberusException)11 AnswerItem (org.cerberus.util.answer.AnswerItem)9 JSONException (org.json.JSONException)9 Connection (java.sql.Connection)8 PreparedStatement (java.sql.PreparedStatement)8 SQLException (java.sql.SQLException)8 FactoryUser (org.cerberus.crud.factory.impl.FactoryUser)8 ILogEventService (org.cerberus.crud.service.ILogEventService)8 ResultSet (java.sql.ResultSet)7 MessageEvent (org.cerberus.engine.entity.MessageEvent)7 JSONArray (org.json.JSONArray)7 UserGroup (org.cerberus.crud.entity.UserGroup)6 IUserGroupService (org.cerberus.crud.service.IUserGroupService)6 AnswerList (org.cerberus.util.answer.AnswerList)6 Test (org.junit.Test)6