diff --git a/model/legacy-private/src/main/java/org/keycloak/storage/UserStorageManager.java b/model/legacy-private/src/main/java/org/keycloak/storage/UserStorageManager.java index 2c82afb6d4..6356da6d59 100755 --- a/model/legacy-private/src/main/java/org/keycloak/storage/UserStorageManager.java +++ b/model/legacy-private/src/main/java/org/keycloak/storage/UserStorageManager.java @@ -389,7 +389,7 @@ public class UserStorageManager extends AbstractStorageManager { UserStorageProviderModel providerModel = new UserStorageProviderModel(realm.getComponent(userFederationId)); - return new UserStorageSyncManager().syncAllUsers(session.getKeycloakSessionFactory(), realm.getId(), providerModel); + return UserStorageSyncManager.syncAllUsers(session.getKeycloakSessionFactory(), realm.getId(), providerModel); }); long end = System.currentTimeMillis(); long timeNeeded = end - start; @@ -142,6 +142,32 @@ public class UserSyncTest extends KeycloakModelTest { assertThat(withRealm(realmId, (session, realm) -> UserStoragePrivateUtil.userLocalStorage(session).getUsersCount(realm)), is(NUMBER_OF_USERS)); } + @Test + public void testRemovedLDAPUserShouldNotFailGetUserByEmail() { + withRealm(realmId, (session, realm) -> { + UserStorageProviderModel providerModel = new UserStorageProviderModel(realm.getComponent(userFederationId)); + // disable cache + providerModel.setCachePolicy(CacheableStorageProviderModel.CachePolicy.NO_CACHE); + realm.updateComponent(providerModel); + + ComponentModel ldapModel = LDAPTestUtils.getLdapProviderModel(realm); + LDAPStorageProvider ldapFedProvider = LDAPTestUtils.getLdapProvider(session, ldapModel); + LDAPTestUtils.addLDAPUser(ldapFedProvider, realm, "user", "UserFN", "UserLN", "user@email.org", "userStreet", "1450"); + return null; + }); + + assertThat(withRealm(realmId, (session, realm) -> session.users().getUserByEmail(realm, "user@email.org")), is(notNullValue())); + + withRealm(realmId, (session, realm) -> { + ComponentModel ldapModel = LDAPTestUtils.getLdapProviderModel(realm); + LDAPStorageProvider ldapFedProvider = LDAPTestUtils.getLdapProvider(session, ldapModel); + LDAPTestUtils.removeLDAPUserByUsername(ldapFedProvider, realm, ldapFedProvider.getLdapIdentityStore().getConfig(), "user"); + return null; + }); + + assertThat(withRealm(realmId, (session, realm) -> session.users().getUserByEmail(realm, "user@email.org")), is(nullValue())); + } + @Test public void testAlwaysReadValueFromLDAPWorksWithNoCachePolicy() { // Create mapper from sn to a new user attribute