How to get the Liferay - User when you are using ICEFaces

Ok, I had an email of someone who needed to know how to get the userId of the currently logged in User when he is using Liferay together with ICEFaces. Well Brian, here´s your answer...

You have to retrieve the ThemeDisplay, which then allows you to access a lot of the information, Liferay is providing:

FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
PortletRequest request = (PortletRequest) ec.getRequest();
ThemeDisplay display = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

If you have the ThemeDisplay, you can access the User ... and the real User (no kidding). Because it is possible to run certain Liferay pages as another User, the User you have to retrieve is the real User:

User realUser = display.getRealUser();

That´s all :)

If you liked this tutorial it would be very nice, if you could click on some of the google ads you see on the right side. It helps me run this block and motivates me ;)

If you have any questions, feel free to leave a comment.