--- Revision None +++ Revision 313765356261 @@ -0,0 +1,194 @@ +faces-config.xml: + + + login utente + logIn + autoshop.web.LogIn + request + + username + #{username} + + + password + #{password} + + + + + bean per la sessione + sessionBean + autoshop.web.SessionBean + session + + username + #{username} + + + isLoggato + #{isLoggato} + + + + ..... + + +logIn.java: + +package autoshop.web; + +import autoshop.ejb.facade.EjbUtenteFacade; +import autoshop.jpa.dominio.Utente; +import javax.faces.context.FacesContext; +import javax.naming.InitialContext; + +/** + * + * @author mill + */ +public class LogIn{ + + private String username; + private String password; + + + public String getUsername(){ + return this.username; + } + + public void setUsername(String username){ + this.username=username; + } + + public String getPassword(){ + return this.password; + } + + public void setPassword(String password){ + this.password=password; + } + + + public String loginAction(){ + String result=""; + EjbUtenteFacade euf=getEjbUtenteFacade(); + Utente utente=euf.effettuaLogin(this.getUsername(),this.getPassword()); + if(utente!=null){ + this.sessionUtil(); + result="success"; + } + else + result="error"; + return result; + } + + private EjbUtenteFacade getEjbUtenteFacade(){ + try{ + InitialContext ctx = new InitialContext(); + return (EjbUtenteFacade)ctx.lookup("AutoShop/EjbUtenteFacadeImpl/remote"); + }catch(Exception e){ + return null; + } + } + + private void sessionUtil(){ + FacesContext context = FacesContext.getCurrentInstance(); + SessionBean sessionBean = (SessionBean)context.getApplication().evaluateExpressionGet(context,"#{sessionBean}",SessionBean.class); + sessionBean.setUsername(this.getUsername()); + sessionBean.setIsLoggato(true); + } + +} + + +SessionBean.java + +package autoshop.web; + +/** + * + * @author mill + */ +public class SessionBean { + + private String username; + private boolean isLoggato; + + + public String getUsername(){ + return username; + } + + public void setUsername(String username){ + this.username=username; + } + + public boolean getIsLoggato(){ + return this.isLoggato; + } + + public void setIsLoggato(boolean isLoggato){ + this.isLoggato=isLoggato; + } + +} + + +SessionUtility.java + +package autoshop.utility; + +import autoshop.web.SessionBean; +import javax.faces.context.FacesContext; + +/** + * + * @author mill + */ +public class SessionUtility { + + public static SessionBean getSessionBean(){ + return (SessionBean)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("sessionBean"); + } + +} + + +ControlloPermessiListener.java + +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package autoshop.utility; + +import javax.faces.application.NavigationHandler; +import javax.faces.context.FacesContext; +import javax.faces.event.PhaseEvent; +import javax.faces.event.PhaseId; +import javax.faces.event.PhaseListener; + + +public class ControlloPermessiListener implements PhaseListener { + + public void afterPhase(PhaseEvent event) { + FacesContext fc = event.getFacesContext(); + //controllo se sono nella pagina di login + boolean loginpage = fc.getViewRoot().getViewId().lastIndexOf("logIn") > -1 ? true : false; + //se non sono nella pagina di login e se non sono loggato + if (!loginpage && !SessionUtility.getSessionBean().getIsLoggato()) { + NavigationHandler nh = fc.getApplication().getNavigationHandler(); + nh.handleNavigation(fc, null, "logout"); + } + } + + public void beforePhase(PhaseEvent event) { + } + + public PhaseId getPhaseId() { + return PhaseId.RESTORE_VIEW; + } +} + +