I have to create custom auditing for the User model to track by whom the user has been deleted. I have tried to create a Liferay module listener for the User model, but I am not able to get the detail by whom the user is being deleted.
Is there any way to get details about who made a change to the User model in Liferay module listener?
My Liferay environment detail
/*
* Below is the sample code that I have tried to create the Liferay module listener for the User model
*/
package com.test.useraudit.modellistner;
import org.osgi.service.component.annotations.Component;
import com.liferay.portal.kernel.exception.ModelListenerException;
import com.liferay.portal.kernel.model.BaseModelListener;
import com.liferay.portal.kernel.model.ModelListener;
import com.liferay.portal.kernel.model.User;
@Component(
immediate = true,
service = ModelListener.class
)
public class CustomUserModelListner extends BaseModelListener<User>{
@Override
public void onBeforeRemove(User user) throws ModelListenerException{
System.out.println("In onBeforeRemove method");
System.out.println("User detail :");
System.out.println(user);
super.onBeforeRemove(user);
}
@Override
public void onAfterRemove(User user) throws ModelListenerException{
System.out.println("In onAfterRemove method");
System.out.println("User detail :");
System.out.println(user);
super.onAfterRemove(user);
}
}
Yes it's possible.
There's an implicit thread local variable called ServiceContext
that contains the calling context details.
Sample:
@Override
public void onBeforeRemove(User user) throws ModelListenerException{
ServiceContext serviceContext =
ServiceContextThreadLocal.getServiceContext();
System.out.println("Calling user:" + serviceContext.getUserId());
}