Skip to content

Commit

Permalink
Merge pull request #149 from khengyun/unittest-login-controler
Browse files Browse the repository at this point in the history
  • Loading branch information
khengyun authored Nov 7, 2023
2 parents 0574c1b + 794a82d commit b4fa24d
Show file tree
Hide file tree
Showing 7 changed files with 455 additions and 59 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,3 @@ jobs:
👋 @{{ author }}
Thank you for raising your pull request.
Please make sure you have followed our contributing guidelines. We will review it as soon as possible
- name: 'Auto-assign issue'
uses: pozil/auto-assign-issue@v1
with:
allowSelfAssign: true
62 changes: 22 additions & 40 deletions src/main/java/Controllers/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,10 @@
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import Validation.ValidationUtils;

public class LoginController extends HttpServlet {

/**
* 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 {
response.setContentType("text/html;charset=UTF-8");
try ( PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Login</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet Login at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
Expand Down Expand Up @@ -75,25 +50,32 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String contextPath = request.getContextPath();
ValidationUtils valid = new ValidationUtils();
HttpSession session = request.getSession();
if (request.getParameter("btnSubmit") != null
&& ((String) request.getParameter("btnSubmit")).equals("Submit")) {
String email = request.getParameter("txtEmail");
String password = (String) request.getAttribute("txtPassword");

Account account = new Account(email, password);

if (!valid.loginValidation(email,password)){
session.setAttribute("isSuccessful", false);
response.sendRedirect("/home#failure_login_info");
return;
}

Account loginAccount = new Account(email, password);
AccountDAO dao = new AccountDAO();
boolean success;
try {
success = dao.login(account);
success = dao.login(loginAccount);
} catch (SQLException ex) {
Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
success = false;
}
// Truy xuất URL hiện tại từ session attribute
HttpSession session = request.getSession();

if (success) {

account = dao.getAccount(email);
session.setAttribute("isSuccessful", success);
Account account = dao.getAccount(email);
String accountType = account.getAccountType();
boolean isRemembered = (request.getParameter("chkRememberMe") != null
&& request.getParameter("chkRememberMe").equals("remember"));
Expand All @@ -110,7 +92,9 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
response.addCookie(cUser);
Cookie cUserID = new Cookie("userID", String.valueOf(userID));
cUser.setMaxAge(cAge);
cUserID.setMaxAge(cAge);
cUser.setPath("/");
cUserID.setPath("/");
response.addCookie(cUserID);
response.sendRedirect("/");
} else if (accountType.equals("admin")) {
Expand All @@ -119,7 +103,6 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
String username = account.getUsername();
username = URLEncoder.encode(username, "UTF-8");
byte adminID = account.getAdminID();
System.out.println("adminID " + adminID);
session.setAttribute("adminID", adminID);
Cookie adminCookie = new Cookie("admin", username);
Cookie adminIDCookie = new Cookie("adminID", Byte.toString(adminID));
Expand All @@ -139,8 +122,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
session.setAttribute("staffID", staffID);
username = URLEncoder.encode(username, "UTF-8");
Cookie staffCookie = new Cookie("staff", username);
Cookie staffIDCookie = new Cookie("staffID", Byte.toString(staffID));

Cookie staffIDCookie = new Cookie("staffID", Byte.toString(staffID));
staffCookie.setMaxAge(cAge);
staffCookie.setPath("/");
staffIDCookie.setMaxAge(cAge);
Expand Down Expand Up @@ -173,15 +155,15 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
String username = account.getUsername();
session = request.getSession();
byte adminID = account.getAdminID();
System.out.println("adminID " + adminID);
session.setAttribute("adminID", adminID);
session.setAttribute("admin", username);
response.sendRedirect("/admin");
} else if (accountType.equals("staff")) {
account = dao.getAccount(email);
String username = account.getUsername();
session = request.getSession();
session.setAttribute("staffID", account.getStaffID());
byte staffID = account.getStaffID();
session.setAttribute("staffID", staffID);
session.setAttribute("staff", username);
response.sendRedirect("/staff");
} else if (accountType.equals("promotionManager")) {
Expand All @@ -195,9 +177,9 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
}
}
} else {
session.setAttribute("isSuccessful", success);
response.sendRedirect("/home#failure_login_info");
}

}
}
}
}
8 changes: 8 additions & 0 deletions src/main/java/Controllers/SignUpController.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import Validation.ValidationUtils;

public class SignUpController extends HttpServlet {

Expand Down Expand Up @@ -78,6 +79,13 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
HttpSession session = request.getSession();
String previousUrl = (String) session.getAttribute("previousUrl");

ValidationUtils valid = new ValidationUtils();

if (!valid.signUpValidation(username,email,pass)){
response.sendRedirect("/home#failure_register");
return;
}

AccountDAO accountDAO = new AccountDAO();
Account account = new Account(username, email, pass, "user");
if (accountDAO.getAccount(email) != null) {
Expand Down
45 changes: 32 additions & 13 deletions src/main/java/DAOs/AccountDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,40 +331,40 @@ public Account getAccount(String email) {
if (rs.getString("account_type").equals("user")) {
// Account is of User type (no adminID)
account = new Account(
rs.getInt("account_id"),
rs.getInt("customer_id"),
rs.getString("account_username"),
rs.getString("account_email"),
rs.getString("account_password"),
rs.getString("account_type"));
rs.getString("account_type")
);
account.setAccountID( rs.getInt("account_id"));
account.setCustomerID(rs.getInt("customer_id"));
} else if (rs.getString("account_type").equals("admin")) {
// Account is of Admin type (no customerID)
account = new Account(
rs.getInt("account_id"),
rs.getByte("admin_id"),
rs.getString("account_username"),
rs.getString("account_email"),
rs.getString("account_password"),
rs.getString("account_type"));
account.setAccountID(rs.getInt("account_id"));
account.setAdminID(rs.getByte("admin_id"));
} else if (rs.getString("account_type").equals("staff")) {
// Account is of Admin type (no customerID)
account = new Account(
rs.getInt("account_id"),
rs.getByte("staff_id"),
account = new Account(
rs.getString("account_username"),
rs.getString("account_email"),
rs.getString("account_password"),
rs.getString("account_type"));

account.setAccountID(rs.getInt("account_id"));
account.setStaffID(rs.getByte("staff_id"));
} else {
// Account is of Admin type (no customerID)
account = new Account(
rs.getInt("account_id"),
rs.getByte("pro_id"),
rs.getString("account_username"),
rs.getString("account_email"),
rs.getString("account_password"),
rs.getString("account_type"));
account.setAccountID(rs.getInt("account_id"));
account.setProID(rs.getByte("pro_id"));
}
}
return account;
Expand All @@ -389,14 +389,33 @@ public Account getAccount(int accountID) {
rs.getString("account_email"),
rs.getString("account_password"),
rs.getString("account_type"));
} else if (rs.getString("account_type").equals("admin")) {
// Account is of Admin type (no customerID)
account = new Account(
rs.getString("account_username"),
rs.getString("account_email"),
rs.getString("account_password"),
rs.getString("account_type"));
account.setAccountID(rs.getInt("account_id"));
account.setAdminID(rs.getByte("admin_id"));
} else if (rs.getString("account_type").equals("staff")) {
// Account is of Admin type (no customerID)
account = new Account(
rs.getString("account_username"),
rs.getString("account_email"),
rs.getString("account_password"),
rs.getString("account_type"));
account.setAccountID(rs.getInt("account_id"));
account.setStaffID(rs.getByte("staff_id"));
} else {
// Account is of Admin type (no customerID)
account = new Account(rs.getInt("account_id"),
rs.getByte("admin_id"),
account = new Account(
rs.getString("account_username"),
rs.getString("account_email"),
rs.getString("account_password"),
rs.getString("account_type"));
account.setAccountID(rs.getInt("account_id"));
account.setProID(rs.getByte("pro_id"));
}
}
return account;
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/Validation/ValidationUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package Validation;

public class ValidationUtils {

public static boolean loginValidation(String email, String password) {
if (isValidEmail(email) && isValidPassword(password)) {
return true;
}
return false;
}

public static boolean signUpValidation(String username, String email, String password) {
if (isValidUsername(username) && isValidEmail(email) && isValidPassword(password)) {
return true;
}
return false;
}

public static boolean isValidUsername(String username) {
// Check if username is not empty
if (username == null || username.trim().isEmpty()) {
return false;
}

// Check minimum and maximum length
if (username.length() < 8 || username.length() > 50) {
return false;
}

// Check if username matches the specified pattern
if (!username.matches("^[a-zA-Z0-9-'_]+$")) {
return false;
}

// Username is valid
return true;
}

private static boolean isValidEmail(String email) {
if (email == null || email.trim().isEmpty()) {
return false;
} else if (email.length() > 255) {
return false;
}
// Kiểm tra định dạng email sử dụng regular expression
// Trả về true nếu email hợp lệ, ngược lại trả về false
return email.matches("^[\\w.-]+@[\\w.-]+\\.[a-zA-Z]{2,}$");
}

private static boolean isValidPassword(String password) {
if (password == null || password.trim().isEmpty()) {
return false;
}
return true;
}
}

6 changes: 4 additions & 2 deletions src/main/webapp/assets/js/validateForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,8 @@ function validateForm() {
txtAccountUsername: {
required: true,
minlength: 8,
maxlength: 50
maxlength: 50,
pattern: /^[a-zA-Z0-9-'_]+$/
},
txtAccountEmail: {
required: true,
Expand All @@ -514,7 +515,8 @@ function validateForm() {
txtAccountUsername: {
required: "Vui lòng nhập Tên Tài khoản Người dùng",
minlength: "Tên tài khoản mới phải có ít nhất 8 ký tự",
maxlength: "Tên Tài khoản Người dùng không được vượt quá 50 ký tự"
maxlength: "Tên Tài khoản Người dùng không được vượt quá 50 ký tự",
pattern: "Tên Tài khoản chỉ chấp nhận chữ, số, dấu gạch ngang, gạch dưới, nháy đơn và không chứa khoảng trắng"
},
txtAccountEmail: {
required: "Vui lòng nhập Email",
Expand Down
Loading

0 comments on commit b4fa24d

Please sign in to comment.