Search in sources :

Example 11 with IUserService

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());
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) CerberusException(org.cerberus.exception.CerberusException) User(org.cerberus.crud.entity.User) JSONObject(org.json.JSONObject) UserSystem(org.cerberus.crud.entity.UserSystem) IUserService(org.cerberus.crud.service.IUserService) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IUserGroupService(org.cerberus.crud.service.IUserGroupService) IUserSystemService(org.cerberus.crud.service.IUserSystemService) UserGroup(org.cerberus.crud.entity.UserGroup)

Example 12 with IUserService

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());
}
Also used : CerberusException(org.cerberus.exception.CerberusException) User(org.cerberus.crud.entity.User) UserSystem(org.cerberus.crud.entity.UserSystem) HttpSession(javax.servlet.http.HttpSession) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IUserGroupService(org.cerberus.crud.service.IUserGroupService) UserGroup(org.cerberus.crud.entity.UserGroup) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) IUserService(org.cerberus.crud.service.IUserService) IUserSystemService(org.cerberus.crud.service.IUserSystemService)

Example 13 with IUserService

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());
}
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 14 with IUserService

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();
}
Also used : Answer(org.cerberus.util.answer.Answer) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) PolicyFactory(org.owasp.html.PolicyFactory) MessageEvent(org.cerberus.engine.entity.MessageEvent) IUserService(org.cerberus.crud.service.IUserService) LogEventService(org.cerberus.crud.service.impl.LogEventService) ILogEventService(org.cerberus.crud.service.ILogEventService) ILogEventService(org.cerberus.crud.service.ILogEventService) AnswerItem(org.cerberus.util.answer.AnswerItem)

Example 15 with IUserService

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());
    }
}
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

IUserService (org.cerberus.crud.service.IUserService)15 ApplicationContext (org.springframework.context.ApplicationContext)15 User (org.cerberus.crud.entity.User)13 CerberusException (org.cerberus.exception.CerberusException)11 ILogEventService (org.cerberus.crud.service.ILogEventService)10 JSONObject (org.json.JSONObject)10 JSONException (org.json.JSONException)9 AnswerItem (org.cerberus.util.answer.AnswerItem)6 UserGroup (org.cerberus.crud.entity.UserGroup)4 IUserGroupService (org.cerberus.crud.service.IUserGroupService)4 MessageEvent (org.cerberus.engine.entity.MessageEvent)4 PrintWriter (java.io.PrintWriter)3 UserSystem (org.cerberus.crud.entity.UserSystem)3 Answer (org.cerberus.util.answer.Answer)3 ArrayList (java.util.ArrayList)2 IUserSystemService (org.cerberus.crud.service.IUserSystemService)2 JSONArray (org.json.JSONArray)2 PolicyFactory (org.owasp.html.PolicyFactory)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1