Skip to content

Commit c38960d

Browse files
committed
for #40: Allow UserGroup services to work for non-local users
JIDs on domains other than the local domain can be in groups. This commit adds support for that in the UserGroups endpoints, with the exception of an endpoint that would make the code iterate over all groups in the system.
1 parent b16342c commit c38960d

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

src/java/org/jivesoftware/openfire/plugin/rest/controller/UserServiceController.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,15 @@ public void deleteRosterItem(String username, String rosterJid) throws SharedGro
401401
*/
402402
public List<String> getUserGroups(String username) throws ServiceException {
403403
log("Get user groups for user: " + username);
404+
if (username.contains("@")) {
405+
final JID jid = new JID(username);
406+
if (jid.getDomain().equals(XMPPServer.getInstance().getServerInfo().getXMPPDomain())) {
407+
username = jid.getNode();
408+
} else {
409+
// Implementing this would require us to iterate over all groups, which is a performance nightmare.
410+
throw new ServiceException("This service cannot be used for non-local users.", username, ExceptionType.GROUP_NOT_FOUND, Response.Status.INTERNAL_SERVER_ERROR);
411+
}
412+
}
404413
User user = getAndCheckUser(username);
405414
Collection<Group> groups = GroupManager.getInstance().getGroups(user);
406415
List<String> groupNames = new ArrayList<String>();
@@ -438,7 +447,7 @@ public void addUserToGroups(String username, UserGroupsEntity userGroupsEntity)
438447
groups.add(group);
439448
}
440449
for (Group group : groups) {
441-
group.getMembers().add(server.createJID(username, null));
450+
group.getMembers().add(username.contains("@") ? new JID(username) : XMPPServer.getInstance().createJID(username, null));
442451
}
443452
}
444453
}
@@ -461,7 +470,7 @@ public void addUserToGroup(String username, String groupName) throws ServiceExce
461470
group = GroupController.getInstance().createGroup(new GroupEntity(groupName, ""));
462471
}
463472

464-
group.getMembers().add(server.createJID(username, null));
473+
group.getMembers().add(username.contains("@") ? new JID(username) : XMPPServer.getInstance().createJID(username, null));
465474
}
466475

467476
/**
@@ -487,7 +496,7 @@ public void deleteUserFromGroups(String username, UserGroupsEntity userGroupsEnt
487496
Response.Status.NOT_FOUND, e);
488497
}
489498
log("Removing user: " + username + " from the group: " + groupName);
490-
group.getMembers().remove(server.createJID(username, null));
499+
group.getMembers().remove(username.contains("@") ? new JID(username) : XMPPServer.getInstance().createJID(username, null));
491500
}
492501
}
493502
}
@@ -508,7 +517,7 @@ public void deleteUserFromGroup(String username, String groupName) throws Servic
508517
throw new ServiceException("Could not find group", groupName, ExceptionType.GROUP_NOT_FOUND,
509518
Response.Status.NOT_FOUND, e);
510519
}
511-
group.getMembers().remove(server.createJID(username, null));
520+
group.getMembers().remove(username.contains("@") ? new JID(username) : XMPPServer.getInstance().createJID(username, null));
512521
}
513522

514523
/**

0 commit comments

Comments
 (0)