Fully resolves list of group trees to be used in Keycloak. The input is group info (usually from LDAP) where each "Group" object contains just it's name and direct children.
The operation also performs validation as rules for LDAP are less strict than for Keycloak (In LDAP, the recursion is possible and multiple parents of single group is also allowed)
53 Map<String, List<String>> parentsTree =
getParentsTree(groups, ignoreMissingGroups);
56 List<String> rootGroups =
new LinkedList<>();
57 for (Map.Entry<String, List<String>> group : parentsTree.entrySet()) {
58 int parentCount = group.getValue().size();
59 if (parentCount == 0) {
60 rootGroups.add(group.getKey());
61 }
else if (parentCount > 1) {
62 throw new GroupTreeResolveException(
"Group '" + group.getKey() +
"' detected to have multiple parents. This is not allowed in Keycloak. Parents are: " + group.getValue());
67 Map<String, Group> asMap =
new TreeMap<>();
68 for (Group group : groups) {
69 asMap.put(group.getGroupName(), group);
73 List<GroupTreeEntry> finalResult =
new LinkedList<>();
74 Set<String> visitedGroups =
new TreeSet<>();
75 for (String rootGroupName : rootGroups) {
76 List<String> subtree =
new LinkedList<>();
77 subtree.add(rootGroupName);
78 GroupTreeEntry groupTree =
resolveGroupTree(rootGroupName, asMap, visitedGroups, subtree);
79 finalResult.add(groupTree);
84 if (visitedGroups.size() != asMap.size()) {
86 for (Map.Entry<String, Group> entry : asMap.entrySet()) {
87 String groupName = entry.getKey();
88 if (!visitedGroups.contains(groupName)) {
89 List<String> subtree =
new LinkedList<>();
90 subtree.add(groupName);
92 Set<String> newVisitedGroups =
new TreeSet<>();
94 visitedGroups.addAll(newVisitedGroups);
99 throw new GroupTreeResolveException(
"Illegal state: Recursion detected, but wasn't able to find it");
List< GroupTreeEntry > resolveGroupTree(List< Group > groups, boolean ignoreMissingGroups)
Definition: GroupTreeResolver.java:51
Map< String, List< String > > getParentsTree(List< Group > groups, boolean ignoreMissingGroups)
Definition: GroupTreeResolver.java:105