Moving RoleStorageProviderModel to the legacy modules

Closes #12656
This commit is contained in:
Alexander Schwartz 2022-06-23 17:39:50 +02:00 committed by Hynek Mlnařík
parent 05f8f3038f
commit ddeab744d0
5 changed files with 34 additions and 32 deletions

View file

@ -23,6 +23,7 @@ import org.keycloak.common.util.reflections.Types;
import org.keycloak.component.ComponentModel;
import org.keycloak.models.ClientModel;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.LegacyRealmModel;
import org.keycloak.models.ModelException;
import org.keycloak.models.RealmModel;
import org.keycloak.models.RoleModel;
@ -71,9 +72,8 @@ public class RoleStorageManager implements RoleProvider {
return getStorageProviderInstance(session, storageModel, factory);
}
public static <T> Stream<RoleStorageProviderModel> getStorageProviders(RealmModel realm, KeycloakSession session, Class<T> type) {
return realm.getRoleStorageProvidersStream()
return ((LegacyRealmModel) realm).getRoleStorageProvidersStream()
.filter(model -> {
RoleStorageProviderFactory factory = getRoleStorageProviderFactory(model, session);
if (factory == null) {
@ -162,9 +162,9 @@ public class RoleStorageManager implements RoleProvider {
/**
* Obtaining roles from an external role storage is time-bounded. In case the external role storage
* isn't available at least roles from a local storage are returned. For this purpose
* the {@link org.keycloak.services.DefaultKeycloakSessionFactory#getRoleStorageProviderTimeout()} property is used.
* the <code>org.keycloak.services.DefaultKeycloakSessionFactory#getRoleStorageProviderTimeout()</code> property is used.
* Default value is 3000 milliseconds and it's configurable.
* See {@link org.keycloak.services.DefaultKeycloakSessionFactory} for details.
* See <code>org.keycloak.services.DefaultKeycloakSessionFactory</code> for details.
*/
@Override
public Stream<RoleModel> searchForRolesStream(RealmModel realm, String search, Integer first, Integer max) {
@ -229,9 +229,9 @@ public class RoleStorageManager implements RoleProvider {
/**
* Obtaining roles from an external role storage is time-bounded. In case the external role storage
* isn't available at least roles from a local storage are returned. For this purpose
* the {@link org.keycloak.services.DefaultKeycloakSessionFactory#getRoleStorageProviderTimeout()} property is used.
* the <code>org.keycloak.services.DefaultKeycloakSessionFactory#getRoleStorageProviderTimeout()} property is used.
* Default value is 3000 milliseconds and it's configurable.
* See {@link org.keycloak.services.DefaultKeycloakSessionFactory} for details.
* See <code>org.keycloak.services.DefaultKeycloakSessionFactory</code> for details.
*/
@Override
public Stream<RoleModel> searchForClientRolesStream(ClientModel client, String search, Integer first, Integer max) {

View file

@ -20,6 +20,8 @@ package org.keycloak.models;
import org.keycloak.models.RealmModel;
import org.keycloak.storage.client.ClientStorageProvider;
import org.keycloak.storage.client.ClientStorageProviderModel;
import org.keycloak.storage.role.RoleStorageProvider;
import org.keycloak.storage.role.RoleStorageProviderModel;
import java.util.List;
import java.util.stream.Collectors;
@ -47,4 +49,24 @@ public interface LegacyRealmModel extends RealmModel {
.map(ClientStorageProviderModel::new)
.sorted(ClientStorageProviderModel.comparator);
}
/**
* @deprecated Use {@link #getRoleStorageProvidersStream() getRoleStorageProvidersStream} instead.
*/
@Deprecated
default List<RoleStorageProviderModel> getRoleStorageProviders() {
return getRoleStorageProvidersStream().collect(Collectors.toList());
}
/**
* Returns sorted {@link RoleStorageProviderModel RoleStorageProviderModel} as a stream.
* It should be used with forEachOrdered if the ordering is required.
* @return Sorted stream of {@link RoleStorageProviderModel}. Never returns {@code null}.
*/
default Stream<RoleStorageProviderModel> getRoleStorageProvidersStream() {
return getComponentsStream(getId(), RoleStorageProvider.class.getName())
.map(RoleStorageProviderModel::new)
.sorted(RoleStorageProviderModel.comparator);
}
}

View file

@ -23,8 +23,6 @@ import org.keycloak.component.ComponentModel;
import org.keycloak.provider.Provider;
import org.keycloak.provider.ProviderEvent;
import org.keycloak.storage.SearchableModelField;
import org.keycloak.storage.role.RoleStorageProvider;
import org.keycloak.storage.role.RoleStorageProviderModel;
import java.util.List;
import java.util.Map;
@ -658,25 +656,6 @@ public interface RealmModel extends RoleContainerModel {
ComponentModel getComponent(String id);
/**
* @deprecated Use {@link #getRoleStorageProvidersStream() getRoleStorageProvidersStream} instead.
*/
@Deprecated
default List<RoleStorageProviderModel> getRoleStorageProviders() {
return getRoleStorageProvidersStream().collect(Collectors.toList());
}
/**
* Returns sorted {@link RoleStorageProviderModel RoleStorageProviderModel} as a stream.
* It should be used with forEachOrdered if the ordering is required.
* @return Sorted stream of {@link RoleStorageProviderModel}. Never returns {@code null}.
*/
default Stream<RoleStorageProviderModel> getRoleStorageProvidersStream() {
return getComponentsStream(getId(), RoleStorageProvider.class.getName())
.map(RoleStorageProviderModel::new)
.sorted(RoleStorageProviderModel.comparator);
}
/**
* Returns stream of ComponentModels that represent StorageProviders for class storageProviderClass in this realm.
* @param storageProviderClass {@code Class<? extends Provider>}

View file

@ -31,6 +31,7 @@ import org.keycloak.events.Details;
import org.keycloak.models.ClientModel;
import org.keycloak.models.Constants;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.LegacyRealmModel;
import org.keycloak.models.RealmModel;
import org.keycloak.models.cache.infinispan.ClientAdapter;
import org.keycloak.representations.AccessToken;
@ -341,7 +342,7 @@ public class ClientStorageTest extends AbstractTestRealmKeycloakTest {
testingClient.server().run(session -> {
RealmModel realm = session.realms().getRealmByName("test");
ClientStorageProviderModel model = realm.getClientStorageProvidersStream().findFirst().get();
ClientStorageProviderModel model = ((LegacyRealmModel) realm).getClientStorageProvidersStream().findFirst().get();
Calendar eviction = Calendar.getInstance();
eviction.add(Calendar.HOUR, 1);
model.setCachePolicy(CacheableStorageProviderModel.CachePolicy.EVICT_DAILY);
@ -366,7 +367,7 @@ public class ClientStorageTest extends AbstractTestRealmKeycloakTest {
testingClient.server().run(session -> {
RealmModel realm = session.realms().getRealmByName("test");
ClientStorageProviderModel model = realm.getClientStorageProvidersStream().findAny().get();
ClientStorageProviderModel model = ((LegacyRealmModel) realm).getClientStorageProvidersStream().findAny().get();
Calendar eviction = Calendar.getInstance();
eviction.add(Calendar.HOUR, 4 * 24);
model.setCachePolicy(CacheableStorageProviderModel.CachePolicy.EVICT_WEEKLY);
@ -394,7 +395,7 @@ public class ClientStorageTest extends AbstractTestRealmKeycloakTest {
testingClient.server().run(session -> {
RealmModel realm = session.realms().getRealmByName("test");
ClientStorageProviderModel model = realm.getClientStorageProvidersStream().findFirst().get();
ClientStorageProviderModel model = ((LegacyRealmModel) realm).getClientStorageProvidersStream().findFirst().get();
model.setCachePolicy(CacheableStorageProviderModel.CachePolicy.MAX_LIFESPAN);
model.setMaxLifespan(1 * 60 * 60 * 1000);
realm.updateComponent(model);
@ -446,7 +447,7 @@ public class ClientStorageTest extends AbstractTestRealmKeycloakTest {
testingClient.server().run(session -> {
RealmModel realm = session.realms().getRealmByName("test");
ClientStorageProviderModel model = realm.getClientStorageProvidersStream().findFirst().get();
ClientStorageProviderModel model = ((LegacyRealmModel) realm).getClientStorageProvidersStream().findFirst().get();
model.setCachePolicy(CacheableStorageProviderModel.CachePolicy.NO_CACHE);
realm.updateComponent(model);
});
@ -466,7 +467,7 @@ public class ClientStorageTest extends AbstractTestRealmKeycloakTest {
private void setDefaultCachePolicy() {
testingClient.server().run(session -> {
RealmModel realm = session.realms().getRealmByName("test");
ClientStorageProviderModel model = realm.getClientStorageProvidersStream().findFirst().get();
ClientStorageProviderModel model = ((LegacyRealmModel) realm).getClientStorageProvidersStream().findFirst().get();
model.setCachePolicy(CacheableStorageProviderModel.CachePolicy.DEFAULT);
realm.updateComponent(model);
});