3 Replies Latest reply on Aug 18, 2009 5:56 AM by allanjun

    Factory method to get credentials?

    allanjun

      Hi there,


      Does anyone know if there is a factory method in seam that can be used to get the credentials?


      I know credentials can be injected with @In, but as we're only use seam in web layer, for services we just use pure ejb, no seam annotations. Therefore, we're looking for a easy way to access credential info such as the current login user name in services.


      Thx.

        • 1. Re: Factory method to get credentials?
          allanjun

          I found that I can use the following, anyone has further suggestions?


          Identity.instance().getCredentials().getUsername();

          • 2. Re: Factory method to get credentials?

            An other way is if you write your own factory:


            @Factory(value = LOGGED_IN_USERNAME, scope = ScopeType.SESSION, autoCreate = true)
               public String getLoggedInUsername() {
                  String username="";
                  Identity identity= Identity.instance();
                  if (identity.isLoggedIn() == true) {
                     username = identity.getPrincipal().getName();
                  } else {
                     username = "none";
                  }
                  return username;
               }





            or you can save the username in the session context (after login)



             @Observer(Identity.EVENT_LOGIN_SUCCESSFUL)
               public void loginSuccessfull() {
                  Contexts.getSessionContext().set("CURRENT_USER", Identity.instance().getCredentials().getUsername());
            }



            and you can read it with


            String username=Contexts.getSessionContext().get("CURRENT_USER");







            • 3. Re: Factory method to get credentials?
              allanjun

              Thanks Marco, that's helpful.