From fe1ede26bea3e24597aa142148f31a7a4e4de09d Mon Sep 17 00:00:00 2001 From: mposolda Date: Sat, 11 Jul 2015 17:27:59 +0200 Subject: [PATCH] KEYCLOAK-1558 Can't import the file exported in 1.2.0.Beta1 through admin console --- .../services/managers/RealmManager.java | 81 +++++++++++-------- .../resources/admin/RealmsAdminResource.java | 8 +- 2 files changed, 50 insertions(+), 39 deletions(-) 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 1aff2edd77fa..a0e12359b11f 100755 --- a/services/src/main/java/org/keycloak/services/managers/RealmManager.java +++ b/services/src/main/java/org/keycloak/services/managers/RealmManager.java @@ -21,7 +21,9 @@ import org.keycloak.models.utils.DefaultRequiredActions; import org.keycloak.models.utils.KeycloakModelUtils; import org.keycloak.models.utils.RepresentationToModel; +import org.keycloak.representations.idm.ApplicationRepresentation; import org.keycloak.representations.idm.ClientRepresentation; +import org.keycloak.representations.idm.OAuthClientRepresentation; import org.keycloak.representations.idm.RealmEventsConfigRepresentation; import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.timer.TimerProvider; @@ -267,13 +269,27 @@ public RealmModel importRealm(RealmRepresentation rep) { setupMasterAdminManagement(realm); if (!hasRealmAdminManagementClient(rep)) setupRealmAdminManagement(realm); if (!hasAccountManagementClient(rep)) setupAccountManagement(realm); - if (!hasImpersonationServiceClient(rep)) setupImpersonationService(realm); + + boolean postponeImpersonationSetup = false; + if (!hasImpersonationServiceClient(rep)) { + if (hasRealmAdminManagementClient(rep)) { + postponeImpersonationSetup = true; + } else { + setupImpersonationService(realm); + } + } if (!hasBrokerClient(rep)) setupBrokerService(realm); if (!hasAdminConsoleClient(rep)) setupAdminConsole(realm); RepresentationToModel.importRealm(session, rep, realm); + // Could happen when migrating from older version and I have exported JSON file, which contains "realm-management" client but not "impersonation" client + // I need to postpone impersonation because it needs "realm-management" client and it's roles set + if (postponeImpersonationSetup) { + setupImpersonationService(realm); + } + setupAuthenticationFlows(realm); setupRequiredActions(realm); @@ -287,50 +303,49 @@ public RealmModel importRealm(RealmRepresentation rep) { } private boolean hasRealmAdminManagementClient(RealmRepresentation rep) { - if (rep.getClients() == null) return false; - for (ClientRepresentation clientRep : rep.getClients()) { - if (clientRep.getClientId().equals(getRealmAdminClientId(rep))) { - return true; - } - } - return false; + String realmAdminClientId = getRealmAdminClientId(rep); + return hasClient(rep, realmAdminClientId); } private boolean hasAccountManagementClient(RealmRepresentation rep) { - if (rep.getClients() == null) return false; - for (ClientRepresentation clientRep : rep.getClients()) { - if (clientRep.getClientId().equals(Constants.ACCOUNT_MANAGEMENT_CLIENT_ID)) { - return true; - } - } - return false; + return hasClient(rep, Constants.ACCOUNT_MANAGEMENT_CLIENT_ID); } private boolean hasImpersonationServiceClient(RealmRepresentation rep) { - if (rep.getClients() == null) return false; - for (ClientRepresentation clientRep : rep.getClients()) { - if (clientRep.getClientId().equals(Constants.IMPERSONATION_SERVICE_CLIENT_ID)) { - return true; - } - } - return false; + return hasClient(rep, Constants.IMPERSONATION_SERVICE_CLIENT_ID); } private boolean hasBrokerClient(RealmRepresentation rep) { - if (rep.getClients() == null) return false; - for (ClientRepresentation clientRep : rep.getClients()) { - if (clientRep.getClientId().equals(Constants.BROKER_SERVICE_CLIENT_ID)) { - return true; - } - } - return false; + return hasClient(rep, Constants.BROKER_SERVICE_CLIENT_ID); } private boolean hasAdminConsoleClient(RealmRepresentation rep) { - if (rep.getClients() == null) return false; - for (ClientRepresentation clientRep : rep.getClients()) { - if (clientRep.getClientId().equals(Constants.ADMIN_CONSOLE_CLIENT_ID)) { - return true; + return hasClient(rep, Constants.ADMIN_CONSOLE_CLIENT_ID); + } + + private boolean hasClient(RealmRepresentation rep, String clientId) { + if (rep.getClients() != null) { + for (ClientRepresentation clientRep : rep.getClients()) { + if (clientRep.getClientId().equals(clientId)) { + return true; + } } } + + // TODO: Just for compatibility with old versions. Should be removed later... + if (rep.getApplications() != null) { + for (ApplicationRepresentation clientRep : rep.getApplications()) { + if (clientRep.getName().equals(clientId)) { + return true; + } + } + } + if (rep.getOauthClients() != null) { + for (OAuthClientRepresentation clientRep : rep.getOauthClients()) { + if (clientRep.getName().equals(clientId)) { + return true; + } + } + } + return false; } diff --git a/services/src/main/java/org/keycloak/services/resources/admin/RealmsAdminResource.java b/services/src/main/java/org/keycloak/services/resources/admin/RealmsAdminResource.java index 2d14b4bc3019..5b5a611a4e75 100755 --- a/services/src/main/java/org/keycloak/services/resources/admin/RealmsAdminResource.java +++ b/services/src/main/java/org/keycloak/services/resources/admin/RealmsAdminResource.java @@ -168,12 +168,8 @@ public Response uploadRealm(@Context final UriInfo uriInfo, MultipartFormDataInp for (InputPart inputPart : inputParts) { // inputPart.getBody doesn't work as content-type is wrong, and inputPart.setMediaType is not supported on AS7 (RestEasy 2.3.2.Final) rep = JsonSerialization.readValue(inputPart.getBodyAsString(), RealmRepresentation.class); - RealmModel realm; - try { - realm = realmManager.importRealm(rep); - } catch (ModelDuplicateException e) { - return ErrorResponse.exists("Realm " + rep.getRealm() + " already exists"); - } + + RealmModel realm = realmManager.importRealm(rep); grantPermissionsToRealmCreator(realm);