Skip to content

Commit

Permalink
Fully working and Error Handling for Registration page
Browse files Browse the repository at this point in the history
  • Loading branch information
panuozzo77 committed Dec 27, 2023
1 parent aefb315 commit 96dec4a
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 30 deletions.
58 changes: 58 additions & 0 deletions src/main/java/controller/RegistrationServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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");
}
}

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");
}
}
}
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
80 changes: 59 additions & 21 deletions src/main/java/model/service/registration/Registration.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +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);
}

/**
* 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));
}

/**
* 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,5 +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);
int registerNewUser(String licenseCode, String email, String password, String name, String surname);
}
2 changes: 1 addition & 1 deletion src/main/java/model/service/user/UserData.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public User getUserByIdOrEmail(Object idOrEmail) {
}

public boolean isTherapist(User user){
return user.getIdTherapist() > 0;
return user.getIdTherapist() == 0;
}
}
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
54 changes: 54 additions & 0 deletions src/main/webapp/JS/registration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

$(document).ready(function() {
$('#signUpButton').click(function(e) {
//e.preventDefault();
let pin = $('#pin').val();
let cognome = $('#cognome').val();
let nome = $('#nome').val();
let email = $('#email').val();
let password = $('#password').val();

let registrationData = {
licenseCode: pin,
surname: cognome,
name: nome,
email: email,
password: password
};

$.ajax({
type: 'POST',
url: '../register',
data: registrationData, // data is passed as form data
success: function(response) {
var resultCode = parseInt(response);
var message;
switch (resultCode) {
case 0:
break;
case 1:
message = "Licenza non valida.";
break;
case 2:
message = "Email già in uso";
break;
case 3:
message = "Impossibile generare l'utente";
break;
case 4:
message = "Impossibile generare anagrafica";
break;
default:
message = "Errore sconosciuto";
}
if(resultCode !== 0) {
alert(message);
}
console.log('User registered error code: ', response);
},
error: function(error) {
console.log('Error while registering user: ', error);
}
});
});
});
10 changes: 6 additions & 4 deletions src/main/webapp/JSP/registration.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
<head>
<%@page contentType="text/html;charset=UTF-8"%>
<meta charset="utf-8" />
<link rel="stylesheet" href="../css/registration.css" />
<link rel="stylesheet" href="../CSS/registration.css" />
</head>
<body>
<form id="registrationForm" action="../registration" method="post">
<!--<form id="registrationForm" action="../registration" method="post"> -->
<div class="element-registration">
<div class="div">
<p class="hai-gi-un-account">
<p class="hai-gia-un-account">
<span class="text-wrapper">Hai già un account?</span>
<span class="span">&nbsp;</span>
<span class="text-wrapper-2">Login</span>
Expand Down Expand Up @@ -71,6 +71,8 @@
// Sostituisci l'elemento span con l'elemento a
spanElement.parentNode.replaceChild(aElement, spanElement);
</script>
</form>
<!--</form> -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="../JS/registration.js"></script>
</body>
</html>

0 comments on commit 96dec4a

Please sign in to comment.