掲示板

User Listener not updating Group relationships

thumbnail
10年前 に Dave Weitzel によって更新されました。

User Listener not updating Group relationships

Regular Member 投稿: 208 参加年月日: 09/11/18 最新の投稿
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):
UserGroup typeGroup = UserGroupLocalServiceUtil.getUserGroup(user.getCompanyId(), gName);
				long typeGroupId = typeGroup.getPrimaryKey();
				
				if(UserGroupUtil.containsUser(typeGroupId,userId)){
					_log.info("user " + userId + " already in group " + typeGroupId);

				}else{
					String userIdS = String.valueOf(userId);
					long[] userIds = StringUtil.split(userIdS, 0L);
					try {
						UserServiceUtil.addUserGroupUsers(typeGroupId, userIds);
		//				UserGroupUtil.addUser(typeGroupId,user);	
		//				UserLocalServiceUtil.addUserGroupUsers(typeGroupId, userIds);
					}catch (Exception e) {
						_log.error(e);
					}
					_log.info("user " + userId + " added to group " + typeGroupId);
					_log.info("testing if that is true " + UserGroupUtil.containsUser(typeGroupId,userId));
				}


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?