6 Replies Latest reply on May 21, 2010 3:51 AM by joco

    How can I retrieve the email address of a user ?

    goschan

      I though that the email address of a user was stored in the set of Principals of a user, I try to list it :

       

      Identity identity = ConversationState.getCurrent().getIdentity();
      Iterator<Principal> u = identity.getSubject().getPrincipals().iterator();
      
      Utils.trace(getClass(), "Principals:");
      
      while (u.hasNext())
      {
        Utils.trace(getClass(), u.next().toString());
      }
      

       

       

      And for the user root :

       

      [2010-04-01 19:54:50,573] TRACE [AuthenticatorBean] Principals:
      [2010-04-01 19:54:50,573] TRACE [AuthenticatorBean] Roles
      [2010-04-01 19:54:50,573] TRACE [AuthenticatorBean] root

       

       

      How can I retrieve the email address of a user ?

        • 1. Re: How can I retrieve the email address of a user ?
          mstruk

          According to Portlet 2.0 Specification PLT.21.2 you should get the user's email (if it's available) by using the following code:

           

           

          {code}

          Map userInfo = (Map) request.getAttribute(PortletRequest.USER_INFO);
          String userEmail = (userInfo!=null) ? (String)
                userInfo.get(PortletRequest.P3PUserInfos.USER_HOMEINFO_ONLINE_EMAIL) : null;

           

          if (userEmail == null)

          {

             userEmail = (userInfo!=null) ? (String)
                   userInfo.get(PortletRequest.P3PUserInfos.USER_BUSINESSINFO_ONLINE_EMAIL) : null;
          }

          {code}

           

          However, at the moment GateIn doesn't make it available in this way (file a feature request here).

           

          Using GateIn specific API you can get it by:

           

          {code}

          PortalContainer container = PortalContainer.getInstance();
          OrganizationService orgService = (OrganizationService) container.getComponentInstanceOfType(OrganizationService.class);

          UserHandler userHandler = orgService.getUserHandler();

          User user = userHandler.findUserByName(request.getUserPrincipal().getName());

          String email = user.getEmail();

           

          {code}

           

          Where 'request' is RenderRequest.

           

          I didn't test this, but maybe it will work

           

          - marko

          • 2. Re: How can I retrieve the email address of a user ?
            goschan

            Thank you, it works

             

            PortalContainer container = PortalContainer.getInstance();
            OrganizationService orgService = (OrganizationService) container.getComponentInstanceOfType(OrganizationService.class);
            UserHandler userHandler = orgService.getUserHandler();
            
            User user = null;
            
            try {
                 user = userHandler.findUserByName(FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal().getName());
            } 
            catch (Exception e1) {
                 // TODO Auto-generated catch block
                 e1.printStackTrace();
            }
            
            String email = user.getEmail();
            

             

             

            Now I want to retrieve all the users of one group. Do youknow how can I do that?

             

            Thank you.

            • 3. Re: How can I retrieve the email address of a user ?
              mstruk

              Take a look at UserHandler class. You have many methods there. This one looks promising:

               

               

              {code}

              public ListAccess<User> findUsersByGroupId(String groupId) throws Exception;

              {code}

               

              - marko

              • 4. Re: How can I retrieve the email address of a user ?
                joco

                Hi Benjamin!

                 

                I tried your code, but I don't know which classes or packages do I need to import. For example I've already imported org.exoplatform.services.organization.OrganizationService class, but the getComponentInstanceOfType method on container variable is not recognised by my environment (NetBeans 6.8). I also tried to look for the UserHandler class, but unfortunately I did not manage to find it.

                Could you please, provide me all imports needed for your example (or the whole .java file, which you works for you)?

                 

                Thanks a lot

                Jozsef

                • 5. Re: How can I retrieve the email address of a user ?
                  trong.tran

                  the org.exoplatform.container.PortalContainer is located in the exo.kernel.container project.

                   

                  you can look for its binaries at : http://repository.jboss.com/maven2/org/exoplatform/kernel/exo.kernel.container/

                  • 6. Re: How can I retrieve the email address of a user ?
                    joco

                    thanks a lot