git-svn-id: https://svn.coded.pt/svn/SIPRP@1541 bb69d46d-e84e-40c8-a05a-06db0d633741

lxbfYeaa
Diogo Neves 15 years ago
parent d4c7594a3c
commit f5876eb452

@ -7,13 +7,19 @@ package phaselistener;
import com.evolute.utils.error.ErrorLogger;
import db.entidades.Utilizador;
import db.providers.UtilizadoresDataProvider;
import global.Global;
import java.util.HashMap;
import java.util.Map;
import javax.faces.component.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import planosactuacao.SessionBean1;
import utils.JSFHTTPUtils;
import utils.JSFUtils;
@ -37,110 +43,218 @@ public class MainPhaseListener implements PhaseListener
}
}
public void beforePhase( PhaseEvent pe )
public void beforePhase(PhaseEvent event)
{
if ( pe.getPhaseId() == PhaseId.RENDER_RESPONSE )
{
System.out.println( "beforePhase() : " );
FacesContext fc = pe.getFacesContext();
SessionBean1 session = JSFUtils.getSessionBean( fc );
String referer = JSFHTTPUtils.getReferer( pe.getFacesContext() );
System.out.println( "\tReferer : " + referer );
HttpServletRequest req = ( HttpServletRequest ) fc.getExternalContext().getRequest();
boolean sessionValid = req.isRequestedSessionIdValid();
System.out.println( "\tSessionValid : " + sessionValid );
if ( event.getPhaseId() == PhaseId.RENDER_RESPONSE )
{
FacesContext fc = event.getFacesContext();
UIViewRoot view = fc.getViewRoot();
System.out.println( "\tView : " + view );
String viewID = view.getViewId();
System.out.println( "\t\tView ID :" + viewID );
boolean loggedIn = session.isLoggedIn();
System.out.println( "\tLogged In : " + loggedIn );
if ( sessionValid && loggedIn )
ExternalContext ec = fc.getExternalContext();
try
{
System.out.println( "\t---> session IS valid && user IS logged in." );
SessionBean1 session = JSFUtils.getSessionBean( fc );
String referer = JSFHTTPUtils.getReferer( fc );
HttpServletRequest req = ( HttpServletRequest ) ec.getRequest();
//disable/enable EditarPlano menu ???
session.setRenderTopMenuImages( true );
session.setRenderSubHeader( false );
if ( referer != null && referer.contains( "/AnaliseAcidentesTrabalho/" ) )
{
HttpSession reqSession = ( HttpSession ) ec.getSession( false );
ServletContext paContext = reqSession.getServletContext();
ServletContext aatContext = paContext.getContext( "/AnaliseAcidentesTrabalho" );
String sessionID = req.getParameter( "sessionID" );
Utilizador currentUser = session.getCurrentUser();
int userType = currentUser == null ? -1 : currentUser.getTipo() == null ? -1 : currentUser.getTipo().intValue();
if ( viewID.matches( "/EditarPlano.jsp" ) || viewID.matches( "/EnviarCorrecao.jsp" ) || viewID.matches( "/ViewPlano.jsp" ) )
{
session.setDisableMenu( false );
session.setDisableMenuPlanos( false );
if ( viewID.matches( "/ViewPlano.jsp" ) )
Map< String, Object > paData = new HashMap< String, Object >();
paData.put( "session", reqSession );
paContext.setAttribute( sessionID, paData );
Map< String, Object > mapData = ( Map< String, Object > ) aatContext.getAttribute( sessionID );
Integer userID = ( Integer ) mapData.get( "userID" );
if ( userID != null )
{
session.setDisableMenuUtilizadores( userType != Global.DIRECTOR_SIPRP );
session.setDisableMenuDadosUtilizador( false );
session.setRenderTopMenuImages( false );
session.setRenderSubHeader( true );
Utilizador currentUser = UtilizadoresDataProvider.getInstance().getUtilizador( userID );
session.setCurrentUser( currentUser );
session.setLoggedIn( true );
session.setAatSessionID( sessionID );
setupPages( fc, session, viewID );
}
else if ( viewID.matches( "/EditarPlano.jsp" ) )
else
{
session.setDisableMenuPlanos( false );
session.setRenderTopMenuImages( false );
session.setRenderSubHeader( true );
gotoLogin( fc, viewID );
}
}
else
{
session.setDisableMenu(false);
if ( viewID.matches( "/ListaPlanos.jsp" ) )
if ( session.isLoggedIn() && session.getCurrentUser() != null )
{
setupPages( fc, session, viewID );
}
else
{
if ( userType == Global.RH || userType == Global.GESTOR )
{
session.setDisableMenuUtilizadores( true );
}
session.setDisableMenuPlanos( true );
gotoLogin( fc, viewID );
}
}
//insert logos
Integer empresaId = currentUser != null ? currentUser.getEmpresa_id() : null;
System.out.println("\tLogo EmpresaID B : " + ( empresaId != null ? empresaId.toString() : null ) );
Logos logos = new Logos();
String logourl = logos.getLogo( fc, empresaId );
session.setLogoEmpresaUrl( logourl );
System.out.println("\t\tURL : " + logourl );
}
else
catch ( Exception e )
{
System.out.println( "\t---> session is NOT valid OR user is NOT logged in." );
System.out.println( "\t\tSession : " + sessionValid );
System.out.println( "\t\tLogged : " + loggedIn );
gotoLogin( fc, viewID );
}
}
}
if ( viewID.compareTo( "/Login.jsp" ) != 0 )
{
System.out.println( "\t\tRedirecting to login page ..." );
try
{
JSFUtils.redirect( fc.getCurrentInstance(), "faces/Login.jsp" );
}
catch ( Exception e )
{
ErrorLogger.logException( e );
System.out.println( "\n\t\t\tERROR REDIRECTING TO LOGIN !" );
}
}
else
{
System.out.println( "\t\tDisplaying Login.jsp . all ok !" );
}
}
}
}
// public void beforePhase( PhaseEvent pe )
// {
// if ( pe.getPhaseId() == PhaseId.RENDER_RESPONSE )
// {
// System.out.println( "beforePhase() : " );
//
// FacesContext fc = pe.getFacesContext();
// SessionBean1 session = JSFUtils.getSessionBean( fc );
// String referer = JSFHTTPUtils.getReferer( pe.getFacesContext() );
// System.out.println( "\tReferer : " + referer );
//
// UIViewRoot view = fc.getViewRoot();
// System.out.println( "\tView : " + view );
// String viewID = view.getViewId();
// System.out.println( "\t\tView ID :" + viewID );
//
//
// if ( referer != null && referer.contains( "/AnaliseAcidentesTrabalho/" ) )
// {
//
// ExternalContext ec = fc.getExternalContext();
// if ( ec != null )
// {
// HttpSession reqSession = ( HttpSession ) ec.getSession( false );
// System.out.println( "\treqSession : " + reqSession );
//
// ServletContext thisContext = reqSession.getServletContext();
// System.out.println( "\tthisContext : " + thisContext );
//
// ServletContext otherContext = thisContext.getContext( "/AnaliseAcidentesTrabalho" );
// System.out.println( "\totherContext : " + otherContext );
//
// String sessionID = ( String ) otherContext.getAttribute( "sessionID" );
// Map< String, Object > mapData = ( Map< String, Object > ) otherContext.getAttribute( sessionID );
// Integer userID = ( Integer ) mapData.get( "userID" );
//
// System.out.println( "\n\tsessionID : " + sessionID );
// System.out.println( "\tuserID : " + userID );
//
// if ( userID != null )
// {
// try
// {
// Utilizador currentUser = UtilizadoresDataProvider.getInstance().getUtilizador( userID );
// session.setCurrentUser( currentUser );
//
// setupPages( fc, session, viewID );
// }
// catch ( Exception e )
// {
// ErrorLogger.logException( e );
// gotoLogin( fc, viewID );
// }
// }
// else
// {
// gotoLogin( fc, viewID );
// }
// }
// }
// else
// {
//
// HttpServletRequest req = ( HttpServletRequest ) fc.getExternalContext().getRequest();
// boolean sessionValid = req.isRequestedSessionIdValid();
// System.out.println( "\tSessionValid : " + sessionValid );
//
// boolean loggedIn = session.isLoggedIn();
// System.out.println( "\tLogged In : " + loggedIn );
//
// if ( sessionValid && loggedIn )
// {
// System.out.println( "\t---> session IS valid && user IS logged in." );
//
// setupPages( fc, session, viewID );
// }
// else
// {
// gotoLogin( fc, viewID );
// }
// }
// }
// }
public PhaseId getPhaseId()
{
return PhaseId.ANY_PHASE;
}
private void gotoLogin( FacesContext fc, String viewID )
{
if ( viewID.compareTo( "/Login.jsp" ) != 0 )
{
try
{
String url = Global.ANALISE_ACIDENTES_URL + "Login.jsp";
JSFUtils.redirect( fc, url );
}
catch ( Exception e )
{
ErrorLogger.logException( e );
System.out.println( "\n\t\t\tERROR REDIRECTING TO LOGIN !" );
}
}
}
private void setupPages( FacesContext fc, SessionBean1 session, String viewID )
{
//disable/enable EditarPlano menu ???
session.setRenderTopMenuImages( true );
session.setRenderSubHeader( false );
Utilizador currentUser = session.getCurrentUser();
int userType = currentUser == null ? -1 : currentUser.getTipo() == null ? -1 : currentUser.getTipo().intValue();
if ( viewID.matches( "/EditarPlano.jsp" ) || viewID.matches( "/EnviarCorrecao.jsp" ) || viewID.matches( "/ViewPlano.jsp" ) )
{
session.setDisableMenu( false );
session.setDisableMenuPlanos( false );
if ( viewID.matches( "/ViewPlano.jsp" ) )
{
session.setDisableMenuUtilizadores( userType != Global.DIRECTOR_SIPRP );
session.setDisableMenuDadosUtilizador( false );
session.setRenderTopMenuImages( false );
session.setRenderSubHeader( true );
}
else if ( viewID.matches( "/EditarPlano.jsp" ) )
{
session.setDisableMenuPlanos( false );
session.setRenderTopMenuImages( false );
session.setRenderSubHeader( true );
}
}
else
{
session.setDisableMenu(false);
if ( viewID.matches( "/ListaPlanos.jsp" ) )
{
if ( userType == Global.RH || userType == Global.GESTOR )
{
session.setDisableMenuUtilizadores( true );
}
session.setDisableMenuPlanos( true );
}
}
//insert logos
Integer empresaId = currentUser != null ? currentUser.getEmpresa_id() : null;
Logos logos = new Logos();
String logourl = logos.getLogo( fc, empresaId );
session.setLogoEmpresaUrl( logourl );
}
}

@ -0,0 +1,33 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package phaselistener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
*
* @author dneves
*/
public class SessionListener implements HttpSessionListener
{
@Override
public void sessionCreated( HttpSessionEvent se )
{
HttpSession session = se.getSession();
System.out.println( "PlanosActuacao : new Session Created : " + session.getId() );
}
@Override
public void sessionDestroyed( HttpSessionEvent se )
{
HttpSession session = se.getSession();
System.out.println( "PlanosActuacao : session destroyed : " + session.getId() );
}
}

@ -34,7 +34,8 @@ public class SessionBean1 extends AbstractSessionBean {
private boolean loggedIn = false;
private Utilizador currentUser = null;
private String msg;
private String aatSessionID = null;
private Integer planoId;
private PlanoActuacao planoActuacao = null;
private Risco currentRisco = null;
@ -392,5 +393,12 @@ public class SessionBean1 extends AbstractSessionBean {
this.isSearchingPlanosActuacao = isSearchingPlanosActuacao;
}
public String getAatSessionID() {
return aatSessionID;
}
public void setAatSessionID(String aatSessionID) {
this.aatSessionID = aatSessionID;
}
}

@ -83,9 +83,12 @@ public class JSFUtils {
public static void logout(FacesContext fc) throws Exception
{
ExternalContext ectx = fc.getCurrentInstance().getExternalContext();
HttpSession session = (HttpSession) ectx.getSession(false);
Utils.doLogout( fc );
HttpSession session = (HttpSession) ectx.getSession(false);
session.invalidate();
Utils.doLogout();
}
public static void navigateTo(FacesContext fc, String outcome)

@ -8,6 +8,12 @@ package utils;
import com.evolute.utils.Singleton;
import com.evolute.utils.error.ErrorLogger;
import db.entidades.Utilizador;
import java.util.Map;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import planosactuacao.SessionBean1;
/**
*
@ -97,9 +103,27 @@ public class Utils {
}
}
public static void doLogout()
public static void doLogout( FacesContext fc )
{
Singleton.setInstance( Singleton.USERNAME, null );
SessionBean1 sessionBean = JSFUtils.getSessionBean( fc );
sessionBean.setCurrentUser( null );
sessionBean.setLoggedIn( false );
String aatSessionID = sessionBean.getAatSessionID();
ExternalContext ec = fc.getExternalContext();
HttpSession session = ( HttpSession ) ec.getSession( false );
ServletContext paContext = session.getServletContext();
ServletContext aatContext = paContext.getContext( "/AnaliseAcidentesTrabalho" );
Map< String, Object > mapData = ( Map< String, Object > ) aatContext.getAttribute( aatSessionID );
if ( mapData != null )
{
HttpSession aatSession = ( HttpSession ) mapData.get( "session" );
aatSession.invalidate();
}
}
}

@ -105,6 +105,9 @@
<listener>
<listener-class>phaselistener.ContextListener</listener-class>
</listener>
<listener>
<listener-class>phaselistener.SessionListener</listener-class>
</listener>
<!-- <security-constraint>
<web-resource-collection>

Loading…
Cancel
Save