Skip to content

Commit

Permalink
Fix mongo model
Browse files Browse the repository at this point in the history
  • Loading branch information
mposolda committed Jan 11, 2016
1 parent 2ec4cb8 commit fe8755d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 31 deletions.
36 changes: 17 additions & 19 deletions model/jpa/src/main/java/org/keycloak/models/jpa/UserAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -433,25 +433,23 @@ public int compare(CredentialEntity credFirst, CredentialEntity credSecond) {

@Override
public List<UserCredentialValueModel> getCredentialsDirectly() {
List<CredentialEntity> credentials = new ArrayList<CredentialEntity>(user.getCredentials());
List<UserCredentialValueModel> result = new ArrayList<UserCredentialValueModel>();

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

result.add(credModel);
}
List<CredentialEntity> credentials = new ArrayList<>(user.getCredentials());
List<UserCredentialValueModel> result = new ArrayList<>();

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);
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,12 @@ public ClientTemplateModel getClientTemplate() {

@Override
public void setClientTemplate(ClientTemplateModel template) {
getMongoEntity().setClientTemplate(template.getId());
if (template == null) {
getMongoEntity().setClientTemplate(null);
} else {
getMongoEntity().setClientTemplate(template.getId());
}

updateMongoEntity();

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ public List<UserCredentialValueModel> getCredentialsDirectly() {
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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public void afterRemove(MongoStoreInvocationContext context) {
// 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);
}
Expand Down
2 changes: 2 additions & 0 deletions testsuite/integration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@
<systemPropertyVariables>
<keycloak.realm.provider>jpa</keycloak.realm.provider>
<keycloak.user.provider>jpa</keycloak.user.provider>
<keycloak.userSessionPersister.provider>jpa</keycloak.userSessionPersister.provider>
<keycloak.eventsStore.provider>jpa</keycloak.eventsStore.provider>

<keycloak.liquibase.logging.level>debug</keycloak.liquibase.logging.level>
Expand Down Expand Up @@ -397,6 +398,7 @@
<systemPropertyVariables>
<keycloak.realm.provider>mongo</keycloak.realm.provider>
<keycloak.user.provider>mongo</keycloak.user.provider>
<keycloak.userSessionPersister.provider>mongo</keycloak.userSessionPersister.provider>
<keycloak.eventsStore.provider>mongo</keycloak.eventsStore.provider>
<keycloak.connectionsMongo.host>${keycloak.connectionsMongo.host}</keycloak.connectionsMongo.host>
<keycloak.connectionsMongo.port>${keycloak.connectionsMongo.port}</keycloak.connectionsMongo.port>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -77,6 +80,20 @@ public void config(RealmManager manager, RealmModel adminstrationRealm, RealmMod
.outerRule(ldapRule)
.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
public void test01_syncNoPreserveGroupInheritance() throws Exception {
KeycloakSession session = keycloakRule.startSession();
Expand Down Expand Up @@ -213,7 +230,7 @@ public void test03_syncWithDropNonExistingGroups() throws Exception {
// 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"));
Expand Down

0 comments on commit fe8755d

Please sign in to comment.