From 3555348f718dd039d364f955449ffe24aac04726 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garcia=20Nu=C3=B1ez?= Date: Sun, 22 Dec 2019 21:08:18 +0100 Subject: [PATCH] =?UTF-8?q?Si=20se=20actualiza=20el=20nombre=20del=20perfi?= =?UTF-8?q?l,=20se=20refresca=20la=20sesi=C3=B3n=20y=20el=20men=C3=BA=20pa?= =?UTF-8?q?ra=20reflejar=20los=20cambios.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../docroot/profile/UpdateProfile.xhtml | 2 +- .../src/managedbean/common/SessionUtils.java | 12 ++++++--- .../profile/UpdateProfileMBean.java | 25 ++++++++++++------- .../managedbean/systemAdmin/LoginMBean.java | 2 +- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/1.sources/MyHealth/docroot/profile/UpdateProfile.xhtml b/1.sources/MyHealth/docroot/profile/UpdateProfile.xhtml index d160518..b0cdb17 100644 --- a/1.sources/MyHealth/docroot/profile/UpdateProfile.xhtml +++ b/1.sources/MyHealth/docroot/profile/UpdateProfile.xhtml @@ -183,7 +183,7 @@
- +
diff --git a/1.sources/MyHealth/src/managedbean/common/SessionUtils.java b/1.sources/MyHealth/src/managedbean/common/SessionUtils.java index 0aa178f..3fb14dc 100644 --- a/1.sources/MyHealth/src/managedbean/common/SessionUtils.java +++ b/1.sources/MyHealth/src/managedbean/common/SessionUtils.java @@ -17,13 +17,17 @@ public class SessionUtils { public static final String SESSION_VAR_USER = "loggedOnUser"; public static final String SESSION_VAR_MESSAGE = "facesMessage"; - public static HttpSession getSession() { + public static HttpSession getSession(boolean create) { FacesContext ctx = FacesContext.getCurrentInstance(); if (ctx != null) - return (HttpSession) ctx.getExternalContext().getSession(false); + return (HttpSession) ctx.getExternalContext().getSession(create); else return null; } + + public static HttpSession getSession() { + return getSession(false); + } public static HttpServletRequest getRequest() { return (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(); @@ -33,8 +37,8 @@ public class SessionUtils { return ((HttpServletRequest) request).getSession(false); } - public static void CreateSession(LoggedUserTO usr) { - HttpSession ses = getSession(); + public static void createOrUpdateSession(LoggedUserTO usr) { + HttpSession ses = getSession(true); ses.setAttribute(SessionUtils.SESSION_VAR_USERNAME, usr.getName()); ses.setAttribute(SessionUtils.SESSION_VAR_USERID, usr.getId()); ses.setAttribute(SessionUtils.SESSION_VAR_USERTYPE, usr.getUserType()); diff --git a/1.sources/MyHealth/src/managedbean/profile/UpdateProfileMBean.java b/1.sources/MyHealth/src/managedbean/profile/UpdateProfileMBean.java index b7dc4c7..be48f9c 100644 --- a/1.sources/MyHealth/src/managedbean/profile/UpdateProfileMBean.java +++ b/1.sources/MyHealth/src/managedbean/profile/UpdateProfileMBean.java @@ -26,8 +26,7 @@ import managedbean.common.SessionUtils; import managedbean.common.ValidationUtils; /** - * ManagedBEan que gestiona el registro de usuarios: Usuarios de tipo "Paciente" - * Usuarios de tipo "Médico de Familia" usuarios de tipo "Médico especialista" + * ManagedBEan que gestiona el registro de usuarios: Usuarios de tipo "Paciente" Usuarios de tipo "Médico de Familia" usuarios de tipo "Médico especialista" * * @author Marcos García Núñez (mgarcianun@uoc.edu) * @@ -175,13 +174,13 @@ public class UpdateProfileMBean extends ManagedBeanBase implements Serializable public void hadleNIFValueChange() { boolean isDupe = false; if (ValidationUtils.checkIfNifAlreadyRegistered(this.getRemoteManagerCommon(), this.userType, this.nif, this.id) == true) { - isDupe = true; + isDupe = true; this.addFacesMessage("frmUpdateProfile:nif", FacesMessage.SEVERITY_WARN, "NIF duplicado", "El nif indicado pertenece a otro usuario previamente registrado"); } - - PrimeFaces.current().ajax().addCallbackParam("NIFisDupe", isDupe); + + PrimeFaces.current().ajax().addCallbackParam("NIFisDupe", isDupe); } - + public List getFamilyDoctorList() { return familyDoctorList; } @@ -257,11 +256,11 @@ public class UpdateProfileMBean extends ManagedBeanBase implements Serializable public void saveData() { int error = 0; - + // Si no hay tipo de usuario, es que algo raro ha pasado (sesión caducada?). salimos. if (this.userType == null) return; - + boolean changePassword = (this.oldPassword != null && this.oldPassword.equals("") == false) || (this.password != null && this.password.equals("") == false); if (this.isUserTypeFamilyDoctor() && this.primaryHealthCareCenter == null) { @@ -279,7 +278,7 @@ public class UpdateProfileMBean extends ManagedBeanBase implements Serializable if (ValidationUtils.checkIfNifAlreadyRegistered(this.getRemoteManagerCommon(), this.userType, this.nif, this.id) == true) { this.addFacesMessage("frmUpdateProfile:nif", FacesMessage.SEVERITY_WARN, "NIF duplicado", "El nif indicado pertenece a otro usuario previamente registrado"); error++; - } + } if (changePassword == true) { // el usuario queire cambiar el password Comprobamos que el password // especificado coincide con el guardado @@ -295,20 +294,25 @@ public class UpdateProfileMBean extends ManagedBeanBase implements Serializable if (error == 0) { try { + LoggedUserTO usr = null; + switch (this.userType) { case PATIENT: PatientTO pat = this.getRemoteManagerProfile().updatePatientData(id, nif, name, surname, password, email); this.setPatientData(pat); + usr = new LoggedUserTO(pat.getId().toString(), pat.getName(), pat.getPassword(), this.userType); break; case FAMILY_DOCTOR: FamilyDoctorTO fd = this.getRemoteManagerProfile().updateFamilyDoctorData(id, nif, name, surname, password, email, this.primaryHealthCareCenter); this.setFamilyDoctorData(fd); + usr = new LoggedUserTO(fd.getId().toString(), fd.getName(), fd.getPassword(), this.userType); break; case SPECIALIST_DOCTOR: SpecialistDoctorTO sd = this.getRemoteManagerProfile().updateSpecialistDoctorData(id, nif, name, surname, password, email, this.medicalSpecialty); this.setSpecialistDoctorData(sd); + usr = new LoggedUserTO(sd.getId().toString(), sd.getName(), sd.getPassword(), this.userType); break; case ADMINISTRATOR: @@ -321,6 +325,9 @@ public class UpdateProfileMBean extends ManagedBeanBase implements Serializable this.oldPassword = ""; } + // Actualizamos la sessión del usuario actual por si ha cambiado el nombre de usuario. + SessionUtils.createOrUpdateSession(usr); + this.addFacesMessage(FacesMessage.SEVERITY_INFO, "Los datos se han guardado", "Los datos de su perfil se han guardado correctamente."); } catch (Exception e) { this.manageException(e); diff --git a/1.sources/MyHealth/src/managedbean/systemAdmin/LoginMBean.java b/1.sources/MyHealth/src/managedbean/systemAdmin/LoginMBean.java index 5674f25..06a7f01 100644 --- a/1.sources/MyHealth/src/managedbean/systemAdmin/LoginMBean.java +++ b/1.sources/MyHealth/src/managedbean/systemAdmin/LoginMBean.java @@ -45,7 +45,7 @@ public class LoginMBean extends ManagedBeanBase { if (usr != null) { loggedIn = true; - SessionUtils.CreateSession(usr); + SessionUtils.createOrUpdateSession(usr); this.addFacesMessageKeep(FacesMessage.SEVERITY_INFO, "Login correcto", "Bienvenido " + usr.getName()); viewRedirect = "/home?faces-redirect=true";