1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| /**
*
* @author Stef
*/
public class LoginPhaseListener implements PhaseListener
{
public static final String VIEWID_LOGIN = "/login/LoginPage.jsp";
public final HashSet<String> adminOnlyViews = new HashSet<String>();
public LoginPhaseListener()
{
//Populate a list of admin-only views
adminOnlyViews.add( "/CampaignList.jsp" );
adminOnlyViews.add( "/AdminPanel.jsp" );
adminOnlyViews.add( "/AdminWelcome.jsp" );
}
@Override
public PhaseId getPhaseId()
{
return PhaseId.RESTORE_VIEW;
}
@Override
public void beforePhase(PhaseEvent event)
{
}
@Override
public void afterPhase(PhaseEvent event)
{
FacesContext fc = event.getFacesContext();
// retrieve the session bean...
LoginSession loginSession = getLoginSessionBean(fc);
// Check if user is not logged in AND not already on the login page.
if ( loginSession == null || !loginSession.isUserLoggedIn() )
{
if(!fc.getViewRoot().getViewId().equals(VIEWID_LOGIN))
executeNavigateAction(fc, "goToLogin");
return;
}
//admin-only views
if( adminOnlyViews.contains( fc.getViewRoot().getViewId() ) && !loginSession.isAdministator() )
{
try
{
//sends a 403!
HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse();
response.sendError(HttpServletResponse.SC_FORBIDDEN);
}
catch (IOException ex)
{
//should never happen
Logger.getLogger(LoginPhaseListener.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
/**
* Executes a global navigation action
* @param fc the current context
* @param action the action name in face-config
*/
private void executeNavigateAction( FacesContext fc, String globalAction )
{
fc.getApplication().getNavigationHandler().handleNavigation(fc, null, globalAction);
}
/**
* Helper method for facilitating session bean retrieval
* @param fc the current faces context
* @return the login session bean, or null if it doesn't exist yet.
*/
private LoginSession getLoginSessionBean( FacesContext fc)
{
return (LoginSession) fc.getApplication().getELResolver().getValue(fc.getELContext(), null, "LoginSessionBean");
}
} |
Partager