Search in sources :

Example 6 with UserStorageProviderFactory

use of org.keycloak.storage.UserStorageProviderFactory in project keycloak by keycloak.

the class UserStorageSyncManager method syncChangedUsers.

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() || !provider.isEnabled()) {
        return SynchronizationResult.ignored();
    }
    final Holder holder = new Holder();
    // Ensure not executed concurrently on this or any other cluster node
    KeycloakModelUtils.runJobInTransaction(sessionFactory, new KeycloakSessionTask() {

        @Override
        public void run(KeycloakSession session) {
            ClusterProvider clusterProvider = session.getProvider(ClusterProvider.class);
            // shared key for "full" and "changed" . Improve if needed
            String taskKey = provider.getId() + "::sync";
            // 30 seconds minimal timeout for now
            int timeout = Math.max(30, provider.getChangedSyncPeriod());
            holder.result = clusterProvider.executeIfNotExecuted(taskKey, timeout, new Callable<SynchronizationResult>() {

                @Override
                public SynchronizationResult call() throws Exception {
                    // See when we did last sync.
                    int oldLastSync = provider.getLastSync();
                    updateLastSyncInterval(sessionFactory, provider, realmId);
                    return ((ImportSynchronization) factory).syncSince(Time.toDate(oldLastSync), sessionFactory, realmId, provider);
                }
            });
        }
    });
    if (holder.result == null || !holder.result.isExecuted()) {
        logger.debugf("syncChangedUsers for federation provider %s was ignored as it's already in progress", provider.getName());
        return SynchronizationResult.ignored();
    } else {
        return holder.result.getResult();
    }
}
Also used : UserStorageProvider(org.keycloak.storage.UserStorageProvider) ClusterProvider(org.keycloak.cluster.ClusterProvider) KeycloakSessionTask(org.keycloak.models.KeycloakSessionTask) UserStorageProviderFactory(org.keycloak.storage.UserStorageProviderFactory) ImportSynchronization(org.keycloak.storage.user.ImportSynchronization) KeycloakSession(org.keycloak.models.KeycloakSession) SynchronizationResult(org.keycloak.storage.user.SynchronizationResult)

Example 7 with UserStorageProviderFactory

use of org.keycloak.storage.UserStorageProviderFactory in project keycloak by keycloak.

the class UserStorageSyncManager method syncAllUsers.

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() || !provider.isEnabled()) {
        return SynchronizationResult.ignored();
    }
    final Holder holder = new Holder();
    // Ensure not executed concurrently on this or any other cluster node
    KeycloakModelUtils.runJobInTransaction(sessionFactory, new KeycloakSessionTask() {

        @Override
        public void run(KeycloakSession session) {
            ClusterProvider clusterProvider = session.getProvider(ClusterProvider.class);
            // shared key for "full" and "changed" . Improve if needed
            String taskKey = provider.getId() + "::sync";
            // 30 seconds minimal timeout for now
            int timeout = Math.max(30, provider.getFullSyncPeriod());
            holder.result = clusterProvider.executeIfNotExecuted(taskKey, timeout, new Callable<SynchronizationResult>() {

                @Override
                public SynchronizationResult call() throws Exception {
                    updateLastSyncInterval(sessionFactory, provider, realmId);
                    return ((ImportSynchronization) factory).sync(sessionFactory, realmId, provider);
                }
            });
        }
    });
    if (holder.result == null || !holder.result.isExecuted()) {
        logger.debugf("syncAllUsers for federation provider %s was ignored as it's already in progress", provider.getName());
        return SynchronizationResult.ignored();
    } else {
        return holder.result.getResult();
    }
}
Also used : UserStorageProvider(org.keycloak.storage.UserStorageProvider) ClusterProvider(org.keycloak.cluster.ClusterProvider) KeycloakSessionTask(org.keycloak.models.KeycloakSessionTask) UserStorageProviderFactory(org.keycloak.storage.UserStorageProviderFactory) ImportSynchronization(org.keycloak.storage.user.ImportSynchronization) KeycloakSession(org.keycloak.models.KeycloakSession) SynchronizationResult(org.keycloak.storage.user.SynchronizationResult)

Example 8 with UserStorageProviderFactory

use of org.keycloak.storage.UserStorageProviderFactory in project keycloak by keycloak.

the class ComponentUtil method notifyUpdated.

public static void notifyUpdated(KeycloakSession session, RealmModel realm, ComponentModel oldModel, ComponentModel newModel) {
    ComponentFactory factory = getComponentFactory(session, newModel);
    factory.onUpdate(session, realm, oldModel, newModel);
    if (factory instanceof UserStorageProviderFactory) {
        ((OnUpdateComponent) session.userStorageManager()).onUpdate(session, realm, oldModel, newModel);
    }
}
Also used : OnUpdateComponent(org.keycloak.storage.OnUpdateComponent) ComponentFactory(org.keycloak.component.ComponentFactory) UserStorageProviderFactory(org.keycloak.storage.UserStorageProviderFactory)

Aggregations

UserStorageProviderFactory (org.keycloak.storage.UserStorageProviderFactory)8 UserStorageProvider (org.keycloak.storage.UserStorageProvider)6 ClusterProvider (org.keycloak.cluster.ClusterProvider)4 ImportSynchronization (org.keycloak.storage.user.ImportSynchronization)4 ComponentModel (org.keycloak.component.ComponentModel)3 KeycloakSession (org.keycloak.models.KeycloakSession)3 SynchronizationResult (org.keycloak.storage.user.SynchronizationResult)3 ComponentFactory (org.keycloak.component.ComponentFactory)2 KeycloakSessionTask (org.keycloak.models.KeycloakSessionTask)2 UserStorageProviderModel (org.keycloak.storage.UserStorageProviderModel)2 IntStream (java.util.stream.IntStream)1 CoreMatchers.is (org.hamcrest.CoreMatchers.is)1 CoreMatchers.notNullValue (org.hamcrest.CoreMatchers.notNullValue)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 Matchers (org.hamcrest.Matchers)1 Assume.assumeThat (org.junit.Assume.assumeThat)1 Test (org.junit.Test)1 Constants (org.keycloak.models.Constants)1 ModelException (org.keycloak.models.ModelException)1 RealmModel (org.keycloak.models.RealmModel)1