cancel
Showing results for 
Search instead for 
Did you mean: 

Current User info lookup

fhomasp
Champ in-the-making
Champ in-the-making
Hey,

I need to lookup the firstname and lastname of the current user, using SDK.  The user logs on normally, has some out-of-the-box functionality but he/she can also go to a custom JSF - Bean page to adjust info.  However, I need to automatically lookup his first and last name so the Alfresco repo (using web service api) can lookup the specific info and populate the bean.  The user should only be able to view/adjust his/her own info.


This might be solved rather easy.  However I have written some kind of class that resolves in an ExceptionInInitializer exception when I redraw the page. I don't really know why and I'd like to know, as I want to learn a bit about Spring in the process.  So if anyone can enlighten me, by all means don't hold back 😉

import javax.transaction.UserTransaction;

import net.sf.acegisecurity.AuthenticationManager;
import net.sf.acegisecurity.providers.dao.SaltSource;

import org.alfresco.repo.cache.SimpleCache;
import org.alfresco.repo.security.authentication.AuthenticationComponent;
import org.alfresco.repo.security.authentication.MD4PasswordEncoder;
import org.alfresco.repo.security.authentication.MutableAuthenticationDao;
import org.alfresco.repo.security.authentication.TicketComponent;
import org.alfresco.repo.security.authentication.InMemoryTicketComponentImpl.Ticket;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.cmr.security.AuthenticationService;
import org.alfresco.util.ApplicationContextHelper;
import org.springframework.context.ApplicationContext;

public class AuthCheck {
   private static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
    private NodeService nodeService;
    private SearchService searchService;
    private NodeRef rootNodeRef;
    private NodeRef systemNodeRef;
    private NodeRef typesNodeRef;
    private NodeRef personAndyNodeRef;
    private DictionaryService dictionaryService;
    private MD4PasswordEncoder passwordEncoder;
    private MutableAuthenticationDao dao;
    private AuthenticationManager authenticationManager;
    private SaltSource saltSource;
    private TicketComponent ticketComponent;
    private SimpleCache<String, Ticket> ticketsCache;
    private AuthenticationService authenticationService;
    private AuthenticationService pubAuthenticationService;
    private AuthenticationComponent authenticationComponent;
    private UserTransaction userTransaction;
    private AuthenticationComponent authenticationComponentImpl;
   
   /**
    * This class checks the filled in user info. 
    * The idea is to use the LDAP.  Otherwise anyone can access anyones info and modify it.
    */
   public AuthCheck() {
      setUp();
   }

   public void setUp(){
   //   authenticationService = (AuthenticationService)ctx.getBean("authenticationService");
    //    pubAuthenticationService = (AuthenticationService)ctx.getBean("AuthenticationService");
    //    authenticationComponent = (AuthenticationComponent)ctx.getBean("authenticationComponent");
        authenticationComponentImpl = (AuthenticationComponent)ctx.getBean("authenticationComponentImpl");
        System.out.println(authenticationComponentImpl.getCurrentUserName()+ " From AuthCheck!!!!!");
   }
2 REPLIES 2

fhomasp
Champ in-the-making
Champ in-the-making
Getting the current username from the current user proved to be no problem.  Getting that persons properties like Firstname and lastname is.
I can get an ID just fine, a QName as well, but personproperties…  I really can't find it.  And I need it.

fhomasp
Champ in-the-making
Champ in-the-making
And so I have found it, after a long period of searching.  I wasted half a day figuring out what I was doing wrong.  It seems that for fetching User Info from the current user I required a started Transaction.
Why this is required for such a simple operation is beyond me.  That's why I suspect there is a simpler way to do this, and if so, by all means let me know.

Anyway, here is the code, which works.


public void setUp(){
   
      FacesContext context = FacesContext.getCurrentInstance();
      ServiceRegistry serviceRegistry = Repository.getServiceRegistry(context);
      AuthenticationService authenticationService = serviceRegistry.getAuthenticationService();
        String username=authenticationService.getCurrentUserName();
        this.username=username;
        System.out.println(username+" \t—> from AuthCheck!!!!!!!!!");

        UserTransaction tx = null;
        try
        {
           //Start the transaction.  Why this is required for such a simple operation is beyond me.
           TransactionService txs = (TransactionService) FacesContextUtils.getRequiredWebApplicationContext(context).getBean("transactionComponent");
           tx = txs.getUserTransaction();
           tx.begin();
           
              PersonService personService = (PersonService)FacesContextUtils.getRequiredWebApplicationContext(context).getBean("personService");
              NodeService nodeService = (NodeService)FacesContextUtils.getRequiredWebApplicationContext(context).getBean("nodeService");
              PersonService ps = serviceRegistry.getPersonService();
                                                 
             System.out.println("\t"+ ps.getUserIdentifier(username));
             NodeRef nr = personService.getPerson(username);
          
       
             String firstName = (String)nodeService.getProperty(nr, ContentModel.PROP_FIRSTNAME);
             String lastName = (String)nodeService.getProperty(nr, ContentModel.PROP_LASTNAME);
      
             System.out.println(firstName +" " +lastName+ " From the Repo!!!!!!!!!!!");
             tx.commit();
             
        }catch (Exception err)
        {
            try { if (tx != null) {tx.rollback();}
            }
            catch (Exception tex) {tex.printStackTrace();}
            err.printStackTrace();
         }
         finally {
            //Normally I'd end the transaction, but it doesn't seem to feature such a method.
         }
   }