Buzzzz
Buzzzz

Reputation: 887

Trigger a logout from a liferay portlet

Being quite new to liferay/portal/portlet development you run into problems daily . Now i'm trying to for a logout from a portlet but have not found a way to accomplish that. How are you supposed to do that? Should I try to send some kind of logout event or something? Greping around in the liferay sources i have found a LogoutAction.java that seems promising but how do one trigger that?

Best Regards Anders Olme

Upvotes: 2

Views: 6550

Answers (4)

Joan
Joan

Reputation: 239

After 2-3 days of investigating I achieved logging in and out with ajax calls. To logout it is not as simple as calling the session.invalidate() but there is only a little more to configure. I will write how I achieved this:

First edit your portal-ext.properties and add this line: session.enable.phishing.protection=false

Then in ALL of your portlets you have to set the private-session-attributes to false. The ordering is important so I'll show you mines:

<portlet>
    <portlet-name>home</portlet-name>
    <icon>/icon.png</icon>
    <instanceable>false</instanceable>
    <private-session-attributes>false</private-session-attributes>
    <header-portlet-css>/css/main.css</header-portlet-css>
    <footer-portlet-javascript>/js/home.js</footer-portlet-javascript>
    <css-class-wrapper>home-portlet</css-class-wrapper>
</portlet>

Once did this the rest is pretty simple. For log in:

public static void login(ResourceRequest request,ResourceResponse response, String liferayUser, String liferayPassword) throws Exception{
    MethodKey key = new MethodKey("com.liferay.portlet.login.util.LoginUtil", "login", HttpServletRequest.class, HttpServletResponse.class, String.class, String.class, boolean.class, String.class);
    PortalClassInvoker.invoke(false, key, new Object[] { PortalUtil.getHttpServletRequest(request), PortalUtil.getHttpServletResponse(response), liferayUser, liferayPassword, false, null});       
}

And for log out:

public static void logout(ResourceRequest resourceRequest) throws Exception{
    HttpServletRequest request = PortalUtil.getHttpServletRequest(resourceRequest); 
    request.getSession().invalidate();
}

I used ResourceRequest because i was doing Ajax calls. The only "problem" is that if you are logged in and want to logout & login with another user you have to do 2 ajax calls (the second one once returned from the first one).

Upvotes: 0

rmartinus
rmartinus

Reputation: 690

You could create a log out link to Liferay's internal logout functionality like this one in your JSP:

<%@page import="com.liferay.portal.util.PortalUtil"%>
.
.
<a href="<%= PortalUtil.getPortalURL(request) %>/c/portal/logout">Log Out</a>

Upvotes: 1

dtruong
dtruong

Reputation: 311

try actionResponse.sendRedirect("/c/portal/logout")

Upvotes: 3

Rostislav Matl
Rostislav Matl

Reputation: 4543

PortletSession.invalidate() ?

Upvotes: 1

Related Questions