Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/development' into 12-user-settin…
Browse files Browse the repository at this point in the history
…gs-provide-all-actions

# Conflicts:
#	src/main/java/model/DAO/DAOUser.java
  • Loading branch information
Cody2806 committed Dec 28, 2023
2 parents 722e216 + 91666e1 commit 970e7c7
Show file tree
Hide file tree
Showing 29 changed files with 1,326 additions and 126 deletions.
75 changes: 75 additions & 0 deletions src/main/java/controller/RegistrationServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package controller;

import model.entity.PersonalInfo;
import model.entity.User;
import model.service.registration.Registration;
import model.service.user.UserData;
import model.service.user.UserRegistry;

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;

@WebServlet("/register")

public class RegistrationServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

String licenseCode = request.getParameter("licenseCode");
String email = request.getParameter("email");
String password = request.getParameter("password");
String name = request.getParameter("name");
String surname = request.getParameter("surname");

Registration registration = new Registration();
int result = registration.registerNewUser(licenseCode, email, password, name, surname);
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(String.valueOf(result));
if(result == 0) {
setSessionAttributes(email, request);
response.sendRedirect("/JSP/legal.jsp");
}
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
UserData ud = new UserData();
HttpSession session = request.getSession();
String parameter = request.getParameter("type");
if(parameter.equals("analytics")) {
Boolean value = Boolean.parseBoolean(request.getParameter("accept"));
ud.updateAnalyticsPreference(String.valueOf(session.getAttribute("id")), value);
}
if(parameter.equals("emailTime")) {
String start = request.getParameter("startTime");
String end = request.getParameter("endTime");
String time = start + "|" + end;
ud.updateEmailTime(String.valueOf(session.getAttribute("id")), time);
response.sendRedirect("/JSP/welcome.jsp");
}
}

private void setSessionAttributes(String email, HttpServletRequest request){
HttpSession session = request.getSession();

UserData userData = new UserData();
UserRegistry userReg = new UserRegistry();

User user = userData.getUserByIdOrEmail(email);
PersonalInfo personalInfo = userReg.getPersonalInfo(user.getId());

session.setAttribute("id", user.getId());
session.setAttribute("name", personalInfo.getFirstname());

if(!userData.isTherapist(user)) {
session.setAttribute("type", "patient");
session.setAttribute("therapist", user.getIdTherapist());
}
else {
session.setAttribute("type", "therapist");
}
}
}
15 changes: 11 additions & 4 deletions src/main/java/controller/SendResetPin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package controller;

import model.entity.User;
import model.service.login.Authenticator;
import model.service.user.UserData;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
Expand All @@ -12,10 +14,15 @@
public class SendResetPin extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
String email = request.getParameter("email");
String pin = new Authenticator().resetPassword(email);
// Store the pin and email in the session for future comparison
// Send back a result
UserData checker = new UserData();

response.setContentType("text/plain");
response.getWriter().println(pin);
if(checker.checkIfEmailExists(email)){
String pin = new Authenticator().resetPassword(email);
response.getWriter().println(pin);
}
else {
response.getWriter().println("NA");
}
}
}
99 changes: 98 additions & 1 deletion src/main/java/model/DAO/DAOUser.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package model.DAO;

import model.entity.User;
import model.service.encryption.Encryption;

import java.sql.Connection;
import java.sql.PreparedStatement;
Expand Down Expand Up @@ -317,4 +316,102 @@ public boolean ControlPassword(int id, String Password)
// Default to false if an exception occurs
return false;
}

/**
* Updates the user's analytics choice in the User table.
*
* @param userId The id of the user.
* @param value The analytics choice value to set for the user.
* @return true if the choice was successfully updated; false otherwise.
*/
public boolean updateAnalyticsPreference(String userId, boolean value) {
Connection connection = null;
PreparedStatement preparedStatement = null;

try {
// Get connection
connection = DAOConnection.getConnection();

// Query to update analytics choice for the given userId
String query = "UPDATE user SET Analytics = ? WHERE ID = ?";

// Prepare the statement
preparedStatement = connection.prepareStatement(query);

// Set the parameters
preparedStatement.setBoolean(1, value);
preparedStatement.setString(2, userId);

// Execute the update query
int rowsModified = preparedStatement.executeUpdate();

// If rowsModified is greater than 0, then a row has been updated.
// So, return true. If not, return false.
return rowsModified > 0;
} catch (SQLException e) {
// Handle the exception (e.g., log or throw)
e.printStackTrace();
} finally {
try {
// Close everything properly
if (preparedStatement != null) preparedStatement.close();
DAOConnection.releaseConnection(connection);
} catch (SQLException e) {
// Handle the exception (e.g., log or throw)
e.printStackTrace();
}
}

// Default to false if an exception occurs
return false;
}

/**
* Updates the user's Email Time in the User table.
*
* @param id The id of the user.
* @param value The email time value to set for the user.
* @return true if the email time was successfully updated; false otherwise.
*/
public boolean updateEmailTime(String id, String value) {
Connection connection = null;
PreparedStatement preparedStatement = null;

try {
// Get connection
connection = DAOConnection.getConnection();

// Query to update Email Time for the given id
String query = "UPDATE user SET NotificationTime = ? WHERE ID = ?";

// Prepare the statement
preparedStatement = connection.prepareStatement(query);

// Set the parameters
preparedStatement.setString(1, value);
preparedStatement.setString(2, id);

// Execute the update query
int rowsModified = preparedStatement.executeUpdate();

// If rowsModified is greater than 0, then a row has been updated.
// So, return true. If not, return false.
return rowsModified > 0;
} catch (SQLException e) {
// Handle the exception (e.g., log or throw)
e.printStackTrace();
} finally {
try {
// Close everything properly
if (preparedStatement != null) preparedStatement.close();
DAOConnection.releaseConnection(connection);
} catch (SQLException e) {
// Handle the exception (e.g., log or throw)
e.printStackTrace();
}
}

// Default to false if an exception occurs
return false;
}
}
2 changes: 1 addition & 1 deletion src/main/java/model/service/license/LicenseActivation.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public boolean isActivable(License license) {
}

public int isForTherapist(License license) {
if (license != null) {
if (license != null && license.getSequence().length()==4) {
return license.getIdUser();
}
return 0;
Expand Down
79 changes: 54 additions & 25 deletions src/main/java/model/service/registration/Registration.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,68 @@ public class Registration implements RegistrationInterface {
* 3 - Unable to create user.
* 4 - Unable to generate personal info.
*/
public int register(String licenseCode, String email, String password, String name, String surname) {
LicenseActivation la = new LicenseActivation();
UserData ud = new UserData();
UserRegistry ur = new UserRegistry();
License license;

license = la.getLicense(licenseCode);
if(la.isActivable(license)) {
if(!ud.checkIfEmailExists(email)) {
Encryption encryption = new Encryption();
String hashed = encryption.encryptPassword(password);
int theNewId = ud.createUser(email, hashed, la.isForTherapist(license));

if(theNewId >= 0) {
if(ur.firstAccess(theNewId, name, surname)) {
return 0; //nessun errore
}
return 4; //non è stato possibile generare l'anagrafica
@Override
public int registerNewUser(String licenseCode, String email, String password, String name, String surname) {
License license = validateLicense(licenseCode);
if(license != null) {
if(isEmailExists(email)) {
return 2; //email non valida
}
String hashed = encryptPassword(password);
int theNewId = createNewUser(email, hashed, license);
if(theNewId >= 0) {
if(createUserPersonalInformation(theNewId, name, surname)) {
LicenseActivation la = new LicenseActivation();
la.activate(license, theNewId);
return 0; // no error
}
return 3; //non è stato possibile generare l'utenza

return 4; //non è stato possibile generare l'anagrafica
}
return 2; //email non valida
return 3; //non è stato possibile generare l'utenza

}
return 1; //licenza non valida
}

/**
* Validates license
*/
private License validateLicense(String licenseCode){
LicenseActivation la = new LicenseActivation();
License license = la.getLicense(licenseCode);
return la.isActivable(license) ? license : null;
}

/**
* Checks if an email already exists or not.
*/
private boolean isEmailExists(String email){
UserData ud = new UserData();
return ud.checkIfEmailExists(email);
}

/**
* Encrypts user password
*/
private String encryptPassword(String password){
Encryption encryption = new Encryption();
return encryption.encryptPassword(password);
}

public boolean resetFromOldPassword(String email, String oldpw, String newpw) {
return false;
/**
* Creates a new user.
*/
private int createNewUser(String email, String hashed, License license){
UserData ud = new UserData();
LicenseActivation la = new LicenseActivation();
return ud.createUser(email, hashed, la.isForTherapist(license));
}

public boolean resetPassword(String email) {
return false;
/**
* Creates a user personal info.
*/
private boolean createUserPersonalInformation(int theNewId, String name, String surname){
UserRegistry ur = new UserRegistry();
return ur.firstAccess(theNewId, name, surname);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,5 @@ public interface RegistrationInterface {
* @param surname è il cognome utente per la sua anagrafica
* @return un codice di errore in base alla casistica
*/
int register(String licenseCode, String email, String password, String name, String surname);

/**
* cambia la password da una vecchia a quella nuova
* @param email è l'email inserita dell'account
* @param oldpw è la password inserita da controllare la validità e sostituire
* @param newpw è la nuova password
* @return True se è stata cambiata con successo. False altrimenti
*/
boolean resetFromOldPassword(String email, String oldpw, String newpw);

/**
* avvia il recupero password tramite email
* @param email è l'email inserita dell'account
* @return True se è stata cambiata con successo. False altrimenti
*/
int registerNewUser(String licenseCode, String email, String password, String name, String surname);
}
10 changes: 9 additions & 1 deletion src/main/java/model/service/user/UserData.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ public User getUserByIdOrEmail(Object idOrEmail) {
}

public boolean isTherapist(User user){
return user.getIdTherapist() > 0;
return user.getIdTherapist() == 0;
}

public boolean updateAnalyticsPreference(String id, Boolean value) {
return daoUser.updateAnalyticsPreference(id, value);
}

public boolean updateEmailTime(String id, String value) {
return daoUser.updateEmailTime(id, value);
}

public void updateUser(int idUser, String Email, String address)
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/model/service/user/UserRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ 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!
return db.createRegistry(id, name, surname);
}

public PersonalInfo getPersonalInfo(int id) {
Expand Down
27 changes: 27 additions & 0 deletions src/main/webapp/CSS/403.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
*{
padding: 0;
margin: 0;
box-sizing: border-box;
font-family: 'Poppins', sans-serif;
text-decoration: none;
list-style: none;
}
.return {
display:flex;
justify-content:center;
width: 100%;
}

.return > button {
width: 30%;
border: none;
padding: 15px 30px;
border-radius: 5px;
margin-bottom: 10px;
}

:root{
--bg-color: #222327;
--text-color: #fff;
--main-color: #29fd53
}
Loading

0 comments on commit 970e7c7

Please sign in to comment.