Fix mongo model

This commit is contained in:
mposolda 2016-01-11 13:00:47 +01:00
parent 2ec4cb82cc
commit fe8755d4a4
7 changed files with 49 additions and 29 deletions

View file

@ -433,25 +433,23 @@ public class UserAdapter implements UserModel {
@Override @Override
public List<UserCredentialValueModel> getCredentialsDirectly() { public List<UserCredentialValueModel> getCredentialsDirectly() {
List<CredentialEntity> credentials = new ArrayList<CredentialEntity>(user.getCredentials()); List<CredentialEntity> credentials = new ArrayList<>(user.getCredentials());
List<UserCredentialValueModel> result = new ArrayList<UserCredentialValueModel>(); List<UserCredentialValueModel> result = new ArrayList<>();
if (credentials != null) { for (CredentialEntity credEntity : credentials) {
for (CredentialEntity credEntity : credentials) { UserCredentialValueModel credModel = new UserCredentialValueModel();
UserCredentialValueModel credModel = new UserCredentialValueModel(); credModel.setType(credEntity.getType());
credModel.setType(credEntity.getType()); credModel.setDevice(credEntity.getDevice());
credModel.setDevice(credEntity.getDevice()); credModel.setValue(credEntity.getValue());
credModel.setValue(credEntity.getValue()); credModel.setCreatedDate(credEntity.getCreatedDate());
credModel.setCreatedDate(credEntity.getCreatedDate()); credModel.setSalt(credEntity.getSalt());
credModel.setSalt(credEntity.getSalt()); credModel.setHashIterations(credEntity.getHashIterations());
credModel.setHashIterations(credEntity.getHashIterations()); credModel.setCounter(credEntity.getCounter());
credModel.setCounter(credEntity.getCounter()); credModel.setAlgorithm(credEntity.getAlgorithm());
credModel.setAlgorithm(credEntity.getAlgorithm()); credModel.setDigits(credEntity.getDigits());
credModel.setDigits(credEntity.getDigits()); credModel.setPeriod(credEntity.getPeriod());
credModel.setPeriod(credEntity.getPeriod());
result.add(credModel); result.add(credModel);
}
} }
return result; return result;

View file

@ -710,7 +710,12 @@ public class ClientAdapter extends AbstractMongoAdapter<MongoClientEntity> imple
@Override @Override
public void setClientTemplate(ClientTemplateModel template) { public void setClientTemplate(ClientTemplateModel template) {
getMongoEntity().setClientTemplate(template.getId()); if (template == null) {
getMongoEntity().setClientTemplate(null);
} else {
getMongoEntity().setClientTemplate(template.getId());
}
updateMongoEntity(); updateMongoEntity();
} }

View file

@ -382,6 +382,8 @@ public class UserAdapter extends AbstractMongoAdapter<MongoUserEntity> implement
credModel.setValue(credEntity.getValue()); credModel.setValue(credEntity.getValue());
credModel.setSalt(credEntity.getSalt()); credModel.setSalt(credEntity.getSalt());
credModel.setHashIterations(credEntity.getHashIterations()); credModel.setHashIterations(credEntity.getHashIterations());
credModel.setAlgorithm(credEntity.getAlgorithm());
if (UserCredentialModel.isOtp(credEntity.getType())) { if (UserCredentialModel.isOtp(credEntity.getType())) {
credModel.setCounter(credEntity.getCounter()); credModel.setCounter(credEntity.getCounter());
if (credEntity.getAlgorithm() == null) { if (credEntity.getAlgorithm() == null) {

View file

@ -1,25 +1,15 @@
package org.keycloak.models.mongo.keycloak.entities; 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.MongoCollection;
import org.keycloak.connections.mongo.api.MongoField;
import org.keycloak.connections.mongo.api.MongoIdentifiableEntity; 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.connections.mongo.api.context.MongoStoreInvocationContext;
import org.keycloak.models.entities.GroupEntity; import org.keycloak.models.entities.GroupEntity;
import org.keycloak.models.entities.RoleEntity;
import java.util.List;
/** /**
*/ */
@MongoCollection(collectionName = "groups") @MongoCollection(collectionName = "groups")
public class MongoGroupEntity extends GroupEntity implements MongoIdentifiableEntity { public class MongoGroupEntity extends GroupEntity implements MongoIdentifiableEntity {
private static final Logger logger = Logger.getLogger(MongoGroupEntity.class);
@Override @Override
public void afterRemove(MongoStoreInvocationContext invContext) { public void afterRemove(MongoStoreInvocationContext invContext) {
} }

View file

@ -26,6 +26,12 @@ public class MongoRealmEntity extends RealmEntity implements MongoIdentifiableEn
// Remove all roles of this realm // Remove all roles of this realm
context.getMongoStore().removeEntities(MongoRoleEntity.class, query, true, context); 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 // Remove all clients of this realm
context.getMongoStore().removeEntities(MongoClientEntity.class, query, true, context); context.getMongoStore().removeEntities(MongoClientEntity.class, query, true, context);
} }

View file

@ -359,6 +359,7 @@
<systemPropertyVariables> <systemPropertyVariables>
<keycloak.realm.provider>jpa</keycloak.realm.provider> <keycloak.realm.provider>jpa</keycloak.realm.provider>
<keycloak.user.provider>jpa</keycloak.user.provider> <keycloak.user.provider>jpa</keycloak.user.provider>
<keycloak.userSessionPersister.provider>jpa</keycloak.userSessionPersister.provider>
<keycloak.eventsStore.provider>jpa</keycloak.eventsStore.provider> <keycloak.eventsStore.provider>jpa</keycloak.eventsStore.provider>
<keycloak.liquibase.logging.level>debug</keycloak.liquibase.logging.level> <keycloak.liquibase.logging.level>debug</keycloak.liquibase.logging.level>
@ -397,6 +398,7 @@
<systemPropertyVariables> <systemPropertyVariables>
<keycloak.realm.provider>mongo</keycloak.realm.provider> <keycloak.realm.provider>mongo</keycloak.realm.provider>
<keycloak.user.provider>mongo</keycloak.user.provider> <keycloak.user.provider>mongo</keycloak.user.provider>
<keycloak.userSessionPersister.provider>mongo</keycloak.userSessionPersister.provider>
<keycloak.eventsStore.provider>mongo</keycloak.eventsStore.provider> <keycloak.eventsStore.provider>mongo</keycloak.eventsStore.provider>
<keycloak.connectionsMongo.host>${keycloak.connectionsMongo.host}</keycloak.connectionsMongo.host> <keycloak.connectionsMongo.host>${keycloak.connectionsMongo.host}</keycloak.connectionsMongo.host>
<keycloak.connectionsMongo.port>${keycloak.connectionsMongo.port}</keycloak.connectionsMongo.port> <keycloak.connectionsMongo.port>${keycloak.connectionsMongo.port}</keycloak.connectionsMongo.port>

View file

@ -1,8 +1,11 @@
package org.keycloak.testsuite.federation.ldap.base; package org.keycloak.testsuite.federation.ldap.base;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before;
import org.junit.ClassRule; import org.junit.ClassRule;
import org.junit.FixMethodOrder; import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
@ -77,6 +80,20 @@ public class LDAPGroupMapperSyncTest {
.outerRule(ldapRule) .outerRule(ldapRule)
.around(keycloakRule); .around(keycloakRule);
@Before
public void before() {
KeycloakSession session = keycloakRule.startSession();
try {
RealmModel realm = session.realms().getRealmByName("test");
List<GroupModel> kcGroups = realm.getTopLevelGroups();
for (GroupModel kcGroup : kcGroups) {
realm.removeGroup(kcGroup);
}
} finally {
keycloakRule.stopSession(session, true);
}
}
@Test @Test
public void test01_syncNoPreserveGroupInheritance() throws Exception { public void test01_syncNoPreserveGroupInheritance() throws Exception {
KeycloakSession session = keycloakRule.startSession(); KeycloakSession session = keycloakRule.startSession();
@ -213,7 +230,7 @@ public class LDAPGroupMapperSyncTest {
// Sync groups again from LDAP. Assert LDAP non-existing groups deleted // Sync groups again from LDAP. Assert LDAP non-existing groups deleted
syncResult = new GroupLDAPFederationMapperFactory().create(session).syncDataFromFederationProviderToKeycloak(mapperModel, ldapProvider, session, realm); syncResult = new GroupLDAPFederationMapperFactory().create(session).syncDataFromFederationProviderToKeycloak(mapperModel, ldapProvider, session, realm);
Assert.assertEquals(3, syncResult.getUpdated()); Assert.assertEquals(3, syncResult.getUpdated());
Assert.assertTrue(syncResult.getRemoved() >= 2); Assert.assertTrue(syncResult.getRemoved() == 2);
// Sync and assert groups updated // Sync and assert groups updated
Assert.assertNotNull(KeycloakModelUtils.findGroupByPath(realm, "/group1/group11")); Assert.assertNotNull(KeycloakModelUtils.findGroupByPath(realm, "/group1/group11"));