Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: User Listener !!
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 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
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 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
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
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
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 !!
Vivek Mehta:
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.
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 !!
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..
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..
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.
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 !!!!!!!!!!!!!!!!
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 !!!!!!!!!!!!!!!!
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
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
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™