diff --git a/model/infinispan/src/main/java/org/keycloak/models/cache/infinispan/RealmAdapter.java b/model/infinispan/src/main/java/org/keycloak/models/cache/infinispan/RealmAdapter.java index 96fee9ba1d53..e932dadc1383 100755 --- a/model/infinispan/src/main/java/org/keycloak/models/cache/infinispan/RealmAdapter.java +++ b/model/infinispan/src/main/java/org/keycloak/models/cache/infinispan/RealmAdapter.java @@ -17,6 +17,8 @@ package org.keycloak.models.cache.infinispan; +import static org.keycloak.models.utils.KeycloakModelUtils.runOnRealm; + import org.keycloak.Config; import org.keycloak.cluster.ClusterProvider; import org.keycloak.common.enums.SslRequired; @@ -916,27 +918,30 @@ public void setSmtpConfig(Map smtpConfig) { @Override public Stream getIdentityProvidersStream() { - return session.identityProviders().getAllStream(); + return runOnRealm(session, this, (session) -> session.identityProviders().getAllStream()); } @Override public IdentityProviderModel getIdentityProviderByAlias(String alias) { - return session.identityProviders().getByAlias(alias); + return runOnRealm(session, this, (session) -> session.identityProviders().getByAlias(alias)); } @Override public void addIdentityProvider(IdentityProviderModel identityProvider) { - session.identityProviders().create(identityProvider); + runOnRealm(session, this, (session) -> session.identityProviders().create(identityProvider)); } @Override public void updateIdentityProvider(IdentityProviderModel identityProvider) { - session.identityProviders().update(identityProvider); + runOnRealm(session, this, (session) -> { + session.identityProviders().update(identityProvider); + return null; + }); } @Override public void removeIdentityProviderByAlias(String alias) { - session.identityProviders().remove(alias); + runOnRealm(session, this, (session) -> session.identityProviders().remove(alias)); } @Override diff --git a/server-spi-private/src/main/java/org/keycloak/models/utils/KeycloakModelUtils.java b/server-spi-private/src/main/java/org/keycloak/models/utils/KeycloakModelUtils.java index ad0fd4a6c11a..c1728199d28c 100755 --- a/server-spi-private/src/main/java/org/keycloak/models/utils/KeycloakModelUtils.java +++ b/server-spi-private/src/main/java/org/keycloak/models/utils/KeycloakModelUtils.java @@ -1215,4 +1215,30 @@ public static void setupAdminPermissionsClient(KeycloakSession session, RealmMod RepresentationToModel.toModel(resourceServerRep, session.getProvider(AuthorizationProvider.class), client); } + /** + *

Runs the given {@code operation} within the scope of the given @{target} realm. + * + *

Only use this method when you need to execute operations in a {@link RealmModel} object that is different + * than the one associated with the {@code session}. + * + * @param session the session + * @param target the target realm + * @param operation the operation + * @return the result from the supplier + */ + public static T runOnRealm(KeycloakSession session, RealmModel target, Function operation) { + KeycloakContext context = session.getContext(); + RealmModel currentRealm = context.getRealm(); + + if (currentRealm.equals(target)) { + return operation.apply(session); + } + + try { + context.setRealm(target); + return operation.apply(session); + } finally { + context.setRealm(currentRealm); + } + } }