Search in sources :

Example 16 with UserStorageSyncManager

use of org.keycloak.services.managers.UserStorageSyncManager in project keycloak by keycloak.

the class UserStorageManager method onUpdate.

@Override
public void onUpdate(KeycloakSession session, RealmModel realm, ComponentModel oldModel, ComponentModel newModel) {
    ComponentFactory factory = ComponentUtil.getComponentFactory(session, newModel);
    if (!(factory instanceof UserStorageProviderFactory))
        return;
    UserStorageProviderModel old = new UserStorageProviderModel(oldModel);
    UserStorageProviderModel newP = new UserStorageProviderModel(newModel);
    if (old.getChangedSyncPeriod() != newP.getChangedSyncPeriod() || old.getFullSyncPeriod() != newP.getFullSyncPeriod() || old.isImportEnabled() != newP.isImportEnabled()) {
        new UserStorageSyncManager().notifyToRefreshPeriodicSync(session, realm, new UserStorageProviderModel(newModel), false);
    }
}
Also used : UserStorageSyncManager(org.keycloak.services.managers.UserStorageSyncManager) ComponentFactory(org.keycloak.component.ComponentFactory)

Example 17 with UserStorageSyncManager

use of org.keycloak.services.managers.UserStorageSyncManager in project keycloak by keycloak.

the class UserStorageProviderResource method syncUsers.

/**
 * Trigger sync of users
 *
 * Action can be "triggerFullSync" or "triggerChangedUsersSync"
 *
 * @param id
 * @param action
 * @return
 */
@POST
@Path("{id}/sync")
@NoCache
@Produces(MediaType.APPLICATION_JSON)
public SynchronizationResult syncUsers(@PathParam("id") String id, @QueryParam("action") String action) {
    auth.users().requireManage();
    ComponentModel model = realm.getComponent(id);
    if (model == null) {
        throw new NotFoundException("Could not find component");
    }
    if (!model.getProviderType().equals(UserStorageProvider.class.getName())) {
        throw new NotFoundException("found, but not a UserStorageProvider");
    }
    UserStorageProviderModel providerModel = new UserStorageProviderModel(model);
    logger.debug("Syncing users");
    UserStorageSyncManager syncManager = new UserStorageSyncManager();
    SynchronizationResult syncResult;
    if ("triggerFullSync".equals(action)) {
        syncResult = syncManager.syncAllUsers(session.getKeycloakSessionFactory(), realm.getId(), providerModel);
    } else if ("triggerChangedUsersSync".equals(action)) {
        syncResult = syncManager.syncChangedUsers(session.getKeycloakSessionFactory(), realm.getId(), providerModel);
    } else if (action == null || action == "") {
        logger.debug("Missing action");
        throw new BadRequestException("Missing action");
    } else {
        logger.debug("Unknown action: " + action);
        throw new BadRequestException("Unknown action: " + action);
    }
    Map<String, Object> eventRep = new HashMap<>();
    eventRep.put("action", action);
    eventRep.put("result", syncResult);
    adminEvent.operation(OperationType.ACTION).resourcePath(session.getContext().getUri()).representation(eventRep).success();
    return syncResult;
}
Also used : UserStorageSyncManager(org.keycloak.services.managers.UserStorageSyncManager) HashMap(java.util.HashMap) ComponentModel(org.keycloak.component.ComponentModel) NotFoundException(javax.ws.rs.NotFoundException) BadRequestException(javax.ws.rs.BadRequestException) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel) SynchronizationResult(org.keycloak.storage.user.SynchronizationResult) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Example 18 with UserStorageSyncManager

use of org.keycloak.services.managers.UserStorageSyncManager in project keycloak by keycloak.

the class UserSyncTest method testManyUsersImport.

@Test
public void testManyUsersImport() {
    IntStream.range(0, NUMBER_OF_USERS).parallel().forEach(index -> inComittedTransaction(index, (session, i) -> {
        final RealmModel realm = session.realms().getRealm(realmId);
        ComponentModel ldapModel = LDAPTestUtils.getLdapProviderModel(realm);
        LDAPStorageProvider ldapFedProvider = LDAPTestUtils.getLdapProvider(session, ldapModel);
        LDAPTestUtils.addLDAPUser(ldapFedProvider, realm, "user" + i, "User" + i + "FN", "User" + i + "LN", "user" + i + "@email.org", null, "12" + i);
        return null;
    }));
    assertThat(withRealm(realmId, (session, realm) -> session.userLocalStorage().getUsersCount(realm)), is(0));
    long start = System.currentTimeMillis();
    SynchronizationResult res = withRealm(realmId, (session, realm) -> {
        UserStorageProviderModel providerModel = new UserStorageProviderModel(realm.getComponent(userFederationId));
        return new UserStorageSyncManager().syncAllUsers(session.getKeycloakSessionFactory(), realm.getId(), providerModel);
    });
    long end = System.currentTimeMillis();
    long timeNeeded = end - start;
    // The sync shouldn't take more than 18 second per user
    assertThat(String.format("User sync took %f seconds per user, but it should take less than 18 seconds", (float) (timeNeeded) / NUMBER_OF_USERS), timeNeeded, Matchers.lessThan((long) (18 * NUMBER_OF_USERS)));
    assertThat(res.getAdded(), is(NUMBER_OF_USERS));
    assertThat(withRealm(realmId, (session, realm) -> session.userLocalStorage().getUsersCount(realm)), is(NUMBER_OF_USERS));
}
Also used : IntStream(java.util.stream.IntStream) CoreMatchers.is(org.hamcrest.CoreMatchers.is) RealmModel(org.keycloak.models.RealmModel) Assume.assumeThat(org.junit.Assume.assumeThat) LDAPStorageProvider(org.keycloak.storage.ldap.LDAPStorageProvider) UserStorageProvider(org.keycloak.storage.UserStorageProvider) LDAPTestUtils(org.keycloak.testsuite.util.LDAPTestUtils) Constants(org.keycloak.models.Constants) Matchers(org.hamcrest.Matchers) KeycloakSession(org.keycloak.models.KeycloakSession) Test(org.junit.Test) LDAPStorageProviderFactory(org.keycloak.storage.ldap.LDAPStorageProviderFactory) UserStorageSyncManager(org.keycloak.services.managers.UserStorageSyncManager) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) ClusterProvider(org.keycloak.cluster.ClusterProvider) UserProvider(org.keycloak.models.UserProvider) ImportSynchronization(org.keycloak.storage.user.ImportSynchronization) RealmProvider(org.keycloak.models.RealmProvider) ComponentModel(org.keycloak.component.ComponentModel) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel) SynchronizationResult(org.keycloak.storage.user.SynchronizationResult) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) UserStorageProviderFactory(org.keycloak.storage.UserStorageProviderFactory) RealmModel(org.keycloak.models.RealmModel) UserStorageSyncManager(org.keycloak.services.managers.UserStorageSyncManager) ComponentModel(org.keycloak.component.ComponentModel) LDAPStorageProvider(org.keycloak.storage.ldap.LDAPStorageProvider) SynchronizationResult(org.keycloak.storage.user.SynchronizationResult) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel) Test(org.junit.Test)

Aggregations

UserStorageSyncManager (org.keycloak.services.managers.UserStorageSyncManager)18 SynchronizationResult (org.keycloak.storage.user.SynchronizationResult)12 Test (org.junit.Test)11 RealmModel (org.keycloak.models.RealmModel)10 ComponentModel (org.keycloak.component.ComponentModel)8 KeycloakSessionFactory (org.keycloak.models.KeycloakSessionFactory)8 UserStorageProviderModel (org.keycloak.storage.UserStorageProviderModel)7 LDAPObject (org.keycloak.storage.ldap.idm.model.LDAPObject)6 HashMap (java.util.HashMap)4 UserModel (org.keycloak.models.UserModel)4 UserProvider (org.keycloak.models.UserProvider)4 BadRequestException (javax.ws.rs.BadRequestException)3 UserCache (org.keycloak.models.cache.UserCache)3 ComponentRepresentation (org.keycloak.representations.idm.ComponentRepresentation)3 UserStorageProvider (org.keycloak.storage.UserStorageProvider)3 LDAPStorageProvider (org.keycloak.storage.ldap.LDAPStorageProvider)3 AbstractAuthTest (org.keycloak.testsuite.AbstractAuthTest)3 NotFoundException (javax.ws.rs.NotFoundException)2 Matchers (org.hamcrest.Matchers)2 ComponentFactory (org.keycloak.component.ComponentFactory)2