adds the ability to set the default groups via kcadm

Closes #19125
This commit is contained in:
Steve Hawkins 2023-09-26 13:31:30 -04:00 committed by Alexander Schwartz
parent 22d093f5c0
commit 7c6f173d3a
3 changed files with 28 additions and 12 deletions

View file

@ -138,6 +138,7 @@ public class LegacyExportImportManager implements ExportImportManager {
this.session = session;
}
@Override
public void exportRealm(RealmModel realm, ExportOptions options, ExportAdapter callback) {
callback.setType(MediaType.APPLICATION_JSON);
callback.writeToOutputStream(outputStream -> {
@ -409,11 +410,7 @@ public class LegacyExportImportManager implements ExportImportManager {
if (rep.getGroups() != null) {
importGroups(newRealm, rep);
if (rep.getDefaultGroups() != null) {
for (String path : rep.getDefaultGroups()) {
GroupModel found = KeycloakModelUtils.findGroupByPath(session, newRealm, path);
if (found == null) throw new RuntimeException("default group in realm rep doesn't exist: " + path);
newRealm.addDefaultGroup(found);
}
KeycloakModelUtils.setDefaultGroups(session, newRealm, rep.getDefaultGroups().stream());
}
}
@ -728,6 +725,9 @@ public class LegacyExportImportManager implements ExportImportManager {
}
}
if (rep.getDefaultGroups() != null) {
KeycloakModelUtils.setDefaultGroups(session, realm, rep.getDefaultGroups().stream());
}
if (rep.getDisplayName() != null) realm.setDisplayName(rep.getDisplayName());
if (rep.getDisplayNameHtml() != null) realm.setDisplayNameHtml(rep.getDisplayNameHtml());
if (rep.isEnabled() != null) realm.setEnabled(rep.isEnabled());

View file

@ -414,11 +414,7 @@ public class MapExportImportManager implements ExportImportManager {
if (rep.getGroups() != null) {
importGroups(newRealm, rep);
if (rep.getDefaultGroups() != null) {
for (String path : rep.getDefaultGroups()) {
GroupModel found = KeycloakModelUtils.findGroupByPath(session, newRealm, path);
if (found == null) throw new RuntimeException("default group in realm rep doesn't exist: " + path);
newRealm.addDefaultGroup(found);
}
KeycloakModelUtils.setDefaultGroups(session, newRealm, rep.getDefaultGroups().stream());
}
}
@ -1018,6 +1014,9 @@ public class MapExportImportManager implements ExportImportManager {
}
}
if (rep.getDefaultGroups() != null) {
KeycloakModelUtils.setDefaultGroups(session, realm, rep.getDefaultGroups().stream());
}
if (rep.getDisplayName() != null) realm.setDisplayName(rep.getDisplayName());
if (rep.getDisplayNameHtml() != null) realm.setDisplayNameHtml(rep.getDisplayNameHtml());
if (rep.isEnabled() != null) realm.setEnabled(rep.isEnabled());

View file

@ -1062,9 +1062,9 @@ public final class KeycloakModelUtils {
/**
* Returns <code>true</code> if given realm has attribute {@link Constants#REALM_ATTR_USERNAME_CASE_SENSITIVE}
* set and its value is <code>true</code>. Otherwise default value of it is returned. The default setting
* set and its value is <code>true</code>. Otherwise default value of it is returned. The default setting
* can be seen at {@link Constants#REALM_ATTR_USERNAME_CASE_SENSITIVE_DEFAULT}.
*
*
* @param realm
* @return See the description
* @throws NullPointerException if <code>realm</code> is <code>null</code>
@ -1072,4 +1072,21 @@ public final class KeycloakModelUtils {
public static boolean isUsernameCaseSensitive(RealmModel realm) {
return realm.getAttribute(REALM_ATTR_USERNAME_CASE_SENSITIVE, REALM_ATTR_USERNAME_CASE_SENSITIVE_DEFAULT);
}
/**
* Sets the default groups on the realm
* @param session
* @param realm
* @param groups
* @throws RuntimeException if a group does not exist
*/
public static void setDefaultGroups(KeycloakSession session, RealmModel realm, Stream<String> groups) {
realm.getDefaultGroupsStream().collect(Collectors.toList()).forEach(realm::removeDefaultGroup);
groups.forEach(path -> {
GroupModel found = KeycloakModelUtils.findGroupByPath(session, realm, path);
if (found == null) throw new RuntimeException("default group in realm rep doesn't exist: " + path);
realm.addDefaultGroup(found);
});
}
}