Post Login and Pre Logout Actions using Session in Liferay :
When user Login into the portal, Login and logout values with current date stored in DB.
Step1:
=====
Create new hook plugin project.
Step2:
=====
Create the service.xml file add the below code
<service-builder package-path="org.monitoring">
<namespace>PKS</namespace>
<entity name="Logindetails" uuid="true" local-service="true" remote-service="true">
<!-- PK fields -->
<column name="loginId" type="long" primary="true" id-type="identity"></column>
<column name="userId" type="long"></column>
<column name="username" type="String"></column>
<column name="emailAddress" type="String"></column>
<column name="loginDate" type="Date"></column>
<column name="logoutDate" type="Date"></column>
<column name="loginIp" type="String"></column>
<!-- Order -->
<order by="asc">
<order-column name="loginId" />
</order>
<!-- Finder methods -->
<finder return-type="Collection" name="findByUserName">
<finder-column name="username"></finder-column>
</finder>
<finder return-type="Collection" name="Name_EmailAddress">
<finder-column name="emailAddress"></finder-column>
<finder-column name="username"></finder-column>
</finder>
</entity>
</service-builder>
Step3:
=====
Create 2 new Custom Classes in Src\ folder
1) LoginDetails
package com.test.hook;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.events.Action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.monitoring.model.Logindetails;
import org.monitoring.model.impl.LogindetailsImpl;
import org.monitoring.service.LogindetailsLocalServiceUtil;
import com.liferay.portal.kernel.events.ActionException;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.User;
import com.liferay.portal.util.PortalUtil;
public class LoginDetails extends Action {
public LoginDetails() {
super();
}
public void run(HttpServletRequest request, HttpServletResponse response) throws ActionException {
/* String date = null;
*/ Date date = new Date();
/*SimpleDateFormat formatter;
formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
date = formatter.format(date1);
System.out.println("+++++ Current Logintime +++++"+date);
*/
System.out.println("===Getting Current Login Time=== :"+date);
try {
User currentUser;
long userId;
String emailaddress;
String username;
try {
currentUser = PortalUtil.getUser(request);
System.out.println("Current Username: "+currentUser);
userId = currentUser.getUserId();
System.out.println("UserId: "+userId);
emailaddress = currentUser.getEmailAddress();
username = currentUser.getFirstName();
String ipAddress = PortalUtil.getOriginalServletRequest(request).getRemoteAddr();
System.out.println("====Your IP Address Is === :"+ipAddress);
long loginId = CounterLocalServiceUtil.increment(Logindetails.class.getName());
Logindetails logindetails = new LogindetailsImpl();
logindetails.setLoginId(loginId);
logindetails.setUserId(userId);
logindetails.setUsername(username);
logindetails.setLoginIp(ipAddress);
logindetails.setEmailAddress(emailaddress);
System.out.println("===Got Current Login Time===:"+date);
logindetails.setLoginDate(date);
LogindetailsLocalServiceUtil.addLogindetails(logindetails);
HttpSession session = request.getSession();
session.setAttribute("loginID",logindetails.getLoginId());
System.out.println("before: "+logindetails.getLoginId());
} catch (PortalException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2) LogoutDetails :
package com.test.hook;
import java.util.Date;
import com.liferay.portal.kernel.events.Action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.monitoring.model.Logindetails;
import org.monitoring.service.LogindetailsLocalServiceUtil;
import com.liferay.portal.kernel.events.ActionException;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
public class LogoutDetails extends Action {
public MonitoringLogout() {
super();
}
public void run(HttpServletRequest request, HttpServletResponse response) throws ActionException {
System.out.println("monitoring portlet created");
System.out.println("===Getting Current Logout Time === :"+new Date());
Date date;
date = new Date();
try {
try {
HttpSession session = request.getSession();
System.out.println("Object o :"+session);
Long sessiId =(Long)session.getAttribute("loginID");
System.out.println("after: "+sessiId);
Logindetails login = LogindetailsLocalServiceUtil.getLogindetails(sessiId);
login.setLogoutDate(date);
LogindetailsLocalServiceUtil.updateLogindetails(login);
} catch (PortalException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Step4 :
=====
Create portal.properties file under Src folder add the below property
login.events.post=com.test.hook.LoginDetails
logout.events.pre=com.test.hook.LogoutDetails
Step 5:
=====
Add the below entity in liferay-hook.xml
<hook>
<portal-properties>portal.properties</portal-properties>
</hook>
Step 6:
======
Deploy the hook and see the results.
When user Login into the portal, Login and logout values with current date stored in DB.
Step1:
=====
Create new hook plugin project.
Step2:
=====
Create the service.xml file add the below code
<service-builder package-path="org.monitoring">
<namespace>PKS</namespace>
<entity name="Logindetails" uuid="true" local-service="true" remote-service="true">
<!-- PK fields -->
<column name="loginId" type="long" primary="true" id-type="identity"></column>
<column name="userId" type="long"></column>
<column name="username" type="String"></column>
<column name="emailAddress" type="String"></column>
<column name="loginDate" type="Date"></column>
<column name="logoutDate" type="Date"></column>
<column name="loginIp" type="String"></column>
<!-- Order -->
<order by="asc">
<order-column name="loginId" />
</order>
<!-- Finder methods -->
<finder return-type="Collection" name="findByUserName">
<finder-column name="username"></finder-column>
</finder>
<finder return-type="Collection" name="Name_EmailAddress">
<finder-column name="emailAddress"></finder-column>
<finder-column name="username"></finder-column>
</finder>
</entity>
</service-builder>
Step3:
=====
Create 2 new Custom Classes in Src\ folder
1) LoginDetails
package com.test.hook;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.events.Action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.monitoring.model.Logindetails;
import org.monitoring.model.impl.LogindetailsImpl;
import org.monitoring.service.LogindetailsLocalServiceUtil;
import com.liferay.portal.kernel.events.ActionException;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.User;
import com.liferay.portal.util.PortalUtil;
public class LoginDetails extends Action {
public LoginDetails() {
super();
}
public void run(HttpServletRequest request, HttpServletResponse response) throws ActionException {
/* String date = null;
*/ Date date = new Date();
/*SimpleDateFormat formatter;
formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
date = formatter.format(date1);
System.out.println("+++++ Current Logintime +++++"+date);
*/
System.out.println("===Getting Current Login Time=== :"+date);
try {
User currentUser;
long userId;
String emailaddress;
String username;
try {
currentUser = PortalUtil.getUser(request);
System.out.println("Current Username: "+currentUser);
userId = currentUser.getUserId();
System.out.println("UserId: "+userId);
emailaddress = currentUser.getEmailAddress();
username = currentUser.getFirstName();
String ipAddress = PortalUtil.getOriginalServletRequest(request).getRemoteAddr();
System.out.println("====Your IP Address Is === :"+ipAddress);
long loginId = CounterLocalServiceUtil.increment(Logindetails.class.getName());
Logindetails logindetails = new LogindetailsImpl();
logindetails.setLoginId(loginId);
logindetails.setUserId(userId);
logindetails.setUsername(username);
logindetails.setLoginIp(ipAddress);
logindetails.setEmailAddress(emailaddress);
System.out.println("===Got Current Login Time===:"+date);
logindetails.setLoginDate(date);
LogindetailsLocalServiceUtil.addLogindetails(logindetails);
HttpSession session = request.getSession();
session.setAttribute("loginID",logindetails.getLoginId());
System.out.println("before: "+logindetails.getLoginId());
} catch (PortalException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2) LogoutDetails :
package com.test.hook;
import java.util.Date;
import com.liferay.portal.kernel.events.Action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.monitoring.model.Logindetails;
import org.monitoring.service.LogindetailsLocalServiceUtil;
import com.liferay.portal.kernel.events.ActionException;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
public class LogoutDetails extends Action {
public MonitoringLogout() {
super();
}
public void run(HttpServletRequest request, HttpServletResponse response) throws ActionException {
System.out.println("monitoring portlet created");
System.out.println("===Getting Current Logout Time === :"+new Date());
Date date;
date = new Date();
try {
try {
HttpSession session = request.getSession();
System.out.println("Object o :"+session);
Long sessiId =(Long)session.getAttribute("loginID");
System.out.println("after: "+sessiId);
Logindetails login = LogindetailsLocalServiceUtil.getLogindetails(sessiId);
login.setLogoutDate(date);
LogindetailsLocalServiceUtil.updateLogindetails(login);
} catch (PortalException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Step4 :
=====
Create portal.properties file under Src folder add the below property
login.events.post=com.test.hook.LoginDetails
logout.events.pre=com.test.hook.LogoutDetails
Step 5:
=====
Add the below entity in liferay-hook.xml
<hook>
<portal-properties>portal.properties</portal-properties>
</hook>
Step 6:
======
Deploy the hook and see the results.