示例#1
0
  private void handleEditUserNameEmail(
      HttpServletRequest request, HttpServletResponse response, HttpSession session)
      throws UnsupportedEncodingException, IOException {
    String username = (String) session.getAttribute(Attribute.USERNAME.toString());
    String newName = request.getParameter("new-name");
    String newEmail = request.getParameter("new-email");
    if (Security.isSafeName(newName) && Security.isSafeEmail(newEmail)) {
      User updateUser = new User(username, "", newName, newEmail, "", "");
      Boolean editSuccessfully = DatabaseApi.editAccount(updateUser);
      session.setAttribute(Attribute.EDIT_NAME_SUCCESSFULLY.toString(), editSuccessfully);
      session.setAttribute(Attribute.EMAIL.toString(), newEmail);
      session.setAttribute(Attribute.NAME.toString(), newName);

    } else {
      session.setAttribute(Attribute.EDIT_NAME_SUCCESSFULLY.toString(), false);
    }
    response.sendRedirect("account.jsp");
  }
示例#2
0
  private void handleCreateAccount(
      HttpServletRequest request, HttpServletResponse response, HttpSession session, String json)
      throws IOException {
    CreateAccountRequest createAccountRequest =
        new Gson().fromJson(json, CreateAccountRequest.class);

    response.setContentType("text/plain");
    PrintWriter out = response.getWriter();

    String username = createAccountRequest.arguments.username;
    String email = createAccountRequest.arguments.email;
    String password = createAccountRequest.arguments.password;
    String confirmPassword = createAccountRequest.arguments.confirmPassword;
    String name = "Enter your name";

    if (Security.isSafeUsername(username)
        && Security.isSafeEmail(email)
        && Security.isSafePassword(password)
        && Security.isSafePassword(confirmPassword)
        && password.equals(confirmPassword)
        && Security.isSafeName(name)) { // Short-circuitry
      User newUser = new User(username, password, name, email);
      boolean addedSuccessfully = DatabaseApi.AddAccount(newUser);
      if (addedSuccessfully) {
        session.setAttribute(Attribute.IS_AUTHORIZED.toString(), true);
        session.setAttribute(Attribute.USERNAME.toString(), username);
        session.setAttribute(Attribute.EMAIL.toString(), email);
        session.setAttribute(Attribute.PASSWORD.toString(), password);
        session.setAttribute(Attribute.IS_FIRST_SIGN_IN.toString(), true);
        out.println(username + " created successfully.");
      } else {
        // TODO Add error message here
      }
    } else {
      response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    }
    out.flush();
    out.close();
  }