From 49ba71fd8fe40037c128ce2019573ca13b7963fe Mon Sep 17 00:00:00 2001 From: Bill Burke Date: Thu, 7 Dec 2017 20:03:10 -0500 Subject: [PATCH] add logic for sync --- .../managers/UserStorageSyncManager.java | 4 ++-- .../FailableHardcodedStorageProvider.java | 12 ++++++++++-- ...ailableHardcodedStorageProviderFactory.java | 18 +++++++++++++++++- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/services/src/main/java/org/keycloak/services/managers/UserStorageSyncManager.java b/services/src/main/java/org/keycloak/services/managers/UserStorageSyncManager.java index 797301cc2a..90d102a979 100755 --- a/services/src/main/java/org/keycloak/services/managers/UserStorageSyncManager.java +++ b/services/src/main/java/org/keycloak/services/managers/UserStorageSyncManager.java @@ -81,7 +81,7 @@ public class UserStorageSyncManager { public SynchronizationResult syncAllUsers(final KeycloakSessionFactory sessionFactory, final String realmId, final UserStorageProviderModel provider) { UserStorageProviderFactory factory = (UserStorageProviderFactory) sessionFactory.getProviderFactory(UserStorageProvider.class, provider.getProviderId()); - if (!(factory instanceof ImportSynchronization) || !provider.isImportEnabled()) { + if (!(factory instanceof ImportSynchronization) || !provider.isImportEnabled() || !provider.isEnabled()) { return SynchronizationResult.ignored(); } @@ -122,7 +122,7 @@ public class UserStorageSyncManager { public SynchronizationResult syncChangedUsers(final KeycloakSessionFactory sessionFactory, final String realmId, final UserStorageProviderModel provider) { UserStorageProviderFactory factory = (UserStorageProviderFactory) sessionFactory.getProviderFactory(UserStorageProvider.class, provider.getProviderId()); - if (!(factory instanceof ImportSynchronization) || !provider.isImportEnabled()) { + if (!(factory instanceof ImportSynchronization) || !provider.isImportEnabled() || !provider.isEnabled()) { return SynchronizationResult.ignored(); } diff --git a/testsuite/integration-deprecated/src/test/java/org/keycloak/testsuite/federation/storage/FailableHardcodedStorageProvider.java b/testsuite/integration-deprecated/src/test/java/org/keycloak/testsuite/federation/storage/FailableHardcodedStorageProvider.java index 9ce2827874..0c221ab31d 100644 --- a/testsuite/integration-deprecated/src/test/java/org/keycloak/testsuite/federation/storage/FailableHardcodedStorageProvider.java +++ b/testsuite/integration-deprecated/src/test/java/org/keycloak/testsuite/federation/storage/FailableHardcodedStorageProvider.java @@ -62,7 +62,11 @@ public class FailableHardcodedStorageProvider implements UserStorageProvider, Us public FailableHardcodedStorageProvider(ComponentModel model, KeycloakSession session) { this.model = model; this.session = session; - componentFail = model.getConfig().getFirst("fail") != null && model.getConfig().getFirst("fail").equalsIgnoreCase("true"); + componentFail = isInFailMode(model); + } + + public static boolean isInFailMode(ComponentModel model) { + return model.getConfig().getFirst("fail") != null && model.getConfig().getFirst("fail").equalsIgnoreCase("true"); } @Override @@ -206,7 +210,11 @@ public class FailableHardcodedStorageProvider implements UserStorageProvider, Us } protected void checkForceFail() { - if (fail || componentFail) throw new RuntimeException("FORCED FAILURE"); + if (fail || componentFail) throwFailure(); + } + + public static void throwFailure() { + throw new RuntimeException("FORCED FAILURE"); } @Override diff --git a/testsuite/integration-deprecated/src/test/java/org/keycloak/testsuite/federation/storage/FailableHardcodedStorageProviderFactory.java b/testsuite/integration-deprecated/src/test/java/org/keycloak/testsuite/federation/storage/FailableHardcodedStorageProviderFactory.java index 46faec0244..de34096051 100644 --- a/testsuite/integration-deprecated/src/test/java/org/keycloak/testsuite/federation/storage/FailableHardcodedStorageProviderFactory.java +++ b/testsuite/integration-deprecated/src/test/java/org/keycloak/testsuite/federation/storage/FailableHardcodedStorageProviderFactory.java @@ -18,9 +18,14 @@ package org.keycloak.testsuite.federation.storage; import org.keycloak.component.ComponentModel; import org.keycloak.models.KeycloakSession; +import org.keycloak.models.KeycloakSessionFactory; import org.keycloak.provider.ProviderConfigProperty; import org.keycloak.storage.UserStorageProviderFactory; +import org.keycloak.storage.UserStorageProviderModel; +import org.keycloak.storage.user.ImportSynchronization; +import org.keycloak.storage.user.SynchronizationResult; +import java.util.Date; import java.util.LinkedList; import java.util.List; @@ -28,7 +33,7 @@ import java.util.List; * @author Bill Burke * @version $Revision: 1 $ */ -public class FailableHardcodedStorageProviderFactory implements UserStorageProviderFactory { +public class FailableHardcodedStorageProviderFactory implements UserStorageProviderFactory, ImportSynchronization { public static final String PROVIDER_ID = "failable-hardcoded-storage"; @@ -52,4 +57,15 @@ public class FailableHardcodedStorageProviderFactory implements UserStorageProvi return OPTIONS; } + @Override + public SynchronizationResult sync(KeycloakSessionFactory sessionFactory, String realmId, UserStorageProviderModel model) { + if (FailableHardcodedStorageProvider.isInFailMode(model)) FailableHardcodedStorageProvider.throwFailure(); + return SynchronizationResult.empty(); + } + + @Override + public SynchronizationResult syncSince(Date lastSync, KeycloakSessionFactory sessionFactory, String realmId, UserStorageProviderModel model) { + if (FailableHardcodedStorageProvider.isInFailMode(model)) FailableHardcodedStorageProvider.throwFailure(); + return SynchronizationResult.empty(); + } }