From 9a715af5bb5f228fd1ec8fb3023cf5f9f8ad6107 Mon Sep 17 00:00:00 2001 From: Sewaaa Date: Fri, 22 Dec 2023 17:44:55 +0100 Subject: [PATCH 1/3] session variable with information of logged user setted --- pom.xml | 5 +++++ src/main/java/controller/LoginController.java | 12 ++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 2b155c4..767f715 100644 --- a/pom.xml +++ b/pom.xml @@ -56,6 +56,11 @@ jakarta.mail 1.6.5 + + org.mortbay.jetty + servlet-api + 4.0.1 + diff --git a/src/main/java/controller/LoginController.java b/src/main/java/controller/LoginController.java index ac349db..e3f4f7a 100644 --- a/src/main/java/controller/LoginController.java +++ b/src/main/java/controller/LoginController.java @@ -1,12 +1,16 @@ package controller; +import model.DAO.DAOUser; +import model.entity.User; +import model.service.login.Authenticator; + import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; import java.io.IOException; -import model.service.login.Authenticator; @WebServlet("/login") public class LoginController extends HttpServlet { @@ -33,7 +37,11 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) if (result > 0) { // Login success, redirect to the welcome page - response.sendRedirect("welcome.jsp"); + HttpSession session = request.getSession(); + DAOUser daouser=new DAOUser(); + User user_logged= daouser.getUserByIdOrEmail(email); + session.setAttribute("user_logged", user_logged); + response.sendRedirect("JSP/welcome.jsp"); } else { // Login failed, redirect back to the login page response.sendRedirect("JSP/login.jsp?error=1"); From 42a34a535af12b6f0811ac68c1b43bd0f8fcec53 Mon Sep 17 00:00:00 2001 From: panuozzo77 Date: Sat, 23 Dec 2023 11:20:29 +0100 Subject: [PATCH 2/3] Modified LoginController -Added method setSessionAttributes in LoginController -Added method getPersonalInfo in DAOPersonalInfo -Refactored DAOAPersonalInfo to DAOPersonalInfo -Added method getPersonalInfo to UserRegistry -Added methods getUserByIdOrEmail and isTherapist in UserData --- ...PersonalInfo.java => DAOPersonalInfo.java} | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) rename src/main/java/model/DAO/{DAOAPersonalInfo.java => DAOPersonalInfo.java} (50%) diff --git a/src/main/java/model/DAO/DAOAPersonalInfo.java b/src/main/java/model/DAO/DAOPersonalInfo.java similarity index 50% rename from src/main/java/model/DAO/DAOAPersonalInfo.java rename to src/main/java/model/DAO/DAOPersonalInfo.java index 7c1a9ec..b9f495e 100644 --- a/src/main/java/model/DAO/DAOAPersonalInfo.java +++ b/src/main/java/model/DAO/DAOPersonalInfo.java @@ -1,11 +1,28 @@ package model.DAO; +import model.entity.PersonalInfo; + import java.sql.Connection; import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.sql.SQLException; public class DAOAPersonalInfo { + private PersonalInfo getPersonalInfoFromResultSet(ResultSet resultSet) throws SQLException { + PersonalInfo personalInfo = new PersonalInfo(); + + personalInfo.setIdUser(resultSet.getInt("ID_user")); + personalInfo.setFirstname(resultSet.getString("Firstname")); + personalInfo.setLastname(resultSet.getString("Lastname")); + personalInfo.setDateOfBirth(resultSet.getDate("DateOfBirth")); + personalInfo.setGender(resultSet.getString("Gender")); + personalInfo.setAddress(resultSet.getString("Address")); + personalInfo.setSsn(resultSet.getString("SSN")); + personalInfo.setPhone(resultSet.getString("Phone")); + + return personalInfo; + } public boolean createRegistry(int id, String name, String surname) { Connection connection = null; PreparedStatement preparedStatementPersonalInfo = null; @@ -47,5 +64,37 @@ public boolean createRegistry(int id, String name, String surname) { return false; // Default to false if an exception occurs } + + public PersonalInfo getPersonalInfo(int id) { + Connection connection = null; + PreparedStatement preparedStatement = null; + ResultSet resultSet = null; + + try { + connection = DAOConnection.getConnection(); + + String sql = "SELECT * FROM personal_info WHERE ID_user = ?"; + preparedStatement = connection.prepareStatement(sql); + preparedStatement.setInt(1, id); + + resultSet = preparedStatement.executeQuery(); + + if (resultSet.next()) { + return getPersonalInfoFromResultSet(resultSet); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + try { + if (resultSet != null) resultSet.close(); + if (preparedStatement != null) preparedStatement.close(); + DAOConnection.releaseConnection(connection); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return null; // Return null if personal_info does not exist + } } From cdebe7d8c65d483d893d3b2c4c5a33d2a2a8a5c5 Mon Sep 17 00:00:00 2001 From: panuozzo77 Date: Sat, 23 Dec 2023 11:24:08 +0100 Subject: [PATCH 3/3] Modified LoginController -Added method setSessionAttributes in LoginController -Added method getPersonalInfo in DAOPersonalInfo -Refactored DAOAPersonalInfo to DAOPersonalInfo -Added method getPersonalInfo to UserRegistry -Added methods getUserByIdOrEmail and isTherapist in UserData -Added Servlet-API in pom.xml --- pom.xml | 7 ++-- src/main/java/controller/LoginController.java | 37 ++++++++++++++----- src/main/java/model/DAO/DAOPersonalInfo.java | 2 +- .../java/model/service/user/UserData.java | 9 +++++ .../java/model/service/user/UserRegistry.java | 9 +++++ 5 files changed, 51 insertions(+), 13 deletions(-) diff --git a/pom.xml b/pom.xml index 767f715..13e599b 100644 --- a/pom.xml +++ b/pom.xml @@ -56,10 +56,11 @@ jakarta.mail 1.6.5 + - org.mortbay.jetty - servlet-api - 4.0.1 + javax.servlet + javax.servlet-api + 3.1.0 diff --git a/src/main/java/controller/LoginController.java b/src/main/java/controller/LoginController.java index e3f4f7a..3039471 100644 --- a/src/main/java/controller/LoginController.java +++ b/src/main/java/controller/LoginController.java @@ -1,8 +1,11 @@ package controller; -import model.DAO.DAOUser; +import model.DAO.DAOPersonalInfo; +import model.entity.PersonalInfo; import model.entity.User; import model.service.login.Authenticator; +import model.service.user.UserData; +import model.service.user.UserRegistry; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; @@ -27,24 +30,40 @@ public void init() { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - String email = request.getParameter("email"); String password = request.getParameter("password"); - System.out.println(email); - System.out.println(password); int result = authService.authenticate(email, password); if (result > 0) { - // Login success, redirect to the welcome page - HttpSession session = request.getSession(); - DAOUser daouser=new DAOUser(); - User user_logged= daouser.getUserByIdOrEmail(email); - session.setAttribute("user_logged", user_logged); + // Login success, defining its Session attributes + setSessionAttributes(result,request); response.sendRedirect("JSP/welcome.jsp"); } else { // Login failed, redirect back to the login page response.sendRedirect("JSP/login.jsp?error=1"); } } + + private void setSessionAttributes(int id, HttpServletRequest request){ + HttpSession session = request.getSession(); + + UserData userData = new UserData(); + UserRegistry userReg = new UserRegistry(); + + User user = userData.getUserByIdOrEmail(id); + PersonalInfo personalInfo = userReg.getPersonalInfo(id); + + session.setAttribute("id", id); + session.setAttribute("name", personalInfo.getFirstname()); + + if(userData.isTherapist(user)) { + session.setAttribute("type", "patient"); + session.setAttribute("therapist", user.getIdTherapist()); + } + else { + session.setAttribute("type", "therapist"); + } + } + } \ No newline at end of file diff --git a/src/main/java/model/DAO/DAOPersonalInfo.java b/src/main/java/model/DAO/DAOPersonalInfo.java index b9f495e..9dbb2a1 100644 --- a/src/main/java/model/DAO/DAOPersonalInfo.java +++ b/src/main/java/model/DAO/DAOPersonalInfo.java @@ -7,7 +7,7 @@ import java.sql.ResultSet; import java.sql.SQLException; -public class DAOAPersonalInfo { +public class DAOPersonalInfo { private PersonalInfo getPersonalInfoFromResultSet(ResultSet resultSet) throws SQLException { PersonalInfo personalInfo = new PersonalInfo(); diff --git a/src/main/java/model/service/user/UserData.java b/src/main/java/model/service/user/UserData.java index a4e6cc1..2c9f14f 100644 --- a/src/main/java/model/service/user/UserData.java +++ b/src/main/java/model/service/user/UserData.java @@ -1,6 +1,7 @@ package model.service.user; import model.DAO.DAOUser; +import model.entity.User; public class UserData /*implements UserDataInterface */{ DAOUser daoUser = new DAOUser(); @@ -12,4 +13,12 @@ public boolean checkIfEmailExists(String email) { public int createUser(String email, String password, int therapistId) { return daoUser.createUser(email, password, therapistId); } + + public User getUserByIdOrEmail(Object idOrEmail) { + return daoUser.getUserByIdOrEmail(idOrEmail); + } + + public boolean isTherapist(User user){ + return user.getIdTherapist() > 0; + } } diff --git a/src/main/java/model/service/user/UserRegistry.java b/src/main/java/model/service/user/UserRegistry.java index 629b141..03ce884 100644 --- a/src/main/java/model/service/user/UserRegistry.java +++ b/src/main/java/model/service/user/UserRegistry.java @@ -1,9 +1,18 @@ package model.service.user; +import model.DAO.DAOPersonalInfo; +import model.entity.PersonalInfo; + public class UserRegistry implements UserRegistryInterface { + DAOPersonalInfo db = new DAOPersonalInfo(); public boolean firstAccess(int id, String name, String surname) {//TODO e da inserire nel metodo di registrazione! System.out.println("hello, this is a placeholder"); return true; //TODO Remove it! } + + public PersonalInfo getPersonalInfo(int id) { + return db.getPersonalInfo(id); + } + }