From eaae4eb986ff86825fa03aba743fa0427539dcd9 Mon Sep 17 00:00:00 2001 From: mposolda Date: Fri, 23 Jan 2015 13:01:39 +0100 Subject: [PATCH] KEYCLOAK-1008 Mongo: Migrate social data from 1.1 database to identity federation data --- .../updater/DefaultMongoUpdaterProvider.java | 4 +- .../updater/updates/Update1_2_0_Beta1.java | 107 ++++++++++++++++++ .../en/en-US/modules/server-installation.xml | 3 +- 3 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 connections/mongo/src/main/java/org/keycloak/connections/mongo/updater/updates/Update1_2_0_Beta1.java diff --git a/connections/mongo/src/main/java/org/keycloak/connections/mongo/updater/DefaultMongoUpdaterProvider.java b/connections/mongo/src/main/java/org/keycloak/connections/mongo/updater/DefaultMongoUpdaterProvider.java index 1f3f6ae198..7c8a44fb89 100644 --- a/connections/mongo/src/main/java/org/keycloak/connections/mongo/updater/DefaultMongoUpdaterProvider.java +++ b/connections/mongo/src/main/java/org/keycloak/connections/mongo/updater/DefaultMongoUpdaterProvider.java @@ -9,6 +9,7 @@ import org.jboss.logging.Logger; import org.keycloak.connections.mongo.updater.updates.Update; import org.keycloak.connections.mongo.updater.updates.Update1_0_0_Final; import org.keycloak.connections.mongo.updater.updates.Update1_1_0_Beta1; +import org.keycloak.connections.mongo.updater.updates.Update1_2_0_Beta1; import java.util.Arrays; import java.util.Collections; @@ -30,7 +31,8 @@ public class DefaultMongoUpdaterProvider implements MongoUpdaterProvider { private Class[] updates = new Class[]{ Update1_0_0_Final.class, - Update1_1_0_Beta1.class + Update1_1_0_Beta1.class, + Update1_2_0_Beta1.class }; @Override diff --git a/connections/mongo/src/main/java/org/keycloak/connections/mongo/updater/updates/Update1_2_0_Beta1.java b/connections/mongo/src/main/java/org/keycloak/connections/mongo/updater/updates/Update1_2_0_Beta1.java new file mode 100644 index 0000000000..a24253c686 --- /dev/null +++ b/connections/mongo/src/main/java/org/keycloak/connections/mongo/updater/updates/Update1_2_0_Beta1.java @@ -0,0 +1,107 @@ +package org.keycloak.connections.mongo.updater.updates; + +import java.util.Map; + +import com.mongodb.BasicDBList; +import com.mongodb.BasicDBObject; +import com.mongodb.BasicDBObjectBuilder; +import com.mongodb.DBCollection; +import com.mongodb.DBCursor; +import com.mongodb.DBObject; + +/** + * @author Marek Posolda + */ +public class Update1_2_0_Beta1 extends Update { + + @Override + public String getId() { + return "1.2.0.Beta1"; + } + + @Override + public void update() { + convertSocialToIdFedRealms(); + convertSocialToIdFedUsers(); + } + + + private void convertSocialToIdFedRealms() { + DBCollection realms = db.getCollection("realms"); + DBCursor realmsCursor = realms.find(); + while (realmsCursor.hasNext()) { + BasicDBObject realm = (BasicDBObject) realmsCursor.next(); + boolean updateProfileOnInitialSocialLogin = realm.getBoolean("updateProfileOnInitialSocialLogin"); + BasicDBObject socialConfig = (BasicDBObject) realm.get("socialConfig"); + + BasicDBList identityProviders = (BasicDBList) realm.get("identityProviders"); + if (identityProviders == null) { + identityProviders = new BasicDBList(); + realm.put("identityProviders", identityProviders); + } + + if (socialConfig != null) { + for (Map.Entry entry : socialConfig.entrySet()) { + if (entry.getKey().endsWith("###key")) { + String socialProviderId = entry.getKey().substring(0, entry.getKey().indexOf("###")); + String clientId = (String) entry.getValue(); + String clientSecret = socialConfig.getString(socialProviderId + "###secret"); + + DBObject identityProviderConfig = new BasicDBObjectBuilder() + .add("clientId", clientId) + .add("clientSecret", clientSecret).get(); + + DBObject identityProvider = new BasicDBObjectBuilder() + .add("providerId", socialProviderId) + .add("name", socialProviderId) + .add("id", socialProviderId) + .add("updateProfileFirstLogin", updateProfileOnInitialSocialLogin) + .add("enabled", true) + .add("config", identityProviderConfig).get(); + + identityProviders.add(identityProvider); + } + } + } + + // Remove obsolete keys from realm + realm.remove("social"); + realm.remove("updateProfileOnInitialSocialLogin"); + realm.remove("socialConfig"); + + // Update realm in DB now + realms.save(realm); + } + } + + private void convertSocialToIdFedUsers() { + DBCollection users = db.getCollection("users"); + DBCursor usersCursor = users.find(); + while (usersCursor.hasNext()) { + BasicDBObject user = (BasicDBObject) usersCursor.next(); + + BasicDBList socialLinks = (BasicDBList) user.get("socialLinks"); + if (socialLinks != null) { + BasicDBList federatedIdentities = (BasicDBList) user.get("federatedIdentities"); + if (federatedIdentities == null) { + federatedIdentities = new BasicDBList(); + user.put("federatedIdentities", federatedIdentities); + } + + for (Object socialLinkObj : socialLinks) { + BasicDBObject socialLink = (BasicDBObject) socialLinkObj; + BasicDBObject idFedLink = new BasicDBObject(); + idFedLink.put("userName", socialLink.get("socialUsername")); + idFedLink.put("userId", socialLink.get("socialUserId")); + idFedLink.put("identityProvider", socialLink.get("socialProvider")); + + federatedIdentities.add(idFedLink); + } + + // Remove obsolete keys and save user + user.remove("socialLinks"); + users.save(user); + } + } + } +} diff --git a/docbook/reference/en/en-US/modules/server-installation.xml b/docbook/reference/en/en-US/modules/server-installation.xml index 3861a43bfe..a4b5455fe3 100755 --- a/docbook/reference/en/en-US/modules/server-installation.xml +++ b/docbook/reference/en/en-US/modules/server-installation.xml @@ -366,7 +366,8 @@ keycloak-war-dist-all-&project.version;/ "host": "127.0.0.1", "port": "27017", "db": "keycloak", - "connectionsPerHost": 100 + "connectionsPerHost": 100, + "databaseSchema": "update" } } ]]>