Fix mongo model

This commit is contained in:
mposolda 2015-01-22 14:57:47 +01:00
parent 72b4790c4f
commit a634f1a65f
6 changed files with 96 additions and 31 deletions

View file

@ -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",

View file

@ -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<String, String> config = new HashMap<String, String>();
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<String, String> getConfig() {

View file

@ -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<RequiredCredentialEntity> requiredCredentials = new ArrayList<RequiredCredentialEntity>();
private List<UserFederationProviderEntity> userFederationProviders = new ArrayList<UserFederationProviderEntity>();
private List<IdentityProviderEntity> identityProviders = new ArrayList<IdentityProviderEntity>();
private Map<String, String> browserSecurityHeaders = new HashMap<String, String>();
private Map<String, String> smtpConfig = new HashMap<String, String>();
@ -375,6 +375,14 @@ public class RealmEntity extends AbstractIdentifiableEntity {
this.userFederationProviders = userFederationProviders;
}
public List<IdentityProviderEntity> getIdentityProviders() {
return identityProviders;
}
public void setIdentityProviders(List<IdentityProviderEntity> identityProviders) {
this.identityProviders = identityProviders;
}
public String getCertificatePem() {
return certificatePem;
}

View file

@ -26,7 +26,7 @@ public class UserEntity extends AbstractIdentifiableEntity {
private Map<String, String> attributes;
private List<UserModel.RequiredAction> requiredActions;
private List<CredentialEntity> credentials = new ArrayList<CredentialEntity>();
private List<FederatedIdentityEntity> socialLinks;
private List<FederatedIdentityEntity> federatedIdentities;
private String federationLink;
public String getUsername() {
@ -125,12 +125,12 @@ public class UserEntity extends AbstractIdentifiableEntity {
this.credentials = credentials;
}
public List<FederatedIdentityEntity> getSocialLinks() {
return socialLinks;
public List<FederatedIdentityEntity> getFederatedIdentities() {
return federatedIdentities;
}
public void setSocialLinks(List<FederatedIdentityEntity> socialLinks) {
this.socialLinks = socialLinks;
public void setFederatedIdentities(List<FederatedIdentityEntity> federatedIdentities) {
this.federatedIdentities = federatedIdentities;
}
public String getFederationLink() {

View file

@ -216,7 +216,7 @@ public class MongoUserProvider implements UserProvider {
public Set<FederatedIdentityModel> getFederatedIdentities(UserModel userModel, RealmModel realm) {
UserModel user = getUserById(userModel.getId(), realm);
MongoUserEntity userEntity = ((UserAdapter) user).getUser();
List<FederatedIdentityEntity> linkEntities = userEntity.getSocialLinks();
List<FederatedIdentityEntity> 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<FederatedIdentityEntity> linkEntities = userEntity.getSocialLinks();
List<FederatedIdentityEntity> 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<FederatedIdentityEntity> linkEntities = userEntity.getSocialLinks();
private FederatedIdentityEntity findFederatedIdentityLink(MongoUserEntity userEntity, String identityProvider) {
List<FederatedIdentityEntity> linkEntities = userEntity.getFederatedIdentities();
if (linkEntities == null) {
return null;
}
for (FederatedIdentityEntity federatedIdentityEntity : linkEntities) {
if (federatedIdentityEntity.getIdentityProvider().equals(socialProvider)) {
if (federatedIdentityEntity.getIdentityProvider().equals(identityProvider)) {
return federatedIdentityEntity;
}
}

View file

@ -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<MongoRealmEntity> impleme
@Override
public List<IdentityProviderModel> getIdentityProviders() {
return null;
List<IdentityProviderModel> identityProviders = new ArrayList<IdentityProviderModel>();
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<MongoRealmEntity> impleme
@Override
public boolean isIdentityFederationEnabled() {
//TODO: support identity federation storage for mongo
return false;
return this.realm.getIdentityProviders() != null && !this.realm.getIdentityProviders().isEmpty();
}
@Override