From a634f1a65f30d0c3bd55711c62a054a0860d6843 Mon Sep 17 00:00:00 2001 From: mposolda Date: Thu, 22 Jan 2015 14:57:47 +0100 Subject: [PATCH] Fix mongo model --- ...DefaultMongoConnectionFactoryProvider.java | 3 +- .../entities/IdentityProviderEntity.java | 46 +++++++++++++------ .../keycloak/models/entities/RealmEntity.java | 10 +++- .../keycloak/models/entities/UserEntity.java | 10 ++-- .../keycloak/adapters/MongoUserProvider.java | 12 ++--- .../mongo/keycloak/adapters/RealmAdapter.java | 46 +++++++++++++++++-- 6 files changed, 96 insertions(+), 31 deletions(-) diff --git a/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java b/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java index 341cb014ca..e52816c825 100644 --- a/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java +++ b/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java @@ -26,9 +26,10 @@ public class DefaultMongoConnectionFactoryProvider implements MongoConnectionPro "org.keycloak.models.mongo.keycloak.entities.MongoRealmEntity", "org.keycloak.models.mongo.keycloak.entities.MongoUserEntity", "org.keycloak.models.mongo.keycloak.entities.MongoRoleEntity", + "org.keycloak.models.entities.IdentityProviderEntity", "org.keycloak.models.entities.RequiredCredentialEntity", "org.keycloak.models.entities.CredentialEntity", - "org.keycloak.models.entities.SocialLinkEntity", + "org.keycloak.models.entities.FederatedIdentityEntity", "org.keycloak.models.mongo.keycloak.entities.MongoApplicationEntity", "org.keycloak.models.mongo.keycloak.entities.MongoOAuthClientEntity", "org.keycloak.models.sessions.mongo.entities.MongoUsernameLoginFailureEntity", diff --git a/model/api/src/main/java/org/keycloak/models/entities/IdentityProviderEntity.java b/model/api/src/main/java/org/keycloak/models/entities/IdentityProviderEntity.java index 06d2ccae08..71109a4fc5 100644 --- a/model/api/src/main/java/org/keycloak/models/entities/IdentityProviderEntity.java +++ b/model/api/src/main/java/org/keycloak/models/entities/IdentityProviderEntity.java @@ -25,19 +25,13 @@ import java.util.Map; */ public class IdentityProviderEntity { - private String id; private String name; - private String iconUrl; + private boolean enabled; + private boolean updateProfileFirstLogin; + private String providerId; + private String id; private Map config = new HashMap(); - public String getId() { - return this.id; - } - - public void setId(String id) { - this.id = id; - } - public String getName() { return this.name; } @@ -46,12 +40,36 @@ public class IdentityProviderEntity { this.name = name; } - public String getIconUrl() { - return this.iconUrl; + public boolean isEnabled() { + return enabled; } - public void setIconUrl(String iconUrl) { - this.iconUrl = iconUrl; + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public boolean isUpdateProfileFirstLogin() { + return updateProfileFirstLogin; + } + + public void setUpdateProfileFirstLogin(boolean updateProfileFirstLogin) { + this.updateProfileFirstLogin = updateProfileFirstLogin; + } + + public String getProviderId() { + return providerId; + } + + public void setProviderId(String providerId) { + this.providerId = providerId; + } + + public String getId() { + return id; + } + + public void setId(String providerNonimalId) { + this.id = providerNonimalId; } public Map getConfig() { diff --git a/model/api/src/main/java/org/keycloak/models/entities/RealmEntity.java b/model/api/src/main/java/org/keycloak/models/entities/RealmEntity.java index 07968085e3..6da7387c5d 100755 --- a/model/api/src/main/java/org/keycloak/models/entities/RealmEntity.java +++ b/model/api/src/main/java/org/keycloak/models/entities/RealmEntity.java @@ -18,7 +18,6 @@ public class RealmEntity extends AbstractIdentifiableEntity { private boolean verifyEmail; private boolean passwordCredentialGrantAllowed; private boolean resetPasswordAllowed; - private boolean social; private String passwordPolicy; //--- brute force settings private boolean bruteForceProtected; @@ -52,6 +51,7 @@ public class RealmEntity extends AbstractIdentifiableEntity { private List requiredCredentials = new ArrayList(); private List userFederationProviders = new ArrayList(); + private List identityProviders = new ArrayList(); private Map browserSecurityHeaders = new HashMap(); private Map smtpConfig = new HashMap(); @@ -375,6 +375,14 @@ public class RealmEntity extends AbstractIdentifiableEntity { this.userFederationProviders = userFederationProviders; } + public List getIdentityProviders() { + return identityProviders; + } + + public void setIdentityProviders(List identityProviders) { + this.identityProviders = identityProviders; + } + public String getCertificatePem() { return certificatePem; } diff --git a/model/api/src/main/java/org/keycloak/models/entities/UserEntity.java b/model/api/src/main/java/org/keycloak/models/entities/UserEntity.java index c84a805c75..d2a20988f8 100755 --- a/model/api/src/main/java/org/keycloak/models/entities/UserEntity.java +++ b/model/api/src/main/java/org/keycloak/models/entities/UserEntity.java @@ -26,7 +26,7 @@ public class UserEntity extends AbstractIdentifiableEntity { private Map attributes; private List requiredActions; private List credentials = new ArrayList(); - private List socialLinks; + private List federatedIdentities; private String federationLink; public String getUsername() { @@ -125,12 +125,12 @@ public class UserEntity extends AbstractIdentifiableEntity { this.credentials = credentials; } - public List getSocialLinks() { - return socialLinks; + public List getFederatedIdentities() { + return federatedIdentities; } - public void setSocialLinks(List socialLinks) { - this.socialLinks = socialLinks; + public void setFederatedIdentities(List federatedIdentities) { + this.federatedIdentities = federatedIdentities; } public String getFederationLink() { diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/MongoUserProvider.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/MongoUserProvider.java index c67952a7be..a542c2292d 100755 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/MongoUserProvider.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/MongoUserProvider.java @@ -216,7 +216,7 @@ public class MongoUserProvider implements UserProvider { public Set getFederatedIdentities(UserModel userModel, RealmModel realm) { UserModel user = getUserById(userModel.getId(), realm); MongoUserEntity userEntity = ((UserAdapter) user).getUser(); - List linkEntities = userEntity.getSocialLinks(); + List linkEntities = userEntity.getFederatedIdentities(); if (linkEntities == null) { return Collections.EMPTY_SET; @@ -234,7 +234,7 @@ public class MongoUserProvider implements UserProvider { private FederatedIdentityEntity findSocialLink(UserModel userModel, String socialProvider, RealmModel realm) { UserModel user = getUserById(userModel.getId(), realm); MongoUserEntity userEntity = ((UserAdapter) user).getUser(); - List linkEntities = userEntity.getSocialLinks(); + List linkEntities = userEntity.getFederatedIdentities(); if (linkEntities == null) { return null; } @@ -311,21 +311,21 @@ public class MongoUserProvider implements UserProvider { public boolean removeFederatedIdentity(RealmModel realm, UserModel userModel, String socialProvider) { UserModel user = getUserById(userModel.getId(), realm); MongoUserEntity userEntity = ((UserAdapter) user).getUser(); - FederatedIdentityEntity federatedIdentityEntity = findSocialLink(userEntity, socialProvider); + FederatedIdentityEntity federatedIdentityEntity = findFederatedIdentityLink(userEntity, socialProvider); if (federatedIdentityEntity == null) { return false; } return getMongoStore().pullItemFromList(userEntity, "federatedIdentities", federatedIdentityEntity, invocationContext); } - private FederatedIdentityEntity findSocialLink(MongoUserEntity userEntity, String socialProvider) { - List linkEntities = userEntity.getSocialLinks(); + private FederatedIdentityEntity findFederatedIdentityLink(MongoUserEntity userEntity, String identityProvider) { + List linkEntities = userEntity.getFederatedIdentities(); if (linkEntities == null) { return null; } for (FederatedIdentityEntity federatedIdentityEntity : linkEntities) { - if (federatedIdentityEntity.getIdentityProvider().equals(socialProvider)) { + if (federatedIdentityEntity.getIdentityProvider().equals(identityProvider)) { return federatedIdentityEntity; } } diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/RealmAdapter.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/RealmAdapter.java index b23d3a0c02..8c323499a7 100755 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/RealmAdapter.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/RealmAdapter.java @@ -15,6 +15,7 @@ import org.keycloak.models.RealmProvider; import org.keycloak.models.RequiredCredentialModel; import org.keycloak.models.RoleModel; import org.keycloak.models.UserFederationProviderModel; +import org.keycloak.models.entities.IdentityProviderEntity; import org.keycloak.models.entities.RequiredCredentialEntity; import org.keycloak.models.entities.UserFederationProviderEntity; import org.keycloak.models.mongo.keycloak.entities.MongoApplicationEntity; @@ -783,22 +784,60 @@ public class RealmAdapter extends AbstractMongoAdapter impleme @Override public List getIdentityProviders() { - return null; + List identityProviders = new ArrayList(); + + for (IdentityProviderEntity entity: realm.getIdentityProviders()) { + IdentityProviderModel identityProviderModel = new IdentityProviderModel(entity.getProviderId(), entity.getId(), entity.getName(), + entity.getConfig()); + + identityProviderModel.setEnabled(entity.isEnabled()); + identityProviderModel.setUpdateProfileFirstLogin(entity.isUpdateProfileFirstLogin()); + + identityProviders.add(identityProviderModel); + } + + return identityProviders; } @Override public void addIdentityProvider(IdentityProviderModel identityProvider) { + IdentityProviderEntity entity = new IdentityProviderEntity(); + entity.setId(identityProvider.getId()); + entity.setProviderId(identityProvider.getProviderId()); + entity.setName(identityProvider.getName()); + entity.setEnabled(identityProvider.isEnabled()); + entity.setUpdateProfileFirstLogin(identityProvider.isUpdateProfileFirstLogin()); + entity.setConfig(identityProvider.getConfig()); + + realm.getIdentityProviders().add(entity); + updateRealm(); } @Override public void removeIdentityProviderById(String providerId) { - + IdentityProviderEntity toRemove; + for (IdentityProviderEntity entity : realm.getIdentityProviders()) { + if (entity.getId().equals(providerId)) { + realm.getIdentityProviders().remove(entity); + updateRealm(); + break; + } + } } @Override public void updateIdentityProvider(IdentityProviderModel identityProvider) { + for (IdentityProviderEntity entity : this.realm.getIdentityProviders()) { + if (entity.getId().equals(identityProvider.getId())) { + entity.setName(identityProvider.getName()); + entity.setEnabled(identityProvider.isEnabled()); + entity.setUpdateProfileFirstLogin(identityProvider.isUpdateProfileFirstLogin()); + entity.setConfig(identityProvider.getConfig()); + } + } + updateRealm(); } @Override @@ -963,8 +1002,7 @@ public class RealmAdapter extends AbstractMongoAdapter impleme @Override public boolean isIdentityFederationEnabled() { - //TODO: support identity federation storage for mongo - return false; + return this.realm.getIdentityProviders() != null && !this.realm.getIdentityProviders().isEmpty(); } @Override