Search in sources :

Example 31 with User

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

the class ReadUserPublic method findUserList.

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");
    AnswerList resp = userService.readByCriteria(startPosition, length, columnName, sort, searchParameter, "");
    JSONArray jsonArray = new JSONArray();
    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()) {
            jsonArray.put(convertUserToJSONObject(user));
        }
    }
    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) JSONObject(org.json.JSONObject) IUserService(org.cerberus.crud.service.IUserService) UserService(org.cerberus.crud.service.impl.UserService) JSONArray(org.json.JSONArray) AnswerList(org.cerberus.util.answer.AnswerList) List(java.util.List) AnswerItem(org.cerberus.util.answer.AnswerItem)

Example 32 with User

use of org.cerberus.crud.entity.User 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());
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) CerberusException(org.cerberus.exception.CerberusException) User(org.cerberus.crud.entity.User) PolicyFactory(org.owasp.html.PolicyFactory) JSONObject(org.json.JSONObject) IUserService(org.cerberus.crud.service.IUserService) ILogEventService(org.cerberus.crud.service.ILogEventService) JSONException(org.json.JSONException)

Example 33 with User

use of org.cerberus.crud.entity.User 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)

Example 34 with User

use of org.cerberus.crud.entity.User 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());
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) CerberusException(org.cerberus.exception.CerberusException) User(org.cerberus.crud.entity.User) IUserService(org.cerberus.crud.service.IUserService) ILogEventService(org.cerberus.crud.service.ILogEventService)

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