Merge pull request #2520 from stianst/KEYCLOAK-2756

KEYCLOAK-2756
This commit is contained in:
Stian Thorgersen 2016-04-06 19:25:53 +02:00
commit 8de8446cb5
6 changed files with 63 additions and 4 deletions

View file

@ -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("<strong>Keycloak</strong>")) {
realm.setDisplayNameHtml("<div class=\"kc-logo-text\"><span>Keycloak</span></div>");
}

View file

@ -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()));

View file

@ -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<String> 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<String> 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) {

View file

@ -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+"}");

View file

@ -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();

View file

@ -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 {