Search in sources :

Example 1 with ProviderEvent

use of org.keycloak.provider.ProviderEvent in project keycloak by keycloak.

the class InfinispanUserSessionProviderFactory method postInit.

@Override
public void postInit(final KeycloakSessionFactory factory) {
    factory.register(new ProviderEventListener() {

        @Override
        public void onEvent(ProviderEvent event) {
            if (event instanceof PostMigrationEvent) {
                int preloadTransactionTimeout = getTimeoutForPreloadingSessionsSeconds();
                log.debugf("Will preload sessions with transaction timeout %d seconds", preloadTransactionTimeout);
                KeycloakModelUtils.runJobInTransactionWithTimeout(factory, (KeycloakSession session) -> {
                    keyGenerator = new InfinispanKeyGenerator();
                    checkRemoteCaches(session);
                    loadPersistentSessions(factory, getMaxErrors(), getSessionsPerSegment());
                    registerClusterListeners(session);
                    loadSessionsFromRemoteCaches(session);
                }, preloadTransactionTimeout);
            } else if (event instanceof UserModel.UserRemovedEvent) {
                UserModel.UserRemovedEvent userRemovedEvent = (UserModel.UserRemovedEvent) event;
                InfinispanUserSessionProvider provider = (InfinispanUserSessionProvider) userRemovedEvent.getKeycloakSession().getProvider(UserSessionProvider.class, getId());
                provider.onUserRemoved(userRemovedEvent.getRealm(), userRemovedEvent.getUser());
            } else if (event instanceof ResetTimeOffsetEvent) {
                if (persisterLastSessionRefreshStore != null) {
                    persisterLastSessionRefreshStore.reset();
                }
                if (lastSessionRefreshStore != null) {
                    lastSessionRefreshStore.reset();
                }
                if (offlineLastSessionRefreshStore != null) {
                    offlineLastSessionRefreshStore.reset();
                }
            }
        }
    });
}
Also used : ProviderEventListener(org.keycloak.provider.ProviderEventListener) ProviderEvent(org.keycloak.provider.ProviderEvent) UserModel(org.keycloak.models.UserModel) UserSessionProvider(org.keycloak.models.UserSessionProvider) ResetTimeOffsetEvent(org.keycloak.models.utils.ResetTimeOffsetEvent) KeycloakSession(org.keycloak.models.KeycloakSession) InfinispanKeyGenerator(org.keycloak.models.sessions.infinispan.util.InfinispanKeyGenerator) PostMigrationEvent(org.keycloak.models.utils.PostMigrationEvent)

Example 2 with ProviderEvent

use of org.keycloak.provider.ProviderEvent in project keycloak by keycloak.

the class AuthorizationStoreFactory method registerSynchronizationListeners.

default void registerSynchronizationListeners(KeycloakSessionFactory factory) {
    Map<Class<? extends ProviderEvent>, Synchronizer> synchronizers = new HashMap<>();
    synchronizers.put(ClientRemovedEvent.class, new ClientApplicationSynchronizer());
    synchronizers.put(RealmRemovedEvent.class, new RealmSynchronizer());
    synchronizers.put(UserRemovedEvent.class, new UserSynchronizer());
    synchronizers.put(GroupModel.GroupRemovedEvent.class, new GroupSynchronizer());
    factory.register(event -> {
        try {
            synchronizers.forEach((eventType, synchronizer) -> {
                if (eventType.isInstance(event)) {
                    synchronizer.synchronize(event, factory);
                }
            });
        } catch (Exception e) {
            throw new RuntimeException("Error synchronizing authorization data.", e);
        }
    });
}
Also used : ProviderEvent(org.keycloak.provider.ProviderEvent) GroupSynchronizer(org.keycloak.authorization.store.syncronization.GroupSynchronizer) RealmSynchronizer(org.keycloak.authorization.store.syncronization.RealmSynchronizer) UserSynchronizer(org.keycloak.authorization.store.syncronization.UserSynchronizer) Synchronizer(org.keycloak.authorization.store.syncronization.Synchronizer) ClientApplicationSynchronizer(org.keycloak.authorization.store.syncronization.ClientApplicationSynchronizer) UserSynchronizer(org.keycloak.authorization.store.syncronization.UserSynchronizer) GroupSynchronizer(org.keycloak.authorization.store.syncronization.GroupSynchronizer) HashMap(java.util.HashMap) RealmSynchronizer(org.keycloak.authorization.store.syncronization.RealmSynchronizer) ClientApplicationSynchronizer(org.keycloak.authorization.store.syncronization.ClientApplicationSynchronizer) GroupModel(org.keycloak.models.GroupModel)

Example 3 with ProviderEvent

use of org.keycloak.provider.ProviderEvent in project keycloak by keycloak.

the class AdminPermissions method registerListener.

public static void registerListener(ProviderEventManager manager) {
    manager.register(new ProviderEventListener() {

        @Override
        public void onEvent(ProviderEvent event) {
            if (event instanceof RoleContainerModel.RoleRemovedEvent) {
                RoleContainerModel.RoleRemovedEvent cast = (RoleContainerModel.RoleRemovedEvent) event;
                RoleModel role = cast.getRole();
                RealmModel realm;
                if (role.getContainer() instanceof ClientModel) {
                    realm = ((ClientModel) role.getContainer()).getRealm();
                } else {
                    realm = (RealmModel) role.getContainer();
                }
                management(cast.getKeycloakSession(), realm).roles().setPermissionsEnabled(role, false);
            } else if (event instanceof ClientModel.ClientRemovedEvent) {
                ClientModel.ClientRemovedEvent cast = (ClientModel.ClientRemovedEvent) event;
                management(cast.getKeycloakSession(), cast.getClient().getRealm()).clients().setPermissionsEnabled(cast.getClient(), false);
            } else if (event instanceof GroupModel.GroupRemovedEvent) {
                GroupModel.GroupRemovedEvent cast = (GroupModel.GroupRemovedEvent) event;
                management(cast.getKeycloakSession(), cast.getRealm()).groups().setPermissionsEnabled(cast.getGroup(), false);
            }
        }
    });
}
Also used : GroupModel(org.keycloak.models.GroupModel) RoleModel(org.keycloak.models.RoleModel) ProviderEventListener(org.keycloak.provider.ProviderEventListener) ProviderEvent(org.keycloak.provider.ProviderEvent) RealmModel(org.keycloak.models.RealmModel) ClientModel(org.keycloak.models.ClientModel) RoleContainerModel(org.keycloak.models.RoleContainerModel)

Aggregations

ProviderEvent (org.keycloak.provider.ProviderEvent)3 GroupModel (org.keycloak.models.GroupModel)2 ProviderEventListener (org.keycloak.provider.ProviderEventListener)2 HashMap (java.util.HashMap)1 ClientApplicationSynchronizer (org.keycloak.authorization.store.syncronization.ClientApplicationSynchronizer)1 GroupSynchronizer (org.keycloak.authorization.store.syncronization.GroupSynchronizer)1 RealmSynchronizer (org.keycloak.authorization.store.syncronization.RealmSynchronizer)1 Synchronizer (org.keycloak.authorization.store.syncronization.Synchronizer)1 UserSynchronizer (org.keycloak.authorization.store.syncronization.UserSynchronizer)1 ClientModel (org.keycloak.models.ClientModel)1 KeycloakSession (org.keycloak.models.KeycloakSession)1 RealmModel (org.keycloak.models.RealmModel)1 RoleContainerModel (org.keycloak.models.RoleContainerModel)1 RoleModel (org.keycloak.models.RoleModel)1 UserModel (org.keycloak.models.UserModel)1 UserSessionProvider (org.keycloak.models.UserSessionProvider)1 InfinispanKeyGenerator (org.keycloak.models.sessions.infinispan.util.InfinispanKeyGenerator)1 PostMigrationEvent (org.keycloak.models.utils.PostMigrationEvent)1 ResetTimeOffsetEvent (org.keycloak.models.utils.ResetTimeOffsetEvent)1