* @param includingSystemGroups if true, also returns the system groups ('GUEST', 'intranet', 'all')
* @return
* @throws java.sql.SQLException
*/
private Element getGroups(ServiceContext context, Profile profile, boolean includingSystemGroups) throws SQLException {
final GroupRepository groupRepository = context.getBean(GroupRepository.class);
final UserGroupRepository userGroupRepository = context.getBean(UserGroupRepository.class);
final Sort sort = SortUtils.createSort(Group_.id);
UserSession session = context.getUserSession();
if (!session.isAuthenticated()) {
return groupRepository.findAllAsXml(Specifications.not(GroupSpecs.isReserved()), sort);
}
Element result;
// you're Administrator
if (Profile.Administrator == session.getProfile()) {
// return all groups
result = groupRepository.findAllAsXml(null, sort);
} else {
Specifications<UserGroup> spec = Specifications.where(UserGroupSpecs.hasUserId(session.getUserIdAsInt()));
// you're no Administrator
// retrieve your groups
if (profile != null) {
spec = spec.and(UserGroupSpecs.hasProfile(profile));
}
Set<Integer> ids = new HashSet<Integer>(userGroupRepository.findGroupIds(spec));
// include system groups if requested (used in harvesters)
if (includingSystemGroups) {
// these DB keys of system groups are hardcoded !
for (ReservedGroup reservedGroup : ReservedGroup.values()) {
ids.add(reservedGroup.getId());
}
}
// retrieve all groups
Element groups = groupRepository.findAllAsXml(null, sort);
// filter all groups so only your groups (+ maybe system groups) are retained
result = Lib.element.pruneChildren(groups, ids);
}
return result;