From da3f1a21a27dc897e0aa38f4986e05d1af711e4d Mon Sep 17 00:00:00 2001 From: mposolda Date: Thu, 6 Mar 2014 20:28:41 +0100 Subject: [PATCH] Fix testsuite with Mongo. Added ClientAdapter and ClientEntity to mongo model to improve inheritance --- .../models/mongo/api/MongoEntity.java | 2 - ...Adapter.java => AbstractMongoAdapter.java} | 14 +- .../keycloak/adapters/ApplicationAdapter.java | 188 +++--------------- .../keycloak/adapters/ClientAdapter.java | 160 +++++++++++++++ .../adapters/MongoKeycloakSession.java | 1 - .../keycloak/adapters/OAuthClientAdapter.java | 150 +------------- .../mongo/keycloak/adapters/RealmAdapter.java | 13 +- .../mongo/keycloak/adapters/RoleAdapter.java | 25 +-- .../mongo/keycloak/adapters/UserAdapter.java | 9 +- .../keycloak/entities/ApplicationEntity.java | 110 +--------- .../mongo/keycloak/entities/ClientEntity.java | 117 +++++++++++ .../keycloak/entities/OAuthClientEntity.java | 112 +---------- .../mongo/keycloak/entities/ScopedEntity.java | 16 -- .../models/mongo/utils/MongoModelUtils.java | 9 +- .../keycloak/models/mongo/test/Address.java | 10 - .../models/mongo/test/AddressWithFlats.java | 24 +++ .../models/mongo/test/MongoStoreTest.java | 14 +- .../keycloak/models/mongo/test/Person.java | 6 +- .../org/keycloak/model/test/ImportTest.java | 17 +- .../model/test/MultipleRealmsTest.java | 3 +- 20 files changed, 384 insertions(+), 616 deletions(-) rename model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/{AbstractAdapter.java => AbstractMongoAdapter.java} (66%) create mode 100644 model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/ClientAdapter.java create mode 100644 model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/ClientEntity.java delete mode 100755 model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/ScopedEntity.java create mode 100644 model/mongo/src/test/java/org/keycloak/models/mongo/test/AddressWithFlats.java diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/api/MongoEntity.java b/model/mongo/src/main/java/org/keycloak/models/mongo/api/MongoEntity.java index 8b91583344..f80f2e3d90 100644 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/api/MongoEntity.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/api/MongoEntity.java @@ -1,7 +1,5 @@ package org.keycloak.models.mongo.api; -import org.keycloak.models.mongo.api.context.MongoStoreInvocationContext; - /** * Base interface for object, which is persisted in Mongo * diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/AbstractAdapter.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/AbstractMongoAdapter.java similarity index 66% rename from model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/AbstractAdapter.java rename to model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/AbstractMongoAdapter.java index 9e4f945cc7..6fcae4c2f6 100644 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/AbstractAdapter.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/AbstractMongoAdapter.java @@ -7,15 +7,19 @@ import org.keycloak.models.mongo.api.context.MongoStoreInvocationContext; /** * @author Marek Posolda */ -public abstract class AbstractAdapter { +public abstract class AbstractMongoAdapter { - protected MongoStoreInvocationContext invocationContext; + protected final MongoStoreInvocationContext invocationContext; - public AbstractAdapter(MongoStoreInvocationContext invocationContext) { + public AbstractMongoAdapter(MongoStoreInvocationContext invocationContext) { this.invocationContext = invocationContext; } - public abstract AbstractMongoIdentifiableEntity getMongoEntity(); + protected abstract T getMongoEntity(); + + protected void updateMongoEntity() { + getMongoStore().updateEntity(getMongoEntity(), invocationContext); + } @Override public boolean equals(Object o) { @@ -23,7 +27,7 @@ public abstract class AbstractAdapter { if (o == null || getClass() != o.getClass()) return false; - AbstractAdapter that = (AbstractAdapter) o; + AbstractMongoAdapter that = (AbstractMongoAdapter) o; if (getMongoEntity() == null && that.getMongoEntity() == null) return true; return getMongoEntity().equals(that.getMongoEntity()); diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/ApplicationAdapter.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/ApplicationAdapter.java index 1c73a19722..d3c2075db6 100755 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/ApplicationAdapter.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/ApplicationAdapter.java @@ -7,11 +7,9 @@ import org.keycloak.models.ClientModel; import org.keycloak.models.RealmModel; import org.keycloak.models.RoleModel; import org.keycloak.models.UserModel; -import org.keycloak.models.mongo.api.AbstractMongoIdentifiableEntity; import org.keycloak.models.mongo.api.context.MongoStoreInvocationContext; import org.keycloak.models.mongo.keycloak.entities.ApplicationEntity; import org.keycloak.models.mongo.keycloak.entities.RoleEntity; -import org.keycloak.models.mongo.keycloak.entities.UserEntity; import org.keycloak.models.mongo.utils.MongoModelUtils; import java.util.ArrayList; @@ -22,125 +20,81 @@ import java.util.Set; /** * @author Marek Posolda */ -public class ApplicationAdapter extends AbstractAdapter implements ApplicationModel { - - private final ApplicationEntity application; - private final RealmModel realm; +public class ApplicationAdapter extends ClientAdapter implements ApplicationModel { public ApplicationAdapter(RealmModel realm, ApplicationEntity applicationEntity, MongoStoreInvocationContext invContext) { - super(invContext); - this.application = applicationEntity; - this.realm = realm; + super(realm, applicationEntity, invContext); } @Override public void updateApplication() { - getMongoStore().updateEntity(application, invocationContext); - } - - @Override - public String getId() { - return application.getId(); - } - - @Override - public String getClientId() { - return getName(); + updateMongoEntity(); } @Override public String getName() { - return application.getName(); + return getMongoEntity().getName(); } @Override public void setName(String name) { - application.setName(name); - } - - @Override - public RealmModel getRealm() { - return realm; - } - - @Override - public boolean isEnabled() { - return application.isEnabled(); - } - - @Override - public void setEnabled(boolean enabled) { - application.setEnabled(enabled); + getMongoEntity().setName(name); + updateMongoEntity(); } @Override public boolean isSurrogateAuthRequired() { - return application.isSurrogateAuthRequired(); + return getMongoEntity().isSurrogateAuthRequired(); } @Override public void setSurrogateAuthRequired(boolean surrogateAuthRequired) { - application.setSurrogateAuthRequired(surrogateAuthRequired); + getMongoEntity().setSurrogateAuthRequired(surrogateAuthRequired); + updateMongoEntity(); } @Override public String getManagementUrl() { - return application.getManagementUrl(); + return getMongoEntity().getManagementUrl(); } @Override public void setManagementUrl(String url) { - application.setManagementUrl(url); + getMongoEntity().setManagementUrl(url); + updateMongoEntity(); } @Override public void setBaseUrl(String url) { - application.setBaseUrl(url); + getMongoEntity().setBaseUrl(url); + updateMongoEntity(); } @Override public String getBaseUrl() { - return application.getBaseUrl(); - } - - @Override - public long getAllowedClaimsMask() { - return application.getAllowedClaimsMask(); - } - - @Override - public void setAllowedClaimsMask(long mask) { - application.setAllowedClaimsMask(mask); - } - - @Override - public int getNotBefore() { - return application.getNotBefore(); - } - - @Override - public void setNotBefore(int notBefore) { - application.setNotBefore(notBefore); + return getMongoEntity().getBaseUrl(); } @Override public boolean isBearerOnly() { - return application.isBearerOnly(); + return getMongoEntity().isBearerOnly(); } @Override public void setBearerOnly(boolean only) { - application.setBearerOnly(only); + getMongoEntity().setBearerOnly(only); + updateMongoEntity(); } @Override public boolean isPublicClient() { - return application.isPublicClient(); + return getMongoEntity().isPublicClient(); } @Override public void setPublicClient(boolean flag) { - application.setPublicClient(flag); + getMongoEntity().setPublicClient(flag); + updateMongoEntity(); } @Override @@ -207,7 +161,7 @@ public class ApplicationAdapter extends AbstractAdapter implements ApplicationMo @Override public void addScope(RoleModel role) { - getMongoStore().pushItemToList(application, "scopeIds", role.getId(), true, invocationContext); + getMongoStore().pushItemToList(getMongoEntity(), "scopeIds", role.getId(), true, invocationContext); } @Override @@ -225,7 +179,7 @@ public class ApplicationAdapter extends AbstractAdapter implements ApplicationMo @Override public List getDefaultRoles() { - return application.getDefaultRoles(); + return getMongoEntity().getDefaultRoles(); } @Override @@ -235,7 +189,7 @@ public class ApplicationAdapter extends AbstractAdapter implements ApplicationMo addRole(name); } - getMongoStore().pushItemToList(application, "defaultRoles", name, true, invocationContext); + getMongoStore().pushItemToList(getMongoEntity(), "defaultRoles", name, true, invocationContext); } @Override @@ -250,97 +204,7 @@ public class ApplicationAdapter extends AbstractAdapter implements ApplicationMo roleNames.add(roleName); } - application.setDefaultRoles(roleNames); - } - - @Override - public AbstractMongoIdentifiableEntity getMongoEntity() { - return application; - } - - @Override - public Set getWebOrigins() { - Set result = new HashSet(); - if (application.getWebOrigins() != null) { - result.addAll(application.getWebOrigins()); - } - return result; - } - - @Override - public void setWebOrigins(Set webOrigins) { - List result = new ArrayList(); - result.addAll(webOrigins); - application.setWebOrigins(result); - } - - @Override - public void addWebOrigin(String webOrigin) { - getMongoStore().pushItemToList(application, "webOrigins", webOrigin, true, invocationContext); - } - - @Override - public void removeWebOrigin(String webOrigin) { - getMongoStore().pullItemFromList(application, "webOrigins", webOrigin, invocationContext); - } - - @Override - public Set getRedirectUris() { - Set result = new HashSet(); - if (application.getRedirectUris() != null) { - result.addAll(application.getRedirectUris()); - } - return result; - } - - @Override - public void setRedirectUris(Set redirectUris) { - List result = new ArrayList(); - result.addAll(redirectUris); - application.setRedirectUris(result); - } - - @Override - public void addRedirectUri(String redirectUri) { - getMongoStore().pushItemToList(application, "redirectUris", redirectUri, true, invocationContext); - } - - @Override - public void removeRedirectUri(String redirectUri) { - getMongoStore().pullItemFromList(application, "redirectUris", redirectUri, invocationContext); - } - - @Override - public String getSecret() { - return application.getSecret(); - } - - @Override - public void setSecret(String secret) { - application.setSecret(secret); - } - - - @Override - public boolean validateSecret(String secret) { - return secret.equals(application.getSecret()); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof ApplicationAdapter)) return false; - if (!super.equals(o)) return false; - - ApplicationAdapter that = (ApplicationAdapter) o; - - if (!application.getId().equals(that.application.getId())) return false; - - return true; - } - - @Override - public int hashCode() { - return application.getId().hashCode(); + getMongoEntity().setDefaultRoles(roleNames); + updateMongoEntity(); } } diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/ClientAdapter.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/ClientAdapter.java new file mode 100644 index 0000000000..8c6fb48566 --- /dev/null +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/ClientAdapter.java @@ -0,0 +1,160 @@ +package org.keycloak.models.mongo.keycloak.adapters; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.keycloak.models.ClientModel; +import org.keycloak.models.RealmModel; +import org.keycloak.models.mongo.api.context.MongoStoreInvocationContext; +import org.keycloak.models.mongo.keycloak.entities.ClientEntity; + +/** + * @author Marek Posolda + */ +public class ClientAdapter extends AbstractMongoAdapter implements ClientModel { + + private final T clientEntity; + private final RealmModel realm; + + public ClientAdapter(RealmModel realm, T clientEntity, MongoStoreInvocationContext invContext) { + super(invContext); + this.clientEntity = clientEntity; + this.realm = realm; + } + + @Override + public T getMongoEntity() { + return clientEntity; + } + + @Override + public String getId() { + return getMongoEntity().getId(); + } + + @Override + public String getClientId() { + return getMongoEntity().getName(); + } + + @Override + public long getAllowedClaimsMask() { + return getMongoEntity().getAllowedClaimsMask(); + } + + @Override + public void setAllowedClaimsMask(long mask) { + getMongoEntity().setAllowedClaimsMask(mask); + updateMongoEntity(); + } + + @Override + public Set getWebOrigins() { + Set result = new HashSet(); + if (getMongoEntity().getWebOrigins() != null) { + result.addAll(clientEntity.getWebOrigins()); + } + return result; + } + + @Override + public void setWebOrigins(Set webOrigins) { + List result = new ArrayList(); + result.addAll(webOrigins); + clientEntity.setWebOrigins(result); + updateMongoEntity(); + } + + @Override + public void addWebOrigin(String webOrigin) { + getMongoStore().pushItemToList(clientEntity, "webOrigins", webOrigin, true, invocationContext); + } + + @Override + public void removeWebOrigin(String webOrigin) { + getMongoStore().pullItemFromList(clientEntity, "webOrigins", webOrigin, invocationContext); + } + + @Override + public Set getRedirectUris() { + Set result = new HashSet(); + if (clientEntity.getRedirectUris() != null) { + result.addAll(clientEntity.getRedirectUris()); + } + return result; + } + + @Override + public void setRedirectUris(Set redirectUris) { + List result = new ArrayList(); + result.addAll(redirectUris); + clientEntity.setRedirectUris(result); + updateMongoEntity(); + } + + @Override + public void addRedirectUri(String redirectUri) { + getMongoStore().pushItemToList(clientEntity, "redirectUris", redirectUri, true, invocationContext); + } + + @Override + public void removeRedirectUri(String redirectUri) { + getMongoStore().pullItemFromList(clientEntity, "redirectUris", redirectUri, invocationContext); + } + + @Override + public boolean isEnabled() { + return clientEntity.isEnabled(); + } + + @Override + public void setEnabled(boolean enabled) { + clientEntity.setEnabled(enabled); + updateMongoEntity(); + } + + @Override + public boolean validateSecret(String secret) { + return secret.equals(clientEntity.getSecret()); + } + + @Override + public String getSecret() { + return clientEntity.getSecret(); + } + + @Override + public void setSecret(String secret) { + clientEntity.setSecret(secret); + updateMongoEntity(); + } + + @Override + public boolean isPublicClient() { + return clientEntity.isPublicClient(); + } + + @Override + public void setPublicClient(boolean flag) { + clientEntity.setPublicClient(flag); + updateMongoEntity(); + } + + @Override + public RealmModel getRealm() { + return realm; + } + + @Override + public int getNotBefore() { + return clientEntity.getNotBefore(); + } + + @Override + public void setNotBefore(int notBefore) { + clientEntity.setNotBefore(notBefore); + updateMongoEntity(); + } +} diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/MongoKeycloakSession.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/MongoKeycloakSession.java index c90fe01650..f02221d604 100755 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/MongoKeycloakSession.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/MongoKeycloakSession.java @@ -6,7 +6,6 @@ import com.mongodb.QueryBuilder; import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakTransaction; import org.keycloak.models.RealmModel; -import org.keycloak.models.UserModel; import org.keycloak.models.mongo.api.MongoStore; import org.keycloak.models.mongo.api.context.MongoStoreInvocationContext; import org.keycloak.models.mongo.impl.context.TransactionMongoStoreInvocationContext; diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/OAuthClientAdapter.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/OAuthClientAdapter.java index d7e1142d19..b9b5e04284 100755 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/OAuthClientAdapter.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/OAuthClientAdapter.java @@ -2,163 +2,21 @@ package org.keycloak.models.mongo.keycloak.adapters; import org.keycloak.models.OAuthClientModel; import org.keycloak.models.RealmModel; -import org.keycloak.models.UserModel; -import org.keycloak.models.mongo.api.AbstractMongoIdentifiableEntity; import org.keycloak.models.mongo.api.context.MongoStoreInvocationContext; import org.keycloak.models.mongo.keycloak.entities.OAuthClientEntity; -import org.keycloak.models.mongo.keycloak.entities.UserEntity; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; /** * @author Marek Posolda */ -public class OAuthClientAdapter extends AbstractAdapter implements OAuthClientModel { - - private final OAuthClientEntity delegate; - private final RealmModel realm; +public class OAuthClientAdapter extends ClientAdapter implements OAuthClientModel { public OAuthClientAdapter(RealmModel realm, OAuthClientEntity oauthClientEntity, MongoStoreInvocationContext invContext) { - super(invContext); - this.delegate = oauthClientEntity; - this.realm = realm; - } - - @Override - public String getId() { - return delegate.getId(); - } - - @Override - public String getClientId() { - return delegate.getName(); + super(realm, oauthClientEntity, invContext); } @Override public void setClientId(String id) { - delegate.setName(id); + getMongoEntity().setName(id); + updateMongoEntity(); } - - @Override - public RealmModel getRealm() { - return realm; - } - - @Override - public long getAllowedClaimsMask() { - return delegate.getAllowedClaimsMask(); - } - - @Override - public void setAllowedClaimsMask(long mask) { - delegate.setAllowedClaimsMask(mask); - } - - @Override - public boolean isEnabled() { - return delegate.isEnabled(); - } - - @Override - public void setEnabled(boolean enabled) { - delegate.setEnabled(enabled); - } - - @Override - public AbstractMongoIdentifiableEntity getMongoEntity() { - return delegate; - } - - @Override - public int getNotBefore() { - return delegate.getNotBefore(); - } - - @Override - public void setNotBefore(int notBefore) { - delegate.setNotBefore(notBefore); - } - - @Override - public boolean isPublicClient() { - return delegate.isPublicClient(); - } - - @Override - public void setPublicClient(boolean flag) { - delegate.setPublicClient(flag); - } - - @Override - public Set getWebOrigins() { - Set result = new HashSet(); - if (delegate.getWebOrigins() != null) { - result.addAll(delegate.getWebOrigins()); - } - return result; - } - - @Override - public void setWebOrigins(Set webOrigins) { - List result = new ArrayList(); - result.addAll(webOrigins); - delegate.setWebOrigins(result); - } - - @Override - public void addWebOrigin(String webOrigin) { - getMongoStore().pushItemToList(delegate, "webOrigins", webOrigin, true, invocationContext); - } - - @Override - public void removeWebOrigin(String webOrigin) { - getMongoStore().pullItemFromList(delegate, "webOrigins", webOrigin, invocationContext); - } - - @Override - public Set getRedirectUris() { - Set result = new HashSet(); - if (delegate.getRedirectUris() != null) { - result.addAll(delegate.getRedirectUris()); - } - return result; - } - - @Override - public void setRedirectUris(Set redirectUris) { - List result = new ArrayList(); - result.addAll(redirectUris); - delegate.setRedirectUris(result); - } - - @Override - public void addRedirectUri(String redirectUri) { - getMongoStore().pushItemToList(delegate, "redirectUris", redirectUri, true, invocationContext); - } - - @Override - public void removeRedirectUri(String redirectUri) { - getMongoStore().pullItemFromList(delegate, "redirectUris", redirectUri, invocationContext); - } - - @Override - public String getSecret() { - return delegate.getSecret(); - } - - @Override - public void setSecret(String secret) { - delegate.setSecret(secret); - } - - - @Override - public boolean validateSecret(String secret) { - return secret.equals(delegate.getSecret()); - } - - } 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 22acd3ff8d..7672c3ed8f 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 @@ -13,7 +13,6 @@ import org.keycloak.models.RoleModel; import org.keycloak.models.SocialLinkModel; import org.keycloak.models.UserCredentialModel; import org.keycloak.models.UserModel; -import org.keycloak.models.mongo.api.AbstractMongoIdentifiableEntity; import org.keycloak.models.mongo.api.context.MongoStoreInvocationContext; import org.keycloak.models.mongo.keycloak.entities.ApplicationEntity; import org.keycloak.models.mongo.keycloak.entities.CredentialEntity; @@ -43,7 +42,7 @@ import java.util.regex.Pattern; /** * @author Marek Posolda */ -public class RealmAdapter extends AbstractAdapter implements RealmModel { +public class RealmAdapter extends AbstractMongoAdapter implements RealmModel { private static final Logger logger = Logger.getLogger(RealmAdapter.class); @@ -187,6 +186,7 @@ public class RealmAdapter extends AbstractAdapter implements RealmModel { @Override public void setNotBefore(int notBefore) { realm.setNotBefore(notBefore); + updateRealm(); } @@ -677,12 +677,12 @@ public class RealmAdapter extends AbstractAdapter implements RealmModel { @Override public void addScopeMapping(ClientModel client, RoleModel role) { - getMongoStore().pushItemToList(((AbstractAdapter)client).getMongoEntity(), "scopeIds", role.getId(), true, invocationContext); + getMongoStore().pushItemToList(((AbstractMongoAdapter)client).getMongoEntity(), "scopeIds", role.getId(), true, invocationContext); } @Override public void deleteScopeMapping(ClientModel client, RoleModel role) { - getMongoStore().pullItemFromList(((AbstractAdapter)client).getMongoEntity(), "scopeIds", role.getId(), invocationContext); + getMongoStore().pullItemFromList(((AbstractMongoAdapter)client).getMongoEntity(), "scopeIds", role.getId(), invocationContext); } @Override @@ -776,6 +776,7 @@ public class RealmAdapter extends AbstractAdapter implements RealmModel { addRequiredCredential(credentialModel, credsEntities); } } + updateRealm(); } @Override @@ -897,7 +898,7 @@ public class RealmAdapter extends AbstractAdapter implements RealmModel { } protected void updateRealm() { - getMongoStore().updateEntity(realm, invocationContext); + super.updateMongoEntity(); } protected RequiredCredentialModel initRequiredCredentialModel(String type) { @@ -1010,7 +1011,7 @@ public class RealmAdapter extends AbstractAdapter implements RealmModel { } @Override - public AbstractMongoIdentifiableEntity getMongoEntity() { + public RealmEntity getMongoEntity() { return realm; } } diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/RoleAdapter.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/RoleAdapter.java index 1165da3452..57ecc660f0 100755 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/RoleAdapter.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/RoleAdapter.java @@ -15,7 +15,6 @@ import org.keycloak.models.mongo.api.context.MongoStoreInvocationContext; import org.keycloak.models.mongo.keycloak.entities.ApplicationEntity; import org.keycloak.models.mongo.keycloak.entities.RealmEntity; import org.keycloak.models.mongo.keycloak.entities.RoleEntity; -import org.keycloak.models.mongo.utils.MongoModelUtils; import org.keycloak.models.utils.KeycloakModelUtils; /** @@ -23,7 +22,7 @@ import org.keycloak.models.utils.KeycloakModelUtils; * * @author Marek Posolda */ -public class RoleAdapter extends AbstractAdapter implements RoleModel { +public class RoleAdapter extends AbstractMongoAdapter implements RoleModel { private final RoleEntity role; private RoleContainerModel roleContainer; @@ -73,7 +72,7 @@ public class RoleAdapter extends AbstractAdapter implements RoleModel { } protected void updateRole() { - getMongoStore().updateEntity(role, invocationContext); + super.updateMongoEntity(); } @Override @@ -141,25 +140,7 @@ public class RoleAdapter extends AbstractAdapter implements RoleModel { } @Override - public AbstractMongoIdentifiableEntity getMongoEntity() { + public RoleEntity getMongoEntity() { return role; } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - RoleAdapter that = (RoleAdapter) o; - - if (!role.getId().equals(that.role.getId())) return false; - - return true; - } - - @Override - public int hashCode() { - return role.getId().hashCode(); - } - } diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/UserAdapter.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/UserAdapter.java index d3d4d6a1b1..abcdffcd02 100755 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/UserAdapter.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/UserAdapter.java @@ -1,15 +1,12 @@ package org.keycloak.models.mongo.keycloak.adapters; import org.keycloak.models.UserModel; -import org.keycloak.models.mongo.api.AbstractMongoIdentifiableEntity; import org.keycloak.models.mongo.api.context.MongoStoreInvocationContext; import org.keycloak.models.mongo.keycloak.entities.UserEntity; -import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Set; @@ -18,7 +15,7 @@ import java.util.Set; * * @author Marek Posolda */ -public class UserAdapter extends AbstractAdapter implements UserModel { +public class UserAdapter extends AbstractMongoAdapter implements UserModel { private final UserEntity user; @@ -156,11 +153,11 @@ public class UserAdapter extends AbstractAdapter implements UserModel { } protected void updateUser() { - getMongoStore().updateEntity(user, invocationContext); + super.updateMongoEntity(); } @Override - public AbstractMongoIdentifiableEntity getMongoEntity() { + public UserEntity getMongoEntity() { return user; } } diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/ApplicationEntity.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/ApplicationEntity.java index ee9f1702af..be086d9997 100755 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/ApplicationEntity.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/ApplicationEntity.java @@ -5,9 +5,7 @@ import java.util.List; import com.mongodb.DBObject; import com.mongodb.QueryBuilder; -import org.keycloak.models.mongo.api.AbstractMongoIdentifiableEntity; import org.keycloak.models.mongo.api.MongoCollection; -import org.keycloak.models.mongo.api.MongoEntity; import org.keycloak.models.mongo.api.MongoField; import org.keycloak.models.mongo.api.context.MongoStoreInvocationContext; @@ -15,46 +13,16 @@ import org.keycloak.models.mongo.api.context.MongoStoreInvocationContext; * @author Marek Posolda */ @MongoCollection(collectionName = "applications") -public class ApplicationEntity extends AbstractMongoIdentifiableEntity implements MongoEntity, ScopedEntity { +public class ApplicationEntity extends ClientEntity { - private String name; - private boolean enabled; private boolean surrogateAuthRequired; private String managementUrl; private String baseUrl; - private String secret; - private int notBefore; private boolean bearerOnly; - private boolean publicClient; - - private String realmId; - private long allowedClaimsMask; - private List scopeIds; - private List webOrigins; - private List redirectUris; - // We are using names of defaultRoles (not ids) private List defaultRoles = new ArrayList(); - @MongoField - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - @MongoField - public boolean isEnabled() { - return enabled; - } - - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } - @MongoField public boolean isSurrogateAuthRequired() { return surrogateAuthRequired; @@ -82,64 +50,6 @@ public class ApplicationEntity extends AbstractMongoIdentifiableEntity implement this.baseUrl = baseUrl; } - @Override - @MongoField - public List getScopeIds() { - return scopeIds; - } - - @Override - public void setScopeIds(List scopeIds) { - this.scopeIds = scopeIds; - } - - @MongoField - public List getWebOrigins() { - return webOrigins; - } - - public void setWebOrigins(List webOrigins) { - this.webOrigins = webOrigins; - } - - @MongoField - public List getRedirectUris() { - return redirectUris; - } - - public void setRedirectUris(List redirectUris) { - this.redirectUris = redirectUris; - } - - - - @MongoField - public long getAllowedClaimsMask() { - return allowedClaimsMask; - } - - public void setAllowedClaimsMask(long allowedClaimsMask) { - this.allowedClaimsMask = allowedClaimsMask; - } - - @MongoField - public String getRealmId() { - return realmId; - } - - public void setRealmId(String realmId) { - this.realmId = realmId; - } - - @MongoField - public String getSecret() { - return secret; - } - - public void setSecret(String secret) { - this.secret = secret; - } - @MongoField public List getDefaultRoles() { return defaultRoles; @@ -149,15 +59,6 @@ public class ApplicationEntity extends AbstractMongoIdentifiableEntity implement this.defaultRoles = defaultRoles; } - @MongoField - public int getNotBefore() { - return notBefore; - } - - public void setNotBefore(int notBefore) { - this.notBefore = notBefore; - } - @MongoField public boolean isBearerOnly() { return bearerOnly; @@ -167,15 +68,6 @@ public class ApplicationEntity extends AbstractMongoIdentifiableEntity implement this.bearerOnly = bearerOnly; } - @MongoField - public boolean isPublicClient() { - return publicClient; - } - - public void setPublicClient(boolean publicClient) { - this.publicClient = publicClient; - } - @Override public void afterRemove(MongoStoreInvocationContext context) { // Remove all roles, which belongs to this application diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/ClientEntity.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/ClientEntity.java new file mode 100644 index 0000000000..9b4e019ad6 --- /dev/null +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/ClientEntity.java @@ -0,0 +1,117 @@ +package org.keycloak.models.mongo.keycloak.entities; + +import java.util.ArrayList; +import java.util.List; + +import org.keycloak.models.mongo.api.AbstractMongoIdentifiableEntity; +import org.keycloak.models.mongo.api.MongoEntity; +import org.keycloak.models.mongo.api.MongoField; + +/** + * @author Marek Posolda + */ +public class ClientEntity extends AbstractMongoIdentifiableEntity implements MongoEntity { + + private String name; + private boolean enabled; + private String secret; + private long allowedClaimsMask; + private int notBefore; + private boolean publicClient; + + private String realmId; + + private List webOrigins = new ArrayList(); + private List redirectUris = new ArrayList(); + private List scopeIds = new ArrayList(); + + @MongoField + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @MongoField + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + @MongoField + public String getSecret() { + return secret; + } + + public void setSecret(String secret) { + this.secret = secret; + } + + @MongoField + public long getAllowedClaimsMask() { + return allowedClaimsMask; + } + + public void setAllowedClaimsMask(long allowedClaimsMask) { + this.allowedClaimsMask = allowedClaimsMask; + } + + @MongoField + public int getNotBefore() { + return notBefore; + } + + public void setNotBefore(int notBefore) { + this.notBefore = notBefore; + } + + @MongoField + public boolean isPublicClient() { + return publicClient; + } + + public void setPublicClient(boolean publicClient) { + this.publicClient = publicClient; + } + + @MongoField + public String getRealmId() { + return realmId; + } + + public void setRealmId(String realmId) { + this.realmId = realmId; + } + + @MongoField + public List getWebOrigins() { + return webOrigins; + } + + public void setWebOrigins(List webOrigins) { + this.webOrigins = webOrigins; + } + + @MongoField + public List getRedirectUris() { + return redirectUris; + } + + public void setRedirectUris(List redirectUris) { + this.redirectUris = redirectUris; + } + + @MongoField + public List getScopeIds() { + return scopeIds; + } + + public void setScopeIds(List scopeIds) { + this.scopeIds = scopeIds; + } +} diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/OAuthClientEntity.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/OAuthClientEntity.java index 7188cd2bd0..7c6ea588f3 100755 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/OAuthClientEntity.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/OAuthClientEntity.java @@ -1,10 +1,6 @@ package org.keycloak.models.mongo.keycloak.entities; -import org.keycloak.models.mongo.api.AbstractMongoIdentifiableEntity; import org.keycloak.models.mongo.api.MongoCollection; -import org.keycloak.models.mongo.api.MongoEntity; -import org.keycloak.models.mongo.api.MongoField; -import org.keycloak.models.mongo.api.context.MongoStoreInvocationContext; import java.util.List; @@ -12,112 +8,6 @@ import java.util.List; * @author Marek Posolda */ @MongoCollection(collectionName = "oauthClients") -public class OAuthClientEntity extends AbstractMongoIdentifiableEntity implements MongoEntity, ScopedEntity { +public class OAuthClientEntity extends ClientEntity { - private String name; - private boolean enabled; - private String realmId; - private String secret; - private long allowedClaimsMask; - private int notBefore; - private boolean publicClient; - private List scopeIds; - private List webOrigins; - private List redirectUris; - - @MongoField - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - @MongoField - public boolean isEnabled() { - return enabled; - } - - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } - - @MongoField - public String getRealmId() { - return realmId; - } - - public void setRealmId(String realmId) { - this.realmId = realmId; - } - - @MongoField - public String getSecret() { - return secret; - } - - public void setSecret(String secret) { - this.secret = secret; - } - - - @MongoField - public long getAllowedClaimsMask() { - return allowedClaimsMask; - } - - public void setAllowedClaimsMask(long allowedClaimsMask) { - this.allowedClaimsMask = allowedClaimsMask; - } - - @MongoField - public List getWebOrigins() { - return webOrigins; - } - - public void setWebOrigins(List webOrigins) { - this.webOrigins = webOrigins; - } - - @MongoField - public List getRedirectUris() { - return redirectUris; - } - - public void setRedirectUris(List redirectUris) { - this.redirectUris = redirectUris; - } - - @MongoField - public List getScopeIds() { - return scopeIds; - } - - public void setScopeIds(List scopeIds) { - this.scopeIds = scopeIds; - } - - @MongoField - public int getNotBefore() { - return notBefore; - } - - public void setNotBefore(int notBefore) { - this.notBefore = notBefore; - } - - @MongoField - public boolean isPublicClient() { - return publicClient; - } - - public void setPublicClient(boolean publicClient) { - this.publicClient = publicClient; - } - - - @Override - public void afterRemove(MongoStoreInvocationContext context) { - } } diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/ScopedEntity.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/ScopedEntity.java deleted file mode 100755 index c1e85f5a24..0000000000 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/ScopedEntity.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.keycloak.models.mongo.keycloak.entities; - -import org.keycloak.models.mongo.api.MongoField; - -import java.util.List; - -/** - * @author Bill Burke - * @version $Revision: 1 $ - */ -public interface ScopedEntity { - @MongoField - List getScopeIds(); - - void setScopeIds(List scopeIds); -} diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/utils/MongoModelUtils.java b/model/mongo/src/main/java/org/keycloak/models/mongo/utils/MongoModelUtils.java index 8b1a26e51e..d485615cfc 100755 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/utils/MongoModelUtils.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/utils/MongoModelUtils.java @@ -1,20 +1,17 @@ package org.keycloak.models.mongo.utils; -import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; import java.util.List; import com.mongodb.DBObject; import com.mongodb.QueryBuilder; -import org.bson.types.ObjectId; import org.keycloak.models.ClientModel; import org.keycloak.models.UserModel; import org.keycloak.models.mongo.api.context.MongoStoreInvocationContext; -import org.keycloak.models.mongo.keycloak.adapters.AbstractAdapter; +import org.keycloak.models.mongo.keycloak.adapters.ClientAdapter; import org.keycloak.models.mongo.keycloak.adapters.UserAdapter; +import org.keycloak.models.mongo.keycloak.entities.ClientEntity; import org.keycloak.models.mongo.keycloak.entities.RoleEntity; -import org.keycloak.models.mongo.keycloak.entities.ScopedEntity; import org.keycloak.models.mongo.keycloak.entities.UserEntity; /** @@ -39,7 +36,7 @@ public class MongoModelUtils { // Get everything including both application and realm scopes public static List getAllScopesOfClient(ClientModel client, MongoStoreInvocationContext invContext) { - ScopedEntity scopedEntity = (ScopedEntity)((AbstractAdapter)client).getMongoEntity(); + ClientEntity scopedEntity = ((ClientAdapter)client).getMongoEntity(); List scopeIds = scopedEntity.getScopeIds(); if (scopeIds == null || scopeIds.isEmpty()) { diff --git a/model/mongo/src/test/java/org/keycloak/models/mongo/test/Address.java b/model/mongo/src/test/java/org/keycloak/models/mongo/test/Address.java index 30aaa27b00..215a9fc44e 100755 --- a/model/mongo/src/test/java/org/keycloak/models/mongo/test/Address.java +++ b/model/mongo/src/test/java/org/keycloak/models/mongo/test/Address.java @@ -12,7 +12,6 @@ public class Address implements MongoEntity { private String street; private int number; - private List flatNumbers; @MongoField public String getStreet() { @@ -31,13 +30,4 @@ public class Address implements MongoEntity { public void setNumber(int number) { this.number = number; } - - @MongoField - public List getFlatNumbers() { - return flatNumbers; - } - - public void setFlatNumbers(List flatNumbers) { - this.flatNumbers = flatNumbers; - } } diff --git a/model/mongo/src/test/java/org/keycloak/models/mongo/test/AddressWithFlats.java b/model/mongo/src/test/java/org/keycloak/models/mongo/test/AddressWithFlats.java new file mode 100644 index 0000000000..0495f43cf1 --- /dev/null +++ b/model/mongo/src/test/java/org/keycloak/models/mongo/test/AddressWithFlats.java @@ -0,0 +1,24 @@ +package org.keycloak.models.mongo.test; + +import java.util.List; + +import org.keycloak.models.mongo.api.MongoField; + +/** + * Just to test inheritance + * + * @author Marek Posolda + */ +public class AddressWithFlats extends Address { + + private List flatNumbers; + + @MongoField + public List getFlatNumbers() { + return flatNumbers; + } + + public void setFlatNumbers(List flatNumbers) { + this.flatNumbers = flatNumbers; + } +} diff --git a/model/mongo/src/test/java/org/keycloak/models/mongo/test/MongoStoreTest.java b/model/mongo/src/test/java/org/keycloak/models/mongo/test/MongoStoreTest.java index 219293b5e7..8128dde27b 100755 --- a/model/mongo/src/test/java/org/keycloak/models/mongo/test/MongoStoreTest.java +++ b/model/mongo/src/test/java/org/keycloak/models/mongo/test/MongoStoreTest.java @@ -28,6 +28,7 @@ public class MongoStoreTest { private static final Class[] MANAGED_DATA_TYPES = (Class[])new Class[] { Person.class, Address.class, + AddressWithFlats.class }; private MongoClient mongoClient; @@ -69,12 +70,12 @@ public class MongoStoreTest { mary.setFirstName("mary"); mary.setKids(asList("Peter", "Paul", "Wendy")); - Address addr1 = new Address(); + AddressWithFlats addr1 = new AddressWithFlats(); addr1.setStreet("Elm"); addr1.setNumber(5); addr1.setFlatNumbers(asList("flat1", "flat2")); - Address addr2 = new Address(); - List
addresses = new ArrayList
(); + AddressWithFlats addr2 = new AddressWithFlats(); + List addresses = new ArrayList(); addresses.add(addr1); addresses.add(addr2); @@ -87,6 +88,11 @@ public class MongoStoreTest { Assert.assertEquals(2, mongoStore.loadEntities(Person.class, new QueryBuilder().get(), context).size()); + // Commit this context + context.commit(); + + Assert.assertEquals(2, mongoStore.loadEntities(Person.class, new QueryBuilder().get(), context).size()); + DBObject query = new QueryBuilder().and("addresses.flatNumbers").is("flat1").get(); List persons = mongoStore.loadEntities(Person.class, query, context); Assert.assertEquals(1, persons.size()); @@ -94,7 +100,7 @@ public class MongoStoreTest { Assert.assertEquals(mary.getFirstName(), "mary"); Assert.assertTrue(mary.getKids().contains("Paul")); Assert.assertEquals(2, mary.getAddresses().size()); - Assert.assertEquals(Address.class, mary.getAddresses().get(0).getClass()); + Assert.assertEquals(AddressWithFlats.class, mary.getAddresses().get(0).getClass()); // Test push/pull mongoStore.pushItemToList(mary, "kids", "Pauline", true, context); diff --git a/model/mongo/src/test/java/org/keycloak/models/mongo/test/Person.java b/model/mongo/src/test/java/org/keycloak/models/mongo/test/Person.java index 88811269a4..e3f804246f 100755 --- a/model/mongo/src/test/java/org/keycloak/models/mongo/test/Person.java +++ b/model/mongo/src/test/java/org/keycloak/models/mongo/test/Person.java @@ -17,7 +17,7 @@ public class Person extends AbstractMongoIdentifiableEntity { private String firstName; private int age; private List kids; - private List
addresses; + private List addresses; private Address mainAddress; private Gender gender; private List genders; @@ -69,11 +69,11 @@ public class Person extends AbstractMongoIdentifiableEntity { } @MongoField - public List
getAddresses() { + public List getAddresses() { return addresses; } - public void setAddresses(List
addresses) { + public void setAddresses(List addresses) { this.addresses = addresses; } diff --git a/model/tests/src/test/java/org/keycloak/model/test/ImportTest.java b/model/tests/src/test/java/org/keycloak/model/test/ImportTest.java index d978bddf2d..16a736985a 100755 --- a/model/tests/src/test/java/org/keycloak/model/test/ImportTest.java +++ b/model/tests/src/test/java/org/keycloak/model/test/ImportTest.java @@ -8,6 +8,7 @@ import org.keycloak.models.AccountRoles; import org.keycloak.models.ApplicationModel; import org.keycloak.models.ClientModel; import org.keycloak.models.Constants; +import org.keycloak.models.OAuthClientModel; import org.keycloak.models.RealmModel; import org.keycloak.models.RequiredCredentialModel; import org.keycloak.models.RoleModel; @@ -29,11 +30,14 @@ public class ImportTest extends AbstractModelTest { @Test public void install() throws Exception { - RealmManager manager = realmManager; RealmRepresentation rep = AbstractModelTest.loadJson("testrealm.json"); - RealmModel realm = manager.createRealm("demo", rep.getRealm()); - manager.importRealm(rep, realm); + RealmModel realm = realmManager.createRealm("demo", rep.getRealm()); + realmManager.importRealm(rep, realm); + // Commit after import + commit(); + + realm = realmManager.getRealm("demo"); Assert.assertTrue(realm.isVerifyEmail()); Assert.assertFalse(realm.isUpdateProfileOnInitialSocialLogin()); @@ -100,10 +104,13 @@ public class ImportTest extends AbstractModelTest { Assert.assertEquals(1, appRoles.size()); Assert.assertEquals("app-admin", appRoles.iterator().next().getName()); + // Test client + ClientModel oauthClient = realm.findClient("oauthclient"); + Assert.assertEquals("clientpassword", oauthClient.getSecret()); + Assert.assertEquals(true, oauthClient.isEnabled()); + Assert.assertNotNull(oauthClient); // Test scope relationship - ClientModel oauthClient = realm.findClient("oauthclient"); - Assert.assertNotNull(oauthClient); Set allScopes = realm.getScopeMappings(oauthClient); Assert.assertEquals(2, allScopes.size()); Assert.assertTrue(allScopes.contains(realm.getRole("admin"))); diff --git a/model/tests/src/test/java/org/keycloak/model/test/MultipleRealmsTest.java b/model/tests/src/test/java/org/keycloak/model/test/MultipleRealmsTest.java index 8b33c3feb2..109ac0af6b 100755 --- a/model/tests/src/test/java/org/keycloak/model/test/MultipleRealmsTest.java +++ b/model/tests/src/test/java/org/keycloak/model/test/MultipleRealmsTest.java @@ -1,7 +1,5 @@ package org.keycloak.model.test; -import java.util.List; - import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -76,6 +74,7 @@ public class MultipleRealmsTest extends AbstractModelTest { RoleModel r1App1Role = r1app1.getRole("app1Role1"); Assert.assertEquals(r1App1Role, realm1.getRoleById(r1App1Role.getId())); + Assert.assertNull(realm2.getRoleById(r1App1Role.getId())); RoleModel r2Role1 = realm2.getRole("role2"); Assert.assertNull(realm1.getRoleById(r2Role1.getId()));