title: How to access a @Named bean in PhaseListener?
body: <p>We are using JBoss 7.1, MySQL/PostgreSQL DB, JSF 2.0 with CDI beans.</p>
  
  <p>I have to implement authentification based on DB by login and password. We have a managment (administration) portal. When the client opens a restricted page without being logged in, it should redirect the request to <code>login.*</code> page if the client is not logged in.</p>
  
  <p>I have tried to do that by using a <code>PhaseListener</code>.
  I can Login and Logout, but when I try to open some another page I ran into a problem: 
  I cannot get <code>@Named("user") public class UserManager</code> bean inside the <code>PhaseListener</code> class. I tried to get it by using <code>FacesContext</code>, &amp; EL..., that all did not help me. </p>
  
  <p>The <code>UserManager</code> validates the login and stores the logged in user as <code>current</code> property. On every request, I want to check in the <code>PhaseListener</code> if <code>#{user.current}</code> is not <code>null</code>. But I can't get the <code>#{user}</code> bean in the <code>PhaseListener</code>.</p>
  
  <p>How can I get a <code>@Named</code> bean in <code>beforePhase()</code> or <code>afterPhase()</code>?</p>
  
  <hr>
  
  <p><strong>Update</strong>: here is my attempt so far:</p>
  
  <pre><code>private boolean loggedIn( FacesContext context ) throws IOException, ServletException
  {
      LOGSTORE.debug( "loggedIn().2 " );
  
      HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
      HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
  
  //  ELContext elContext = FacesContext.getCurrentInstance().getELContext();
  //  UserManager userManager = (UserManager) FacesContext.getCurrentInstance().getApplication()
  //      .getELResolver().getValue( elContext, null, "user" );
  
      HttpSession session = (HttpSession) context.getExternalContext().getSession( true );
      UserManager userManager = (UserManager) session.getAttribute( "user" );
  
  //  UserManager userManager = (UserManager) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get( "user" );
  
      if (!StringUtils.contains( ((HttpServletRequest) context.getExternalContext().getRequest())
          .getRequestURL().toString(), URL_SESSION_EXPIRED ))
      {
  
          if (userManager == null || !userManager.isLoggedIn())
          {
              LOGSTORE.debug( " userManager is " + (userManager == null ? "" : "not ") + " null" );
              if (userManager != null)
              {
                  LOGSTORE.debug( " userManager.isLoggedIn() is "
                      + (userManager.isLoggedIn() ? "TRUE" : "FALSE") );
              }
              LOGSTORE.debug( " doPhaseFilter() - START REDIRECT " );
              response.sendRedirect( request.getContextPath() + "/" + homepage + "?auth-failed" );
          }
          return false;
  
      } else
      {
          LOGSTORE.debug( "loggedIn().3 it is " + homepage );
          return true;
      }
  }
  </code></pre>