From fe8755d4a49105f686762d9e57aa0c002cb0d515 Mon Sep 17 00:00:00 2001 From: mposolda Date: Mon, 11 Jan 2016 13:00:47 +0100 Subject: [PATCH] Fix mongo model --- .../org/keycloak/models/jpa/UserAdapter.java | 32 +++++++++---------- .../keycloak/adapters/ClientAdapter.java | 7 +++- .../mongo/keycloak/adapters/UserAdapter.java | 2 ++ .../keycloak/entities/MongoGroupEntity.java | 10 ------ .../keycloak/entities/MongoRealmEntity.java | 6 ++++ testsuite/integration/pom.xml | 2 ++ .../ldap/base/LDAPGroupMapperSyncTest.java | 19 ++++++++++- 7 files changed, 49 insertions(+), 29 deletions(-) diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/UserAdapter.java b/model/jpa/src/main/java/org/keycloak/models/jpa/UserAdapter.java index ded42a7047..aabe19fd70 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/UserAdapter.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/UserAdapter.java @@ -433,25 +433,23 @@ public class UserAdapter implements UserModel { @Override public List getCredentialsDirectly() { - List credentials = new ArrayList(user.getCredentials()); - List result = new ArrayList(); + List credentials = new ArrayList<>(user.getCredentials()); + List result = new ArrayList<>(); - if (credentials != null) { - for (CredentialEntity credEntity : credentials) { - UserCredentialValueModel credModel = new UserCredentialValueModel(); - credModel.setType(credEntity.getType()); - credModel.setDevice(credEntity.getDevice()); - credModel.setValue(credEntity.getValue()); - credModel.setCreatedDate(credEntity.getCreatedDate()); - credModel.setSalt(credEntity.getSalt()); - credModel.setHashIterations(credEntity.getHashIterations()); - credModel.setCounter(credEntity.getCounter()); - credModel.setAlgorithm(credEntity.getAlgorithm()); - credModel.setDigits(credEntity.getDigits()); - credModel.setPeriod(credEntity.getPeriod()); + for (CredentialEntity credEntity : credentials) { + UserCredentialValueModel credModel = new UserCredentialValueModel(); + credModel.setType(credEntity.getType()); + credModel.setDevice(credEntity.getDevice()); + credModel.setValue(credEntity.getValue()); + credModel.setCreatedDate(credEntity.getCreatedDate()); + credModel.setSalt(credEntity.getSalt()); + credModel.setHashIterations(credEntity.getHashIterations()); + credModel.setCounter(credEntity.getCounter()); + credModel.setAlgorithm(credEntity.getAlgorithm()); + credModel.setDigits(credEntity.getDigits()); + credModel.setPeriod(credEntity.getPeriod()); - result.add(credModel); - } + result.add(credModel); } return result; 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 index 02ccea48f2..455a80b4b1 100755 --- 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 @@ -710,7 +710,12 @@ public class ClientAdapter extends AbstractMongoAdapter imple @Override public void setClientTemplate(ClientTemplateModel template) { - getMongoEntity().setClientTemplate(template.getId()); + if (template == null) { + getMongoEntity().setClientTemplate(null); + } else { + getMongoEntity().setClientTemplate(template.getId()); + } + updateMongoEntity(); } 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 5e822b0ffa..9c585022be 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 @@ -382,6 +382,8 @@ public class UserAdapter extends AbstractMongoAdapter implement credModel.setValue(credEntity.getValue()); credModel.setSalt(credEntity.getSalt()); credModel.setHashIterations(credEntity.getHashIterations()); + credModel.setAlgorithm(credEntity.getAlgorithm()); + if (UserCredentialModel.isOtp(credEntity.getType())) { credModel.setCounter(credEntity.getCounter()); if (credEntity.getAlgorithm() == null) { diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/MongoGroupEntity.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/MongoGroupEntity.java index 80e51a92f1..5768e8466b 100755 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/MongoGroupEntity.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/MongoGroupEntity.java @@ -1,25 +1,15 @@ package org.keycloak.models.mongo.keycloak.entities; -import com.mongodb.DBObject; -import com.mongodb.QueryBuilder; -import org.jboss.logging.Logger; import org.keycloak.connections.mongo.api.MongoCollection; -import org.keycloak.connections.mongo.api.MongoField; import org.keycloak.connections.mongo.api.MongoIdentifiableEntity; -import org.keycloak.connections.mongo.api.MongoStore; import org.keycloak.connections.mongo.api.context.MongoStoreInvocationContext; import org.keycloak.models.entities.GroupEntity; -import org.keycloak.models.entities.RoleEntity; - -import java.util.List; /** */ @MongoCollection(collectionName = "groups") public class MongoGroupEntity extends GroupEntity implements MongoIdentifiableEntity { - private static final Logger logger = Logger.getLogger(MongoGroupEntity.class); - @Override public void afterRemove(MongoStoreInvocationContext invContext) { } diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/MongoRealmEntity.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/MongoRealmEntity.java index 78f107692c..2ace378809 100755 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/MongoRealmEntity.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/MongoRealmEntity.java @@ -26,6 +26,12 @@ public class MongoRealmEntity extends RealmEntity implements MongoIdentifiableEn // Remove all roles of this realm context.getMongoStore().removeEntities(MongoRoleEntity.class, query, true, context); + // Remove all client templates of this realm + context.getMongoStore().removeEntities(MongoClientTemplateEntity.class, query, true, context); + + // Remove all client templates of this realm + context.getMongoStore().removeEntities(MongoGroupEntity.class, query, true, context); + // Remove all clients of this realm context.getMongoStore().removeEntities(MongoClientEntity.class, query, true, context); } diff --git a/testsuite/integration/pom.xml b/testsuite/integration/pom.xml index 017e8d1fed..552e011a1d 100755 --- a/testsuite/integration/pom.xml +++ b/testsuite/integration/pom.xml @@ -359,6 +359,7 @@ jpa jpa + jpa jpa debug @@ -397,6 +398,7 @@ mongo mongo + mongo mongo ${keycloak.connectionsMongo.host} ${keycloak.connectionsMongo.port} diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/federation/ldap/base/LDAPGroupMapperSyncTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/federation/ldap/base/LDAPGroupMapperSyncTest.java index f7e17f6312..1fb20e9311 100644 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/federation/ldap/base/LDAPGroupMapperSyncTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/federation/ldap/base/LDAPGroupMapperSyncTest.java @@ -1,8 +1,11 @@ package org.keycloak.testsuite.federation.ldap.base; +import java.util.List; import java.util.Map; +import org.junit.After; import org.junit.Assert; +import org.junit.Before; import org.junit.ClassRule; import org.junit.FixMethodOrder; import org.junit.Test; @@ -77,6 +80,20 @@ public class LDAPGroupMapperSyncTest { .outerRule(ldapRule) .around(keycloakRule); + @Before + public void before() { + KeycloakSession session = keycloakRule.startSession(); + try { + RealmModel realm = session.realms().getRealmByName("test"); + List kcGroups = realm.getTopLevelGroups(); + for (GroupModel kcGroup : kcGroups) { + realm.removeGroup(kcGroup); + } + } finally { + keycloakRule.stopSession(session, true); + } + } + @Test public void test01_syncNoPreserveGroupInheritance() throws Exception { KeycloakSession session = keycloakRule.startSession(); @@ -213,7 +230,7 @@ public class LDAPGroupMapperSyncTest { // Sync groups again from LDAP. Assert LDAP non-existing groups deleted syncResult = new GroupLDAPFederationMapperFactory().create(session).syncDataFromFederationProviderToKeycloak(mapperModel, ldapProvider, session, realm); Assert.assertEquals(3, syncResult.getUpdated()); - Assert.assertTrue(syncResult.getRemoved() >= 2); + Assert.assertTrue(syncResult.getRemoved() == 2); // Sync and assert groups updated Assert.assertNotNull(KeycloakModelUtils.findGroupByPath(realm, "/group1/group11"));