Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
Liferay 7.1: get the logged-in user in a @ManagedBean
I use Liferay 7.1 GA-1. I want to access the currently logged-in user Information.
How is this done?
Is this possible or am I approaching the issue wrongly?
Any
help or comment is very welcome.
Regards,
Claus
Claus Lüthje:Is this possible or am I approaching the issue wrongly?
Any help or comment is very welcome.Regards,
Claus
When nobody reacts, it often helps to get more specific. Post code, a minimal sample. What do you have, how do you use it - e.g. is it on the HTTP-touching layer? On the business layer?
Quite often, identity should be explicit part of the API, but I have no clue if this would be good here, as I'm not sure what you're doing where, why and how.
I try to be more specific, which may be a little hard as I'm not at liberty to share the code in its interity.
I use Liveray 7.1 GA 1 CE on Java 8.
Let's give it a try:
I have a ManagedBean declared like this:
@ManagedBean(name = "mybean")
@ViewScoped
public class MyBean implements Serializable {
// ...
public void startWork(){
// here I need the currently logged-in used
}
//…
}
Accessing the managed bean, I have a JSF-based XHTML with PrimeFaces:
<h:body>
<h:form>
...
<p:ajax event="rowDblselect" listener="#{mybean.startWork()}" />
…
</h:form>
</h:body>
This basically works fine. The only thing I don't know how to achieve is a reference to the currently logged-in user.
I hope this clarifies things a Little...
Regards
Claus
I added LiferayPortletHelperUtil.getUser(); to get the user, but when I add the corresponding Maven dependencies, the portlet isn't deployed anymore.
My code looks like this:
User user = LiferayPortletHelperUtil.getScopeGroupUser();
if (user != null) {
try {
System.out.println("*** Login: "+user.getLogin());
System.out.println("*** Full Name: "+user.getFullName());
} catch (PortalException ex) {
// log error
}
} }
I don't get any errors in the log. The portlet just isn't available for selection in the UI.
As the JARs don't seem to be provided, I tried to include them into the war. This didn't help
Look into the gogo shell, it's available from the control panel in 7.1
https://dev.liferay.com/develop/reference/-/knowledge_base/7-0/using-the-felix-gogo-shell
Enter "lb <yourmodule>"
(or only "lb" if you are not sure about the name and skim the list to find your module)
I guess, it will be in resolved state. Try "diag" and it probably will show you that it is missing at least one dependency. My best guess is that you have a dependency on a 7.0 version of some module instead of a 7.1 version. In that case, you probably just need to increase the version number in gradle. But that's just guesswork.
Hi Christoph,
thanks for the hint. My portlet is in active state and no unresolved dependencies (or versions) are shown when using diag.
While this widget is visible with 'lb' and 'diag' it can't be found to be added to the page.
Deployment went by without problems (apart from https://issues.liferay.com/browse/LPS-84485 , which doesn't seem to matter in this context)
Regards
Claus
Hi Claus,
Can you try using PortalUtil.getUser():
FacesContext facesContext = FacesContext<span class="hljs-preprocessor">.getCurrentInstance</span>()<span class="hljs-comment">;</span>
ExternalContext externalContext = facesContext<span class="hljs-preprocessor">.getExternalContext</span>()<span class="hljs-comment">;</span>
PortletRequest portletRequest = (PortletRequest) externalContext<span class="hljs-preprocessor">.getRequest</span>()<span class="hljs-comment">;</span>
User user = <span class="hljs-keyword">com</span><span class="hljs-preprocessor">.liferay</span><span class="hljs-preprocessor">.portal</span><span class="hljs-preprocessor">.kernel</span><span class="hljs-preprocessor">.util</span><span class="hljs-preprocessor">.PortalUtil</span><span class="hljs-preprocessor">.getUser</span>(portletRequest)<span class="hljs-comment">;</span>
Also, make sure that your Liferay Portal dependencies are marked
<scope>provided</scope> (for Maven) or
providedCompile (for Gradle) so that they are not
included in your WAR’s
WEB-INF/lib folder. Otherwise you may have
duplicate Liferay Portal classes on the classpath which can cause the
WAR to fail to deploy.
If that doesn’t work, please provide the shortest, simplest portlet that reproduces the issue and attach it to your post.
- Kyle