Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
Dave Weitzel
User Listener not updating Group relationships
April 26, 2013 5:58 AM
Answer

Dave Weitzel

Rank: Regular Member

Posts: 168

Join Date: November 18, 2009

Recent Posts

I am writing my first listener extending BaseModelListener<User> .
I am in the onAfterUpdate() method monitoring a n expando field that records the "type" of user.
If that value changes I need to ensure the user is put int a specific UserGroup for that type so that the user gets the right access controls for web content and other services.
I am using exactly the same code that is used by Administrators using managing UserGroups (EditUerGroupAssignmentsAction.java):
 1UserGroup typeGroup = UserGroupLocalServiceUtil.getUserGroup(user.getCompanyId(), gName);
 2                long typeGroupId = typeGroup.getPrimaryKey();
 3               
 4                if(UserGroupUtil.containsUser(typeGroupId,userId)){
 5                    _log.info("user " + userId + " already in group " + typeGroupId);
 6
 7                }else{
 8                    String userIdS = String.valueOf(userId);
 9                    long[] userIds = StringUtil.split(userIdS, 0L);
10                    try {
11                        UserServiceUtil.addUserGroupUsers(typeGroupId, userIds);
12        //                UserGroupUtil.addUser(typeGroupId,user);   
13        //                UserLocalServiceUtil.addUserGroupUsers(typeGroupId, userIds);
14                    }catch (Exception e) {
15                        _log.error(e);
16                    }
17                    _log.info("user " + userId + " added to group " + typeGroupId);
18                    _log.info("testing if that is true " + UserGroupUtil.containsUser(typeGroupId,userId));
19                }


Whichever method I call none of them will actually update the assignments and the user is not added to the group.

Can anyone advise why processing something in a Listener would be failing? To me this is a pretty standard requirement (there are three field I will be monitoring like this). It isnt as if we are updating the user from within the User Listener and creating a loop. I see no exceptions being thrown.

As a side question is there any way of monitoring to see if that field was affected in the update (like doing a beforeUpdate and then afterUpdate) so I can tell if it was changed easily?