From 58d403cf24e6d57d2776d6b85f914db3eecf331d Mon Sep 17 00:00:00 2001 From: Hynek Mlnarik Date: Wed, 3 Nov 2021 20:46:32 +0100 Subject: [PATCH] KEYCLOAK-19726 Fix return types of ModelCriteriaBuilder methods --- .../MapRootAuthenticationSessionProvider.java | 6 +- .../MapPermissionTicketStore.java | 30 +++--- .../map/authorization/MapPolicyStore.java | 19 ++-- .../map/authorization/MapResourceStore.java | 22 ++--- .../map/authorization/MapScopeStore.java | 12 +-- .../models/map/client/MapClientProvider.java | 20 ++-- .../clientscope/MapClientScopeProvider.java | 6 +- .../models/map/group/MapGroupProvider.java | 28 +++--- .../MapUserLoginFailureProvider.java | 25 ++--- .../models/map/realm/MapRealmProvider.java | 10 +- .../models/map/role/MapRoleProvider.java | 20 ++-- .../map/storage/ModelCriteriaBuilder.java | 10 +- .../models/map/storage/QueryParameters.java | 9 +- .../ConcurrentHashMapKeycloakTransaction.java | 8 +- .../storage/chm/ConcurrentHashMapStorage.java | 11 +-- ...ncurrentHashMapStorageProviderFactory.java | 4 +- .../storage/chm/MapModelCriteriaBuilder.java | 12 +-- .../UserSessionConcurrentHashMapStorage.java | 5 +- .../criteria/DefaultModelCriteria.java | 33 +++---- .../storage/criteria/ModelCriteriaNode.java | 35 +++---- .../models/map/user/MapUserProvider.java | 44 ++++----- .../userSession/MapUserSessionProvider.java | 96 +++++++++---------- .../criteria/DefaultModelCriteriaTest.java | 4 +- 23 files changed, 232 insertions(+), 237 deletions(-) diff --git a/model/map/src/main/java/org/keycloak/models/map/authSession/MapRootAuthenticationSessionProvider.java b/model/map/src/main/java/org/keycloak/models/map/authSession/MapRootAuthenticationSessionProvider.java index 2097529614..82dbc79074 100644 --- a/model/map/src/main/java/org/keycloak/models/map/authSession/MapRootAuthenticationSessionProvider.java +++ b/model/map/src/main/java/org/keycloak/models/map/authSession/MapRootAuthenticationSessionProvider.java @@ -25,8 +25,8 @@ import org.keycloak.models.ModelDuplicateException; import org.keycloak.models.RealmModel; import org.keycloak.models.map.storage.MapKeycloakTransaction; import org.keycloak.models.map.storage.MapStorage; -import org.keycloak.models.map.storage.ModelCriteriaBuilder; import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator; +import org.keycloak.models.map.storage.criteria.DefaultModelCriteria; import org.keycloak.models.utils.RealmInfoUtil; import org.keycloak.sessions.AuthenticationSessionCompoundId; import org.keycloak.sessions.AuthenticationSessionProvider; @@ -131,7 +131,7 @@ public class MapRootAuthenticationSessionProvider implements AuthenticationSessi int expired = Time.currentTime() - RealmInfoUtil.getDettachedClientSessionLifespan(realm); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.TIMESTAMP, Operator.LT, expired); @@ -143,7 +143,7 @@ public class MapRootAuthenticationSessionProvider implements AuthenticationSessi @Override public void onRealmRemoved(RealmModel realm) { Objects.requireNonNull(realm, "The provided realm can't be null!"); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()); tx.delete(withCriteria(mcb)); diff --git a/model/map/src/main/java/org/keycloak/models/map/authorization/MapPermissionTicketStore.java b/model/map/src/main/java/org/keycloak/models/map/authorization/MapPermissionTicketStore.java index 2f71c8a63d..0c2898f7ef 100644 --- a/model/map/src/main/java/org/keycloak/models/map/authorization/MapPermissionTicketStore.java +++ b/model/map/src/main/java/org/keycloak/models/map/authorization/MapPermissionTicketStore.java @@ -31,9 +31,9 @@ import org.keycloak.models.map.authorization.adapter.MapPermissionTicketAdapter; import org.keycloak.models.map.authorization.entity.MapPermissionTicketEntity; import org.keycloak.models.map.storage.MapKeycloakTransaction; import org.keycloak.models.map.storage.MapStorage; -import org.keycloak.models.map.storage.ModelCriteriaBuilder; -import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator; +import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator; +import org.keycloak.models.map.storage.criteria.DefaultModelCriteria; import java.util.Collections; import java.util.EnumMap; import java.util.List; @@ -67,8 +67,8 @@ public class MapPermissionTicketStore implements PermissionTicketStore { return new MapPermissionTicketAdapter(origEntity, authorizationProvider.getStoreFactory()); } - private ModelCriteriaBuilder forResourceServer(String resourceServerId) { - ModelCriteriaBuilder mcb = criteria(); + private DefaultModelCriteria forResourceServer(String resourceServerId) { + DefaultModelCriteria mcb = criteria(); return resourceServerId == null ? mcb @@ -78,10 +78,10 @@ public class MapPermissionTicketStore implements PermissionTicketStore { @Override public long count(Map attributes, String resourceServerId) { - ModelCriteriaBuilder mcb = forResourceServer(resourceServerId).and( + DefaultModelCriteria mcb = forResourceServer(resourceServerId).and( attributes.entrySet().stream() - .map(this::filterEntryToModelCriteriaBuilder) - .toArray(ModelCriteriaBuilder[]::new) + .map(this::filterEntryToDefaultModelCriteria) + .toArray(DefaultModelCriteria[]::new) ); return tx.getCount(withCriteria(mcb)); @@ -94,7 +94,7 @@ public class MapPermissionTicketStore implements PermissionTicketStore { String owner = authorizationProvider.getStoreFactory().getResourceStore().findById(resourceId, resourceServer.getId()).getOwner(); // @UniqueConstraint(columnNames = {"OWNER", "REQUESTER", "RESOURCE_SERVER_ID", "RESOURCE_ID", "SCOPE_ID"}) - ModelCriteriaBuilder mcb = forResourceServer(resourceServer.getId()) + DefaultModelCriteria mcb = forResourceServer(resourceServer.getId()) .compare(SearchableFields.OWNER, Operator.EQ, owner) .compare(SearchableFields.RESOURCE_ID, Operator.EQ, resourceId) .compare(SearchableFields.REQUESTER, Operator.EQ, requester); @@ -183,7 +183,7 @@ public class MapPermissionTicketStore implements PermissionTicketStore { @Override public List find(Map attributes, String resourceServerId, int firstResult, int maxResult) { - ModelCriteriaBuilder mcb = forResourceServer(resourceServerId); + DefaultModelCriteria mcb = forResourceServer(resourceServerId); if (attributes.containsKey(PermissionTicket.FilterOption.RESOURCE_NAME)) { String expectedResourceName = attributes.remove(PermissionTicket.FilterOption.RESOURCE_NAME); @@ -201,8 +201,8 @@ public class MapPermissionTicketStore implements PermissionTicketStore { mcb = mcb.and( attributes.entrySet().stream() - .map(this::filterEntryToModelCriteriaBuilder) - .toArray(ModelCriteriaBuilder[]::new) + .map(this::filterEntryToDefaultModelCriteria) + .toArray(DefaultModelCriteria[]::new) ); return tx.read(withCriteria(mcb).pagination(firstResult, maxResult, SearchableFields.ID)) @@ -210,11 +210,11 @@ public class MapPermissionTicketStore implements PermissionTicketStore { .collect(Collectors.toList()); } - private ModelCriteriaBuilder filterEntryToModelCriteriaBuilder(Map.Entry entry) { + private DefaultModelCriteria filterEntryToDefaultModelCriteria(Map.Entry entry) { PermissionTicket.FilterOption name = entry.getKey(); String value = entry.getValue(); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); switch (name) { case ID: case SCOPE_ID: @@ -263,7 +263,7 @@ public class MapPermissionTicketStore implements PermissionTicketStore { @Override public List findGrantedResources(String requester, String name, int first, int max) { - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REQUESTER, Operator.EQ, requester) .compare(SearchableFields.GRANTED_TIMESTAMP, Operator.EXISTS); @@ -295,7 +295,7 @@ public class MapPermissionTicketStore implements PermissionTicketStore { @Override public List findGrantedOwnerResources(String owner, int first, int max) { - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.OWNER, Operator.EQ, owner); return paginatedStream(tx.read(withCriteria(mcb).orderBy(SearchableFields.RESOURCE_ID, ASCENDING)) diff --git a/model/map/src/main/java/org/keycloak/models/map/authorization/MapPolicyStore.java b/model/map/src/main/java/org/keycloak/models/map/authorization/MapPolicyStore.java index b5f6b3df5f..358290e102 100644 --- a/model/map/src/main/java/org/keycloak/models/map/authorization/MapPolicyStore.java +++ b/model/map/src/main/java/org/keycloak/models/map/authorization/MapPolicyStore.java @@ -29,7 +29,6 @@ import org.keycloak.models.map.authorization.adapter.MapPolicyAdapter; import org.keycloak.models.map.authorization.entity.MapPolicyEntity; import org.keycloak.models.map.storage.MapKeycloakTransaction; import org.keycloak.models.map.storage.MapStorage; -import org.keycloak.models.map.storage.ModelCriteriaBuilder; import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator; import org.keycloak.models.map.storage.criteria.DefaultModelCriteria; import org.keycloak.representations.idm.authorization.AbstractPolicyRepresentation; @@ -63,8 +62,8 @@ public class MapPolicyStore implements PolicyStore { return new MapPolicyAdapter(origEntity, authorizationProvider.getStoreFactory()); } - private ModelCriteriaBuilder forResourceServer(String resourceServerId) { - ModelCriteriaBuilder mcb = criteria(); + private DefaultModelCriteria forResourceServer(String resourceServerId) { + DefaultModelCriteria mcb = criteria(); return resourceServerId == null ? mcb @@ -77,7 +76,7 @@ public class MapPolicyStore implements PolicyStore { LOG.tracef("create(%s, %s, %s)%s", representation.getId(), resourceServer.getId(), resourceServer, getShortStackTrace()); // @UniqueConstraint(columnNames = {"NAME", "RESOURCE_SERVER_ID"}) - ModelCriteriaBuilder mcb = forResourceServer(resourceServer.getId()) + DefaultModelCriteria mcb = forResourceServer(resourceServer.getId()) .compare(SearchableFields.NAME, Operator.EQ, representation.getName()); if (tx.getCount(withCriteria(mcb)) > 0) { @@ -136,11 +135,11 @@ public class MapPolicyStore implements PolicyStore { public List findByResourceServer(Map attributes, String resourceServerId, int firstResult, int maxResult) { LOG.tracef("findByResourceServer(%s, %s, %d, %d)%s", attributes, resourceServerId, firstResult, maxResult, getShortStackTrace()); - ModelCriteriaBuilder mcb = forResourceServer(resourceServerId).and( + DefaultModelCriteria mcb = forResourceServer(resourceServerId).and( attributes.entrySet().stream() - .map(this::filterEntryToModelCriteriaBuilder) + .map(this::filterEntryToDefaultModelCriteria) .filter(Objects::nonNull) - .toArray(ModelCriteriaBuilder[]::new) + .toArray(DefaultModelCriteria[]::new) ); if (!attributes.containsKey(Policy.FilterOption.OWNER) && !attributes.containsKey(Policy.FilterOption.ANY_OWNER)) { @@ -154,11 +153,11 @@ public class MapPolicyStore implements PolicyStore { .collect(Collectors.toList()); } - private ModelCriteriaBuilder filterEntryToModelCriteriaBuilder(Map.Entry entry) { + private DefaultModelCriteria filterEntryToDefaultModelCriteria(Map.Entry entry) { Policy.FilterOption name = entry.getKey(); String[] value = entry.getValue(); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); switch (name) { case ID: case SCOPE_ID: @@ -220,7 +219,7 @@ public class MapPolicyStore implements PolicyStore { @Override public void findByScopeIds(List scopeIds, String resourceId, String resourceServerId, Consumer consumer) { - ModelCriteriaBuilder mcb = forResourceServer(resourceServerId) + DefaultModelCriteria mcb = forResourceServer(resourceServerId) .compare(SearchableFields.TYPE, Operator.EQ, "scope") .compare(SearchableFields.SCOPE_ID, Operator.IN, scopeIds); diff --git a/model/map/src/main/java/org/keycloak/models/map/authorization/MapResourceStore.java b/model/map/src/main/java/org/keycloak/models/map/authorization/MapResourceStore.java index 6b9d3d55a7..54dfe974c7 100644 --- a/model/map/src/main/java/org/keycloak/models/map/authorization/MapResourceStore.java +++ b/model/map/src/main/java/org/keycloak/models/map/authorization/MapResourceStore.java @@ -29,9 +29,9 @@ import org.keycloak.models.map.authorization.adapter.MapResourceAdapter; import org.keycloak.models.map.authorization.entity.MapResourceEntity; import org.keycloak.models.map.storage.MapKeycloakTransaction; import org.keycloak.models.map.storage.MapStorage; -import org.keycloak.models.map.storage.ModelCriteriaBuilder; -import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator; +import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator; +import org.keycloak.models.map.storage.criteria.DefaultModelCriteria; import java.util.Arrays; import java.util.LinkedList; import java.util.List; @@ -61,8 +61,8 @@ public class MapResourceStore implements ResourceStore { return new MapResourceAdapter(origEntity, authorizationProvider.getStoreFactory()); } - private ModelCriteriaBuilder forResourceServer(String resourceServerId) { - ModelCriteriaBuilder mcb = criteria(); + private DefaultModelCriteria forResourceServer(String resourceServerId) { + DefaultModelCriteria mcb = criteria(); return resourceServerId == null ? mcb @@ -74,7 +74,7 @@ public class MapResourceStore implements ResourceStore { public Resource create(String id, String name, ResourceServer resourceServer, String owner) { LOG.tracef("create(%s, %s, %s, %s)%s", id, name, resourceServer, owner, getShortStackTrace()); // @UniqueConstraint(columnNames = {"NAME", "RESOURCE_SERVER_ID", "OWNER"}) - ModelCriteriaBuilder mcb = forResourceServer(resourceServer.getId()) + DefaultModelCriteria mcb = forResourceServer(resourceServer.getId()) .compare(SearchableFields.NAME, Operator.EQ, name) .compare(SearchableFields.OWNER, Operator.EQ, owner); @@ -156,10 +156,10 @@ public class MapResourceStore implements ResourceStore { @Override public List findByResourceServer(Map attributes, String resourceServerId, int firstResult, int maxResult) { LOG.tracef("findByResourceServer(%s, %s, %d, %d)%s", attributes, resourceServerId, firstResult, maxResult, getShortStackTrace()); - ModelCriteriaBuilder mcb = forResourceServer(resourceServerId).and( + DefaultModelCriteria mcb = forResourceServer(resourceServerId).and( attributes.entrySet().stream() - .map(this::filterEntryToModelCriteriaBuilder) - .toArray(ModelCriteriaBuilder[]::new) + .map(this::filterEntryToDefaultModelCriteria) + .toArray(DefaultModelCriteria[]::new) ); return tx.read(withCriteria(mcb).pagination(firstResult, maxResult, SearchableFields.NAME)) @@ -167,11 +167,11 @@ public class MapResourceStore implements ResourceStore { .collect(Collectors.toList()); } - private ModelCriteriaBuilder filterEntryToModelCriteriaBuilder(Map.Entry entry) { + private DefaultModelCriteria filterEntryToDefaultModelCriteria(Map.Entry entry) { Resource.FilterOption name = entry.getKey(); String[] value = entry.getValue(); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); switch (name) { case ID: case SCOPE_ID: @@ -231,7 +231,7 @@ public class MapResourceStore implements ResourceStore { public void findByType(String type, String owner, String resourceServerId, Consumer consumer) { LOG.tracef("findByType(%s, %s, %s, %s)%s", type, owner, resourceServerId, consumer, getShortStackTrace()); - ModelCriteriaBuilder mcb = forResourceServer(resourceServerId) + DefaultModelCriteria mcb = forResourceServer(resourceServerId) .compare(SearchableFields.TYPE, Operator.EQ, type); if (owner != null) { diff --git a/model/map/src/main/java/org/keycloak/models/map/authorization/MapScopeStore.java b/model/map/src/main/java/org/keycloak/models/map/authorization/MapScopeStore.java index f357301137..3cb5731a38 100644 --- a/model/map/src/main/java/org/keycloak/models/map/authorization/MapScopeStore.java +++ b/model/map/src/main/java/org/keycloak/models/map/authorization/MapScopeStore.java @@ -29,9 +29,9 @@ import org.keycloak.models.map.authorization.adapter.MapScopeAdapter; import org.keycloak.models.map.authorization.entity.MapScopeEntity; import org.keycloak.models.map.storage.MapKeycloakTransaction; import org.keycloak.models.map.storage.MapStorage; -import org.keycloak.models.map.storage.ModelCriteriaBuilder; -import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator; +import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator; +import org.keycloak.models.map.storage.criteria.DefaultModelCriteria; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -59,8 +59,8 @@ public class MapScopeStore implements ScopeStore { return new MapScopeAdapter(origEntity, authorizationProvider.getStoreFactory()); } - private ModelCriteriaBuilder forResourceServer(String resourceServerId) { - ModelCriteriaBuilder mcb = criteria(); + private DefaultModelCriteria forResourceServer(String resourceServerId) { + DefaultModelCriteria mcb = criteria(); return resourceServerId == null ? mcb @@ -74,7 +74,7 @@ public class MapScopeStore implements ScopeStore { // @UniqueConstraint(columnNames = {"NAME", "RESOURCE_SERVER_ID"}) - ModelCriteriaBuilder mcb = forResourceServer(resourceServer.getId()) + DefaultModelCriteria mcb = forResourceServer(resourceServer.getId()) .compare(SearchableFields.NAME, Operator.EQ, name); if (tx.getCount(withCriteria(mcb)) > 0) { @@ -130,7 +130,7 @@ public class MapScopeStore implements ScopeStore { @Override public List findByResourceServer(Map attributes, String resourceServerId, int firstResult, int maxResult) { - ModelCriteriaBuilder mcb = forResourceServer(resourceServerId); + DefaultModelCriteria mcb = forResourceServer(resourceServerId); for (Scope.FilterOption filterOption : attributes.keySet()) { String[] value = attributes.get(filterOption); diff --git a/model/map/src/main/java/org/keycloak/models/map/client/MapClientProvider.java b/model/map/src/main/java/org/keycloak/models/map/client/MapClientProvider.java index 6b2e70d565..34ae170126 100644 --- a/model/map/src/main/java/org/keycloak/models/map/client/MapClientProvider.java +++ b/model/map/src/main/java/org/keycloak/models/map/client/MapClientProvider.java @@ -40,10 +40,10 @@ import java.util.stream.Collectors; import java.util.stream.Stream; import org.keycloak.models.map.storage.MapStorage; -import org.keycloak.models.map.storage.ModelCriteriaBuilder; -import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator; import static org.keycloak.common.util.StackUtil.getShortStackTrace; import org.keycloak.models.ClientScopeModel; +import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator; +import org.keycloak.models.map.storage.criteria.DefaultModelCriteria; import static org.keycloak.models.map.storage.QueryParameters.Order.ASCENDING; import static org.keycloak.models.map.storage.QueryParameters.withCriteria; import static org.keycloak.models.map.storage.criteria.DefaultModelCriteria.criteria; @@ -119,7 +119,7 @@ public class MapClientProvider implements ClientProvider { @Override public Stream getClientsStream(RealmModel realm, Integer firstResult, Integer maxResults) { - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()); return tx.read(withCriteria(mcb).pagination(firstResult, maxResults, SearchableFields.CLIENT_ID)) @@ -128,7 +128,7 @@ public class MapClientProvider implements ClientProvider { @Override public Stream getClientsStream(RealmModel realm) { - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()); return tx.read(withCriteria(mcb).orderBy(SearchableFields.CLIENT_ID, ASCENDING)) @@ -212,7 +212,7 @@ public class MapClientProvider implements ClientProvider { @Override public long getClientsCount(RealmModel realm) { - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()); return tx.getCount(withCriteria(mcb)); @@ -239,7 +239,7 @@ public class MapClientProvider implements ClientProvider { } LOG.tracef("getClientByClientId(%s, %s)%s", realm, clientId, getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.CLIENT_ID, Operator.EQ, clientId); @@ -256,7 +256,7 @@ public class MapClientProvider implements ClientProvider { return Stream.empty(); } - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.CLIENT_ID, Operator.ILIKE, "%" + clientId + "%"); @@ -266,7 +266,7 @@ public class MapClientProvider implements ClientProvider { @Override public Stream searchClientsByAttributes(RealmModel realm, Map attributes, Integer firstResult, Integer maxResults) { - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()); for (Map.Entry entry : attributes.entrySet()) { @@ -331,7 +331,7 @@ public class MapClientProvider implements ClientProvider { @Override public Map> getAllRedirectUrisOfEnabledClients(RealmModel realm) { - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.ENABLED, Operator.EQ, Boolean.TRUE); @@ -346,7 +346,7 @@ public class MapClientProvider implements ClientProvider { } public void preRemove(RealmModel realm, RoleModel role) { - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.SCOPE_MAPPING_ROLE, Operator.EQ, role.getId()); diff --git a/model/map/src/main/java/org/keycloak/models/map/clientscope/MapClientScopeProvider.java b/model/map/src/main/java/org/keycloak/models/map/clientscope/MapClientScopeProvider.java index 9477389c46..799b7c4190 100644 --- a/model/map/src/main/java/org/keycloak/models/map/clientscope/MapClientScopeProvider.java +++ b/model/map/src/main/java/org/keycloak/models/map/clientscope/MapClientScopeProvider.java @@ -32,8 +32,8 @@ import org.keycloak.models.ModelDuplicateException; import org.keycloak.models.RealmModel; import org.keycloak.models.map.storage.MapKeycloakTransaction; import org.keycloak.models.map.storage.MapStorage; -import org.keycloak.models.map.storage.ModelCriteriaBuilder; import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator; +import org.keycloak.models.map.storage.criteria.DefaultModelCriteria; import org.keycloak.models.utils.KeycloakModelUtils; import static org.keycloak.models.map.storage.QueryParameters.Order.ASCENDING; @@ -68,7 +68,7 @@ public class MapClientScopeProvider implements ClientScopeProvider { @Override public Stream getClientScopesStream(RealmModel realm) { - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()); return tx.read(withCriteria(mcb).orderBy(SearchableFields.NAME, ASCENDING)) @@ -78,7 +78,7 @@ public class MapClientScopeProvider implements ClientScopeProvider { @Override public ClientScopeModel addClientScope(RealmModel realm, String id, String name) { // Check Db constraint: @UniqueConstraint(columnNames = {"REALM_ID", "NAME"}) - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.NAME, Operator.EQ, name); diff --git a/model/map/src/main/java/org/keycloak/models/map/group/MapGroupProvider.java b/model/map/src/main/java/org/keycloak/models/map/group/MapGroupProvider.java index eb562d9fbe..ecc5391220 100644 --- a/model/map/src/main/java/org/keycloak/models/map/group/MapGroupProvider.java +++ b/model/map/src/main/java/org/keycloak/models/map/group/MapGroupProvider.java @@ -28,10 +28,10 @@ import org.keycloak.models.RoleModel; import org.keycloak.models.map.storage.MapKeycloakTransaction; import org.keycloak.models.map.storage.MapStorage; -import org.keycloak.models.map.storage.ModelCriteriaBuilder; import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator; import org.keycloak.models.map.storage.QueryParameters; +import org.keycloak.models.map.storage.criteria.DefaultModelCriteria; import java.util.Objects; import java.util.function.Function; import java.util.function.UnaryOperator; @@ -80,9 +80,9 @@ public class MapGroupProvider implements GroupProvider { return getGroupsStreamInternal(realm, null, null); } - private Stream getGroupsStreamInternal(RealmModel realm, UnaryOperator> modifier, UnaryOperator> queryParametersModifier) { + private Stream getGroupsStreamInternal(RealmModel realm, UnaryOperator> modifier, UnaryOperator> queryParametersModifier) { LOG.tracef("getGroupsStream(%s)%s", realm, getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()); if (modifier != null) { @@ -101,7 +101,7 @@ public class MapGroupProvider implements GroupProvider { @Override public Stream getGroupsStream(RealmModel realm, Stream ids, String search, Integer first, Integer max) { - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.ID, Operator.IN, ids) .compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()); @@ -116,7 +116,7 @@ public class MapGroupProvider implements GroupProvider { @Override public Long getGroupsCount(RealmModel realm, Boolean onlyTopGroups) { LOG.tracef("getGroupsCount(%s, %s)%s", realm, onlyTopGroups, getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()); if (Objects.equals(onlyTopGroups, Boolean.TRUE)) { @@ -128,7 +128,7 @@ public class MapGroupProvider implements GroupProvider { @Override public Long getGroupsCountByNameContaining(RealmModel realm, String search) { - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.NAME, Operator.ILIKE, "%" + search + "%"); @@ -139,7 +139,7 @@ public class MapGroupProvider implements GroupProvider { public Stream getGroupsByRoleStream(RealmModel realm, RoleModel role, Integer firstResult, Integer maxResults) { LOG.tracef("getGroupsByRole(%s, %s, %d, %d)%s", realm, role, firstResult, maxResults, getShortStackTrace()); return getGroupsStreamInternal(realm, - (ModelCriteriaBuilder mcb) -> mcb.compare(SearchableFields.ASSIGNED_ROLE, Operator.EQ, role.getId()), + (DefaultModelCriteria mcb) -> mcb.compare(SearchableFields.ASSIGNED_ROLE, Operator.EQ, role.getId()), qp -> qp.offset(firstResult).limit(maxResults) ); } @@ -148,7 +148,7 @@ public class MapGroupProvider implements GroupProvider { public Stream getTopLevelGroupsStream(RealmModel realm) { LOG.tracef("getTopLevelGroupsStream(%s)%s", realm, getShortStackTrace()); return getGroupsStreamInternal(realm, - (ModelCriteriaBuilder mcb) -> mcb.compare(SearchableFields.PARENT_ID, Operator.NOT_EXISTS), + (DefaultModelCriteria mcb) -> mcb.compare(SearchableFields.PARENT_ID, Operator.NOT_EXISTS), null ); } @@ -157,7 +157,7 @@ public class MapGroupProvider implements GroupProvider { public Stream getTopLevelGroupsStream(RealmModel realm, Integer firstResult, Integer maxResults) { LOG.tracef("getTopLevelGroupsStream(%s, %s, %s)%s", realm, firstResult, maxResults, getShortStackTrace()); return getGroupsStreamInternal(realm, - (ModelCriteriaBuilder mcb) -> mcb.compare(SearchableFields.PARENT_ID, Operator.NOT_EXISTS), + (DefaultModelCriteria mcb) -> mcb.compare(SearchableFields.PARENT_ID, Operator.NOT_EXISTS), qp -> qp.offset(firstResult).limit(maxResults) ); } @@ -167,7 +167,7 @@ public class MapGroupProvider implements GroupProvider { LOG.tracef("searchForGroupByNameStream(%s, %s, %d, %d)%s", realm, search, firstResult, maxResults, getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.NAME, Operator.ILIKE, "%" + search + "%"); @@ -188,7 +188,7 @@ public class MapGroupProvider implements GroupProvider { LOG.tracef("createGroup(%s, %s, %s, %s)%s", realm, id, name, toParent, getShortStackTrace()); // Check Db constraint: uniqueConstraints = { @UniqueConstraint(columnNames = {"REALM_ID", "PARENT_GROUP", "NAME"})} String parentId = toParent == null ? null : toParent.getId(); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.PARENT_ID, Operator.EQ, parentId) .compare(SearchableFields.NAME, Operator.EQ, name); @@ -255,7 +255,7 @@ public class MapGroupProvider implements GroupProvider { } String parentId = toParent == null ? null : toParent.getId(); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.PARENT_ID, Operator.EQ, parentId) .compare(SearchableFields.NAME, Operator.EQ, group.getName()); @@ -277,7 +277,7 @@ public class MapGroupProvider implements GroupProvider { public void addTopLevelGroup(RealmModel realm, GroupModel subGroup) { LOG.tracef("addTopLevelGroup(%s, %s)%s", realm, subGroup, getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.PARENT_ID, Operator.EQ, (Object) null) .compare(SearchableFields.NAME, Operator.EQ, subGroup.getName()); @@ -293,7 +293,7 @@ public class MapGroupProvider implements GroupProvider { public void preRemove(RealmModel realm, RoleModel role) { LOG.tracef("preRemove(%s, %s)%s", realm, role, getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.ASSIGNED_ROLE, Operator.EQ, role.getId()); try (Stream toRemove = tx.read(withCriteria(mcb))) { diff --git a/model/map/src/main/java/org/keycloak/models/map/loginFailure/MapUserLoginFailureProvider.java b/model/map/src/main/java/org/keycloak/models/map/loginFailure/MapUserLoginFailureProvider.java index 93b7ab1d76..ced4a2ef12 100644 --- a/model/map/src/main/java/org/keycloak/models/map/loginFailure/MapUserLoginFailureProvider.java +++ b/model/map/src/main/java/org/keycloak/models/map/loginFailure/MapUserLoginFailureProvider.java @@ -23,8 +23,9 @@ import org.keycloak.models.RealmModel; import org.keycloak.models.UserLoginFailureModel; import org.keycloak.models.map.storage.MapKeycloakTransaction; import org.keycloak.models.map.storage.MapStorage; -import org.keycloak.models.map.storage.ModelCriteriaBuilder; +import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator; +import org.keycloak.models.map.storage.criteria.DefaultModelCriteria; import java.util.function.Function; import static org.keycloak.common.util.StackUtil.getShortStackTrace; @@ -54,9 +55,9 @@ public class MapUserLoginFailureProvider implements UserLoginFailureProvider { @Override public UserLoginFailureModel getUserLoginFailure(RealmModel realm, String userId) { - ModelCriteriaBuilder mcb = criteria(); - mcb = mcb.compare(UserLoginFailureModel.SearchableFields.REALM_ID, ModelCriteriaBuilder.Operator.EQ, realm.getId()) - .compare(UserLoginFailureModel.SearchableFields.USER_ID, ModelCriteriaBuilder.Operator.EQ, userId); + DefaultModelCriteria mcb = criteria(); + mcb = mcb.compare(UserLoginFailureModel.SearchableFields.REALM_ID, Operator.EQ, realm.getId()) + .compare(UserLoginFailureModel.SearchableFields.USER_ID, Operator.EQ, userId); LOG.tracef("getUserLoginFailure(%s, %s)%s", realm, userId, getShortStackTrace()); @@ -68,9 +69,9 @@ public class MapUserLoginFailureProvider implements UserLoginFailureProvider { @Override public UserLoginFailureModel addUserLoginFailure(RealmModel realm, String userId) { - ModelCriteriaBuilder mcb = criteria(); - mcb = mcb.compare(UserLoginFailureModel.SearchableFields.REALM_ID, ModelCriteriaBuilder.Operator.EQ, realm.getId()) - .compare(UserLoginFailureModel.SearchableFields.USER_ID, ModelCriteriaBuilder.Operator.EQ, userId); + DefaultModelCriteria mcb = criteria(); + mcb = mcb.compare(UserLoginFailureModel.SearchableFields.REALM_ID, Operator.EQ, realm.getId()) + .compare(UserLoginFailureModel.SearchableFields.USER_ID, Operator.EQ, userId); LOG.tracef("addUserLoginFailure(%s, %s)%s", realm, userId, getShortStackTrace()); @@ -87,9 +88,9 @@ public class MapUserLoginFailureProvider implements UserLoginFailureProvider { @Override public void removeUserLoginFailure(RealmModel realm, String userId) { - ModelCriteriaBuilder mcb = criteria(); - mcb = mcb.compare(UserLoginFailureModel.SearchableFields.REALM_ID, ModelCriteriaBuilder.Operator.EQ, realm.getId()) - .compare(UserLoginFailureModel.SearchableFields.USER_ID, ModelCriteriaBuilder.Operator.EQ, userId); + DefaultModelCriteria mcb = criteria(); + mcb = mcb.compare(UserLoginFailureModel.SearchableFields.REALM_ID, Operator.EQ, realm.getId()) + .compare(UserLoginFailureModel.SearchableFields.USER_ID, Operator.EQ, userId); LOG.tracef("removeUserLoginFailure(%s, %s)%s", realm, userId, getShortStackTrace()); @@ -98,8 +99,8 @@ public class MapUserLoginFailureProvider implements UserLoginFailureProvider { @Override public void removeAllUserLoginFailures(RealmModel realm) { - ModelCriteriaBuilder mcb = criteria(); - mcb = mcb.compare(UserLoginFailureModel.SearchableFields.REALM_ID, ModelCriteriaBuilder.Operator.EQ, realm.getId()); + DefaultModelCriteria mcb = criteria(); + mcb = mcb.compare(UserLoginFailureModel.SearchableFields.REALM_ID, Operator.EQ, realm.getId()); LOG.tracef("removeAllUserLoginFailures(%s)%s", realm, getShortStackTrace()); diff --git a/model/map/src/main/java/org/keycloak/models/map/realm/MapRealmProvider.java b/model/map/src/main/java/org/keycloak/models/map/realm/MapRealmProvider.java index 40e0dc5a1f..fe1b0d8e86 100644 --- a/model/map/src/main/java/org/keycloak/models/map/realm/MapRealmProvider.java +++ b/model/map/src/main/java/org/keycloak/models/map/realm/MapRealmProvider.java @@ -34,8 +34,8 @@ import org.keycloak.models.RealmProvider; import org.keycloak.models.RoleModel; import org.keycloak.models.map.storage.MapKeycloakTransaction; import org.keycloak.models.map.storage.MapStorage; -import org.keycloak.models.map.storage.ModelCriteriaBuilder; import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator; +import org.keycloak.models.map.storage.criteria.DefaultModelCriteria; import org.keycloak.models.utils.KeycloakModelUtils; import static org.keycloak.models.map.storage.QueryParameters.Order.ASCENDING; import static org.keycloak.models.map.storage.QueryParameters.withCriteria; @@ -97,7 +97,7 @@ public class MapRealmProvider implements RealmProvider { LOG.tracef("getRealmByName(%s)%s", name, getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.NAME, Operator.EQ, name); String realmId = tx.read(withCriteria(mcb)) @@ -115,13 +115,13 @@ public class MapRealmProvider implements RealmProvider { @Override public Stream getRealmsWithProviderTypeStream(Class type) { - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.COMPONENT_PROVIDER_TYPE, Operator.EQ, type.getName()); return getRealmsStream(mcb); } - private Stream getRealmsStream(ModelCriteriaBuilder mcb) { + private Stream getRealmsStream(DefaultModelCriteria mcb) { return tx.read(withCriteria(mcb).orderBy(SearchableFields.NAME, ASCENDING)) .map(this::entityToAdapter); } @@ -160,7 +160,7 @@ public class MapRealmProvider implements RealmProvider { @Override public void removeExpiredClientInitialAccess() { - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.CLIENT_INITIAL_ACCESS, Operator.EXISTS); tx.read(withCriteria(mcb)) diff --git a/model/map/src/main/java/org/keycloak/models/map/role/MapRoleProvider.java b/model/map/src/main/java/org/keycloak/models/map/role/MapRoleProvider.java index 551e7361f3..5d3062f30b 100644 --- a/model/map/src/main/java/org/keycloak/models/map/role/MapRoleProvider.java +++ b/model/map/src/main/java/org/keycloak/models/map/role/MapRoleProvider.java @@ -38,8 +38,8 @@ import static org.keycloak.models.map.storage.criteria.DefaultModelCriteria.crit import org.keycloak.models.RoleContainerModel; import org.keycloak.models.RoleModel.SearchableFields; import org.keycloak.models.RoleProvider; -import org.keycloak.models.map.storage.ModelCriteriaBuilder; import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator; +import org.keycloak.models.map.storage.criteria.DefaultModelCriteria; public class MapRoleProvider implements RoleProvider { @@ -78,7 +78,7 @@ public class MapRoleProvider implements RoleProvider { @Override public Stream getRealmRolesStream(RealmModel realm, Integer first, Integer max) { - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.IS_CLIENT_ROLE, Operator.NE, true); @@ -91,7 +91,7 @@ public class MapRoleProvider implements RoleProvider { LOG.tracef("getRolesStream(%s, %s, %s, %d, %d)%s", realm, ids, search, first, max, getShortStackTrace()); if (ids == null) return Stream.empty(); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(RoleModel.SearchableFields.ID, Operator.IN, ids) .compare(RoleModel.SearchableFields.REALM_ID, Operator.EQ, realm.getId()); @@ -105,7 +105,7 @@ public class MapRoleProvider implements RoleProvider { @Override public Stream getRealmRolesStream(RealmModel realm) { - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.IS_CLIENT_ROLE, Operator.NE, true); @@ -134,7 +134,7 @@ public class MapRoleProvider implements RoleProvider { @Override public Stream getClientRolesStream(ClientModel client, Integer first, Integer max) { - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, client.getRealm().getId()) .compare(SearchableFields.CLIENT_ID, Operator.EQ, client.getId()); @@ -144,7 +144,7 @@ public class MapRoleProvider implements RoleProvider { @Override public Stream getClientRolesStream(ClientModel client) { - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, client.getRealm().getId()) .compare(SearchableFields.CLIENT_ID, Operator.EQ, client.getId()); @@ -195,7 +195,7 @@ public class MapRoleProvider implements RoleProvider { } LOG.tracef("getRealmRole(%s, %s)%s", realm, name, getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.NAME, Operator.ILIKE, name); @@ -215,7 +215,7 @@ public class MapRoleProvider implements RoleProvider { } LOG.tracef("getClientRole(%s, %s)%s", client, name, getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, client.getRealm().getId()) .compare(SearchableFields.CLIENT_ID, Operator.EQ, client.getId()) .compare(SearchableFields.NAME, Operator.ILIKE, name); @@ -249,7 +249,7 @@ public class MapRoleProvider implements RoleProvider { if (search == null) { return Stream.empty(); } - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .or( mcb.compare(SearchableFields.NAME, Operator.ILIKE, "%" + search + "%"), @@ -265,7 +265,7 @@ public class MapRoleProvider implements RoleProvider { if (search == null) { return Stream.empty(); } - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, client.getRealm().getId()) .compare(SearchableFields.CLIENT_ID, Operator.EQ, client.getId()) .or( diff --git a/model/map/src/main/java/org/keycloak/models/map/storage/ModelCriteriaBuilder.java b/model/map/src/main/java/org/keycloak/models/map/storage/ModelCriteriaBuilder.java index 60d1da6a6c..941ab196be 100644 --- a/model/map/src/main/java/org/keycloak/models/map/storage/ModelCriteriaBuilder.java +++ b/model/map/src/main/java/org/keycloak/models/map/storage/ModelCriteriaBuilder.java @@ -51,7 +51,7 @@ import org.keycloak.storage.SearchableModelField; * * @author hmlnarik */ -public interface ModelCriteriaBuilder { +public interface ModelCriteriaBuilder> { /** * The operators are very basic ones for this use case. In the real scenario, @@ -117,7 +117,7 @@ public interface ModelCriteriaBuilder { * @return * @throws CriterionNotSupported If the operator is not supported for the given field. */ - ModelCriteriaBuilder compare(SearchableModelField modelField, Operator op, Object... value); + Self compare(SearchableModelField modelField, Operator op, Object... value); /** * Creates and returns a new instance of {@code ModelCriteriaBuilder} that @@ -136,7 +136,7 @@ public interface ModelCriteriaBuilder { * * @throws CriterionNotSupported If the operator is not supported for the given field. */ - ModelCriteriaBuilder and(ModelCriteriaBuilder... builders); + Self and(Self... builders); /** * Creates and returns a new instance of {@code ModelCriteriaBuilder} that @@ -155,7 +155,7 @@ public interface ModelCriteriaBuilder { * * @throws CriterionNotSupported If the operator is not supported for the given field. */ - ModelCriteriaBuilder or(ModelCriteriaBuilder... builders); + Self or(Self... builders); /** * Creates and returns a new instance of {@code ModelCriteriaBuilder} that @@ -168,6 +168,6 @@ public interface ModelCriteriaBuilder { * @return * @throws CriterionNotSupported If the operator is not supported for the given field. */ - ModelCriteriaBuilder not(ModelCriteriaBuilder builder); + Self not(Self builder); } diff --git a/model/map/src/main/java/org/keycloak/models/map/storage/QueryParameters.java b/model/map/src/main/java/org/keycloak/models/map/storage/QueryParameters.java index e52b3b49b3..c422685920 100644 --- a/model/map/src/main/java/org/keycloak/models/map/storage/QueryParameters.java +++ b/model/map/src/main/java/org/keycloak/models/map/storage/QueryParameters.java @@ -1,5 +1,6 @@ package org.keycloak.models.map.storage; +import org.keycloak.models.map.storage.criteria.DefaultModelCriteria; import org.keycloak.storage.SearchableModelField; import java.util.LinkedList; @@ -19,12 +20,12 @@ public class QueryParameters { private Integer offset; private Integer limit; private final List> orderBy = new LinkedList<>(); - private ModelCriteriaBuilder mcb; + private DefaultModelCriteria mcb; public QueryParameters() { } - public QueryParameters(ModelCriteriaBuilder mcb) { + public QueryParameters(DefaultModelCriteria mcb) { this.mcb = mcb; } @@ -35,7 +36,7 @@ public class QueryParameters { * @param model type * @return a new {@code QueryParameters} instance */ - public static QueryParameters withCriteria(ModelCriteriaBuilder mcb) { + public static QueryParameters withCriteria(DefaultModelCriteria mcb) { return new QueryParameters<>(mcb); } @@ -98,7 +99,7 @@ public class QueryParameters { return limit; } - public ModelCriteriaBuilder getModelCriteriaBuilder() { + public DefaultModelCriteria getModelCriteriaBuilder() { return mcb; } diff --git a/model/map/src/main/java/org/keycloak/models/map/storage/chm/ConcurrentHashMapKeycloakTransaction.java b/model/map/src/main/java/org/keycloak/models/map/storage/chm/ConcurrentHashMapKeycloakTransaction.java index a530eadbfa..7913805a1a 100644 --- a/model/map/src/main/java/org/keycloak/models/map/storage/chm/ConcurrentHashMapKeycloakTransaction.java +++ b/model/map/src/main/java/org/keycloak/models/map/storage/chm/ConcurrentHashMapKeycloakTransaction.java @@ -167,8 +167,8 @@ public class ConcurrentHashMapKeycloakTransaction read(QueryParameters queryParameters) { - DefaultModelCriteria mcb = (DefaultModelCriteria) queryParameters.getModelCriteriaBuilder(); - MapModelCriteriaBuilder mapMcb = (MapModelCriteriaBuilder) mcb.flashToModelCriteriaBuilder(map.createCriteriaBuilder()); + DefaultModelCriteria mcb = queryParameters.getModelCriteriaBuilder(); + MapModelCriteriaBuilder mapMcb = mcb.flashToModelCriteriaBuilder(map.createCriteriaBuilder()); Predicate filterOutAllBulkDeletedObjects = tasks.values().stream() .filter(BulkDeleteOperation.class::isInstance) @@ -400,8 +400,8 @@ public class ConcurrentHashMapKeycloakTransaction getFilterForNonDeletedObjects() { - DefaultModelCriteria mcb = (DefaultModelCriteria) queryParameters.getModelCriteriaBuilder(); - MapModelCriteriaBuilder mmcb = (MapModelCriteriaBuilder) mcb.flashToModelCriteriaBuilder(map.createCriteriaBuilder()); + DefaultModelCriteria mcb = queryParameters.getModelCriteriaBuilder(); + MapModelCriteriaBuilder mmcb = mcb.flashToModelCriteriaBuilder(map.createCriteriaBuilder()); Predicate entityFilter = mmcb.getEntityFilter(); Predicate keyFilter = mmcb.getKeyFilter(); diff --git a/model/map/src/main/java/org/keycloak/models/map/storage/chm/ConcurrentHashMapStorage.java b/model/map/src/main/java/org/keycloak/models/map/storage/chm/ConcurrentHashMapStorage.java index b05abe1e50..93e0097874 100644 --- a/model/map/src/main/java/org/keycloak/models/map/storage/chm/ConcurrentHashMapStorage.java +++ b/model/map/src/main/java/org/keycloak/models/map/storage/chm/ConcurrentHashMapStorage.java @@ -23,7 +23,6 @@ import org.keycloak.models.map.common.AbstractEntity; import org.keycloak.models.map.common.DeepCloner; import org.keycloak.models.map.common.UpdatableEntity; import org.keycloak.models.map.storage.MapStorage; -import org.keycloak.models.map.storage.ModelCriteriaBuilder; import org.keycloak.models.map.storage.QueryParameters; import org.keycloak.models.map.storage.criteria.DefaultModelCriteria; import org.keycloak.storage.SearchableModelField; @@ -126,7 +125,7 @@ public class ConcurrentHashMapStorage queryParameters) { - DefaultModelCriteria criteria = (DefaultModelCriteria) queryParameters.getModelCriteriaBuilder(); + DefaultModelCriteria criteria = queryParameters.getModelCriteriaBuilder(); if (criteria == null) { long res = store.size(); @@ -135,7 +134,7 @@ public class ConcurrentHashMapStorage mcb = (MapModelCriteriaBuilder) criteria.flashToModelCriteriaBuilder(createCriteriaBuilder()); + MapModelCriteriaBuilder mcb = criteria.flashToModelCriteriaBuilder(createCriteriaBuilder()); Predicate keyFilter = mcb.getKeyFilter(); Predicate entityFilter = mcb.getEntityFilter(); Stream> storeStream = store.entrySet().stream(); @@ -162,7 +161,7 @@ public class ConcurrentHashMapStorage(this, keyConvertor, cloner) : sessionTransaction; } - public ModelCriteriaBuilder createCriteriaBuilder() { + public MapModelCriteriaBuilder createCriteriaBuilder() { return new MapModelCriteriaBuilder<>(keyConvertor, fieldPredicates); } @@ -178,13 +177,13 @@ public class ConcurrentHashMapStorage read(QueryParameters queryParameters) { - DefaultModelCriteria criteria = (DefaultModelCriteria) queryParameters.getModelCriteriaBuilder(); + DefaultModelCriteria criteria = queryParameters.getModelCriteriaBuilder(); if (criteria == null) { return Stream.empty(); } - MapModelCriteriaBuilder mcb = (MapModelCriteriaBuilder) criteria.flashToModelCriteriaBuilder(createCriteriaBuilder()); + MapModelCriteriaBuilder mcb = criteria.flashToModelCriteriaBuilder(createCriteriaBuilder()); Stream> stream = store.entrySet().stream(); Predicate keyFilter = mcb.getKeyFilter(); diff --git a/model/map/src/main/java/org/keycloak/models/map/storage/chm/ConcurrentHashMapStorageProviderFactory.java b/model/map/src/main/java/org/keycloak/models/map/storage/chm/ConcurrentHashMapStorageProviderFactory.java index 3a187fb219..d0ece83114 100644 --- a/model/map/src/main/java/org/keycloak/models/map/storage/chm/ConcurrentHashMapStorageProviderFactory.java +++ b/model/map/src/main/java/org/keycloak/models/map/storage/chm/ConcurrentHashMapStorageProviderFactory.java @@ -65,12 +65,12 @@ import java.util.concurrent.ConcurrentHashMap; import org.jboss.logging.Logger; import org.keycloak.models.map.storage.MapStorageProvider; import org.keycloak.models.map.storage.MapStorageProviderFactory; -import org.keycloak.models.map.storage.ModelCriteriaBuilder; import org.keycloak.models.map.userSession.MapAuthenticatedClientSessionEntity; import org.keycloak.models.map.user.MapUserEntity; import org.keycloak.models.map.userSession.MapUserSessionEntity; import org.keycloak.provider.EnvironmentDependentProviderFactory; import org.keycloak.provider.ProviderConfigProperty; +import org.keycloak.models.map.storage.criteria.DefaultModelCriteria; import org.keycloak.sessions.RootAuthenticationSessionModel; import java.util.Collections; import java.util.HashMap; @@ -241,7 +241,7 @@ public class ConcurrentHashMapStorageProviderFactory implements AmphibianProvide try { if (storageDirectory != null) { LOG.debugf("Storing contents to %s", f.getCanonicalPath()); - final ModelCriteriaBuilder readAllCriteria = criteria(); + final DefaultModelCriteria readAllCriteria = criteria(); Serialization.MAPPER.writeValue(f, store.read(withCriteria(readAllCriteria))); } else { LOG.debugf("Not storing contents of %s because directory not set", mapName); diff --git a/model/map/src/main/java/org/keycloak/models/map/storage/chm/MapModelCriteriaBuilder.java b/model/map/src/main/java/org/keycloak/models/map/storage/chm/MapModelCriteriaBuilder.java index 21f27cfe4f..ca7d047aee 100644 --- a/model/map/src/main/java/org/keycloak/models/map/storage/chm/MapModelCriteriaBuilder.java +++ b/model/map/src/main/java/org/keycloak/models/map/storage/chm/MapModelCriteriaBuilder.java @@ -32,7 +32,7 @@ import java.util.stream.Collectors; * * @author hmlnarik */ -public class MapModelCriteriaBuilder implements ModelCriteriaBuilder { +public class MapModelCriteriaBuilder implements ModelCriteriaBuilder> { @FunctionalInterface public static interface UpdatePredicatesFunc { @@ -70,7 +70,7 @@ public class MapModelCriteriaBuilder implements @SafeVarargs @SuppressWarnings("unchecked") @Override - public final MapModelCriteriaBuilder and(ModelCriteriaBuilder... builders) { + public final MapModelCriteriaBuilder and(MapModelCriteriaBuilder... builders) { Predicate resIndexFilter = Stream.of(builders).map(MapModelCriteriaBuilder.class::cast).map(MapModelCriteriaBuilder::getKeyFilter).reduce(keyFilter, Predicate::and); Predicate resEntityFilter = Stream.of(builders).map(MapModelCriteriaBuilder.class::cast).map(MapModelCriteriaBuilder::getEntityFilter).reduce(entityFilter, Predicate::and); return new MapModelCriteriaBuilder<>(keyConvertor, fieldPredicates, resIndexFilter, resEntityFilter); @@ -79,7 +79,7 @@ public class MapModelCriteriaBuilder implements @SafeVarargs @SuppressWarnings("unchecked") @Override - public final MapModelCriteriaBuilder or(ModelCriteriaBuilder... builders) { + public final MapModelCriteriaBuilder or(MapModelCriteriaBuilder... builders) { Predicate resIndexFilter = Stream.of(builders).map(MapModelCriteriaBuilder.class::cast).map(MapModelCriteriaBuilder::getKeyFilter).reduce(ALWAYS_FALSE, Predicate::or); Predicate resEntityFilter = Stream.of(builders).map(MapModelCriteriaBuilder.class::cast).map(MapModelCriteriaBuilder::getEntityFilter).reduce(ALWAYS_FALSE, Predicate::or); return new MapModelCriteriaBuilder<>( @@ -92,10 +92,10 @@ public class MapModelCriteriaBuilder implements @SuppressWarnings("unchecked") @Override - public MapModelCriteriaBuilder not(ModelCriteriaBuilder builder) { + public MapModelCriteriaBuilder not(MapModelCriteriaBuilder builder) { MapModelCriteriaBuilder b = (MapModelCriteriaBuilder) builder; - Predicate resIndexFilter = b.getKeyFilter() == ALWAYS_TRUE ? ALWAYS_TRUE : b.getKeyFilter().negate(); - Predicate resEntityFilter = b.getEntityFilter() == ALWAYS_TRUE ? ALWAYS_TRUE : b.getEntityFilter().negate(); + Predicate resIndexFilter = builder.getKeyFilter() == ALWAYS_TRUE ? ALWAYS_TRUE : builder.getKeyFilter().negate(); + Predicate resEntityFilter = builder.getEntityFilter() == ALWAYS_TRUE ? ALWAYS_TRUE : builder.getEntityFilter().negate(); return new MapModelCriteriaBuilder<>( keyConvertor, diff --git a/model/map/src/main/java/org/keycloak/models/map/storage/chm/UserSessionConcurrentHashMapStorage.java b/model/map/src/main/java/org/keycloak/models/map/storage/chm/UserSessionConcurrentHashMapStorage.java index ff8adb701d..0ebf1a07b6 100644 --- a/model/map/src/main/java/org/keycloak/models/map/storage/chm/UserSessionConcurrentHashMapStorage.java +++ b/model/map/src/main/java/org/keycloak/models/map/storage/chm/UserSessionConcurrentHashMapStorage.java @@ -23,7 +23,6 @@ import org.keycloak.models.UserSessionModel; import org.keycloak.models.map.common.AbstractEntity; import org.keycloak.models.map.common.DeepCloner; import org.keycloak.models.map.storage.MapKeycloakTransaction; -import org.keycloak.models.map.storage.ModelCriteriaBuilder; import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator; import org.keycloak.models.map.storage.QueryParameters; import org.keycloak.models.map.storage.criteria.DefaultModelCriteria; @@ -56,7 +55,7 @@ public class UserSessionConcurrentHashMapStorage extends ConcurrentHashMapSto @Override public long delete(QueryParameters queryParameters) { Set ids = read(queryParameters).map(AbstractEntity::getId).collect(Collectors.toSet()); - ModelCriteriaBuilder csMcb = DefaultModelCriteria.criteria() + DefaultModelCriteria csMcb = DefaultModelCriteria.criteria() .compare(AuthenticatedClientSessionModel.SearchableFields.USER_SESSION_ID, Operator.IN, ids); clientSessionTr.delete(withCriteria(csMcb)); return super.delete(queryParameters); @@ -64,7 +63,7 @@ public class UserSessionConcurrentHashMapStorage extends ConcurrentHashMapSto @Override public boolean delete(String key) { - ModelCriteriaBuilder csMcb = DefaultModelCriteria.criteria() + DefaultModelCriteria csMcb = DefaultModelCriteria.criteria() .compare(AuthenticatedClientSessionModel.SearchableFields.USER_SESSION_ID, Operator.EQ, key); clientSessionTr.delete(withCriteria(csMcb)); return super.delete(key); diff --git a/model/map/src/main/java/org/keycloak/models/map/storage/criteria/DefaultModelCriteria.java b/model/map/src/main/java/org/keycloak/models/map/storage/criteria/DefaultModelCriteria.java index 63811d58bb..eaad91475c 100644 --- a/model/map/src/main/java/org/keycloak/models/map/storage/criteria/DefaultModelCriteria.java +++ b/model/map/src/main/java/org/keycloak/models/map/storage/criteria/DefaultModelCriteria.java @@ -26,7 +26,7 @@ import java.util.stream.Stream; * Descriptive model criteria implementation which in other words represents a Boolean formula on searchable fields. * @author hmlnarik */ -public class DefaultModelCriteria implements ModelCriteriaBuilder { +public class DefaultModelCriteria implements ModelCriteriaBuilder> { private static final DefaultModelCriteria INSTANCE = new DefaultModelCriteria<>(null); @@ -63,18 +63,15 @@ public class DefaultModelCriteria implements ModelCriteriaBuilder { } @Override - public DefaultModelCriteria and(ModelCriteriaBuilder... mcbs) { + public DefaultModelCriteria and(DefaultModelCriteria... mcbs) { if (mcbs.length == 1) { - ModelCriteriaNode toBeChild = ((DefaultModelCriteria) mcbs[0]).node; - if (toBeChild.getNodeOperator() == ExtOperator.AND || toBeChild.getNodeOperator() == ExtOperator.OR) { - return (DefaultModelCriteria) mcbs[0]; - } + return compare(mcbs[0].node); } final ModelCriteriaNode targetNode = new ModelCriteriaNode<>(ExtOperator.AND); AtomicBoolean hasFalseNode = new AtomicBoolean(false); - for (ModelCriteriaBuilder mcb : mcbs) { - final ModelCriteriaNode nodeToAdd = ((DefaultModelCriteria) mcb).node; + for (DefaultModelCriteria mcb : mcbs) { + final ModelCriteriaNode nodeToAdd = mcb.node; getNodesToAddForAndOr(nodeToAdd, ExtOperator.AND) .filter(ModelCriteriaNode::isNotTrueNode) .peek(n -> { if (n.isFalseNode()) hasFalseNode.lazySet(true); }) @@ -95,18 +92,15 @@ public class DefaultModelCriteria implements ModelCriteriaBuilder { } @Override - public DefaultModelCriteria or(ModelCriteriaBuilder... mcbs) { + public DefaultModelCriteria or(DefaultModelCriteria... mcbs) { if (mcbs.length == 1) { - ModelCriteriaNode toBeChild = ((DefaultModelCriteria) mcbs[0]).node; - if (toBeChild.getNodeOperator() == ExtOperator.AND || toBeChild.getNodeOperator() == ExtOperator.OR) { - return ((DefaultModelCriteria) mcbs[0]); - } + return compare(mcbs[0].node); } final ModelCriteriaNode targetNode = new ModelCriteriaNode<>(ExtOperator.OR); AtomicBoolean hasTrueNode = new AtomicBoolean(false); - for (ModelCriteriaBuilder mcb : mcbs) { - final ModelCriteriaNode nodeToAdd = ((DefaultModelCriteria) mcb).node; + for (DefaultModelCriteria mcb : mcbs) { + final ModelCriteriaNode nodeToAdd = mcb.node; getNodesToAddForAndOr(nodeToAdd, ExtOperator.OR) .filter(ModelCriteriaNode::isNotFalseNode) .peek(n -> { if (n.isTrueNode()) hasTrueNode.lazySet(true); }) @@ -127,12 +121,13 @@ public class DefaultModelCriteria implements ModelCriteriaBuilder { } @Override - public DefaultModelCriteria not(ModelCriteriaBuilder mcb) { - final ModelCriteriaNode targetNode = new ModelCriteriaNode<>(ExtOperator.NOT); - ModelCriteriaNode toBeChild = ((DefaultModelCriteria) mcb).node; + public DefaultModelCriteria not(DefaultModelCriteria mcb) { + ModelCriteriaNode toBeChild = mcb.node; if (toBeChild.getNodeOperator() == ExtOperator.NOT) { return compare(toBeChild.getChildren().get(0).cloneTree()); } + + final ModelCriteriaNode targetNode = new ModelCriteriaNode<>(ExtOperator.NOT); targetNode.addChild(toBeChild.cloneTree()); return compare(targetNode); } @@ -143,7 +138,7 @@ public class DefaultModelCriteria implements ModelCriteriaBuilder { * @param mcb {@code ModelCriteriaBuilder} to copy the contents onto * @return Updated {@code ModelCriteriaBuilder} */ - public > C flashToModelCriteriaBuilder(C mcb) { + public > C flashToModelCriteriaBuilder(C mcb) { if (isEmpty()) { return mcb; } diff --git a/model/map/src/main/java/org/keycloak/models/map/storage/criteria/ModelCriteriaNode.java b/model/map/src/main/java/org/keycloak/models/map/storage/criteria/ModelCriteriaNode.java index fe8470b996..052ac785fe 100644 --- a/model/map/src/main/java/org/keycloak/models/map/storage/criteria/ModelCriteriaNode.java +++ b/model/map/src/main/java/org/keycloak/models/map/storage/criteria/ModelCriteriaNode.java @@ -20,6 +20,7 @@ import org.keycloak.models.map.storage.ModelCriteriaBuilder; import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator; import org.keycloak.models.map.storage.tree.DefaultTreeNode; import org.keycloak.storage.SearchableModelField; +import java.lang.reflect.Array; import java.util.Arrays; import java.util.Collections; import java.util.Objects; @@ -35,45 +36,45 @@ public class ModelCriteriaNode extends DefaultTreeNode> public static enum ExtOperator { AND { - @Override public > C apply(C mcb, ModelCriteriaNode node) { + @Override public > C apply(C mcb, ModelCriteriaNode node) { if (node.getChildren().isEmpty()) { return null; } - final ModelCriteriaBuilder[] operands = node.getChildren().stream() + final C[] operands = node.getChildren().stream() .map(n -> n.flashToModelCriteriaBuilder(mcb)) .filter(Objects::nonNull) - .toArray(ModelCriteriaBuilder[]::new); - return operands.length == 0 ? null : (C) mcb.and(operands); + .toArray(n -> (C[]) Array.newInstance(mcb.getClass(), n)); + return operands.length == 0 ? null : mcb.and(operands); } @Override public String toString(ModelCriteriaNode node) { return "(" + node.getChildren().stream().map(ModelCriteriaNode::toString).collect(Collectors.joining(" && ")) + ")"; } }, OR { - @Override public > C apply(C mcb, ModelCriteriaNode node) { + @Override public > C apply(C mcb, ModelCriteriaNode node) { if (node.getChildren().isEmpty()) { return null; } - final ModelCriteriaBuilder[] operands = node.getChildren().stream() + final C[] operands = node.getChildren().stream() .map(n -> n.flashToModelCriteriaBuilder(mcb)) .filter(Objects::nonNull) - .toArray(ModelCriteriaBuilder[]::new); - return operands.length == 0 ? null : (C) mcb.or(operands); + .toArray(n -> (C[]) Array.newInstance(mcb.getClass(), n)); + return operands.length == 0 ? null : mcb.or(operands); } @Override public String toString(ModelCriteriaNode node) { return "(" + node.getChildren().stream().map(ModelCriteriaNode::toString).collect(Collectors.joining(" || ")) + ")"; } }, NOT { - @Override public > C apply(C mcb, ModelCriteriaNode node) { - return (C) mcb.not(node.getChildren().iterator().next().flashToModelCriteriaBuilder(mcb)); + @Override public > C apply(C mcb, ModelCriteriaNode node) { + return mcb.not(node.getChildren().iterator().next().flashToModelCriteriaBuilder(mcb)); } @Override public String toString(ModelCriteriaNode node) { return "! " + node.getChildren().iterator().next().toString(); } }, ATOMIC_FORMULA { - @Override public > C apply(C mcb, ModelCriteriaNode node) { + @Override public > C apply(C mcb, ModelCriteriaNode node) { return (C) mcb.compare( node.field, node.simpleOperator, @@ -85,16 +86,16 @@ public class ModelCriteriaNode extends DefaultTreeNode> } }, __FALSE__ { - @Override public > C apply(C mcb, ModelCriteriaNode node) { - return (C) mcb.or(); + @Override public > C apply(C mcb, ModelCriteriaNode node) { + return mcb.or((C[]) Array.newInstance(mcb.getClass(), 0)); } @Override public String toString(ModelCriteriaNode node) { return "__FALSE__"; } }, __TRUE__ { - @Override public > C apply(C mcb, ModelCriteriaNode node) { - return (C) mcb.and(); + @Override public > C apply(C mcb, ModelCriteriaNode node) { + return mcb.and((C[]) Array.newInstance(mcb.getClass(), 0)); } @Override public String toString(ModelCriteriaNode node) { return "__TRUE__"; @@ -102,7 +103,7 @@ public class ModelCriteriaNode extends DefaultTreeNode> } ; - public abstract > C apply(C mcbCreator, ModelCriteriaNode node); + public abstract > C apply(C mcbCreator, ModelCriteriaNode node); public abstract String toString(ModelCriteriaNode node); } @@ -186,7 +187,7 @@ public class ModelCriteriaNode extends DefaultTreeNode> return getNodeOperator() != ExtOperator.__TRUE__; } - public > C flashToModelCriteriaBuilder(C mcb) { + public > C flashToModelCriteriaBuilder(C mcb) { final C res = nodeOperator.apply(mcb, this); return res == null ? mcb : res; } diff --git a/model/map/src/main/java/org/keycloak/models/map/user/MapUserProvider.java b/model/map/src/main/java/org/keycloak/models/map/user/MapUserProvider.java index 36447e20e5..df333b8daa 100644 --- a/model/map/src/main/java/org/keycloak/models/map/user/MapUserProvider.java +++ b/model/map/src/main/java/org/keycloak/models/map/user/MapUserProvider.java @@ -43,8 +43,8 @@ import org.keycloak.models.UserModel.SearchableFields; import org.keycloak.models.UserProvider; import org.keycloak.models.map.storage.MapKeycloakTransaction; import org.keycloak.models.map.storage.MapStorage; -import org.keycloak.models.map.storage.ModelCriteriaBuilder; import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator; +import org.keycloak.models.map.storage.criteria.DefaultModelCriteria; import org.keycloak.storage.StorageId; import org.keycloak.storage.UserStorageProvider; import org.keycloak.storage.client.ClientStorageProvider; @@ -154,7 +154,7 @@ public class MapUserProvider implements UserProvider.Streams, UserCredentialStor public void preRemove(RealmModel realm, IdentityProviderModel provider) { String socialProvider = provider.getAlias(); LOG.tracef("preRemove[RealmModel realm, IdentityProviderModel provider](%s, %s)%s", realm, socialProvider, getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.IDP_AND_USER, Operator.EQ, socialProvider); @@ -189,7 +189,7 @@ public class MapUserProvider implements UserProvider.Streams, UserCredentialStor @Override public UserModel getUserByFederatedIdentity(RealmModel realm, FederatedIdentityModel socialLink) { LOG.tracef("getUserByFederatedIdentity(%s, %s)%s", realm, socialLink, getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.IDP_AND_USER, Operator.EQ, socialLink.getIdentityProvider(), socialLink.getUserId()); @@ -278,7 +278,7 @@ public class MapUserProvider implements UserProvider.Streams, UserCredentialStor @Override public UserModel getServiceAccount(ClientModel client) { LOG.tracef("getServiceAccount(%s)%s", client.getId(), getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, client.getRealm().getId()) .compare(SearchableFields.SERVICE_ACCOUNT_CLIENT, Operator.EQ, client.getId()); @@ -301,7 +301,7 @@ public class MapUserProvider implements UserProvider.Streams, UserCredentialStor @Override public UserModel addUser(RealmModel realm, String id, String username, boolean addDefaultRoles, boolean addDefaultRequiredActions) { LOG.tracef("addUser(%s, %s, %s, %s, %s)%s", realm, id, username, addDefaultRoles, addDefaultRequiredActions, getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.USERNAME, Operator.EQ, username); @@ -341,7 +341,7 @@ public class MapUserProvider implements UserProvider.Streams, UserCredentialStor @Override public void preRemove(RealmModel realm) { LOG.tracef("preRemove[RealmModel](%s)%s", realm, getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()); tx.delete(withCriteria(mcb)); @@ -350,7 +350,7 @@ public class MapUserProvider implements UserProvider.Streams, UserCredentialStor @Override public void removeImportedUsers(RealmModel realm, String storageProviderId) { LOG.tracef("removeImportedUsers(%s, %s)%s", realm, storageProviderId, getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.FEDERATION_LINK, Operator.EQ, storageProviderId); @@ -360,7 +360,7 @@ public class MapUserProvider implements UserProvider.Streams, UserCredentialStor @Override public void unlinkUsers(RealmModel realm, String storageProviderId) { LOG.tracef("unlinkUsers(%s, %s)%s", realm, storageProviderId, getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.FEDERATION_LINK, Operator.EQ, storageProviderId); @@ -373,7 +373,7 @@ public class MapUserProvider implements UserProvider.Streams, UserCredentialStor public void preRemove(RealmModel realm, RoleModel role) { String roleId = role.getId(); LOG.tracef("preRemove[RoleModel](%s, %s)%s", realm, roleId, getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.ASSIGNED_ROLE, Operator.EQ, roleId); @@ -386,7 +386,7 @@ public class MapUserProvider implements UserProvider.Streams, UserCredentialStor public void preRemove(RealmModel realm, GroupModel group) { String groupId = group.getId(); LOG.tracef("preRemove[GroupModel](%s, %s)%s", realm, groupId, getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.ASSIGNED_GROUP, Operator.EQ, groupId); @@ -399,7 +399,7 @@ public class MapUserProvider implements UserProvider.Streams, UserCredentialStor public void preRemove(RealmModel realm, ClientModel client) { String clientId = client.getId(); LOG.tracef("preRemove[ClientModel](%s, %s)%s", realm, clientId, getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.CONSENT_FOR_CLIENT, Operator.EQ, clientId); @@ -418,7 +418,7 @@ public class MapUserProvider implements UserProvider.Streams, UserCredentialStor String clientScopeId = clientScope.getId(); LOG.tracef("preRemove[ClientScopeModel](%s)%s", clientScopeId, getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, clientScope.getRealm().getId()) .compare(SearchableFields.CONSENT_WITH_CLIENT_SCOPE, Operator.EQ, clientScopeId); @@ -436,7 +436,7 @@ public class MapUserProvider implements UserProvider.Streams, UserCredentialStor removeImportedUsers(realm, componentId); } if (component.getProviderType().equals(ClientStorageProvider.class.getName())) { - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.CONSENT_CLIENT_FEDERATION_LINK, Operator.EQ, componentId); @@ -464,7 +464,7 @@ public class MapUserProvider implements UserProvider.Streams, UserCredentialStor public void grantToAllUsers(RealmModel realm, RoleModel role) { String roleId = role.getId(); LOG.tracef("grantToAllUsers(%s, %s)%s", realm, roleId, getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()); try (Stream s = tx.read(withCriteria(mcb))) { @@ -482,7 +482,7 @@ public class MapUserProvider implements UserProvider.Streams, UserCredentialStor public UserModel getUserByUsername(RealmModel realm, String username) { if (username == null) return null; LOG.tracef("getUserByUsername(%s, %s)%s", realm, username, getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.USERNAME, Operator.ILIKE, username); @@ -495,7 +495,7 @@ public class MapUserProvider implements UserProvider.Streams, UserCredentialStor @Override public UserModel getUserByEmail(RealmModel realm, String email) { LOG.tracef("getUserByEmail(%s, %s)%s", realm, email, getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.EMAIL, Operator.EQ, email); @@ -526,7 +526,7 @@ public class MapUserProvider implements UserProvider.Streams, UserCredentialStor @Override public int getUsersCount(RealmModel realm, boolean includeServiceAccount) { LOG.tracef("getUsersCount(%s, %s)%s", realm, includeServiceAccount, getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()); if (! includeServiceAccount) { @@ -539,7 +539,7 @@ public class MapUserProvider implements UserProvider.Streams, UserCredentialStor @Override public Stream getUsersStream(RealmModel realm, Integer firstResult, Integer maxResults, boolean includeServiceAccounts) { LOG.tracef("getUsersStream(%s, %d, %d, %s)%s", realm, firstResult, maxResults, includeServiceAccounts, getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()); if (! includeServiceAccounts) { @@ -569,7 +569,7 @@ public class MapUserProvider implements UserProvider.Streams, UserCredentialStor public Stream searchForUserStream(RealmModel realm, Map attributes, Integer firstResult, Integer maxResults) { LOG.tracef("searchForUserStream(%s, %s, %d, %d)%s", realm, attributes, firstResult, maxResults, getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()); if (! session.getAttributeOrDefault(UserModel.INCLUDE_SERVICE_ACCOUNT, true)) { @@ -675,7 +675,7 @@ public class MapUserProvider implements UserProvider.Streams, UserCredentialStor @Override public Stream getGroupMembersStream(RealmModel realm, GroupModel group, Integer firstResult, Integer maxResults) { LOG.tracef("getGroupMembersStream(%s, %s, %d, %d)%s", realm, group.getId(), firstResult, maxResults, getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.ASSIGNED_GROUP, Operator.EQ, group.getId()); @@ -686,7 +686,7 @@ public class MapUserProvider implements UserProvider.Streams, UserCredentialStor @Override public Stream searchForUserByUserAttributeStream(RealmModel realm, String attrName, String attrValue) { LOG.tracef("searchForUserByUserAttributeStream(%s, %s, %s)%s", realm, attrName, attrValue, getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.ATTRIBUTE, Operator.EQ, attrName, attrValue); @@ -714,7 +714,7 @@ public class MapUserProvider implements UserProvider.Streams, UserCredentialStor @Override public Stream getRoleMembersStream(RealmModel realm, RoleModel role, Integer firstResult, Integer maxResults) { LOG.tracef("getRoleMembersStream(%s, %s, %d, %d)%s", realm, role, firstResult, maxResults, getShortStackTrace()); - ModelCriteriaBuilder mcb = criteria(); + DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) .compare(SearchableFields.ASSIGNED_ROLE, Operator.EQ, role.getId()); diff --git a/model/map/src/main/java/org/keycloak/models/map/userSession/MapUserSessionProvider.java b/model/map/src/main/java/org/keycloak/models/map/userSession/MapUserSessionProvider.java index 8aa7761611..3ea94bfa8f 100644 --- a/model/map/src/main/java/org/keycloak/models/map/userSession/MapUserSessionProvider.java +++ b/model/map/src/main/java/org/keycloak/models/map/userSession/MapUserSessionProvider.java @@ -29,7 +29,7 @@ import org.keycloak.models.UserSessionModel; import org.keycloak.models.UserSessionProvider; import org.keycloak.models.map.storage.MapKeycloakTransaction; import org.keycloak.models.map.storage.MapStorage; -import org.keycloak.models.map.storage.ModelCriteriaBuilder; +import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator; import org.keycloak.models.map.storage.criteria.DefaultModelCriteria; import java.util.Arrays; @@ -173,12 +173,12 @@ public class MapUserSessionProvider implements UserSessionProvider { return null; } - ModelCriteriaBuilder mcb = criteria(); - mcb = mcb.compare(AuthenticatedClientSessionModel.SearchableFields.ID, ModelCriteriaBuilder.Operator.EQ, clientSessionId) - .compare(AuthenticatedClientSessionModel.SearchableFields.USER_SESSION_ID, ModelCriteriaBuilder.Operator.EQ, userSession.getId()) - .compare(AuthenticatedClientSessionModel.SearchableFields.REALM_ID, ModelCriteriaBuilder.Operator.EQ, userSession.getRealm().getId()) - .compare(AuthenticatedClientSessionModel.SearchableFields.CLIENT_ID, ModelCriteriaBuilder.Operator.EQ, client.getId()) - .compare(AuthenticatedClientSessionModel.SearchableFields.IS_OFFLINE, ModelCriteriaBuilder.Operator.EQ, offline); + DefaultModelCriteria mcb = criteria(); + mcb = mcb.compare(AuthenticatedClientSessionModel.SearchableFields.ID, Operator.EQ, clientSessionId) + .compare(AuthenticatedClientSessionModel.SearchableFields.USER_SESSION_ID, Operator.EQ, userSession.getId()) + .compare(AuthenticatedClientSessionModel.SearchableFields.REALM_ID, Operator.EQ, userSession.getRealm().getId()) + .compare(AuthenticatedClientSessionModel.SearchableFields.CLIENT_ID, Operator.EQ, client.getId()) + .compare(AuthenticatedClientSessionModel.SearchableFields.IS_OFFLINE, Operator.EQ, offline); return clientSessionTx.read(withCriteria(mcb)) .findFirst() @@ -236,8 +236,8 @@ public class MapUserSessionProvider implements UserSessionProvider { return userEntityToAdapterFunc(realm).apply(userSessionEntity); } - ModelCriteriaBuilder mcb = realmAndOfflineCriteriaBuilder(realm, false) - .compare(UserSessionModel.SearchableFields.ID, ModelCriteriaBuilder.Operator.EQ, id); + DefaultModelCriteria mcb = realmAndOfflineCriteriaBuilder(realm, false) + .compare(UserSessionModel.SearchableFields.ID, Operator.EQ, id); return userSessionTx.read(withCriteria(mcb)) .findFirst() @@ -247,8 +247,8 @@ public class MapUserSessionProvider implements UserSessionProvider { @Override public Stream getUserSessionsStream(RealmModel realm, UserModel user) { - ModelCriteriaBuilder mcb = realmAndOfflineCriteriaBuilder(realm, false) - .compare(UserSessionModel.SearchableFields.USER_ID, ModelCriteriaBuilder.Operator.EQ, user.getId()); + DefaultModelCriteria mcb = realmAndOfflineCriteriaBuilder(realm, false) + .compare(UserSessionModel.SearchableFields.USER_ID, Operator.EQ, user.getId()); LOG.tracef("getUserSessionsStream(%s, %s)%s", realm, user, getShortStackTrace()); @@ -259,8 +259,8 @@ public class MapUserSessionProvider implements UserSessionProvider { @Override public Stream getUserSessionsStream(RealmModel realm, ClientModel client) { - ModelCriteriaBuilder mcb = realmAndOfflineCriteriaBuilder(realm, false) - .compare(UserSessionModel.SearchableFields.CLIENT_ID, ModelCriteriaBuilder.Operator.EQ, client.getId()); + DefaultModelCriteria mcb = realmAndOfflineCriteriaBuilder(realm, false) + .compare(UserSessionModel.SearchableFields.CLIENT_ID, Operator.EQ, client.getId()); LOG.tracef("getUserSessionsStream(%s, %s)%s", realm, client, getShortStackTrace()); @@ -274,8 +274,8 @@ public class MapUserSessionProvider implements UserSessionProvider { Integer firstResult, Integer maxResults) { LOG.tracef("getUserSessionsStream(%s, %s, %s, %s)%s", realm, client, firstResult, maxResults, getShortStackTrace()); - ModelCriteriaBuilder mcb = realmAndOfflineCriteriaBuilder(realm, false) - .compare(UserSessionModel.SearchableFields.CLIENT_ID, ModelCriteriaBuilder.Operator.EQ, client.getId()); + DefaultModelCriteria mcb = realmAndOfflineCriteriaBuilder(realm, false) + .compare(UserSessionModel.SearchableFields.CLIENT_ID, Operator.EQ, client.getId()); return userSessionTx.read(withCriteria(mcb).pagination(firstResult, maxResults, @@ -286,8 +286,8 @@ public class MapUserSessionProvider implements UserSessionProvider { @Override public Stream getUserSessionByBrokerUserIdStream(RealmModel realm, String brokerUserId) { - ModelCriteriaBuilder mcb = realmAndOfflineCriteriaBuilder(realm, false) - .compare(UserSessionModel.SearchableFields.BROKER_USER_ID, ModelCriteriaBuilder.Operator.EQ, brokerUserId); + DefaultModelCriteria mcb = realmAndOfflineCriteriaBuilder(realm, false) + .compare(UserSessionModel.SearchableFields.BROKER_USER_ID, Operator.EQ, brokerUserId); LOG.tracef("getUserSessionByBrokerUserIdStream(%s, %s)%s", realm, brokerUserId, getShortStackTrace()); @@ -298,8 +298,8 @@ public class MapUserSessionProvider implements UserSessionProvider { @Override public UserSessionModel getUserSessionByBrokerSessionId(RealmModel realm, String brokerSessionId) { - ModelCriteriaBuilder mcb = realmAndOfflineCriteriaBuilder(realm, false) - .compare(UserSessionModel.SearchableFields.BROKER_SESSION_ID, ModelCriteriaBuilder.Operator.EQ, brokerSessionId); + DefaultModelCriteria mcb = realmAndOfflineCriteriaBuilder(realm, false) + .compare(UserSessionModel.SearchableFields.BROKER_SESSION_ID, Operator.EQ, brokerSessionId); LOG.tracef("getUserSessionByBrokerSessionId(%s, %s)%s", realm, brokerSessionId, getShortStackTrace()); @@ -331,8 +331,8 @@ public class MapUserSessionProvider implements UserSessionProvider { @Override public long getActiveUserSessions(RealmModel realm, ClientModel client) { - ModelCriteriaBuilder mcb = realmAndOfflineCriteriaBuilder(realm, false) - .compare(UserSessionModel.SearchableFields.CLIENT_ID, ModelCriteriaBuilder.Operator.EQ, client.getId()); + DefaultModelCriteria mcb = realmAndOfflineCriteriaBuilder(realm, false) + .compare(UserSessionModel.SearchableFields.CLIENT_ID, Operator.EQ, client.getId()); LOG.tracef("getActiveUserSessions(%s, %s)%s", realm, client, getShortStackTrace()); @@ -341,7 +341,7 @@ public class MapUserSessionProvider implements UserSessionProvider { @Override public Map getActiveClientSessionStats(RealmModel realm, boolean offline) { - ModelCriteriaBuilder mcb = realmAndOfflineCriteriaBuilder(realm, offline); + DefaultModelCriteria mcb = realmAndOfflineCriteriaBuilder(realm, offline); LOG.tracef("getActiveClientSessionStats(%s, %s)%s", realm, offline, getShortStackTrace()); @@ -358,8 +358,8 @@ public class MapUserSessionProvider implements UserSessionProvider { public void removeUserSession(RealmModel realm, UserSessionModel session) { Objects.requireNonNull(session, "The provided user session can't be null!"); - ModelCriteriaBuilder mcb = realmAndOfflineCriteriaBuilder(realm, false) - .compare(UserSessionModel.SearchableFields.ID, ModelCriteriaBuilder.Operator.EQ, session.getId()); + DefaultModelCriteria mcb = realmAndOfflineCriteriaBuilder(realm, false) + .compare(UserSessionModel.SearchableFields.ID, Operator.EQ, session.getId()); LOG.tracef("removeUserSession(%s, %s)%s", realm, session, getShortStackTrace()); @@ -368,9 +368,9 @@ public class MapUserSessionProvider implements UserSessionProvider { @Override public void removeUserSessions(RealmModel realm, UserModel user) { - ModelCriteriaBuilder mcb = criteria(); - mcb = mcb.compare(UserSessionModel.SearchableFields.REALM_ID, ModelCriteriaBuilder.Operator.EQ, realm.getId()) - .compare(UserSessionModel.SearchableFields.USER_ID, ModelCriteriaBuilder.Operator.EQ, user.getId()); + DefaultModelCriteria mcb = criteria(); + mcb = mcb.compare(UserSessionModel.SearchableFields.REALM_ID, Operator.EQ, realm.getId()) + .compare(UserSessionModel.SearchableFields.USER_ID, Operator.EQ, user.getId()); LOG.tracef("removeUserSessions(%s, %s)%s", realm, user, getShortStackTrace()); @@ -389,7 +389,7 @@ public class MapUserSessionProvider implements UserSessionProvider { @Override public void removeUserSessions(RealmModel realm) { - ModelCriteriaBuilder mcb = realmAndOfflineCriteriaBuilder(realm, false); + DefaultModelCriteria mcb = realmAndOfflineCriteriaBuilder(realm, false); LOG.tracef("removeUserSessions(%s)%s", realm, getShortStackTrace()); @@ -442,13 +442,13 @@ public class MapUserSessionProvider implements UserSessionProvider { LOG.tracef("removeOfflineUserSession(%s, %s)%s", realm, userSession, getShortStackTrace()); - ModelCriteriaBuilder mcb; + DefaultModelCriteria mcb; if (userSession.isOffline()) { userSessionTx.delete(userSession.getId()); } else if (userSession.getNote(CORRESPONDING_SESSION_ID) != null) { String uk = userSession.getNote(CORRESPONDING_SESSION_ID); mcb = realmAndOfflineCriteriaBuilder(realm, true) - .compare(UserSessionModel.SearchableFields.ID, ModelCriteriaBuilder.Operator.EQ, uk); + .compare(UserSessionModel.SearchableFields.ID, Operator.EQ, uk); userSessionTx.delete(withCriteria(mcb)); userSession.removeNote(CORRESPONDING_SESSION_ID); } @@ -477,8 +477,8 @@ public class MapUserSessionProvider implements UserSessionProvider { @Override public Stream getOfflineUserSessionsStream(RealmModel realm, UserModel user) { - ModelCriteriaBuilder mcb = realmAndOfflineCriteriaBuilder(realm, true) - .compare(UserSessionModel.SearchableFields.USER_ID, ModelCriteriaBuilder.Operator.EQ, user.getId()); + DefaultModelCriteria mcb = realmAndOfflineCriteriaBuilder(realm, true) + .compare(UserSessionModel.SearchableFields.USER_ID, Operator.EQ, user.getId()); LOG.tracef("getOfflineUserSessionsStream(%s, %s)%s", realm, user, getShortStackTrace()); @@ -489,8 +489,8 @@ public class MapUserSessionProvider implements UserSessionProvider { @Override public UserSessionModel getOfflineUserSessionByBrokerSessionId(RealmModel realm, String brokerSessionId) { - ModelCriteriaBuilder mcb = realmAndOfflineCriteriaBuilder(realm, true) - .compare(UserSessionModel.SearchableFields.BROKER_SESSION_ID, ModelCriteriaBuilder.Operator.EQ, brokerSessionId); + DefaultModelCriteria mcb = realmAndOfflineCriteriaBuilder(realm, true) + .compare(UserSessionModel.SearchableFields.BROKER_SESSION_ID, Operator.EQ, brokerSessionId); LOG.tracef("getOfflineUserSessionByBrokerSessionId(%s, %s)%s", realm, brokerSessionId, getShortStackTrace()); @@ -502,8 +502,8 @@ public class MapUserSessionProvider implements UserSessionProvider { @Override public Stream getOfflineUserSessionByBrokerUserIdStream(RealmModel realm, String brokerUserId) { - ModelCriteriaBuilder mcb = realmAndOfflineCriteriaBuilder(realm, true) - .compare(UserSessionModel.SearchableFields.BROKER_USER_ID, ModelCriteriaBuilder.Operator.EQ, brokerUserId); + DefaultModelCriteria mcb = realmAndOfflineCriteriaBuilder(realm, true) + .compare(UserSessionModel.SearchableFields.BROKER_USER_ID, Operator.EQ, brokerUserId); LOG.tracef("getOfflineUserSessionByBrokerUserIdStream(%s, %s)%s", realm, brokerUserId, getShortStackTrace()); @@ -514,8 +514,8 @@ public class MapUserSessionProvider implements UserSessionProvider { @Override public long getOfflineSessionsCount(RealmModel realm, ClientModel client) { - ModelCriteriaBuilder mcb = realmAndOfflineCriteriaBuilder(realm, true) - .compare(UserSessionModel.SearchableFields.CLIENT_ID, ModelCriteriaBuilder.Operator.EQ, client.getId()); + DefaultModelCriteria mcb = realmAndOfflineCriteriaBuilder(realm, true) + .compare(UserSessionModel.SearchableFields.CLIENT_ID, Operator.EQ, client.getId()); LOG.tracef("getOfflineSessionsCount(%s, %s)%s", realm, client, getShortStackTrace()); @@ -525,8 +525,8 @@ public class MapUserSessionProvider implements UserSessionProvider { @Override public Stream getOfflineUserSessionsStream(RealmModel realm, ClientModel client, Integer firstResult, Integer maxResults) { - ModelCriteriaBuilder mcb = realmAndOfflineCriteriaBuilder(realm, true) - .compare(UserSessionModel.SearchableFields.CLIENT_ID, ModelCriteriaBuilder.Operator.EQ, client.getId()); + DefaultModelCriteria mcb = realmAndOfflineCriteriaBuilder(realm, true) + .compare(UserSessionModel.SearchableFields.CLIENT_ID, Operator.EQ, client.getId()); LOG.tracef("getOfflineUserSessionsStream(%s, %s, %s, %s)%s", realm, client, firstResult, maxResults, getShortStackTrace()); @@ -574,9 +574,9 @@ public class MapUserSessionProvider implements UserSessionProvider { } // first get a user entity by ID - ModelCriteriaBuilder mcb = criteria(); - mcb = mcb.compare(UserSessionModel.SearchableFields.REALM_ID, ModelCriteriaBuilder.Operator.EQ, realm.getId()) - .compare(UserSessionModel.SearchableFields.ID, ModelCriteriaBuilder.Operator.EQ, userSessionId); + DefaultModelCriteria mcb = criteria(); + mcb = mcb.compare(UserSessionModel.SearchableFields.REALM_ID, Operator.EQ, realm.getId()) + .compare(UserSessionModel.SearchableFields.ID, Operator.EQ, userSessionId); // check if it's an offline user session MapUserSessionEntity userSessionEntity = userSessionTx.read(withCriteria(mcb)).findFirst().orElse(null); @@ -587,7 +587,7 @@ public class MapUserSessionProvider implements UserSessionProvider { } else { // no session found by the given ID, try to find by corresponding session ID mcb = realmAndOfflineCriteriaBuilder(realm, true) - .compare(UserSessionModel.SearchableFields.CORRESPONDING_SESSION_ID, ModelCriteriaBuilder.Operator.EQ, userSessionId); + .compare(UserSessionModel.SearchableFields.CORRESPONDING_SESSION_ID, Operator.EQ, userSessionId); return userSessionTx.read(withCriteria(mcb)); } @@ -595,17 +595,17 @@ public class MapUserSessionProvider implements UserSessionProvider { String offlineUserSessionId = userSessionEntity.getNote(CORRESPONDING_SESSION_ID); if (offlineUserSessionId != null) { mcb = realmAndOfflineCriteriaBuilder(realm, true) - .compare(UserSessionModel.SearchableFields.ID, ModelCriteriaBuilder.Operator.EQ, offlineUserSessionId); + .compare(UserSessionModel.SearchableFields.ID, Operator.EQ, offlineUserSessionId); return userSessionTx.read(withCriteria(mcb)); } return Stream.empty(); } - private ModelCriteriaBuilder realmAndOfflineCriteriaBuilder(RealmModel realm, boolean offline) { + private DefaultModelCriteria realmAndOfflineCriteriaBuilder(RealmModel realm, boolean offline) { return DefaultModelCriteria.criteria() - .compare(UserSessionModel.SearchableFields.REALM_ID, ModelCriteriaBuilder.Operator.EQ, realm.getId()) - .compare(UserSessionModel.SearchableFields.IS_OFFLINE, ModelCriteriaBuilder.Operator.EQ, offline); + .compare(UserSessionModel.SearchableFields.REALM_ID, Operator.EQ, realm.getId()) + .compare(UserSessionModel.SearchableFields.IS_OFFLINE, Operator.EQ, offline); } private MapUserSessionEntity getUserSessionById(String id) { diff --git a/model/map/src/test/java/org/keycloak/models/map/storage/criteria/DefaultModelCriteriaTest.java b/model/map/src/test/java/org/keycloak/models/map/storage/criteria/DefaultModelCriteriaTest.java index 90b54d99f4..d3efd66799 100644 --- a/model/map/src/test/java/org/keycloak/models/map/storage/criteria/DefaultModelCriteriaTest.java +++ b/model/map/src/test/java/org/keycloak/models/map/storage/criteria/DefaultModelCriteriaTest.java @@ -45,7 +45,7 @@ public class DefaultModelCriteriaTest { assertThat(v.and(), hasToString("__TRUE__")); assertThat(v.and(v.or()), hasToString("__FALSE__")); - assertThat(v.and(v.compare(CLIENT_ID, Operator.EQ, 3)), hasToString("(clientId EQ [3])")); + assertThat(v.and(v.compare(CLIENT_ID, Operator.EQ, 3)), hasToString("clientId EQ [3]")); assertThat(v.and(v.compare(CLIENT_ID, Operator.EQ, 3), v.or()), hasToString("__FALSE__")); assertThat(v.and(v.compare(CLIENT_ID, Operator.EQ, 4).compare(ID, Operator.EQ, 5)), hasToString("(clientId EQ [4] && id EQ [5])")); assertThat(v.and(v.compare(CLIENT_ID, Operator.EQ, 4), v.compare(ID, Operator.EQ, 5)), hasToString("(clientId EQ [4] && id EQ [5])")); @@ -57,7 +57,7 @@ public class DefaultModelCriteriaTest { assertThat(v.or(), hasToString("__FALSE__")); assertThat(v.or(v.and()), hasToString("__TRUE__")); - assertThat(v.or(v.compare(CLIENT_ID, Operator.EQ, 3)), hasToString("(clientId EQ [3])")); + assertThat(v.or(v.compare(CLIENT_ID, Operator.EQ, 3)), hasToString("clientId EQ [3]")); assertThat(v.or(v.compare(CLIENT_ID, Operator.EQ, 3), v.and()), hasToString("__TRUE__")); assertThat(v.or(v.compare(CLIENT_ID, Operator.EQ, 4).compare(ID, Operator.EQ, 5)), hasToString("(clientId EQ [4] && id EQ [5])")); assertThat(v.or(v.compare(CLIENT_ID, Operator.EQ, 4), v.compare(ID, Operator.EQ, 5)), hasToString("(clientId EQ [4] || id EQ [5])"));