Merge pull request #1060 from stianst/master

KEYCLOAK-1080 Unrecognized field social when importing from 1.1.0.Final
This commit is contained in:
Stian Thorgersen 2015-03-19 14:50:49 +01:00
commit 8a38597b3a
4 changed files with 136 additions and 6 deletions

View file

@ -25,6 +25,13 @@ public class RealmRepresentation {
protected Boolean verifyEmail; protected Boolean verifyEmail;
protected Boolean resetPasswordAllowed; protected Boolean resetPasswordAllowed;
@Deprecated
protected Boolean social;
@Deprecated
protected Boolean updateProfileOnInitialSocialLogin;
@Deprecated
protected Map<String, String> socialProviders;
protected Boolean userCacheEnabled; protected Boolean userCacheEnabled;
protected Boolean realmCacheEnabled; protected Boolean realmCacheEnabled;
@ -313,6 +320,22 @@ public class RealmRepresentation {
this.resetPasswordAllowed = resetPassword; this.resetPasswordAllowed = resetPassword;
} }
public Boolean isSocial() {
return social;
}
public void setSocial(Boolean social) {
this.social = social;
}
public Boolean isUpdateProfileOnInitialSocialLogin() {
return updateProfileOnInitialSocialLogin;
}
public void setUpdateProfileOnInitialSocialLogin(Boolean updateProfileOnInitialSocialLogin) {
this.updateProfileOnInitialSocialLogin = updateProfileOnInitialSocialLogin;
}
public Map<String, String> getBrowserSecurityHeaders() { public Map<String, String> getBrowserSecurityHeaders() {
return browserSecurityHeaders; return browserSecurityHeaders;
} }
@ -321,6 +344,14 @@ public class RealmRepresentation {
this.browserSecurityHeaders = browserSecurityHeaders; this.browserSecurityHeaders = browserSecurityHeaders;
} }
public Map<String, String> getSocialProviders() {
return socialProviders;
}
public void setSocialProviders(Map<String, String> socialProviders) {
this.socialProviders = socialProviders;
}
public Map<String, String> getSmtpServer() { public Map<String, String> getSmtpServer() {
return smtpServer; return smtpServer;
} }
@ -482,10 +513,6 @@ public class RealmRepresentation {
} }
public List<IdentityProviderRepresentation> getIdentityProviders() { public List<IdentityProviderRepresentation> getIdentityProviders() {
if (this.identityProviders == null) {
this.identityProviders = new ArrayList<IdentityProviderRepresentation>();
}
return identityProviders; return identityProviders;
} }
@ -494,11 +521,12 @@ public class RealmRepresentation {
} }
public void addIdentityProvider(IdentityProviderRepresentation identityProviderRepresentation) { public void addIdentityProvider(IdentityProviderRepresentation identityProviderRepresentation) {
getIdentityProviders().add(identityProviderRepresentation); if (identityProviders == null) identityProviders = new LinkedList<>();
identityProviders.add(identityProviderRepresentation);
} }
public boolean isIdentityFederationEnabled() { public boolean isIdentityFederationEnabled() {
return !getIdentityProviders().isEmpty(); return identityProviders != null && !identityProviders.isEmpty();
} }
public List<ProtocolMapperRepresentation> getProtocolMappers() { public List<ProtocolMapperRepresentation> getProtocolMappers() {

View file

@ -0,0 +1,35 @@
package org.keycloak.representations.idm;
/**
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/
public class SocialLinkRepresentation {
protected String socialProvider;
protected String socialUserId;
protected String socialUsername;
public String getSocialProvider() {
return socialProvider;
}
public void setSocialProvider(String socialProvider) {
this.socialProvider = socialProvider;
}
public String getSocialUserId() {
return socialUserId;
}
public void setSocialUserId(String socialUserId) {
this.socialUserId = socialUserId;
}
public String getSocialUsername() {
return socialUsername;
}
public void setSocialUsername(String socialUsername) {
this.socialUsername = socialUsername;
}
}

View file

@ -25,6 +25,8 @@ public class UserRepresentation {
protected List<CredentialRepresentation> credentials; protected List<CredentialRepresentation> credentials;
protected List<String> requiredActions; protected List<String> requiredActions;
protected List<FederatedIdentityRepresentation> federatedIdentities; protected List<FederatedIdentityRepresentation> federatedIdentities;
@Deprecated
protected List<SocialLinkRepresentation> socialLinks;
protected List<String> realmRoles; protected List<String> realmRoles;
protected Map<String, List<String>> applicationRoles; protected Map<String, List<String>> applicationRoles;
@ -147,6 +149,14 @@ public class UserRepresentation {
this.federatedIdentities = federatedIdentities; this.federatedIdentities = federatedIdentities;
} }
public List<SocialLinkRepresentation> getSocialLinks() {
return socialLinks;
}
public void setSocialLinks(List<SocialLinkRepresentation> socialLinks) {
this.socialLinks = socialLinks;
}
public List<String> getRealmRoles() { public List<String> getRealmRoles() {
return realmRoles; return realmRoles;
} }

View file

@ -31,6 +31,7 @@ import org.keycloak.representations.idm.ProtocolMapperRepresentation;
import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.representations.idm.RoleRepresentation; import org.keycloak.representations.idm.RoleRepresentation;
import org.keycloak.representations.idm.ScopeMappingRepresentation; import org.keycloak.representations.idm.ScopeMappingRepresentation;
import org.keycloak.representations.idm.SocialLinkRepresentation;
import org.keycloak.representations.idm.UserFederationProviderRepresentation; import org.keycloak.representations.idm.UserFederationProviderRepresentation;
import org.keycloak.representations.idm.UserRepresentation; import org.keycloak.representations.idm.UserRepresentation;
@ -39,6 +40,7 @@ import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -48,6 +50,8 @@ public class RepresentationToModel {
private static Logger logger = Logger.getLogger(RepresentationToModel.class); private static Logger logger = Logger.getLogger(RepresentationToModel.class);
public static void importRealm(KeycloakSession session, RealmRepresentation rep, RealmModel newRealm) { public static void importRealm(KeycloakSession session, RealmRepresentation rep, RealmModel newRealm) {
convertDeprecatedSocialProviders(rep);
newRealm.setName(rep.getRealm()); newRealm.setName(rep.getRealm());
if (rep.isEnabled() != null) newRealm.setEnabled(rep.isEnabled()); if (rep.isEnabled() != null) newRealm.setEnabled(rep.isEnabled());
if (rep.isBruteForceProtected() != null) newRealm.setBruteForceProtected(rep.isBruteForceProtected()); if (rep.isBruteForceProtected() != null) newRealm.setBruteForceProtected(rep.isBruteForceProtected());
@ -255,6 +259,57 @@ public class RepresentationToModel {
} }
} }
private static void convertDeprecatedSocialProviders(RealmRepresentation rep) {
if (rep.isSocial() != null && rep.isSocial() && rep.getSocialProviders() != null && !rep.getSocialProviders().isEmpty() && rep.getIdentityProviders() == null) {
Boolean updateProfileFirstLogin = rep.isUpdateProfileOnInitialSocialLogin() != null && rep.isUpdateProfileOnInitialSocialLogin();
if (rep.getSocialProviders() != null) {
List<IdentityProviderRepresentation> identityProviders = new LinkedList<>();
for (String k : rep.getSocialProviders().keySet()) {
if (k.endsWith(".key")) {
String providerId = k.split("\\.")[0];
String key = rep.getSocialProviders().get(k);
String secret = rep.getSocialProviders().get(k.replace(".key", ".secret"));
IdentityProviderRepresentation identityProvider = new IdentityProviderRepresentation();
identityProvider.setId(providerId);
identityProvider.setProviderId(providerId);
identityProvider.setName(providerId);
identityProvider.setEnabled(true);
identityProvider.setUpdateProfileFirstLogin(updateProfileFirstLogin);
Map<String, String> config = new HashMap<>();
config.put("clientId", key);
config.put("clientSecret", secret);
identityProvider.setConfig(config);
identityProviders.add(identityProvider);
}
}
rep.setIdentityProviders(identityProviders);
}
}
rep.setSocial(null);
rep.setSocialProviders(null);
rep.setUpdateProfileOnInitialSocialLogin(false);
}
private static void convertDeprecatedSocialProviders(UserRepresentation user) {
if (user.getSocialLinks() != null && !user.getSocialLinks().isEmpty() && user.getFederatedIdentities() == null) {
List<FederatedIdentityRepresentation> federatedIdentities = new LinkedList<>();
for (SocialLinkRepresentation social : user.getSocialLinks()) {
FederatedIdentityRepresentation federatedIdentity = new FederatedIdentityRepresentation();
federatedIdentity.setIdentityProvider(social.getSocialProvider());
federatedIdentity.setUserId(social.getSocialUserId());
federatedIdentity.setUserName(social.getSocialUsername());
}
user.setFederatedIdentities(federatedIdentities);
}
user.setSocialLinks(null);
}
public static void updateRealm(RealmRepresentation rep, RealmModel realm) { public static void updateRealm(RealmRepresentation rep, RealmModel realm) {
if (rep.getRealm() != null) { if (rep.getRealm() != null) {
realm.setName(rep.getRealm()); realm.setName(rep.getRealm());
@ -688,6 +743,8 @@ public class RepresentationToModel {
// Users // Users
public static UserModel createUser(KeycloakSession session, RealmModel newRealm, UserRepresentation userRep, Map<String, ApplicationModel> appMap) { public static UserModel createUser(KeycloakSession session, RealmModel newRealm, UserRepresentation userRep, Map<String, ApplicationModel> appMap) {
convertDeprecatedSocialProviders(userRep);
// Import users just to user storage. Don't federate // Import users just to user storage. Don't federate
UserModel user = session.userStorage().addUser(newRealm, userRep.getId(), userRep.getUsername(), false); UserModel user = session.userStorage().addUser(newRealm, userRep.getId(), userRep.getUsername(), false);
user.setEnabled(userRep.isEnabled()); user.setEnabled(userRep.isEnabled());