diff --git a/src/main/java/io/hardingadonis/miu/controller/web/AboutServlet.java b/src/main/java/io/hardingadonis/miu/controller/web/AboutServlet.java index 3b91796e..8b3188ea 100644 --- a/src/main/java/io/hardingadonis/miu/controller/web/AboutServlet.java +++ b/src/main/java/io/hardingadonis/miu/controller/web/AboutServlet.java @@ -1,5 +1,7 @@ package io.hardingadonis.miu.controller.web; +import io.hardingadonis.miu.model.*; +import io.hardingadonis.miu.model.detail.*; import java.io.*; import javax.servlet.*; import javax.servlet.annotation.*; @@ -13,6 +15,15 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); + + HttpSession session = request.getSession(); + + User user = (User)session.getAttribute("user"); + + if ((user != null) && (user.getStatus() == UserStatus.DEACTIVATE)) { + response.sendRedirect("verify"); + return; + } request.getRequestDispatcher("/view/web/about.jsp").forward(request, response); } diff --git a/src/main/java/io/hardingadonis/miu/controller/web/AddAdressServlet.java b/src/main/java/io/hardingadonis/miu/controller/web/AddAdressServlet.java index f2eeeab8..7ebef833 100644 --- a/src/main/java/io/hardingadonis/miu/controller/web/AddAdressServlet.java +++ b/src/main/java/io/hardingadonis/miu/controller/web/AddAdressServlet.java @@ -1,6 +1,7 @@ package io.hardingadonis.miu.controller.web; import io.hardingadonis.miu.model.*; +import io.hardingadonis.miu.model.detail.*; import io.hardingadonis.miu.services.*; import java.io.*; import javax.servlet.*; @@ -22,7 +23,12 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) response.sendRedirect("login"); return; } - + + if (user.getStatus() == UserStatus.DEACTIVATE) { + response.sendRedirect("verify"); + return; + } + if (user.getAddress().size() >= 5) { response.sendRedirect("delivery-address"); return; @@ -47,10 +53,10 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) String phone = request.getParameter("phone"); String newAddress = phone + ", " + receiver + ", " + specific + ", " + ward + ", " + district + ", " + city; - + user.getAddress().add(newAddress); Singleton.userDAO.update(user); - + response.sendRedirect("delivery-address"); } } diff --git a/src/main/java/io/hardingadonis/miu/controller/web/CartServlet.java b/src/main/java/io/hardingadonis/miu/controller/web/CartServlet.java index ecd19bb3..c6b5e2dc 100644 --- a/src/main/java/io/hardingadonis/miu/controller/web/CartServlet.java +++ b/src/main/java/io/hardingadonis/miu/controller/web/CartServlet.java @@ -1,5 +1,7 @@ package io.hardingadonis.miu.controller.web; +import io.hardingadonis.miu.model.*; +import io.hardingadonis.miu.model.detail.*; import io.hardingadonis.miu.services.*; import java.io.*; import javax.servlet.*; @@ -15,6 +17,15 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); + HttpSession session = request.getSession(); + + User user = (User) session.getAttribute("user"); + + if ((user != null) && (user.getStatus() == UserStatus.DEACTIVATE)) { + response.sendRedirect("verify"); + return; + } + String cartDataStr = getCartCookie(request); request.setAttribute("cart_data_str", cartDataStr); diff --git a/src/main/java/io/hardingadonis/miu/controller/web/ChangePasswordServlet.java b/src/main/java/io/hardingadonis/miu/controller/web/ChangePasswordServlet.java index 306e6e1c..4e778b33 100644 --- a/src/main/java/io/hardingadonis/miu/controller/web/ChangePasswordServlet.java +++ b/src/main/java/io/hardingadonis/miu/controller/web/ChangePasswordServlet.java @@ -1,6 +1,7 @@ package io.hardingadonis.miu.controller.web; import io.hardingadonis.miu.model.*; +import io.hardingadonis.miu.model.detail.*; import io.hardingadonis.miu.services.*; import java.io.*; import javax.servlet.*; @@ -9,7 +10,7 @@ @WebServlet(name = "ChangePasswordServlet", urlPatterns = {"/change-password"}) public class ChangePasswordServlet extends HttpServlet { - + private static final String CHANGE_PASSWORD_SUCCESS_PARAM = "changePasswordSuccess"; @Override @@ -25,6 +26,11 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) return; } + if (user.getStatus() == UserStatus.DEACTIVATE) { + response.sendRedirect("verify"); + return; + } + request.getRequestDispatcher("/view/web/change-password.jsp").forward(request, response); } @@ -46,10 +52,10 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) } else { user.setHashedPassword(hashedNewPassword); Singleton.userDAO.update(user); - + request.getSession(false).invalidate(); response.sendRedirect("login?" + CHANGE_PASSWORD_SUCCESS_PARAM + "=true"); - + return; } diff --git a/src/main/java/io/hardingadonis/miu/controller/web/CheckoutServlet.java b/src/main/java/io/hardingadonis/miu/controller/web/CheckoutServlet.java index ee77445e..694efdf2 100644 --- a/src/main/java/io/hardingadonis/miu/controller/web/CheckoutServlet.java +++ b/src/main/java/io/hardingadonis/miu/controller/web/CheckoutServlet.java @@ -31,6 +31,11 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) return; } + if (user.getStatus() == UserStatus.DEACTIVATE) { + response.sendRedirect("verify"); + return; + } + long totalPrice = getTotalPrice(getCartCookie(request)); request.setAttribute("total_price", totalPrice); diff --git a/src/main/java/io/hardingadonis/miu/controller/web/CheckoutStatusServlet.java b/src/main/java/io/hardingadonis/miu/controller/web/CheckoutStatusServlet.java index 9c4dc5ad..7b8e4a5e 100644 --- a/src/main/java/io/hardingadonis/miu/controller/web/CheckoutStatusServlet.java +++ b/src/main/java/io/hardingadonis/miu/controller/web/CheckoutStatusServlet.java @@ -1,5 +1,7 @@ package io.hardingadonis.miu.controller.web; +import io.hardingadonis.miu.model.User; +import io.hardingadonis.miu.model.detail.*; import java.io.*; import javax.servlet.*; import javax.servlet.annotation.*; @@ -16,8 +18,16 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) HttpSession session = request.getSession(); - if ((session.getAttribute("user") == null) || (session.getAttribute("order") == null) || (session.getAttribute("cart_cookie") == null)) { + User user = (User) session.getAttribute("user"); + + if ((user != null) && (user.getStatus() == UserStatus.DEACTIVATE)) { + response.sendRedirect("verify"); + return; + } + + if ((user == null) || (session.getAttribute("order") == null) || (session.getAttribute("cart_cookie") == null)) { response.sendRedirect("home"); + return; } request.getRequestDispatcher("/view/web/checkout-status.jsp").forward(request, response); diff --git a/src/main/java/io/hardingadonis/miu/controller/web/DeliveryAddressServlet.java b/src/main/java/io/hardingadonis/miu/controller/web/DeliveryAddressServlet.java index 88a48fb6..a5b1ece2 100644 --- a/src/main/java/io/hardingadonis/miu/controller/web/DeliveryAddressServlet.java +++ b/src/main/java/io/hardingadonis/miu/controller/web/DeliveryAddressServlet.java @@ -1,6 +1,7 @@ package io.hardingadonis.miu.controller.web; import io.hardingadonis.miu.model.*; +import io.hardingadonis.miu.model.detail.*; import io.hardingadonis.miu.services.*; import java.io.*; import javax.servlet.*; @@ -22,6 +23,11 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) response.sendRedirect("login"); return; } + + if (user.getStatus() == UserStatus.DEACTIVATE) { + response.sendRedirect("verify"); + return; + } request.getRequestDispatcher("/view/web/delivery-address.jsp").forward(request, response); } diff --git a/src/main/java/io/hardingadonis/miu/controller/web/HomeServlet.java b/src/main/java/io/hardingadonis/miu/controller/web/HomeServlet.java index e46cae58..eec50ce2 100644 --- a/src/main/java/io/hardingadonis/miu/controller/web/HomeServlet.java +++ b/src/main/java/io/hardingadonis/miu/controller/web/HomeServlet.java @@ -1,5 +1,7 @@ package io.hardingadonis.miu.controller.web; +import io.hardingadonis.miu.model.*; +import io.hardingadonis.miu.model.detail.*; import java.io.*; import javax.servlet.*; import javax.servlet.annotation.*; @@ -13,6 +15,15 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); + + HttpSession session = request.getSession(); + + User user = (User)session.getAttribute("user"); + + if ((user != null) && (user.getStatus() == UserStatus.DEACTIVATE)) { + response.sendRedirect("verify"); + return; + } request.getRequestDispatcher("/view/web/home.jsp").forward(request, response); } diff --git a/src/main/java/io/hardingadonis/miu/controller/web/LoginServlet.java b/src/main/java/io/hardingadonis/miu/controller/web/LoginServlet.java index 9b01703e..12cd5b25 100644 --- a/src/main/java/io/hardingadonis/miu/controller/web/LoginServlet.java +++ b/src/main/java/io/hardingadonis/miu/controller/web/LoginServlet.java @@ -25,24 +25,27 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); - + String email = request.getParameter("email"); String password = request.getParameter("password"); + + HttpSession session = request.getSession(); User user = Singleton.userDAO.get(email); - if (user != null && user.getHashedPassword().equals(Hash.SHA256(password)) && user.getStatus() == UserStatus.ACTIVATE) { - HttpSession session = request.getSession(); + if (user != null && user.getHashedPassword().equals(Hash.SHA256(password))) { session.setAttribute("user", user); - response.sendRedirect("home"); - return; + if (user.getStatus() == UserStatus.ACTIVATE) { + response.sendRedirect("home"); + return; + } else { + response.sendRedirect("verify"); + return; + } } String errorMsg = "Sai mật khẩu!"; - if (user != null && user.getStatus() == UserStatus.DEACTIVATE) { - errorMsg = "Tài khoản đã bị khóa!"; - } if (user == null) { errorMsg = "Tài khoản không tồn tại!"; email = null; @@ -50,7 +53,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) request.setAttribute("email", email); request.setAttribute("errorMsg", errorMsg); - + this.doGet(request, response); } } diff --git a/src/main/java/io/hardingadonis/miu/controller/web/ProductDetailServlet.java b/src/main/java/io/hardingadonis/miu/controller/web/ProductDetailServlet.java index f1f8fba7..fe1e1a95 100644 --- a/src/main/java/io/hardingadonis/miu/controller/web/ProductDetailServlet.java +++ b/src/main/java/io/hardingadonis/miu/controller/web/ProductDetailServlet.java @@ -1,6 +1,7 @@ package io.hardingadonis.miu.controller.web; import io.hardingadonis.miu.model.*; +import io.hardingadonis.miu.model.detail.*; import io.hardingadonis.miu.services.*; import java.io.*; import javax.servlet.*; @@ -15,6 +16,15 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); + + HttpSession session = request.getSession(); + + User user = (User)session.getAttribute("user"); + + if ((user != null) && (user.getStatus() == UserStatus.DEACTIVATE)) { + response.sendRedirect("verify"); + return; + } String IDStr = request.getParameter("id"); diff --git a/src/main/java/io/hardingadonis/miu/controller/web/ProfileServlet.java b/src/main/java/io/hardingadonis/miu/controller/web/ProfileServlet.java index ac71b63c..d03acde1 100644 --- a/src/main/java/io/hardingadonis/miu/controller/web/ProfileServlet.java +++ b/src/main/java/io/hardingadonis/miu/controller/web/ProfileServlet.java @@ -35,6 +35,11 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) return; } + if (user.getStatus() == UserStatus.DEACTIVATE) { + response.sendRedirect("verify"); + return; + } + request.setAttribute("gender", user.getGender().toString()); request.getRequestDispatcher("/view/web/profile.jsp").forward(request, response); diff --git a/src/main/java/io/hardingadonis/miu/controller/web/PurchaseHistoryDetailServlet.java b/src/main/java/io/hardingadonis/miu/controller/web/PurchaseHistoryDetailServlet.java index 4add72ba..80176805 100644 --- a/src/main/java/io/hardingadonis/miu/controller/web/PurchaseHistoryDetailServlet.java +++ b/src/main/java/io/hardingadonis/miu/controller/web/PurchaseHistoryDetailServlet.java @@ -1,6 +1,7 @@ package io.hardingadonis.miu.controller.web; import io.hardingadonis.miu.model.*; +import io.hardingadonis.miu.model.detail.*; import io.hardingadonis.miu.services.*; import java.io.*; import java.util.*; @@ -18,15 +19,22 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) response.setContentType("text/html; charset=UTF-8"); HttpSession session = request.getSession(); + + User user = (User) session.getAttribute("user"); + int orderID = Integer.parseInt(request.getParameter("id")); - User user = (User) session.getAttribute("user"); Order order = Singleton.orderDAO.get(orderID); if (user == null) { response.sendRedirect("login"); return; } + + if (user.getStatus() == UserStatus.DEACTIVATE) { + response.sendRedirect("verify"); + return; + } if (order.getUserID() != user.getID()) { response.sendRedirect("purchase-history?tab=all"); diff --git a/src/main/java/io/hardingadonis/miu/controller/web/PurchaseHistoryServlet.java b/src/main/java/io/hardingadonis/miu/controller/web/PurchaseHistoryServlet.java index 91b857f4..cc6df4a7 100644 --- a/src/main/java/io/hardingadonis/miu/controller/web/PurchaseHistoryServlet.java +++ b/src/main/java/io/hardingadonis/miu/controller/web/PurchaseHistoryServlet.java @@ -12,225 +12,230 @@ @WebServlet(name = "PurchaseHistoryServlet", urlPatterns = {"/purchase-history"}) public class PurchaseHistoryServlet extends HttpServlet { - + private static final int ORDER_PER_PAGE = 5; private static final int PAGES_PER_GROUP = 3; private static final int MINIMUM_TWO_SIDE = 1; - + @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); - + HttpSession session = request.getSession(); User user = (User) session.getAttribute("user"); String tab = request.getParameter("tab"); - + if ((user == null) || (tab == null)) { response.sendRedirect("login"); return; } - + + if (user.getStatus() == UserStatus.DEACTIVATE) { + response.sendRedirect("verify"); + return; + } + List orderList = null; int endPage = 0; int page = 0; - + switch (tab) { case "all": { endPage = (int) Math.ceil(Singleton.orderDAO.countByUserID(user.getID()) / (double) ORDER_PER_PAGE); page = parsePage(request, endPage); - + orderList = Singleton.orderDAO.getAllWithUserID(user.getID(), (page - 1) * ORDER_PER_PAGE, ORDER_PER_PAGE); break; } - + case "processing": { endPage = (int) Math.ceil(Singleton.orderDAO.countAllWithUserIDAndStatus(user.getID(), OrderStatus.PROCESSING) / (double) ORDER_PER_PAGE); page = parsePage(request, endPage); - + orderList = Singleton.orderDAO.getAllWithUserIDAndStatus(user.getID(), OrderStatus.PROCESSING, (page - 1) * ORDER_PER_PAGE, ORDER_PER_PAGE); break; } - + case "shipping": { endPage = (int) Math.ceil(Singleton.orderDAO.countAllWithUserIDAndStatus(user.getID(), OrderStatus.SHIPPING) / (double) ORDER_PER_PAGE); page = parsePage(request, endPage); - + orderList = Singleton.orderDAO.getAllWithUserIDAndStatus(user.getID(), OrderStatus.SHIPPING, (page - 1) * ORDER_PER_PAGE, ORDER_PER_PAGE); break; } - + case "done": { endPage = (int) Math.ceil(Singleton.orderDAO.countAllWithUserIDAndStatus(user.getID(), OrderStatus.DONE) / (double) ORDER_PER_PAGE); page = parsePage(request, endPage); - + orderList = Singleton.orderDAO.getAllWithUserIDAndStatus(user.getID(), OrderStatus.DONE, (page - 1) * ORDER_PER_PAGE, ORDER_PER_PAGE); break; } - + case "canceled": { endPage = (int) Math.ceil(Singleton.orderDAO.countAllWithUserIDAndStatus(user.getID(), OrderStatus.CANCELED) / (double) ORDER_PER_PAGE); page = parsePage(request, endPage); - + orderList = Singleton.orderDAO.getAllWithUserIDAndStatus(user.getID(), OrderStatus.CANCELED, (page - 1) * ORDER_PER_PAGE, ORDER_PER_PAGE); break; } } - + String paginationStr = createPagination(tab, page, endPage); - + request.setAttribute("order_list", orderList); request.setAttribute("pagination_str", paginationStr); request.setAttribute("end_page", endPage); - + session.setAttribute("prevURL", "purchase-history?tab=" + tab + "&page=" + page); - + request.getRequestDispatcher("/view/web/purchase-history.jsp").forward(request, response); } - + @Override protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { int ID = Integer.parseInt(request.getParameter("id")); - + Order order = Singleton.orderDAO.get(ID); - + if (order != null) { order.setStatus(OrderStatus.CANCELED); Singleton.orderDAO.update(order); - + JSONObject jsonResponse = new JSONObject(); jsonResponse.put("status", "success"); jsonResponse.put("message", "Order canceled successfully"); - + response.setContentType("application/json"); response.getWriter().write(jsonResponse.toString()); - + response.setStatus(HttpServletResponse.SC_OK); } - + } catch (NumberFormatException ex) { System.err.println(ex.getMessage()); } } - + private static int parsePage(HttpServletRequest request, int endPage) { try { int page = Integer.parseInt(request.getParameter("page")); - + if ((page < 1) || (page > endPage)) { page = 1; } - + return page; } catch (NumberFormatException ex) { return 1; } } - + private static String createPrevBtn(String tab, int page) { StringBuilder prevBtn = new StringBuilder("Trước"); - + return prevBtn.toString(); } - + private static String createNextBtn(String tab, int page) { StringBuilder nextBtn = new StringBuilder("Sau"); - + return nextBtn.toString(); } - + private static String createSingleIndex(String tab, int page, int index) { StringBuilder singleIndex = new StringBuilder("").append(index).append(""); - + return singleIndex.toString(); } - + private static String createNormalPagination(String tab, int page, int beginPage, int endPage) { StringBuilder normalPagination = new StringBuilder(); - + for (int i = beginPage; i <= endPage; i++) { normalPagination.append(createSingleIndex(tab, page, i)); } - + return normalPagination.toString(); } - + private static String createNormalPagination(String tab, int page, int endPage) { return createNormalPagination(tab, page, 1, endPage); } - + private static String createManyPagination(String tab, int page, int endPage) { StringBuilder manyPagination = new StringBuilder(); - + if (page <= PAGES_PER_GROUP + MINIMUM_TWO_SIDE) { manyPagination.append(createNormalPagination(tab, page, 1, page < 3 ? 3 : page + 1)); - + manyPagination.append("..."); - + manyPagination.append(createSingleIndex(tab, page, endPage)); } - + if ((page > PAGES_PER_GROUP + MINIMUM_TWO_SIDE) && (page <= endPage - PAGES_PER_GROUP - MINIMUM_TWO_SIDE)) { manyPagination.append(createSingleIndex(tab, page, 1)); manyPagination.append("..."); - + manyPagination.append(createSingleIndex(tab, page, page - 1)); manyPagination.append(createSingleIndex(tab, page, page)); manyPagination.append(createSingleIndex(tab, page, page + 1)); - + manyPagination.append("..."); manyPagination.append(createSingleIndex(tab, page, endPage)); } - + if (page > endPage - PAGES_PER_GROUP - MINIMUM_TWO_SIDE) { manyPagination.append(createSingleIndex(tab, page, 1)); - + manyPagination.append("..."); - + manyPagination.append(createNormalPagination(tab, page, page - 1, endPage)); - + } - + return manyPagination.toString(); } - + private static String createPagination(String tab, int page, int endPage) { StringBuilder pagination = new StringBuilder(); - + if (page != 1) { pagination.append(createPrevBtn(tab, page)); } - + if (endPage < PAGES_PER_GROUP + (MINIMUM_TWO_SIDE + 1) * 2) { pagination.append(createNormalPagination(tab, page, endPage)); } else { pagination.append(createManyPagination(tab, page, endPage)); } - + if (page != endPage) { pagination.append(createNextBtn(tab, page)); } - + return pagination.toString(); } } diff --git a/src/main/java/io/hardingadonis/miu/controller/web/RegisterServlet.java b/src/main/java/io/hardingadonis/miu/controller/web/RegisterServlet.java index 0c6b0479..66abc35f 100644 --- a/src/main/java/io/hardingadonis/miu/controller/web/RegisterServlet.java +++ b/src/main/java/io/hardingadonis/miu/controller/web/RegisterServlet.java @@ -49,12 +49,10 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) user.setHashedPassword(Hash.SHA256(password)); user.setAvatarPath(gender == UserGender.MALE ? MALE_AVATAR : FEMALE_AVATAR); user.setAddress(Collections.emptyList()); - user.setStatus(UserStatus.ACTIVATE); + user.setStatus(UserStatus.DEACTIVATE); Singleton.userDAO.insert(user); - Singleton.email.sendWelcomeEmail(user); - response.sendRedirect("login?" + REGISTER_SUCCESS_PARAM + "=true"); } else { String errorMsg = "Email đã tồn tại!"; diff --git a/src/main/java/io/hardingadonis/miu/controller/web/SearchServlet.java b/src/main/java/io/hardingadonis/miu/controller/web/SearchServlet.java index 511e0c59..d641cb13 100644 --- a/src/main/java/io/hardingadonis/miu/controller/web/SearchServlet.java +++ b/src/main/java/io/hardingadonis/miu/controller/web/SearchServlet.java @@ -1,6 +1,7 @@ package io.hardingadonis.miu.controller.web; import io.hardingadonis.miu.model.*; +import io.hardingadonis.miu.model.detail.*; import io.hardingadonis.miu.services.*; import java.io.*; import java.util.*; @@ -20,6 +21,15 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); + + HttpSession session = request.getSession(); + + User user = (User)session.getAttribute("user"); + + if ((user != null) && (user.getStatus() == UserStatus.DEACTIVATE)) { + response.sendRedirect("verify"); + return; + } String searchName = parseName(request); int categoryID = parseCategoryID(request); diff --git a/src/main/java/io/hardingadonis/miu/controller/web/VerifyServlet.java b/src/main/java/io/hardingadonis/miu/controller/web/VerifyServlet.java new file mode 100644 index 00000000..c81aef62 --- /dev/null +++ b/src/main/java/io/hardingadonis/miu/controller/web/VerifyServlet.java @@ -0,0 +1,69 @@ +package io.hardingadonis.miu.controller.web; + +import io.hardingadonis.miu.model.*; +import io.hardingadonis.miu.model.detail.*; +import io.hardingadonis.miu.services.*; +import java.io.*; +import javax.servlet.*; +import javax.servlet.annotation.*; +import javax.servlet.http.*; + +@WebServlet(name = "VerifyServlet", urlPatterns = {"/verify"}) +public class VerifyServlet extends HttpServlet { + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + request.setCharacterEncoding("UTF-8"); + response.setContentType("text/html; charset=UTF-8"); + + HttpSession session = request.getSession(); + + User user = (User) session.getAttribute("user"); + + if ((user == null) || (user.getStatus() == UserStatus.ACTIVATE)) { + response.sendRedirect("home"); + return; + } + + String email = request.getParameter("email"); + + String msg = null; + + if ((email == null) || (email.isEmpty())) { + String hashedStr = Hash.SHA256(email + System.currentTimeMillis()); + + session.setAttribute("hashed_str", hashedStr); + + Singleton.email.sendVerifyEmail(user, hashedStr, request); + + msg = "Bạn đã nhận được một email xác thực tài khoản. Vui lòng kiểm tra email!"; + } else { + if (user.getEmail().equals(email)) { + String hashedStrParameter = request.getParameter("code"); + String hashedStrSession = (String) session.getAttribute("hashed_str"); + + if ((hashedStrParameter != null) && (hashedStrSession != null) && (hashedStrParameter.equals(hashedStrSession))) { + user.setStatus(UserStatus.ACTIVATE); + + session.setAttribute("hashed_str", null); + + request.setAttribute("success", true); + + Singleton.userDAO.update(user); + Singleton.email.sendWelcomeEmail(user); + + msg = "Xác thực thành công!"; + } else { + msg = "Xác thực thất bại!"; + } + } else { + msg = "Xác thực thất bại!"; + } + } + + request.setAttribute("msg", msg); + + request.getRequestDispatcher("/view/web/verify.jsp").forward(request, response); + } +} diff --git a/src/main/java/io/hardingadonis/miu/services/Email.java b/src/main/java/io/hardingadonis/miu/services/Email.java index 66118160..7766cc7e 100644 --- a/src/main/java/io/hardingadonis/miu/services/Email.java +++ b/src/main/java/io/hardingadonis/miu/services/Email.java @@ -1,8 +1,11 @@ package io.hardingadonis.miu.services; import io.hardingadonis.miu.model.*; +import javax.servlet.http.*; public interface Email { public void sendWelcomeEmail(User user); + + public void sendVerifyEmail(User user, String code, HttpServletRequest request); } diff --git a/src/main/java/io/hardingadonis/miu/services/impl/gmail/EmailGmailImpl.java b/src/main/java/io/hardingadonis/miu/services/impl/gmail/EmailGmailImpl.java index db20e56b..8b340b4e 100644 --- a/src/main/java/io/hardingadonis/miu/services/impl/gmail/EmailGmailImpl.java +++ b/src/main/java/io/hardingadonis/miu/services/impl/gmail/EmailGmailImpl.java @@ -6,6 +6,7 @@ import jakarta.mail.internet.*; import java.io.*; import java.util.*; +import javax.servlet.http.*; public class EmailGmailImpl implements Email { @@ -52,9 +53,31 @@ public void sendWelcomeEmail(User user) { message.setContent(msgStr, "text/html; charset=UTF-8"); + Transport.send(message); + } catch (MessagingException ex) { + System.err.println(ex.getMessage()); + } + } + + @Override + public void sendVerifyEmail(User user, String code, HttpServletRequest request) { + Session session = Session.getInstance(this.props, this.getAuthenticator()); + + try { + MimeMessage message = new MimeMessage(session); + + message.setFrom(new InternetAddress(this.email)); + message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(user.getEmail())); + message.setSubject("Miu Shop, xác thực tài khoản!", "UTF-8"); + + String verifyLink = Server.getServerLink(request) + "verify?email=" + user.getEmail() + "&code=" + code; + + String msgStr = String.format("

Miu Shop, xác thực tài khoản!

Cảm ơn %s đã đăng ký tài khoản tại Miu Shop. Để hoàn tất quá trình đăng ký, vui lòng xác thực tài khoản bằng cách nhấn vào nút bên dưới:

Xác thực tài khoản
", user.getFullName(), verifyLink); + + message.setContent(msgStr, "text/html; charset=UTF-8"); + Transport.send(message); - System.out.println("Sent a welcome letter to " + user.getEmail()); } catch (MessagingException ex) { System.err.println(ex.getMessage()); } diff --git a/src/main/webapp/view/web/login.jsp b/src/main/webapp/view/web/login.jsp index e814d90d..cecd7dab 100644 --- a/src/main/webapp/view/web/login.jsp +++ b/src/main/webapp/view/web/login.jsp @@ -21,9 +21,8 @@ <%-- Kiểm tra xem có URL parameter registerSuccess hay không --%> @@ -35,9 +34,8 @@ <%-- Kiểm tra xem có URL parameter changePasswordSuccess hay không --%> diff --git a/src/main/webapp/view/web/verify.jsp b/src/main/webapp/view/web/verify.jsp new file mode 100644 index 00000000..a5a429a9 --- /dev/null +++ b/src/main/webapp/view/web/verify.jsp @@ -0,0 +1,50 @@ +<%@ page contentType="text/html" pageEncoding="UTF-8" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + + + + + + + + + + + + + + + Miu Shop | Xác thực + + + + <%@include file="common/_header.jsp" %> + +
+
+
+
+ Xác thực tài khoản +
+
+
+ ${msg} +
+
+ + + +
+
+
+
+ + <%@include file="common/_footer.jsp" %> + + + + + + \ No newline at end of file