Show live or logged in users

This blog is for creating an interface which will show number of live users and some other details.

First of all create a portlet which will provide interface and then create a hook within the portlet.Below link willl help to create hook in portlet.

http://www.learnandpost.com/2012/05/create-hook-inside-portlet-liferay.html

view.jsp

Add below line of code in portlet view.jsp

<%@page import="com.liferay.portal.service.UserLocalServiceUtil"%>
<%@page import="com.liferay.portal.model.User"%>
<%@page import="java.util.Iterator"%>
<%@page import="com.services.loggedinuser.LoggedInUsers"%>
<%@page import="java.util.Set"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
 
<style>
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}
 
td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
}
th {
background-color: #e1e1e1;
}
tr:nth-child(even) {
    background-color: #eee;
}
</style>
 
<div style="border:1px solid #ccc;padding:5pt;">
<b>Total number of Live Users : </b>
 
<%
Set<Long> users_set = LoggedInUsers.loggedInUsers();
%>
<b><%=users_set.size() %></b><br/>
<table>
<thead>
<tr><th>User Id</th><th>Screen Name</th><th>Full Name</th><th>IP Address</th><th>Login Date</th></tr>
</thead>
<tbody>
<%
Iterator<Long> it = users_set.iterator();
while(it.hasNext()){
long user_id = it.next();
User user = UserLocalServiceUtil.getUser(user_id);
String fullName = user.getFullName();
String ipAddress = user.getLastLoginIP();
String screenname = user.getScreenName();
String loginDate = user.getLoginDate().toString();
%>
<tr>
<td><%=user_id %></td>
<td><%=screenname %></td>
<td><%= fullName%></td>
<td><%=ipAddress %></td>
<td><%=loginDate %></td>
</tr>
<%
}
%>
</tbody>
</table>
</div>
 
Now it s time to work on hook which we have create and which will tell about live users.
liferay-hook.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 6.2.0//EN" "http://www.liferay.com/dtd/liferay-hook_6_2_0.dtd">
 
<hook>
<portal-properties>portal.properties</portal-properties>
</hook>
portal.properties
login.events.post=com.services.loggedinuser.LoginPostAction 
servlet.session.destroy.events=com.services.loggedinuser.SessionDestroyAction
 
Create LoginPostAction and SessionDestroyAction class.
LoginPostAction
package com.services.loggedinuser;
import com.liferay.portal.kernel.events.Action;
import com.liferay.portal.util.PortalUtil;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class LoginPostAction extends Action {
@Override
public void run(HttpServletRequest request, HttpServletResponse response) {
long userId = PortalUtil.getUserId(request);
LoggedInUsers.addUser(userId);
System.out.println("Logged in user :"+userId);
}
}
 
SessionDestroyAction
package com.services.loggedinuser;
import com.liferay.portal.kernel.events.SessionAction;
import com.liferay.portal.kernel.util.WebKeys;
 
import javax.servlet.http.HttpSession;
 
public class SessionDestroyAction extends SessionAction{
@Override
public void run(HttpSession session) {
Long userId = (Long) session.getAttribute(WebKeys.USER_ID);
LoggedInUsers.removeUser(userId);
System.out.println("Log out user :"+userId);
}
}
Also create one model object for live users.
 
LoggedInUsers 
package com.services.loggedinuser;
 
import java.util.HashSet;
import java.util.Set;
 
public class LoggedInUsers {
private static Set<Long> users = new HashSet<Long>();
 
public static void addUser(long userId) {
users.add(userId);
}
 
public static void clearUsers() {
users.clear();
}
 
public static void removeUser(long userId) {
users.remove(userId);
}
 
public static int countUsers() {
return users.size();
}
 
public static boolean isLogged(long userId) {
return users.contains(userId);
}
 
public static Set<Long> loggedInUsers(){
return users;
}
}
 

Write a blog post too!

Write a deep dive into how you use Liferay projects in your technology stack. Or let people know useful tips and tricks for a particular functionality. The Liferay community needs you!

Login or Create an account