Cambios varios para login de usuario
* Login de diferentes usuarios completado. * Cambiado esquema de BBDD, campos texto a character varying. * Cambiada relación entre paciente y medico de cabecera. * Movida clase UserType a paquete common ya que se utiliza en el EJB. * Datos de prueba para realizar logins, el password es admin para todos (hashMD5).
This commit is contained in:
@@ -33,7 +33,7 @@
|
||||
</div>
|
||||
<hr />
|
||||
<div id="menuDiv">
|
||||
<p:growl id="messages" sticky="false" showDetail="true" life="3000" />
|
||||
<p:growl id="messages" sticky="false" showDetail="true" life="15000" />
|
||||
<p:ajaxStatus style="width:32px; height:32px; position:fixed; right:32px; bottom:32px">
|
||||
<f:facet name="start">
|
||||
<i id="loginSpin" class="pi pi-spin pi-spinner" style="font-size: 3em"></i>
|
||||
|
||||
@@ -1,13 +1,18 @@
|
||||
package TO;
|
||||
|
||||
import managedbean.common.UserType;
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
import common.UserType;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Marcos García Núñez (mgarcianun@uoc.edu)
|
||||
*
|
||||
*/
|
||||
public class LoggedUserTO {
|
||||
@XmlRootElement(name = "LoggedUser")
|
||||
public class LoggedUserTO implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private String id;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package managedbean.common;
|
||||
package common;
|
||||
|
||||
public enum UserType {
|
||||
PATIENT("Paciente"),
|
||||
@@ -12,20 +12,17 @@ import TO.LoggedUserTO;
|
||||
import TO.MedicalSpecialtyTO;
|
||||
import TO.PrimaryHealthCareCenterTO;
|
||||
import common.HashUtils;
|
||||
import common.UserType;
|
||||
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
|
||||
* métodos de la capa de negocio que implementan la logica de negocio y la
|
||||
* interacción con la capa de persistencia.
|
||||
*
|
||||
* @author mark
|
||||
* @author Marcos García Núñez (mgarcianun@uoc.edu)
|
||||
*
|
||||
*/
|
||||
@Stateless
|
||||
@@ -97,27 +94,37 @@ public class SystemAdminFacadeBean implements SystemAdminFacadeRemote {
|
||||
// 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);
|
||||
usr = new LoggedUserTO(adm.getEmail(), adm.getEmail(), adm.getPassword(), UserType.ADMINISTRADOR);
|
||||
} else {
|
||||
Integer iId = Integer.valueOf(id);
|
||||
// Try to login Patient
|
||||
PatientJPA pat = entman.find(PatientJPA.class, iId);
|
||||
try {
|
||||
int iId = Integer.parseInt(id);
|
||||
|
||||
if (pat != null) {
|
||||
usr = new LoggedUserTO(pat.getId().toString(), pat.getName(), pat.getPassword(), UserType.PATIENT);
|
||||
} else {
|
||||
FamilyDoctorJPA fdoc = entman.find(FamilyDoctorJPA.class, iId);
|
||||
// Try to login Patient, FamilyDoctor or SpecialistDoctor
|
||||
// TODO: Si Patient, FamilyDoctor o Specialist Doctor tienen el mismo id, solo
|
||||
// el paciente se puede logear. ¿Deberíamos cambiar esto permitiendo seleccionar
|
||||
// el perfil a logearse?
|
||||
PatientJPA pat = entman.find(PatientJPA.class, iId);
|
||||
|
||||
if (fdoc != null) {
|
||||
usr = new LoggedUserTO(fdoc.getId().toString(), fdoc.getName(), fdoc.getPassword(), UserType.FAMILY_DOCTOR);
|
||||
if (pat != null) {
|
||||
usr = new LoggedUserTO(String.valueOf(pat.getId()), pat.getName(), pat.getPassword(), UserType.PATIENT);
|
||||
} else {
|
||||
SpecialistDoctorJPA sdoc = entman.find(SpecialistDoctorJPA.class, iId);
|
||||
FamilyDoctorJPA fdoc = entman.find(FamilyDoctorJPA.class, iId);
|
||||
|
||||
if (sdoc != null) {
|
||||
usr = new LoggedUserTO(sdoc.getId().toString(), sdoc.getName(), sdoc.getPassword(), UserType.SPECIALIST_DOCTOR);
|
||||
if (fdoc != null) {
|
||||
usr = new LoggedUserTO(String.valueOf(fdoc.getId()), fdoc.getName(), fdoc.getPassword(), UserType.FAMILY_DOCTOR);
|
||||
} else {
|
||||
SpecialistDoctorJPA sdoc = entman.find(SpecialistDoctorJPA.class, iId);
|
||||
|
||||
if (sdoc != null) {
|
||||
usr = new LoggedUserTO(String.valueOf(sdoc.getId()), sdoc.getName(), sdoc.getPassword(), UserType.SPECIALIST_DOCTOR);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (NumberFormatException nf) {
|
||||
// id is not an intenger, so, login fails
|
||||
usr = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (usr != null) {
|
||||
|
||||
@@ -9,10 +9,8 @@ import TO.PrimaryHealthCareCenterTO;
|
||||
import TO.LoggedUserTO;
|
||||
|
||||
/**
|
||||
* Interfaz remota del EJB Definimos los métodos que estarán disponibles para
|
||||
* los clientes del EJB
|
||||
*
|
||||
* @author mark
|
||||
* @author Marcos García Núñez (mgarcianun@uoc.edu)
|
||||
*
|
||||
*/
|
||||
@Remote
|
||||
@@ -21,6 +19,8 @@ public interface SystemAdminFacadeRemote {
|
||||
* Definimos la interfaz remota
|
||||
*/
|
||||
public Collection<MedicalSpecialtyTO> listAllMedicalSpecialities();
|
||||
|
||||
public Collection<PrimaryHealthCareCenterTO> listAllCAPs();
|
||||
|
||||
public LoggedUserTO login(String id, String pwd);
|
||||
}
|
||||
@@ -26,7 +26,7 @@ public class FamilyDoctorJPA implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
private Integer id;
|
||||
private int id;
|
||||
private String nif;
|
||||
private String name;
|
||||
private String surname;
|
||||
@@ -42,7 +42,7 @@ public class FamilyDoctorJPA implements Serializable {
|
||||
super();
|
||||
}
|
||||
|
||||
public FamilyDoctorJPA(Integer id, String nif, String name, String surname, String password, String email) {
|
||||
public FamilyDoctorJPA(int id, String nif, String name, String surname, String password, String email) {
|
||||
this.id = id;
|
||||
this.nif = nif;
|
||||
this.name = name;
|
||||
@@ -53,11 +53,11 @@ public class FamilyDoctorJPA implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.IDENTITY)
|
||||
public Integer getId() {
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer value) {
|
||||
public void setId(int value) {
|
||||
this.id = value;
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ public class PatientJPA implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.IDENTITY)
|
||||
private Integer id;
|
||||
private int id;
|
||||
private String nif;
|
||||
private String name;
|
||||
private String surname;
|
||||
@@ -40,7 +40,7 @@ public class PatientJPA implements Serializable {
|
||||
super();
|
||||
}
|
||||
|
||||
public PatientJPA(Integer id, String nif, String name, String surname, String password, String email) {
|
||||
public PatientJPA(int id, String nif, String name, String surname, String password, String email) {
|
||||
this.id = id;
|
||||
this.nif = nif;
|
||||
this.name = name;
|
||||
@@ -49,11 +49,11 @@ public class PatientJPA implements Serializable {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer value) {
|
||||
public void setId(int value) {
|
||||
this.id = value;
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ public class SpecialistDoctorJPA implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
private Integer id;
|
||||
private int id;
|
||||
private String nif;
|
||||
private String name;
|
||||
private String surname;
|
||||
@@ -48,11 +48,11 @@ public class SpecialistDoctorJPA implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.IDENTITY)
|
||||
public Integer getId() {
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer value) {
|
||||
public void setId(int value) {
|
||||
this.id = value;
|
||||
}
|
||||
|
||||
|
||||
@@ -22,8 +22,8 @@ import TO.MedicalSpecialtyTO;
|
||||
import TO.PatientTO;
|
||||
import TO.PrimaryHealthCareCenterTO;
|
||||
import TO.SpecialistDoctorTO;
|
||||
import common.UserType;
|
||||
import ejb.systemAdmin.SystemAdminFacadeRemote;
|
||||
import managedbean.common.UserType;
|
||||
import managedbean.common.ValidationUtils;
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,19 +1,24 @@
|
||||
package managedbean.systemAdmin;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.faces.application.FacesMessage;
|
||||
import javax.faces.context.FacesContext;
|
||||
import javax.inject.Named;
|
||||
import javax.naming.Context;
|
||||
import javax.naming.InitialContext;
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
import org.primefaces.PrimeFaces;
|
||||
|
||||
import TO.LoggedUserTO;
|
||||
import ejb.systemAdmin.SystemAdminFacadeRemote;
|
||||
import managedbean.common.SessionUtils;
|
||||
|
||||
@Named("loginView")
|
||||
@RequestScoped
|
||||
public class LoginMBean {
|
||||
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
@@ -38,25 +43,35 @@ public class LoginMBean {
|
||||
FacesMessage message = null;
|
||||
boolean loggedIn = false;
|
||||
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
LoggedUserTO usr = null;
|
||||
|
||||
if (username != null && username.equals("admin") && password != null && password.equals("admin")) {
|
||||
loggedIn = true;
|
||||
HttpSession session = SessionUtils.getSession();
|
||||
session.setAttribute("username", username);
|
||||
session.setAttribute("userid", "1");
|
||||
message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Bienvenido", username);
|
||||
if (username != null && password != null) {
|
||||
try {
|
||||
Properties props = System.getProperties();
|
||||
Context ctx = new InitialContext(props);
|
||||
SystemAdminFacadeRemote remoteManager = (SystemAdminFacadeRemote) ctx.lookup("java:app/MyHealth.jar/SystemAdminFacadeBean!ejb.systemAdmin.SystemAdminFacadeRemote");
|
||||
|
||||
return ("home?faces-redirect=true");
|
||||
} else {
|
||||
loggedIn = false;
|
||||
usr = remoteManager.login(username, password);
|
||||
|
||||
if (usr != null) {
|
||||
loggedIn = true;
|
||||
HttpSession session = SessionUtils.getSession();
|
||||
session.setAttribute("username", usr.getName());
|
||||
session.setAttribute("userid", "1");
|
||||
session.setAttribute("userid", usr.getUserType());
|
||||
session.setAttribute("loggedInUser", usr);
|
||||
|
||||
message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Bienvenido", usr.getName());
|
||||
|
||||
return ("home?faces-redirect=true");
|
||||
} else
|
||||
message = new FacesMessage(FacesMessage.SEVERITY_WARN, "Loggin Error", "El usuario o la contraseña son incorrectos");
|
||||
} catch (Exception ex) {
|
||||
message = new FacesMessage(FacesMessage.SEVERITY_FATAL, "Error fatal", ex.getMessage());
|
||||
}
|
||||
|
||||
} else
|
||||
message = new FacesMessage(FacesMessage.SEVERITY_WARN, "Loggin Error", "El usuario o la contraseña son incorrectos");
|
||||
}
|
||||
|
||||
FacesContext.getCurrentInstance().addMessage(null, message);
|
||||
PrimeFaces.current().ajax().addCallbackParam("loggedIn", loggedIn);
|
||||
@@ -68,9 +83,10 @@ public class LoginMBean {
|
||||
public String logout() {
|
||||
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Sessión cerrada", "Ha cerrado correctament su ssesión. Hasta la vista");
|
||||
FacesContext.getCurrentInstance().addMessage(null, message);
|
||||
|
||||
|
||||
HttpSession session = SessionUtils.getSession();
|
||||
session.invalidate();
|
||||
|
||||
return "home?faces-redirect=true";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user