From 32ce0a7eb6c1d96855f54626ddd368739498f711 Mon Sep 17 00:00:00 2001 From: mgarcianun Date: Fri, 6 Dec 2019 00:18:20 +0100 Subject: [PATCH] =?UTF-8?q?Incluidas=20clases=20para=20permitir=20el=20log?= =?UTF-8?q?in=20de=20diferentes=20tipos=20de=20usuaario.=20*=20POJO=20para?= =?UTF-8?q?=20usuario=20logeado.=20*=20Hash=20para=20claves=20de=20usuario?= =?UTF-8?q?s=20*=20M=C3=A9todos=20EJB=20para=20login?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1.sources/MyHealth/build.xml | 28 +++++++-- 1.sources/MyHealth/src/TO/LoggedUserTO.java | 62 +++++++++++++++++++ 1.sources/MyHealth/src/common/HashUtils.java | 24 +++++++ .../src/ejb/profile/ProfileFacadeBean.java | 13 ++-- .../systemAdmin/SystemAdminFacadeBean.java | 47 ++++++++++++++ .../systemAdmin/SystemAdminFacadeRemote.java | 2 + .../MyHealth/src/jpa/AdministratorJPA.java | 52 ++++++++++++++++ .../MyHealth/src/jpa/FamilyDoctorJPA.java | 4 +- 8 files changed, 218 insertions(+), 14 deletions(-) create mode 100644 1.sources/MyHealth/src/TO/LoggedUserTO.java create mode 100644 1.sources/MyHealth/src/common/HashUtils.java create mode 100644 1.sources/MyHealth/src/jpa/AdministratorJPA.java diff --git a/1.sources/MyHealth/build.xml b/1.sources/MyHealth/build.xml index a771ff4..1def254 100644 --- a/1.sources/MyHealth/build.xml +++ b/1.sources/MyHealth/build.xml @@ -11,8 +11,9 @@ + - + @@ -20,6 +21,11 @@ + + + + + @@ -33,6 +39,7 @@ + @@ -52,13 +59,23 @@ - + - + + + + + + + + + + + @@ -83,10 +100,11 @@ - - + + + diff --git a/1.sources/MyHealth/src/TO/LoggedUserTO.java b/1.sources/MyHealth/src/TO/LoggedUserTO.java new file mode 100644 index 0000000..bf208ee --- /dev/null +++ b/1.sources/MyHealth/src/TO/LoggedUserTO.java @@ -0,0 +1,62 @@ +package TO; + +import managedbean.common.UserType; + +/** + * + * @author Marcos García Núñez (mgarcianun@uoc.edu) + * + */ +public class LoggedUserTO { + private static final long serialVersionUID = 1L; + + private String id; + private String password; + private String name; + private UserType userType; + + public LoggedUserTO() { + super(); + } + + public LoggedUserTO(String usrId, String usrName, String usrPwd, UserType usrType) { + id = usrId; + name = usrName; + password = usrPwd; + userType = usrType; + + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public UserType getUserType() { + return userType; + } + + public void setUserType(UserType userType) { + this.userType = userType; + } + +} diff --git a/1.sources/MyHealth/src/common/HashUtils.java b/1.sources/MyHealth/src/common/HashUtils.java new file mode 100644 index 0000000..5b7bd53 --- /dev/null +++ b/1.sources/MyHealth/src/common/HashUtils.java @@ -0,0 +1,24 @@ +package common; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +import javax.xml.bind.DatatypeConverter; + +public class HashUtils { + + public static String hashMD5(String stringValue) { + byte[] digest = null; + + try { + MessageDigest md = MessageDigest.getInstance("MD5"); + md.update(stringValue.getBytes()); + digest = md.digest(); + } catch (Exception ex) { + // TODO: Register exception to log. + } + + return (DatatypeConverter.printHexBinary(digest).toUpperCase()); + } + +} diff --git a/1.sources/MyHealth/src/ejb/profile/ProfileFacadeBean.java b/1.sources/MyHealth/src/ejb/profile/ProfileFacadeBean.java index c6719fa..1b0fb33 100644 --- a/1.sources/MyHealth/src/ejb/profile/ProfileFacadeBean.java +++ b/1.sources/MyHealth/src/ejb/profile/ProfileFacadeBean.java @@ -9,6 +9,7 @@ import TO.MedicalSpecialtyTO; import TO.PatientTO; import TO.PrimaryHealthCareCenterTO; import TO.SpecialistDoctorTO; +import common.HashUtils; import jpa.FamilyDoctorJPA; import jpa.MedicalSpecialtyJPA; import jpa.PatientJPA; @@ -45,7 +46,7 @@ public class ProfileFacadeBean implements ProfileFacadeRemote { public PatientTO registerPatient(Integer id, String nif, String name, String surname, String password, String email) { PatientTO paTO = null; - PatientJPA ms = new PatientJPA(id, nif, name, surname, password, email); + PatientJPA ms = new PatientJPA(id, nif, name, surname, HashUtils.hashMD5(password), email); entman.persist(ms); paTO = new PatientTO(ms.getId(), ms.getNif(), ms.getName(), ms.getSurname(), ms.getPassword(), ms.getEmail()); @@ -59,7 +60,7 @@ public class ProfileFacadeBean implements ProfileFacadeRemote { // TODO: Lanzar error si no se encuentra la especialidad. if (ms != null) { - SpecialistDoctorJPA sd = new SpecialistDoctorJPA(id, nif, name, surname, password, email); + SpecialistDoctorJPA sd = new SpecialistDoctorJPA(id, nif, name, surname, HashUtils.hashMD5(password), email); entman.persist(sd); sdTO = new SpecialistDoctorTO(sd.getId(), sd.getNif(), sd.getName(), sd.getSurname(), sd.getPassword(), sd.getEmail()); } @@ -75,7 +76,7 @@ public class ProfileFacadeBean implements ProfileFacadeRemote { // TODO: Lanzar error si no encontramos el cap!!!!! if (phcC != null) { - FamilyDoctorJPA fd = new FamilyDoctorJPA(id, nif, name, surname, password, email); + FamilyDoctorJPA fd = new FamilyDoctorJPA(id, nif, name, surname, HashUtils.hashMD5(password), email); fd.setPrimaryHealthCareCenter(phcC); entman.persist(fd); @@ -94,7 +95,7 @@ public class ProfileFacadeBean implements ProfileFacadeRemote { fd.setNif(nif); fd.setName(name); fd.setSurname(surname); - fd.setPassword(password); + fd.setPassword(HashUtils.hashMD5(password)); fd.setEmail(email); entman.persist(fd); @@ -117,7 +118,7 @@ public class ProfileFacadeBean implements ProfileFacadeRemote { sd.setNif(nif); sd.setName(name); sd.setSurname(surname); - sd.setPassword(password); + sd.setPassword(HashUtils.hashMD5(password)); sd.setEmail(email); sd.setMedicalSpecialty(ms); @@ -137,7 +138,7 @@ public class ProfileFacadeBean implements ProfileFacadeRemote { fd.setNif(nif); fd.setName(name); fd.setSurname(surname); - fd.setPassword(password); + fd.setPassword(HashUtils.hashMD5(password)); fd.setEmail(email); // TODO: Es posible actualizar el cap? ¿No debería utilizar el método diff --git a/1.sources/MyHealth/src/ejb/systemAdmin/SystemAdminFacadeBean.java b/1.sources/MyHealth/src/ejb/systemAdmin/SystemAdminFacadeBean.java index 255bca7..0bea789 100644 --- a/1.sources/MyHealth/src/ejb/systemAdmin/SystemAdminFacadeBean.java +++ b/1.sources/MyHealth/src/ejb/systemAdmin/SystemAdminFacadeBean.java @@ -8,10 +8,17 @@ import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; +import TO.LoggedUserTO; import TO.MedicalSpecialtyTO; import TO.PrimaryHealthCareCenterTO; +import common.HashUtils; +import jpa.AdministratorJPA; +import jpa.FamilyDoctorJPA; import jpa.MedicalSpecialtyJPA; +import jpa.PatientJPA; import jpa.PrimaryHealthCareCenterJPA; +import jpa.SpecialistDoctorJPA; +import managedbean.common.UserType; /** * EJB Session Bean Class para la Practica 2, Ejercicio 1 (ISCSD) Implementa los @@ -83,4 +90,44 @@ public class SystemAdminFacadeBean implements SystemAdminFacadeRemote { return allCAPs; } + + public LoggedUserTO login(String id, String pwd) { + LoggedUserTO usr = null; + + // First try to login as Admin + AdministratorJPA adm = entman.find(AdministratorJPA.class, id); + if (adm != null) { + usr = new LoggedUserTO(adm.getEmail(), "", adm.getPassword(), UserType.ADMINISTRADOR); + } else { + Integer iId = Integer.valueOf(id); + // Try to login Patient + PatientJPA pat = entman.find(PatientJPA.class, iId); + + if (pat != null) { + usr = new LoggedUserTO(pat.getId().toString(), pat.getName(), pat.getPassword(), UserType.PATIENT); + } else { + FamilyDoctorJPA fdoc = entman.find(FamilyDoctorJPA.class, iId); + + if (fdoc != null) { + usr = new LoggedUserTO(fdoc.getId().toString(), fdoc.getName(), fdoc.getPassword(), UserType.FAMILY_DOCTOR); + } else { + SpecialistDoctorJPA sdoc = entman.find(SpecialistDoctorJPA.class, iId); + + if (sdoc != null) { + usr = new LoggedUserTO(sdoc.getId().toString(), sdoc.getName(), sdoc.getPassword(), UserType.SPECIALIST_DOCTOR); + } + } + } + } + + if (usr != null) { + // Comprobamos el password + if (usr.getPassword().equals(HashUtils.hashMD5(pwd)) == false) { + // Bad Password, devolvemos null! + usr = null; + } + } + + return usr; + } } diff --git a/1.sources/MyHealth/src/ejb/systemAdmin/SystemAdminFacadeRemote.java b/1.sources/MyHealth/src/ejb/systemAdmin/SystemAdminFacadeRemote.java index 472581e..98e9c14 100644 --- a/1.sources/MyHealth/src/ejb/systemAdmin/SystemAdminFacadeRemote.java +++ b/1.sources/MyHealth/src/ejb/systemAdmin/SystemAdminFacadeRemote.java @@ -6,6 +6,7 @@ import javax.ejb.Remote; import TO.MedicalSpecialtyTO; import TO.PrimaryHealthCareCenterTO; +import TO.LoggedUserTO; /** * Interfaz remota del EJB Definimos los métodos que estarán disponibles para @@ -21,4 +22,5 @@ public interface SystemAdminFacadeRemote { */ public Collection listAllMedicalSpecialities(); public Collection listAllCAPs(); + public LoggedUserTO login(String id, String pwd); } \ No newline at end of file diff --git a/1.sources/MyHealth/src/jpa/AdministratorJPA.java b/1.sources/MyHealth/src/jpa/AdministratorJPA.java new file mode 100644 index 0000000..75dcf3f --- /dev/null +++ b/1.sources/MyHealth/src/jpa/AdministratorJPA.java @@ -0,0 +1,52 @@ +package jpa; + +import java.io.Serializable; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +/** + * + * @author Marcos García Núñez (mgarcianun@uoc.edu) + * + */ +@Entity +@Table(name = "MyHealth.Administrator") +public class AdministratorJPA implements Serializable { + + private static final long serialVersionUID = 1L; + + @Id + private String email; + private String password; + + /** + * Class constructor methods + */ + public AdministratorJPA() { + super(); + } + + public AdministratorJPA(String email, String password) { + this.email = email; + this.password = password; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + +} diff --git a/1.sources/MyHealth/src/jpa/FamilyDoctorJPA.java b/1.sources/MyHealth/src/jpa/FamilyDoctorJPA.java index d9621e0..7933390 100644 --- a/1.sources/MyHealth/src/jpa/FamilyDoctorJPA.java +++ b/1.sources/MyHealth/src/jpa/FamilyDoctorJPA.java @@ -123,6 +123,4 @@ public class FamilyDoctorJPA implements Serializable { public void setPrimaryHealthCareCenter(PrimaryHealthCareCenterJPA center) { this.primaryHealthCareCenter = center; } -} - - +} \ No newline at end of file