diff --git a/server-spi/src/main/java/org/keycloak/migration/migrators/MigrateTo1_9_0.java b/server-spi/src/main/java/org/keycloak/migration/migrators/MigrateTo1_9_0.java index f8c709b471..fb24598afa 100644 --- a/server-spi/src/main/java/org/keycloak/migration/migrators/MigrateTo1_9_0.java +++ b/server-spi/src/main/java/org/keycloak/migration/migrators/MigrateTo1_9_0.java @@ -38,7 +38,7 @@ public class MigrateTo1_9_0 { public static final ModelVersion VERSION = new ModelVersion("1.9.0"); public void migrate(KeycloakSession session) { - RealmModel realm = session.realms().getRealmByName(Config.getAdminRealm()); + RealmModel realm = session.realms().getRealm(Config.getAdminRealm()); if (realm != null && realm.getDisplayNameHtml() != null && realm.getDisplayNameHtml().equals("Keycloak")) { realm.setDisplayNameHtml("
Keycloak
"); } diff --git a/server-spi/src/main/java/org/keycloak/models/ImpersonationConstants.java b/server-spi/src/main/java/org/keycloak/models/ImpersonationConstants.java index 9c5681f68e..05f6b450dd 100755 --- a/server-spi/src/main/java/org/keycloak/models/ImpersonationConstants.java +++ b/server-spi/src/main/java/org/keycloak/models/ImpersonationConstants.java @@ -36,7 +36,7 @@ public class ImpersonationConstants { adminRealm = realm; adminRole = realm.getRole(AdminRoles.ADMIN); } else { - adminRealm = model.getRealmByName(Config.getAdminRealm()); + adminRealm = model.getRealm(Config.getAdminRealm()); adminRole = adminRealm.getRole(AdminRoles.ADMIN); } ClientModel realmAdminApp = adminRealm.getClientByClientId(KeycloakModelUtils.getMasterRealmAdminApplicationClientId(realm.getName())); diff --git a/server-spi/src/main/java/org/keycloak/models/utils/RepresentationToModel.java b/server-spi/src/main/java/org/keycloak/models/utils/RepresentationToModel.java index d07902d374..e6bb3c58c2 100755 --- a/server-spi/src/main/java/org/keycloak/models/utils/RepresentationToModel.java +++ b/server-spi/src/main/java/org/keycloak/models/utils/RepresentationToModel.java @@ -80,6 +80,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -620,9 +621,40 @@ public class RepresentationToModel { public static void renameRealm(RealmModel realm, String name) { if (name.equals(realm.getName())) return; + + String oldName = realm.getName(); + ClientModel masterApp = realm.getMasterAdminClient(); masterApp.setClientId(KeycloakModelUtils.getMasterRealmAdminApplicationClientId(name)); realm.setName(name); + + ClientModel adminClient = realm.getClientByClientId(Constants.ADMIN_CONSOLE_CLIENT_ID); + if (adminClient != null) { + if (adminClient.getBaseUrl() != null) { + adminClient.setBaseUrl(adminClient.getBaseUrl().replace("/admin/" + oldName + "/", "/admin/" + name + "/")); + } + Set adminRedirectUris = new HashSet<>(); + for (String r : adminClient.getRedirectUris()) { + adminRedirectUris.add(replace(r, "/admin/" + oldName + "/", "/admin/" + name + "/")); + } + adminClient.setRedirectUris(adminRedirectUris); + } + + ClientModel accountClient = realm.getClientByClientId(Constants.ACCOUNT_MANAGEMENT_CLIENT_ID); + if (accountClient != null) { + if (accountClient.getBaseUrl() != null) { + accountClient.setBaseUrl(accountClient.getBaseUrl().replace("/realms/" + oldName + "/", "/realms/" + name + "/")); + } + Set accountRedirectUris = new HashSet<>(); + for (String r : accountClient.getRedirectUris()) { + accountRedirectUris.add(replace(r, "/realms/" + oldName + "/", "/realms/" + name + "/")); + } + accountClient.setRedirectUris(accountRedirectUris); + } + } + + private static String replace(String url, String target, String replacement) { + return url != null ? url.replace(target, replacement) : null; } public static void updateRealm(RealmRepresentation rep, RealmModel realm) { diff --git a/services/src/main/java/org/keycloak/services/managers/RealmManager.java b/services/src/main/java/org/keycloak/services/managers/RealmManager.java index b1dcaff095..b46a0e877b 100755 --- a/services/src/main/java/org/keycloak/services/managers/RealmManager.java +++ b/services/src/main/java/org/keycloak/services/managers/RealmManager.java @@ -293,7 +293,7 @@ public class RealmManager implements RealmImporter { createRealmRole.setDescription("${role_" + AdminRoles.CREATE_REALM + "}"); createRealmRole.setScopeParamRequired(false); } else { - adminRealm = model.getRealmByName(Config.getAdminRealm()); + adminRealm = model.getRealm(Config.getAdminRealm()); adminRole = adminRealm.getRole(AdminRoles.ADMIN); } adminRole.setDescription("${role_"+AdminRoles.ADMIN+"}"); diff --git a/services/src/main/java/org/keycloak/services/resources/WelcomeResource.java b/services/src/main/java/org/keycloak/services/resources/WelcomeResource.java index ef4bef219c..7a2632b751 100755 --- a/services/src/main/java/org/keycloak/services/resources/WelcomeResource.java +++ b/services/src/main/java/org/keycloak/services/resources/WelcomeResource.java @@ -231,7 +231,6 @@ public class WelcomeResource { } private void updateCsrfChecks() { - RealmModel realm = session.realms().getRealmByName(Config.getAdminRealm()); Cookie cookie = headers.getCookies().get(KEYCLOAK_STATE_CHECKER); if (cookie != null) { stateChecker = cookie.getValue(); diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/RealmTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/RealmTest.java index 511e6b58e2..6b842b04b3 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/RealmTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/RealmTest.java @@ -70,6 +70,34 @@ public class RealmTest extends AbstractClientTest { } } + @Test + public void renameRealm() { + RealmRepresentation rep = new RealmRepresentation(); + rep.setId("old"); + rep.setRealm("old"); + + try { + keycloak.realms().create(rep); + + rep.setRealm("new"); + keycloak.realm("old").update(rep); + + // Check client in master realm renamed + assertEquals(0, keycloak.realm("master").clients().findByClientId("old-realm").size()); + assertEquals(1, keycloak.realm("master").clients().findByClientId("new-realm").size()); + + ClientRepresentation adminClient = keycloak.realm("new").clients().findByClientId(Constants.ADMIN_CONSOLE_CLIENT_ID).get(0); + assertEquals("/auth/admin/new/console/index.html", adminClient.getBaseUrl()); + assertEquals("/auth/admin/new/console/*", adminClient.getRedirectUris().get(0)); + + ClientRepresentation accountClient = keycloak.realm("new").clients().findByClientId(Constants.ACCOUNT_MANAGEMENT_CLIENT_ID).get(0); + assertEquals("/auth/realms/new/account", accountClient.getBaseUrl()); + assertEquals("/auth/realms/new/account/*", accountClient.getRedirectUris().get(0)); + } finally { + keycloak.realms().realm(rep.getRealm()).remove(); + } + } + @Test public void createRealmEmpty() { try {