Si se actualiza el nombre del perfil, se refresca la sesión y el menú

para reflejar los cambios.
This commit is contained in:
Marcos Garcia Nuñez
2019-12-22 21:08:18 +01:00
parent ed1938f44a
commit 3555348f71
4 changed files with 26 additions and 15 deletions

View File

@@ -183,7 +183,7 @@
<div class="ui-g-12 ui-g-nopad"> <div class="ui-g-12 ui-g-nopad">
<div class="ui-g-4 ui-md-4"></div> <div class="ui-g-4 ui-md-4"></div>
<div class="ui-g-2 ui-md-2 "> <div class="ui-g-2 ui-md-2 ">
<p:commandButton validateClient="true" value="Guardar" update="frmUpdateProfile" action="#{UpdateProfile.saveData}" icon="pi pi-check" /> <p:commandButton validateClient="true" value="Guardar" update="frmUpdateProfile,frmMenu" action="#{UpdateProfile.saveData}" icon="pi pi-check" />
</div> </div>
<div class="ui-g-2 ui-md-2"> <div class="ui-g-2 ui-md-2">
<p:button value="Volver" outcome="/home" icon="pi pi-home" /> <p:button value="Volver" outcome="/home" icon="pi pi-home" />

View File

@@ -17,13 +17,17 @@ public class SessionUtils {
public static final String SESSION_VAR_USER = "loggedOnUser"; public static final String SESSION_VAR_USER = "loggedOnUser";
public static final String SESSION_VAR_MESSAGE = "facesMessage"; public static final String SESSION_VAR_MESSAGE = "facesMessage";
public static HttpSession getSession() { public static HttpSession getSession(boolean create) {
FacesContext ctx = FacesContext.getCurrentInstance(); FacesContext ctx = FacesContext.getCurrentInstance();
if (ctx != null) if (ctx != null)
return (HttpSession) ctx.getExternalContext().getSession(false); return (HttpSession) ctx.getExternalContext().getSession(create);
else else
return null; return null;
} }
public static HttpSession getSession() {
return getSession(false);
}
public static HttpServletRequest getRequest() { public static HttpServletRequest getRequest() {
return (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(); return (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
@@ -33,8 +37,8 @@ public class SessionUtils {
return ((HttpServletRequest) request).getSession(false); return ((HttpServletRequest) request).getSession(false);
} }
public static void CreateSession(LoggedUserTO usr) { public static void createOrUpdateSession(LoggedUserTO usr) {
HttpSession ses = getSession(); HttpSession ses = getSession(true);
ses.setAttribute(SessionUtils.SESSION_VAR_USERNAME, usr.getName()); ses.setAttribute(SessionUtils.SESSION_VAR_USERNAME, usr.getName());
ses.setAttribute(SessionUtils.SESSION_VAR_USERID, usr.getId()); ses.setAttribute(SessionUtils.SESSION_VAR_USERID, usr.getId());
ses.setAttribute(SessionUtils.SESSION_VAR_USERTYPE, usr.getUserType()); ses.setAttribute(SessionUtils.SESSION_VAR_USERTYPE, usr.getUserType());

View File

@@ -26,8 +26,7 @@ import managedbean.common.SessionUtils;
import managedbean.common.ValidationUtils; import managedbean.common.ValidationUtils;
/** /**
* ManagedBEan que gestiona el registro de usuarios: Usuarios de tipo "Paciente" * ManagedBEan que gestiona el registro de usuarios: Usuarios de tipo "Paciente" Usuarios de tipo "Médico de Familia" usuarios de tipo "Médico especialista"
* Usuarios de tipo "Médico de Familia" usuarios de tipo "Médico especialista"
* *
* @author Marcos García Núñez (mgarcianun@uoc.edu) * @author Marcos García Núñez (mgarcianun@uoc.edu)
* *
@@ -175,13 +174,13 @@ public class UpdateProfileMBean extends ManagedBeanBase implements Serializable
public void hadleNIFValueChange() { public void hadleNIFValueChange() {
boolean isDupe = false; boolean isDupe = false;
if (ValidationUtils.checkIfNifAlreadyRegistered(this.getRemoteManagerCommon(), this.userType, this.nif, this.id) == true) { 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"); 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<FamilyDoctorTO> getFamilyDoctorList() { public List<FamilyDoctorTO> getFamilyDoctorList() {
return familyDoctorList; return familyDoctorList;
} }
@@ -257,11 +256,11 @@ public class UpdateProfileMBean extends ManagedBeanBase implements Serializable
public void saveData() { public void saveData() {
int error = 0; int error = 0;
// Si no hay tipo de usuario, es que algo raro ha pasado (sesión caducada?). salimos. // Si no hay tipo de usuario, es que algo raro ha pasado (sesión caducada?). salimos.
if (this.userType == null) if (this.userType == null)
return; return;
boolean changePassword = (this.oldPassword != null && this.oldPassword.equals("") == false) || (this.password != null && this.password.equals("") == false); boolean changePassword = (this.oldPassword != null && this.oldPassword.equals("") == false) || (this.password != null && this.password.equals("") == false);
if (this.isUserTypeFamilyDoctor() && this.primaryHealthCareCenter == null) { 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) { 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"); this.addFacesMessage("frmUpdateProfile:nif", FacesMessage.SEVERITY_WARN, "NIF duplicado", "El nif indicado pertenece a otro usuario previamente registrado");
error++; error++;
} }
if (changePassword == true) { if (changePassword == true) {
// el usuario queire cambiar el password Comprobamos que el password // el usuario queire cambiar el password Comprobamos que el password
// especificado coincide con el guardado // especificado coincide con el guardado
@@ -295,20 +294,25 @@ public class UpdateProfileMBean extends ManagedBeanBase implements Serializable
if (error == 0) { if (error == 0) {
try { try {
LoggedUserTO usr = null;
switch (this.userType) { switch (this.userType) {
case PATIENT: case PATIENT:
PatientTO pat = this.getRemoteManagerProfile().updatePatientData(id, nif, name, surname, password, email); PatientTO pat = this.getRemoteManagerProfile().updatePatientData(id, nif, name, surname, password, email);
this.setPatientData(pat); this.setPatientData(pat);
usr = new LoggedUserTO(pat.getId().toString(), pat.getName(), pat.getPassword(), this.userType);
break; break;
case FAMILY_DOCTOR: case FAMILY_DOCTOR:
FamilyDoctorTO fd = this.getRemoteManagerProfile().updateFamilyDoctorData(id, nif, name, surname, password, email, this.primaryHealthCareCenter); FamilyDoctorTO fd = this.getRemoteManagerProfile().updateFamilyDoctorData(id, nif, name, surname, password, email, this.primaryHealthCareCenter);
this.setFamilyDoctorData(fd); this.setFamilyDoctorData(fd);
usr = new LoggedUserTO(fd.getId().toString(), fd.getName(), fd.getPassword(), this.userType);
break; break;
case SPECIALIST_DOCTOR: case SPECIALIST_DOCTOR:
SpecialistDoctorTO sd = this.getRemoteManagerProfile().updateSpecialistDoctorData(id, nif, name, surname, password, email, this.medicalSpecialty); SpecialistDoctorTO sd = this.getRemoteManagerProfile().updateSpecialistDoctorData(id, nif, name, surname, password, email, this.medicalSpecialty);
this.setSpecialistDoctorData(sd); this.setSpecialistDoctorData(sd);
usr = new LoggedUserTO(sd.getId().toString(), sd.getName(), sd.getPassword(), this.userType);
break; break;
case ADMINISTRATOR: case ADMINISTRATOR:
@@ -321,6 +325,9 @@ public class UpdateProfileMBean extends ManagedBeanBase implements Serializable
this.oldPassword = ""; 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."); this.addFacesMessage(FacesMessage.SEVERITY_INFO, "Los datos se han guardado", "Los datos de su perfil se han guardado correctamente.");
} catch (Exception e) { } catch (Exception e) {
this.manageException(e); this.manageException(e);

View File

@@ -45,7 +45,7 @@ public class LoginMBean extends ManagedBeanBase {
if (usr != null) { if (usr != null) {
loggedIn = true; loggedIn = true;
SessionUtils.CreateSession(usr); SessionUtils.createOrUpdateSession(usr);
this.addFacesMessageKeep(FacesMessage.SEVERITY_INFO, "Login correcto", "Bienvenido " + usr.getName()); this.addFacesMessageKeep(FacesMessage.SEVERITY_INFO, "Login correcto", "Bienvenido " + usr.getName());
viewRedirect = "/home?faces-redirect=true"; viewRedirect = "/home?faces-redirect=true";