Java Code Examples for org.keycloak.models.KeycloakSession#realms()
The following examples show how to use
org.keycloak.models.KeycloakSession#realms() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImportUtils.java From keycloak with Apache License 2.0 | 5 votes |
/** * 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 for (RealmModel currRealm : model.getRealms()) { currRealm.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; }
Example 2
Source File: ImportUtils.java From keycloak with Apache License 2.0 | 5 votes |
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(); } }
Example 3
Source File: ImportUtils.java From keycloak with Apache License 2.0 | 5 votes |
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(); } }
Example 4
Source File: RealmManager.java From keycloak with Apache License 2.0 | 4 votes |
public RealmManager(KeycloakSession session) { this.session = session; this.model = session.realms(); }
Example 5
Source File: EmailEventListenerProvider.java From keycloak with Apache License 2.0 | 4 votes |
public EmailEventListenerProvider(KeycloakSession session, EmailTemplateProvider emailTemplateProvider, Set<EventType> includedEvents) { this.session = session; this.model = session.realms(); this.emailTemplateProvider = emailTemplateProvider; this.includedEvents = includedEvents; }