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 0d44cf4400..19b4cb2ad9 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 @@ -103,7 +103,7 @@ public class MapRootAuthenticationSessionProvider implements AuthenticationSe throw new ModelDuplicateException("Root authentication session exists: " + entity.getId()); } - tx.create(entity); + entity = tx.create(entity); return entityToAdapterFunc(realm).apply(entity); } 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 3b3a5701d5..7bdb83fae8 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 @@ -128,7 +128,7 @@ public class MapPermissionTicketStore> implements Permis entity.setOwner(owner); entity.setResourceServerId(resourceServer.getId()); - tx.create(entity); + entity = tx.create(entity); return entityToAdapter(entity); } 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 8b987c6b61..988ea50174 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 @@ -96,7 +96,7 @@ public class MapPolicyStore implements PolicyStore { entity.setName(representation.getName()); entity.setResourceServerId(resourceServer.getId()); - tx.create(entity); + entity = tx.create(entity); return entityToAdapter(entity); } diff --git a/model/map/src/main/java/org/keycloak/models/map/authorization/MapResourceServerStore.java b/model/map/src/main/java/org/keycloak/models/map/authorization/MapResourceServerStore.java index 4d317bfdd3..fd3e7ee9c9 100644 --- a/model/map/src/main/java/org/keycloak/models/map/authorization/MapResourceServerStore.java +++ b/model/map/src/main/java/org/keycloak/models/map/authorization/MapResourceServerStore.java @@ -82,7 +82,7 @@ public class MapResourceServerStore implements ResourceServerStore { MapResourceServerEntity entity = new MapResourceServerEntity<>(resourceServerStore.getKeyConvertor().fromString(clientId)); - tx.create(entity); + entity = tx.create(entity); return entityToAdapter(entity); } 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 492309736b..da62c4bd33 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 @@ -96,7 +96,7 @@ public class MapResourceStore> implements ResourceStore entity.setResourceServerId(resourceServer.getId()); entity.setOwner(owner); - tx.create(entity); + entity = tx.create(entity); return entityToAdapter(entity); } 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 36e4b33b17..1440926ac1 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 @@ -94,7 +94,7 @@ public class MapScopeStore implements ScopeStore { entity.setName(name); entity.setResourceServerId(resourceServer.getId()); - tx.create(entity); + entity = tx.create(entity); return entityToAdapter(entity); } 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 dc401f5862..6b4a9c6ed1 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 @@ -159,7 +159,7 @@ public class MapClientProvider implements ClientProvider { if (tx.read(entity.getId()) != null) { throw new ModelDuplicateException("Client exists: " + id); } - tx.create(entity); + entity = tx.create(entity); final ClientModel resource = entityToAdapterFunc(realm).apply(entity); // TODO: Sending an event should be extracted to store layer 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 b31963fa17..7c72faac6a 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 @@ -102,7 +102,7 @@ public class MapClientScopeProvider implements ClientScopeProvider { if (tx.read(entity.getId()) != null) { throw new ModelDuplicateException("Client scope exists: " + id); } - tx.create(entity); + entity = tx.create(entity); return entityToAdapterFunc(realm).apply(entity); } diff --git a/model/map/src/main/java/org/keycloak/models/map/common/MapStorageUtils.java b/model/map/src/main/java/org/keycloak/models/map/common/MapStorageUtils.java index 742014bce0..a446f58823 100644 --- a/model/map/src/main/java/org/keycloak/models/map/common/MapStorageUtils.java +++ b/model/map/src/main/java/org/keycloak/models/map/common/MapStorageUtils.java @@ -37,7 +37,6 @@ public class MapStorageUtils { */ public static > V registerEntityForChanges(MapKeycloakTransaction tx, V origEntity) { final V res = tx.read(origEntity.getId(), id -> Serialization.from(origEntity)); - tx.updateIfChanged(res, AbstractEntity::isUpdated); - return res; + return tx.updateIfChanged(res, AbstractEntity::isUpdated); } } 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 94c7ae06db..105497e219 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 @@ -220,7 +220,7 @@ public class MapGroupProvider implements GroupProvider { if (tx.read(entity.getId()) != null) { throw new ModelDuplicateException("Group exists: " + entityId); } - tx.create(entity); + entity = tx.create(entity); return entityToAdapterFunc(realm).apply(entity); } 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 4a31a4a3ad..73be5cf3e5 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 @@ -86,7 +86,7 @@ public class MapUserLoginFailureProvider implements UserLoginFailureProvider if (userLoginFailureEntity == null) { userLoginFailureEntity = new MapUserLoginFailureEntity<>(userLoginFailureStore.getKeyConvertor().yieldNewUniqueKey(), realm.getId(), userId); - userLoginFailureTx.create(userLoginFailureEntity); + userLoginFailureEntity = userLoginFailureTx.create(userLoginFailureEntity); } return userLoginFailureEntityToAdapterFunc(realm).apply(userLoginFailureEntity); 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 775c8220e6..70a6031e89 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 @@ -89,7 +89,7 @@ public class MapRealmProvider implements RealmProvider { MapRealmEntity entity = new MapRealmEntity<>(kId); entity.setName(name); - tx.create(entity); + entity = tx.create(entity); return entityToAdapter(entity); } 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 c2b1bdc997..ce5d9e8c3c 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 @@ -81,7 +81,7 @@ public class MapRoleProvider implements RoleProvider { if (tx.read(entity.getId()) != null) { throw new ModelDuplicateException("Role exists: " + id); } - tx.create(entity); + entity = tx.create(entity); return entityToAdapterFunc(realm).apply(entity); } @@ -122,7 +122,7 @@ public class MapRoleProvider implements RoleProvider { if (tx.read(entity.getId()) != null) { throw new ModelDuplicateException("Role exists: " + id); } - tx.create(entity); + entity = tx.create(entity); return entityToAdapterFunc(client.getRealm()).apply(entity); } diff --git a/model/map/src/main/java/org/keycloak/models/map/storage/MapKeycloakTransaction.java b/model/map/src/main/java/org/keycloak/models/map/storage/MapKeycloakTransaction.java index d1a71bf591..98a201f22d 100644 --- a/model/map/src/main/java/org/keycloak/models/map/storage/MapKeycloakTransaction.java +++ b/model/map/src/main/java/org/keycloak/models/map/storage/MapKeycloakTransaction.java @@ -29,8 +29,9 @@ public interface MapKeycloakTransaction, M> exten * Instructs this transaction to add a new value into the underlying store on commit. * * @param value the value + * @return Entity representing the {@code value} in the store. It may or may not be the same instance as {@code value} */ - void create(V value); + V create(V value); /** * Provides possibility to lookup for values by a {@code key} in the underlying store with respect to changes done @@ -77,8 +78,9 @@ public interface MapKeycloakTransaction, M> exten * underlying store on commit. * * @param value updated version of the value + * @return Entity representing the {@code value} in the store. It may or may not be the same instance as {@code value} */ - void update(V value); + V update(V value); /** * Returns an updated version of the {@code orig} object as updated in this transaction. @@ -98,17 +100,19 @@ public interface MapKeycloakTransaction, M> exten * * @param value new version of the value. Must not alter the {@code id} of the entity * @param shouldPut predicate to check in commit phase + * @return Entity representing the {@code value} in the store. It may or may not be the same instance as {@code value} * @see AbstractEntity#getId() */ - void updateIfChanged(V value, Predicate shouldPut); + V updateIfChanged(V value, Predicate shouldPut); /** * Instructs this transaction to delete a value associated with the identifier {@code key} from the underlying store * on commit. * + * @return Returns {@code true} if the object has been deleted or result cannot be determined, {@code false} otherwise. * @param key identifier of a value */ - void delete(K key); + boolean delete(K key); /** * Instructs this transaction to remove values (identified by {@code mcb} filter) from the underlying store on commit. diff --git a/model/map/src/main/java/org/keycloak/models/map/storage/MapStorage.java b/model/map/src/main/java/org/keycloak/models/map/storage/MapStorage.java index ec09d754a5..fb2b2d3dfe 100644 --- a/model/map/src/main/java/org/keycloak/models/map/storage/MapStorage.java +++ b/model/map/src/main/java/org/keycloak/models/map/storage/MapStorage.java @@ -131,7 +131,7 @@ public interface MapStorage, M> { * * @return See description. Never returns {@code null} */ - public MapKeycloakTransaction createTransaction(KeycloakSession session); + MapKeycloakTransaction createTransaction(KeycloakSession session); /** * Returns a {@link StringKeyConvertor} that is used to convert primary keys @@ -139,6 +139,6 @@ public interface MapStorage, M> { * * @return See above. Never returns {@code null}. */ - public StringKeyConvertor getKeyConvertor(); + StringKeyConvertor getKeyConvertor(); } 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 805b170dc8..6813a94e78 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 @@ -183,19 +183,21 @@ public class ConcurrentHashMapKeycloakTransaction } @Override - public void update(V value) { + public V update(V value) { K key = value.getId(); addTask(key, new UpdateOperation(value)); + return value; } @Override - public void create(V value) { + public V create(V value) { K key = value.getId(); addTask(key, new CreateOperation(value)); + return value; } @Override - public void updateIfChanged(V value, Predicate shouldPut) { + public V updateIfChanged(V value, Predicate shouldPut) { K key = value.getId(); log.tracef("Adding operation UPDATE_IF_CHANGED for %s @ %08x", key, System.identityHashCode(value)); @@ -209,12 +211,13 @@ public class ConcurrentHashMapKeycloakTransaction } @Override public MapOperation getOperation() { return MapOperation.UPDATE; } }; - tasks.merge(taskKey, op, this::merge); + return tasks.merge(taskKey, op, this::merge).getValue(); } @Override - public void delete(K key) { + public boolean delete(K key) { addTask(key, new DeleteOperation(key)); + return true; } 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 73490a5477..99eeb98731 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 @@ -62,10 +62,10 @@ public class UserSessionConcurrentHashMapStorage extends ConcurrentHashMapSto } @Override - public void delete(K key) { + public boolean delete(K key) { ModelCriteriaBuilder csMcb = clientSessionStore.createCriteriaBuilder().compare(AuthenticatedClientSessionModel.SearchableFields.USER_SESSION_ID, Operator.EQ, key); clientSessionTr.delete(key, withCriteria(csMcb)); - super.delete(key); + return super.delete(key); } } 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 f64fe9e680..b45f00facc 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 @@ -336,7 +336,7 @@ public class MapUserProvider implements UserProvider.Streams, UserCredentialS entity.setUsername(username.toLowerCase()); entity.setCreatedTimestamp(Time.currentTimeMillis()); - tx.create(entity); + entity = tx.create(entity); final UserModel userModel = entityToAdapterFunc(realm).apply(entity); if (addDefaultRoles) { 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 28886943a0..f798de0e82 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 @@ -161,7 +161,7 @@ public class MapUserSessionProvider implements UserSessionProvider { LOG.tracef("createClientSession(%s, %s, %s)%s", realm, client, userSession, getShortStackTrace()); - clientSessionTx.create(entity); + entity = clientSessionTx.create(entity); MapUserSessionEntity userSessionEntity = getUserSessionById(userSessionStore.getKeyConvertor().fromString(userSession.getId())); @@ -226,7 +226,7 @@ public class MapUserSessionProvider implements UserSessionProvider { throw new ModelDuplicateException("User session exists: " + entity.getId()); } - userSessionTx.create(entity); + entity = userSessionTx.create(entity); } UserSessionModel userSession = userEntityToAdapterFunc(realm).apply(entity); @@ -441,7 +441,7 @@ public class MapUserSessionProvider implements UserSessionProvider { offlineUserSession.setLastSessionRefresh(currentTime); setUserSessionExpiration(offlineUserSession, userSession.getRealm()); - userSessionTx.create(offlineUserSession); + offlineUserSession = userSessionTx.create(offlineUserSession); return userEntityToAdapterFunc(userSession.getRealm()).apply(offlineUserSession); } @@ -490,7 +490,7 @@ public class MapUserSessionProvider implements UserSessionProvider { userSessionEntity.get().addAuthenticatedClientSession(clientSession.getClient().getId(), clientSessionStore.getKeyConvertor().keyToString(clientSessionEntity.getId())); } - clientSessionTx.create(clientSessionEntity); + clientSessionEntity = clientSessionTx.create(clientSessionEntity); return clientEntityToAdapterFunc(clientSession.getRealm(), clientSession.getClient(), offlineUserSession).apply(clientSessionEntity); @@ -577,7 +577,7 @@ public class MapUserSessionProvider implements UserSessionProvider { userSessionEntity.addAuthenticatedClientSession(entry.getKey(), clientSessionStore.getKeyConvertor().keyToString(clientSession.getId())); - clientSessionTx.create(clientSession); + clientSession = clientSessionTx.create(clientSession); } return userSessionEntity;