Maleen Abewardana
Maleen Abewardana

Reputation: 14552

Get user details in liferay layout listener

I'm creating a listener for liferay Layout model. I want to get the page creating/updating user details to the log. Here is a snippet from my code.

public class LayoutListener extends BaseModelListener<Layout> {
private final static Logger log = Logger.getLogger(LayoutListener.class);

  @Override
  public void onAfterRemove(Layout layout) throws ModelListenerException {
      // Need to find user deatils here.
      if (log.isInfoEnabled()) {
          log.info("Page -- " + layout.getName() + " -- removed.");
      }
      super.onAfterRemove(layout);
  }
}

How can I get the relevant user who is deleting the page inside this method?

PS - I was able to get user from accessing the current thread. But I need to know a proper way to do this.

Upvotes: 1

Views: 474

Answers (1)

Prakash K
Prakash K

Reputation: 11698

Well here is how liferay fetches it for listeners in its Audit EE plugin:

if(PrincipalThreadLocal.getName() != null) {
    userId = GetterUtil.getLong(PrincipalThreadLocal.getName());
}

And we are also using the same thing in our custom listeners for Blogs and Documents.

Upvotes: 1

Related Questions