Fix testsuite with Mongo. Added ClientAdapter and ClientEntity to mongo model to improve inheritance

This commit is contained in:
mposolda 2014-03-06 20:28:41 +01:00
parent 2edb93ef54
commit da3f1a21a2
20 changed files with 384 additions and 616 deletions

View file

@ -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
*

View file

@ -7,15 +7,19 @@ import org.keycloak.models.mongo.api.context.MongoStoreInvocationContext;
/**
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/
public abstract class AbstractAdapter {
public abstract class AbstractMongoAdapter<T extends AbstractMongoIdentifiableEntity> {
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());

View file

@ -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 <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/
public class ApplicationAdapter extends AbstractAdapter implements ApplicationModel {
private final ApplicationEntity application;
private final RealmModel realm;
public class ApplicationAdapter extends ClientAdapter<ApplicationEntity> 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<String> 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<String> getWebOrigins() {
Set<String> result = new HashSet<String>();
if (application.getWebOrigins() != null) {
result.addAll(application.getWebOrigins());
}
return result;
}
@Override
public void setWebOrigins(Set<String> webOrigins) {
List<String> result = new ArrayList<String>();
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<String> getRedirectUris() {
Set<String> result = new HashSet<String>();
if (application.getRedirectUris() != null) {
result.addAll(application.getRedirectUris());
}
return result;
}
@Override
public void setRedirectUris(Set<String> redirectUris) {
List<String> result = new ArrayList<String>();
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();
}
}

View file

@ -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 <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/
public class ClientAdapter<T extends ClientEntity> extends AbstractMongoAdapter<T> 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<String> getWebOrigins() {
Set<String> result = new HashSet<String>();
if (getMongoEntity().getWebOrigins() != null) {
result.addAll(clientEntity.getWebOrigins());
}
return result;
}
@Override
public void setWebOrigins(Set<String> webOrigins) {
List<String> result = new ArrayList<String>();
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<String> getRedirectUris() {
Set<String> result = new HashSet<String>();
if (clientEntity.getRedirectUris() != null) {
result.addAll(clientEntity.getRedirectUris());
}
return result;
}
@Override
public void setRedirectUris(Set<String> redirectUris) {
List<String> result = new ArrayList<String>();
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();
}
}

View file

@ -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;

View file

@ -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 <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/
public class OAuthClientAdapter extends AbstractAdapter implements OAuthClientModel {
private final OAuthClientEntity delegate;
private final RealmModel realm;
public class OAuthClientAdapter extends ClientAdapter<OAuthClientEntity> 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<String> getWebOrigins() {
Set<String> result = new HashSet<String>();
if (delegate.getWebOrigins() != null) {
result.addAll(delegate.getWebOrigins());
}
return result;
}
@Override
public void setWebOrigins(Set<String> webOrigins) {
List<String> result = new ArrayList<String>();
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<String> getRedirectUris() {
Set<String> result = new HashSet<String>();
if (delegate.getRedirectUris() != null) {
result.addAll(delegate.getRedirectUris());
}
return result;
}
@Override
public void setRedirectUris(Set<String> redirectUris) {
List<String> result = new ArrayList<String>();
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());
}
}

View file

@ -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 <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/
public class RealmAdapter extends AbstractAdapter implements RealmModel {
public class RealmAdapter extends AbstractMongoAdapter<RealmEntity> 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;
}
}

View file

@ -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 <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/
public class RoleAdapter extends AbstractAdapter implements RoleModel {
public class RoleAdapter extends AbstractMongoAdapter<RoleEntity> 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();
}
}

View file

@ -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 <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/
public class UserAdapter extends AbstractAdapter implements UserModel {
public class UserAdapter extends AbstractMongoAdapter<UserEntity> 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;
}
}

View file

@ -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 <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/
@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<String> scopeIds;
private List<String> webOrigins;
private List<String> redirectUris;
// We are using names of defaultRoles (not ids)
private List<String> defaultRoles = new ArrayList<String>();
@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<String> getScopeIds() {
return scopeIds;
}
@Override
public void setScopeIds(List<String> scopeIds) {
this.scopeIds = scopeIds;
}
@MongoField
public List<String> getWebOrigins() {
return webOrigins;
}
public void setWebOrigins(List<String> webOrigins) {
this.webOrigins = webOrigins;
}
@MongoField
public List<String> getRedirectUris() {
return redirectUris;
}
public void setRedirectUris(List<String> 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<String> 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

View file

@ -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 <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/
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<String> webOrigins = new ArrayList<String>();
private List<String> redirectUris = new ArrayList<String>();
private List<String> scopeIds = new ArrayList<String>();
@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<String> getWebOrigins() {
return webOrigins;
}
public void setWebOrigins(List<String> webOrigins) {
this.webOrigins = webOrigins;
}
@MongoField
public List<String> getRedirectUris() {
return redirectUris;
}
public void setRedirectUris(List<String> redirectUris) {
this.redirectUris = redirectUris;
}
@MongoField
public List<String> getScopeIds() {
return scopeIds;
}
public void setScopeIds(List<String> scopeIds) {
this.scopeIds = scopeIds;
}
}

View file

@ -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 <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/
@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<String> scopeIds;
private List<String> webOrigins;
private List<String> 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<String> getWebOrigins() {
return webOrigins;
}
public void setWebOrigins(List<String> webOrigins) {
this.webOrigins = webOrigins;
}
@MongoField
public List<String> getRedirectUris() {
return redirectUris;
}
public void setRedirectUris(List<String> redirectUris) {
this.redirectUris = redirectUris;
}
@MongoField
public List<String> getScopeIds() {
return scopeIds;
}
public void setScopeIds(List<String> 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) {
}
}

View file

@ -1,16 +0,0 @@
package org.keycloak.models.mongo.keycloak.entities;
import org.keycloak.models.mongo.api.MongoField;
import java.util.List;
/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
*/
public interface ScopedEntity {
@MongoField
List<String> getScopeIds();
void setScopeIds(List<String> scopeIds);
}

View file

@ -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<RoleEntity> getAllScopesOfClient(ClientModel client, MongoStoreInvocationContext invContext) {
ScopedEntity scopedEntity = (ScopedEntity)((AbstractAdapter)client).getMongoEntity();
ClientEntity scopedEntity = ((ClientAdapter)client).getMongoEntity();
List<String> scopeIds = scopedEntity.getScopeIds();
if (scopeIds == null || scopeIds.isEmpty()) {

View file

@ -12,7 +12,6 @@ public class Address implements MongoEntity {
private String street;
private int number;
private List<String> 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<String> getFlatNumbers() {
return flatNumbers;
}
public void setFlatNumbers(List<String> flatNumbers) {
this.flatNumbers = flatNumbers;
}
}

View file

@ -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 <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/
public class AddressWithFlats extends Address {
private List<String> flatNumbers;
@MongoField
public List<String> getFlatNumbers() {
return flatNumbers;
}
public void setFlatNumbers(List<String> flatNumbers) {
this.flatNumbers = flatNumbers;
}
}

View file

@ -28,6 +28,7 @@ public class MongoStoreTest {
private static final Class<? extends MongoEntity>[] MANAGED_DATA_TYPES = (Class<? extends MongoEntity>[])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<Address> addresses = new ArrayList<Address>();
AddressWithFlats addr2 = new AddressWithFlats();
List<AddressWithFlats> addresses = new ArrayList<AddressWithFlats>();
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<Person> 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);

View file

@ -17,7 +17,7 @@ public class Person extends AbstractMongoIdentifiableEntity {
private String firstName;
private int age;
private List<String> kids;
private List<Address> addresses;
private List<AddressWithFlats> addresses;
private Address mainAddress;
private Gender gender;
private List<Gender> genders;
@ -69,11 +69,11 @@ public class Person extends AbstractMongoIdentifiableEntity {
}
@MongoField
public List<Address> getAddresses() {
public List<AddressWithFlats> getAddresses() {
return addresses;
}
public void setAddresses(List<Address> addresses) {
public void setAddresses(List<AddressWithFlats> addresses) {
this.addresses = addresses;
}

View file

@ -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<RoleModel> allScopes = realm.getScopeMappings(oauthClient);
Assert.assertEquals(2, allScopes.size());
Assert.assertTrue(allScopes.contains(realm.getRole("admin")));

View file

@ -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()));