Incluidas clases para permitir el login de diferentes tipos de usuaario.

* POJO para usuario logeado.
* Hash para claves de usuarios
* Métodos EJB para login
This commit is contained in:
mgarcianun
2019-12-06 00:18:20 +01:00
parent 96c514f8a5
commit 32ce0a7eb6
8 changed files with 218 additions and 14 deletions

View File

@@ -11,8 +11,9 @@
<property name="buildjar" value="${build}/jar" />
<property name="buildwar" value="${build}/war" />
<property name="dist" value="${source}/dist" />
<property name="docroot" value="${source}/docroot" />
<property name="jboss-config" value="default" />
<property name="deploy" value="${jboss.home}\standalone\deployments" />
<property name="deploy" value="${jboss.home}/standalone/deployments" />
<property name="jboss.module.dir" value="${jboss.home}/modules" />
<path id="jboss.classpath">
@@ -20,6 +21,11 @@
<include name="**/*.jar" />
</fileset>
</path>
<path id="lib.dir">
<fileset dir="${docroot}/WEB-INF/lib">
<include name="**/*.jar" />
</fileset>
</path>
<target name="all" depends="clean, init, ear" />
@@ -33,6 +39,7 @@
<mkdir dir="${buildwar}" />
<mkdir dir="${buildjar}/META-INF" />
<mkdir dir="${buildwar}/WEB-INF" />
<mkdir dir="${buildwar}/WEB-INF/lib" />
<mkdir dir="${buildwar}/WEB-INF/classes" />
<mkdir dir="${dist}" />
</target>
@@ -52,13 +59,23 @@
<!-- Compile the client application, creating the structure buildwar -->
<target name="compileWar" depends="init">
<copy todir="${buildwar}">
<fileset dir="${source}/docroot" />
<fileset dir="${docroot}" />
</copy>
<javac srcdir="${sourcesrc}" destdir="${buildwar}/WEB-INF/classes" includes="managedbean/*.java" classpathref="jboss.classpath" includeantruntime="true" />
<javac srcdir="${sourcesrc}" destdir="${buildwar}/WEB-INF/classes" includes="managedbean/**/*.java" includeantruntime="true">
<classpath>
<path refid="jboss.classpath" />
<path refid="lib.dir" />
</classpath>
</javac>
<delete verbose="true" dir="${buildwar}/WEB-INF/classes/ejb" />
<delete verbose="true" dir="${buildwar}/WEB-INF/classes/jpa" />
<delete verbose="true" dir="${buildwar}/WEB-INF/classes/TO" />
</target>
<!-- <war destfile="${build.dir}/CrunchifyRESTJerseyExample.war" webxml="WebContent/WEB-INF/web.xml"> -->
<!-- <classes dir="${build.dir}" /> -->
<!-- <lib dir="${lib.dir}"> -->
<!-- </lib> -->
<!-- </war> -->
<!-- Update the WAR file and create if not exist -->
<target name="deployWar" depends="compileWar">
@@ -83,10 +100,11 @@
<fileset dir="${dist}" includes="**/*" />
<fileset dir="${build}" includes="**/*" />
<fileset dir="${buildjar}" includes="**/*" />
<fileset dir="${buildwar}" includes="**/*" />
<fileset dir="${buildjar}/META-INF" includes="**/*" />
<fileset dir="${buildwar}/WEB-INF" includes="**/*" />
<fileset dir="${buildwar}/WEB-INF/lib" includes="**/*" />
<fileset dir="${buildwar}/WEB-INF/classes" includes="**/*" />
<fileset dir="${buildwar}/WEB-INF" includes="**/*" />
<fileset dir="${buildwar}" includes="**/*" />
</delete>
</target>
</project>

View File

@@ -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;
}
}

View File

@@ -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());
}
}

View File

@@ -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

View File

@@ -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;
}
}

View File

@@ -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<MedicalSpecialtyTO> listAllMedicalSpecialities();
public Collection<PrimaryHealthCareCenterTO> listAllCAPs();
public LoggedUserTO login(String id, String pwd);
}

View File

@@ -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;
}
}

View File

@@ -124,5 +124,3 @@ public class FamilyDoctorJPA implements Serializable {
this.primaryHealthCareCenter = center;
}
}