1

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

  • Liferay portal : liferay-ce-portal-tomcat-7.0-ga5
  • Database : postgres (PostgreSQL) 9.5.17
  • IDE : eclipse-oxygen 4.7.3a
/*
 * 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);
    }
}

2 Answers2

2

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());
    }
Daniele Baggio
  • 2,157
  • 1
  • 14
  • 21
0

Yes it’s possible and that’s included OOTB in Liferay. The delete events already it’s trace. You can see the Audit Event and System Event table.

You could use the Audit Event Framework for your customisations.

Antonio Musarra
  • 370
  • 1
  • 14