RE: User Listener !!

Vivek Mehta, modified 6 Years ago. Junior Member Posts: 48 Join Date: 4/7/19 Recent Posts
Hello Everyone!I am using two functions  :-
                                                         1)  onBeforeUpdate(User user) 
                                                        2) onAfterUpdate(User user)
Now these are working just perfectly onBeforeUpdate()  is being called just before onAfterUpdate(). But the thing where problem arises is suppose  the user's first name was : "helenOld" and then i updated the first name to : "helenNew" . Now when i am checking the user object ; the first name in both the beforeUpdate() and afterUpdate() is "helenNew". So please someone help me out in this as i thought of implementing this method for tracking what has been updated.But since Both the functions contain new modified data how can i do that !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
thumbnail
Christoph Rabel, modified 6 Years ago. Liferay Legend Posts: 1555 Join Date: 9/24/09 Recent Posts
onBeforeUpdate is called with the new user object.
You basically need to implement it in the same way as in the UserModelListener:
public void onBeforeUpdate(User newUser) throws ModelListenerException {
  try {
    User oldUser = _userLocalService.getUser(newUser.getUserId());
    List<Attribute> attributes = getModifiedAttributes(newUser, oldUser);
    if (!attributes.isEmpty())  trackChanges();
  }
  catch (Exception e) {
   throw new ModelListenerException(e);
  }
}
https://github.com/liferay/liferay-portal/blob/7.2.x/modules/apps/portal-security-audit/portal-security-audit-event-generators-user-management/src/main/java/com/liferay/portal/security/audit/event/generators/user/management/internal/model/listener/UserModelListener.java
Vivek Mehta, modified 6 Years ago. Junior Member Posts: 48 Join Date: 4/7/19 Recent Posts
Hi christoph!!I am having problem in implementing this. I think it is because I am using liferay 6.2  .  Can you please tell how to implement this in liferay 6.2 !!
thumbnail
Olaf Kock, modified 6 Years ago. Liferay Legend Posts: 6441 Join Date: 9/23/08 Recent Posts
Vivek Mehta:

Hi christoph!!I am having problem in implementing this. I think it is because I am using liferay 6.2  .  Can you please tell how to implement this in liferay 6.2 !!
Can you elaborate on your problem? Your first post sounded like you've already implemented it, but got unexpected data piped in to your listener. Christoph explained why this is the correct and expected data and how to figure out what changed.
Vivek Mehta, modified 6 Years ago. Junior Member Posts: 48 Join Date: 4/7/19 Recent Posts
Hi, The  problem that I am facing is that I pasted the code of christoph and imported necessary packages. But I am not able to impoort anything for auditmessagebuilder  in the  :-
if (!attributes.isEmpty())
{
AuditMessage auditMessage =AuditMessageBuilder.buildAuditMessage(EventTypes.UPDATE, User.class.getName(),newUser.getUserId(), attributes);
_auditRouter.route(auditMessage);
}
and hence not able to implement it..
thumbnail
Christoph Rabel, modified 6 Years ago. Liferay Legend Posts: 1555 Join Date: 9/24/09 Recent Posts
I am not sure if that class is available to you at all. But I am also not sure, why you need it. You should track the changes yourself, in whichever way you think is necessary. If you want to send these data to the auditing service, well, you don't need to, the UserModelListener does that for you.
Vivek Mehta, modified 6 Years ago. Junior Member Posts: 48 Join Date: 4/7/19 Recent Posts
So please help in how can i track what has been updated in the user ;example : firstname , lastname, email address , etc.  the code  snippet that you sent above contains the method   getModifiedAttributes(newUser, oldUser);    and in the document that you attached with it contains the definition for getModifiedAttributes(newUser, oldUser); as   :- 
protected List<Attribute> getModifiedAttributes(
            User newUser, User oldUser) {            AttributesBuilder attributesBuilder = new AttributesBuilder(
                newUser, oldUser);            attributesBuilder.add("active");
            attributesBuilder.add("agreedToTermsOfUse");
            attributesBuilder.add("comments");
            attributesBuilder.add("emailAddress");
            attributesBuilder.add("languageId");
            attributesBuilder.add("reminderQueryAnswer");
            attributesBuilder.add("reminderQueryQuestion");
            attributesBuilder.add("screenName");
            attributesBuilder.add("timeZoneId");
        
            List<Attribute> attributes = attributesBuilder.getAttributes();            if (newUser.isPasswordModified()) {
                attributes.add(new Attribute("password"));
            }            return attributes;
        }
    But here also AttributeBuilder class is not available for me. So how can i implement this code !!!!!!!!!!!!!!!!
thumbnail
Christoph Rabel, modified 6 Years ago. Liferay Legend Posts: 1555 Join Date: 9/24/09 Recent Posts
Well, you could for example write your own comparison function. e.g.
List<String> getModifiedAttributes(User newUser, User oldUser) { 
  List<String> changelist = new LinkedList<String>();
  if (!oldUser.getFirstname().equals(newUser.getFirstname())) changelist.add("Firstname " + oldUser.getFirstname()  + " --> " newUser.getFirstname());
  if (!oldUser.getLastname().equals(newUser.getLastname())) changelist.add("Lastname " + oldUser.getLastname()  + " --> " newUser.getLastname());
....
  return changelist;
}
Or you could create your own version of the generic
https://github.com/liferay/liferay-portal/blob/master/modules/apps/portal-security-audit/portal-security-audit-event-generators-api/src/main/java/com/liferay/portal/security/audit/event/generators/util/AttributesBuilder.java
In case BeanPropertiesUtil isn't available in 6.2.:
https://github.com/liferay/liferay-portal/blob/master/portal-kernel/src/com/liferay/portal/kernel/bean/BeanPropertiesUtil.java