Search in sources :

Example 1 with UserFederationMapperRepresentation

use of org.keycloak.representations.idm.UserFederationMapperRepresentation in project keycloak by keycloak.

the class RepresentationToModel method importUserFederationProvidersAndMappers.

public static void importUserFederationProvidersAndMappers(KeycloakSession session, RealmRepresentation rep, RealmModel newRealm) {
    // providers to convert to component model
    Set<String> convertSet = new HashSet<>();
    convertSet.add(LDAPConstants.LDAP_PROVIDER);
    convertSet.add("kerberos");
    Map<String, String> mapperConvertSet = new HashMap<>();
    mapperConvertSet.put(LDAPConstants.LDAP_PROVIDER, "org.keycloak.storage.ldap.mappers.LDAPStorageMapper");
    Map<String, ComponentModel> userStorageModels = new HashMap<>();
    if (rep.getUserFederationProviders() != null) {
        for (UserFederationProviderRepresentation fedRep : rep.getUserFederationProviders()) {
            if (convertSet.contains(fedRep.getProviderName())) {
                ComponentModel component = convertFedProviderToComponent(newRealm.getId(), fedRep);
                userStorageModels.put(fedRep.getDisplayName(), newRealm.importComponentModel(component));
            }
        }
    }
    // This is for case, when you have hand-written JSON file with LDAP userFederationProvider, but WITHOUT any userFederationMappers configured. Default LDAP mappers need to be created in that case.
    Set<String> storageProvidersWhichShouldImportDefaultMappers = new HashSet<>(userStorageModels.keySet());
    if (rep.getUserFederationMappers() != null) {
        for (UserFederationMapperRepresentation representation : rep.getUserFederationMappers()) {
            if (userStorageModels.containsKey(representation.getFederationProviderDisplayName())) {
                ComponentModel parent = userStorageModels.get(representation.getFederationProviderDisplayName());
                String newMapperType = mapperConvertSet.get(parent.getProviderId());
                ComponentModel mapper = convertFedMapperToComponent(newRealm, parent, representation, newMapperType);
                newRealm.importComponentModel(mapper);
                storageProvidersWhichShouldImportDefaultMappers.remove(representation.getFederationProviderDisplayName());
            }
        }
    }
    for (String providerDisplayName : storageProvidersWhichShouldImportDefaultMappers) {
        ComponentUtil.notifyCreated(session, newRealm, userStorageModels.get(providerDisplayName));
    }
}
Also used : UserFederationProviderRepresentation(org.keycloak.representations.idm.UserFederationProviderRepresentation) MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) HashMap(java.util.HashMap) ComponentModel(org.keycloak.component.ComponentModel) UserFederationMapperRepresentation(org.keycloak.representations.idm.UserFederationMapperRepresentation) ArtifactBindingUtils.computeArtifactBindingIdentifierString(org.keycloak.protocol.saml.util.ArtifactBindingUtils.computeArtifactBindingIdentifierString) HashSet(java.util.HashSet)

Aggregations

HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 MultivaluedHashMap (org.keycloak.common.util.MultivaluedHashMap)1 ComponentModel (org.keycloak.component.ComponentModel)1 ArtifactBindingUtils.computeArtifactBindingIdentifierString (org.keycloak.protocol.saml.util.ArtifactBindingUtils.computeArtifactBindingIdentifierString)1 UserFederationMapperRepresentation (org.keycloak.representations.idm.UserFederationMapperRepresentation)1 UserFederationProviderRepresentation (org.keycloak.representations.idm.UserFederationProviderRepresentation)1