Search in sources :

Example 1 with IFactoryUserGroup

use of org.cerberus.crud.factory.IFactoryUserGroup 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 IFactoryUserGroup

use of org.cerberus.crud.factory.IFactoryUserGroup 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();
}
Also used : User(org.cerberus.crud.entity.User) IFactoryUser(org.cerberus.crud.factory.IFactoryUser) PolicyFactory(org.owasp.html.PolicyFactory) MessageEvent(org.cerberus.engine.entity.MessageEvent) JSONArray(org.json.JSONArray) LogEventService(org.cerberus.crud.service.impl.LogEventService) LinkedList(java.util.LinkedList) FactoryUserGroup(org.cerberus.crud.factory.impl.FactoryUserGroup) IFactoryUserGroup(org.cerberus.crud.factory.IFactoryUserGroup) Answer(org.cerberus.util.answer.Answer) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) UserGroupService(org.cerberus.crud.service.impl.UserGroupService) IFactoryUserSystem(org.cerberus.crud.factory.IFactoryUserSystem) IFactoryUserGroup(org.cerberus.crud.factory.IFactoryUserGroup) IEmailService(org.cerberus.service.email.IEmailService) IFactoryUser(org.cerberus.crud.factory.IFactoryUser)

Aggregations

User (org.cerberus.crud.entity.User)2 IFactoryUserGroup (org.cerberus.crud.factory.IFactoryUserGroup)2 IFactoryUserSystem (org.cerberus.crud.factory.IFactoryUserSystem)2 FactoryUserGroup (org.cerberus.crud.factory.impl.FactoryUserGroup)2 LogEventService (org.cerberus.crud.service.impl.LogEventService)2 MessageEvent (org.cerberus.engine.entity.MessageEvent)2 Answer (org.cerberus.util.answer.Answer)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 ApplicationContext (org.springframework.context.ApplicationContext)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 UserGroup (org.cerberus.crud.entity.UserGroup)1 UserSystem (org.cerberus.crud.entity.UserSystem)1 IFactoryUser (org.cerberus.crud.factory.IFactoryUser)1 ILogEventService (org.cerberus.crud.service.ILogEventService)1 IUserGroupService (org.cerberus.crud.service.IUserGroupService)1 IUserService (org.cerberus.crud.service.IUserService)1 IUserSystemService (org.cerberus.crud.service.IUserSystemService)1 UserGroupService (org.cerberus.crud.service.impl.UserGroupService)1