private void addDefaultMappers(KeycloakSession session, DBCollection clients) { DBCursor clientsCursor = clients.find(); try { while (clientsCursor.hasNext()) { BasicDBObject currentClient = (BasicDBObject) clientsCursor.next(); BasicDBList dbProtocolMappers = new BasicDBList(); currentClient.put("protocolMappers", dbProtocolMappers); Object claimMask = currentClient.get("allowedClaimsMask"); MigrationProvider migrationProvider = session.getProvider(MigrationProvider.class); List<ProtocolMapperRepresentation> protocolMappers = migrationProvider.getMappersForClaimMask((Long) claimMask); for (ProtocolMapperRepresentation protocolMapper : protocolMappers) { BasicDBObject dbMapper = new BasicDBObject(); dbMapper.put("id", KeycloakModelUtils.generateId()); dbMapper.put("protocol", protocolMapper.getProtocol()); dbMapper.put("name", protocolMapper.getName()); dbMapper.put("consentRequired", protocolMapper.isConsentRequired()); dbMapper.put("consentText", protocolMapper.getConsentText()); dbMapper.put("protocolMapper", protocolMapper.getProtocolMapper()); Map<String, String> config = protocolMapper.getConfig(); BasicDBObject dbConfig = MapMapper.convertMap(config); dbMapper.put("config", dbConfig); dbProtocolMappers.add(dbMapper); } // Remove obsolete keys from client currentClient.remove("allowedClaimsMask"); log.debugv("Added default mappers to application {1}", currentClient.get("name")); clients.save(currentClient); } } finally { clientsCursor.close(); } }
protected void addDefaultProtocolMappers() throws SQLException, DatabaseException { String protocolMapperTableName = database.correctObjectName("PROTOCOL_MAPPER", Table.class); String protocolMapperCfgTableName = database.correctObjectName("PROTOCOL_MAPPER_CONFIG", Table.class); PreparedStatement statement = jdbcConnection.prepareStatement("select ID, NAME, ALLOWED_CLAIMS_MASK from CLIENT"); try { ResultSet resultSet = statement.executeQuery(); try { boolean first = true; while (resultSet.next()) { if (first) { confirmationMessage.append("Migrating claimsMask to protocol mappers for clients: "); first = false; } Object acmObj = resultSet.getObject("ALLOWED_CLAIMS_MASK"); long mask = (acmObj != null) ? ((Number) acmObj).longValue() : ClaimMask.ALL; MigrationProvider migrationProvider = this.kcSession.getProvider(MigrationProvider.class); List<ProtocolMapperRepresentation> protocolMappers = migrationProvider.getMappersForClaimMask(mask); for (ProtocolMapperRepresentation protocolMapper : protocolMappers) { String mapperId = KeycloakModelUtils.generateId(); InsertStatement insert = new InsertStatement(null, null, protocolMapperTableName) .addColumnValue("ID", mapperId) .addColumnValue("PROTOCOL", protocolMapper.getProtocol()) .addColumnValue("NAME", protocolMapper.getName()) .addColumnValue("CONSENT_REQUIRED", protocolMapper.isConsentRequired()) .addColumnValue("CONSENT_TEXT", protocolMapper.getConsentText()) .addColumnValue("PROTOCOL_MAPPER_NAME", protocolMapper.getProtocolMapper()) .addColumnValue("CLIENT_ID", resultSet.getString("ID")); statements.add(insert); for (Map.Entry<String, String> cfgEntry : protocolMapper.getConfig().entrySet()) { InsertStatement cfgInsert = new InsertStatement(null, null, protocolMapperCfgTableName) .addColumnValue("PROTOCOL_MAPPER_ID", mapperId) .addColumnValue("NAME", cfgEntry.getKey()) .addColumnValue("VALUE", cfgEntry.getValue()); statements.add(cfgInsert); } } confirmationMessage.append(resultSet.getString("NAME") + ", "); } // It means that some provider where processed if (!first) { confirmationMessage.append(". "); } } finally { resultSet.close(); } } finally { statement.close(); } }