Search in sources :

Example 1 with RealmProvider

use of org.keycloak.models.RealmProvider in project keycloak by keycloak.

the class ImportUtils method importFederatedUsersFromStream.

// Assuming that it's invoked inside transaction
public static void importFederatedUsersFromStream(KeycloakSession session, String realmName, ObjectMapper mapper, InputStream is) throws IOException {
    RealmProvider model = session.realms();
    JsonFactory factory = mapper.getJsonFactory();
    JsonParser parser = factory.createJsonParser(is);
    try {
        parser.nextToken();
        while (parser.nextToken() == JsonToken.FIELD_NAME) {
            if ("realm".equals(parser.getText())) {
                parser.nextToken();
                String currRealmName = parser.getText();
                if (!currRealmName.equals(realmName)) {
                    throw new IllegalStateException("Trying to import users into invalid realm. Realm name: " + realmName + ", Expected realm name: " + currRealmName);
                }
            } else if ("federatedUsers".equals(parser.getText())) {
                parser.nextToken();
                if (parser.getCurrentToken() == JsonToken.START_ARRAY) {
                    parser.nextToken();
                }
                // TODO: support for more transactions per single users file (if needed)
                List<UserRepresentation> userReps = new ArrayList<UserRepresentation>();
                while (parser.getCurrentToken() == JsonToken.START_OBJECT) {
                    UserRepresentation user = parser.readValueAs(UserRepresentation.class);
                    userReps.add(user);
                    parser.nextToken();
                }
                importFederatedUsers(session, model, realmName, userReps);
                if (parser.getCurrentToken() == JsonToken.END_ARRAY) {
                    parser.nextToken();
                }
            }
        }
    } finally {
        parser.close();
    }
}
Also used : RealmProvider(org.keycloak.models.RealmProvider) JsonFactory(com.fasterxml.jackson.core.JsonFactory) ArrayList(java.util.ArrayList) List(java.util.List) JsonParser(com.fasterxml.jackson.core.JsonParser) UserRepresentation(org.keycloak.representations.idm.UserRepresentation)

Example 2 with RealmProvider

use of org.keycloak.models.RealmProvider in project keycloak by keycloak.

the class ImportUtils method importRealm.

/**
 * Fully import realm from representation, save it to model and return model of newly created realm
 *
 * @param session
 * @param rep
 * @param strategy specifies whether to overwrite or ignore existing realm or user entries
 * @param skipUserDependent If true, then import of any models, which needs users already imported in DB, will be skipped. For example authorization
 * @return newly imported realm (or existing realm if ignoreExisting is true and realm of this name already exists)
 */
public static boolean importRealm(KeycloakSession session, RealmRepresentation rep, Strategy strategy, boolean skipUserDependent) {
    String realmName = rep.getRealm();
    RealmProvider model = session.realms();
    RealmModel realm = model.getRealmByName(realmName);
    if (realm != null) {
        if (strategy == Strategy.IGNORE_EXISTING) {
            logger.infof("Realm '%s' already exists. Import skipped", realmName);
            return false;
        } else {
            logger.infof("Realm '%s' already exists. Removing it before import", realmName);
            if (Config.getAdminRealm().equals(realm.getId())) {
                // Delete all masterAdmin apps due to foreign key constraints
                model.getRealmsStream().forEach(r -> r.setMasterAdminClient(null));
            }
            // TODO: For migration between versions, it should be possible to delete just realm but keep it's users
            model.removeRealm(realm.getId());
        }
    }
    RealmManager realmManager = new RealmManager(session);
    realmManager.importRealm(rep, skipUserDependent);
    if (System.getProperty(ExportImportConfig.ACTION) != null) {
        logger.infof("Realm '%s' imported", realmName);
    }
    return true;
}
Also used : RealmModel(org.keycloak.models.RealmModel) RealmProvider(org.keycloak.models.RealmProvider) RealmManager(org.keycloak.services.managers.RealmManager)

Example 3 with RealmProvider

use of org.keycloak.models.RealmProvider in project keycloak by keycloak.

the class ImportUtils method importUsersFromStream.

// Assuming that it's invoked inside transaction
public static void importUsersFromStream(KeycloakSession session, String realmName, ObjectMapper mapper, InputStream is) throws IOException {
    RealmProvider model = session.realms();
    JsonFactory factory = mapper.getJsonFactory();
    JsonParser parser = factory.createJsonParser(is);
    try {
        parser.nextToken();
        while (parser.nextToken() == JsonToken.FIELD_NAME) {
            if ("realm".equals(parser.getText())) {
                parser.nextToken();
                String currRealmName = parser.getText();
                if (!currRealmName.equals(realmName)) {
                    throw new IllegalStateException("Trying to import users into invalid realm. Realm name: " + realmName + ", Expected realm name: " + currRealmName);
                }
            } else if ("users".equals(parser.getText())) {
                parser.nextToken();
                if (parser.getCurrentToken() == JsonToken.START_ARRAY) {
                    parser.nextToken();
                }
                // TODO: support for more transactions per single users file (if needed)
                List<UserRepresentation> userReps = new ArrayList<UserRepresentation>();
                while (parser.getCurrentToken() == JsonToken.START_OBJECT) {
                    UserRepresentation user = parser.readValueAs(UserRepresentation.class);
                    userReps.add(user);
                    parser.nextToken();
                }
                importUsers(session, model, realmName, userReps);
                if (parser.getCurrentToken() == JsonToken.END_ARRAY) {
                    parser.nextToken();
                }
            }
        }
    } finally {
        parser.close();
    }
}
Also used : RealmProvider(org.keycloak.models.RealmProvider) JsonFactory(com.fasterxml.jackson.core.JsonFactory) ArrayList(java.util.ArrayList) List(java.util.List) JsonParser(com.fasterxml.jackson.core.JsonParser) UserRepresentation(org.keycloak.representations.idm.UserRepresentation)

Example 4 with RealmProvider

use of org.keycloak.models.RealmProvider in project keycloak by keycloak.

the class TestingResourceProvider method getRealmByName.

private RealmModel getRealmByName(String realmName) {
    RealmProvider realmProvider = session.getProvider(RealmProvider.class);
    RealmModel realm = realmProvider.getRealmByName(realmName);
    if (realm == null) {
        throw new NotFoundException("Realm not found");
    }
    return realm;
}
Also used : RealmModel(org.keycloak.models.RealmModel) RealmProvider(org.keycloak.models.RealmProvider) NotFoundException(javax.ws.rs.NotFoundException)

Aggregations

RealmProvider (org.keycloak.models.RealmProvider)4 JsonFactory (com.fasterxml.jackson.core.JsonFactory)2 JsonParser (com.fasterxml.jackson.core.JsonParser)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 RealmModel (org.keycloak.models.RealmModel)2 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)2 NotFoundException (javax.ws.rs.NotFoundException)1 RealmManager (org.keycloak.services.managers.RealmManager)1