From 20cde9cd0054792f2ced63e63995afce08fc3a5e Mon Sep 17 00:00:00 2001 From: Bill Burke Date: Thu, 17 Jul 2014 15:41:02 -0400 Subject: [PATCH] realmId and roleId used in User and user relationships --- .../keycloak/models/jpa/JpaUserProvider.java | 63 +++++++------------ .../entities/AuthenticationLinkEntity.java | 2 +- .../models/jpa/entities/CredentialEntity.java | 2 +- .../models/jpa/entities/SocialLinkEntity.java | 15 +++-- .../jpa/entities/UserAttributeEntity.java | 2 +- .../models/jpa/entities/UserEntity.java | 34 +++++----- .../jpa/entities/UserRoleMappingEntity.java | 2 +- 7 files changed, 50 insertions(+), 70 deletions(-) diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/JpaUserProvider.java b/model/jpa/src/main/java/org/keycloak/models/jpa/JpaUserProvider.java index 24c31c3d3c..f6e2af6e55 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/JpaUserProvider.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/JpaUserProvider.java @@ -9,12 +9,8 @@ import org.keycloak.models.UserCredentialModel; import org.keycloak.models.UserModel; import org.keycloak.models.UserProvider; import org.keycloak.models.jpa.entities.AuthenticationLinkEntity; -import org.keycloak.models.jpa.entities.CredentialEntity; -import org.keycloak.models.jpa.entities.RealmEntity; -import org.keycloak.models.jpa.entities.RoleEntity; import org.keycloak.models.jpa.entities.SocialLinkEntity; import org.keycloak.models.jpa.entities.UserEntity; -import org.keycloak.models.jpa.entities.UserRoleMappingEntity; import org.keycloak.models.utils.CredentialValidation; import org.keycloak.models.utils.KeycloakModelUtils; @@ -49,8 +45,7 @@ public class JpaUserProvider implements UserProvider { UserEntity entity = new UserEntity(); entity.setId(id); entity.setUsername(username); - RealmEntity realmEntity = em.getReference(RealmEntity.class, realm.getId()); - entity.setRealm(realmEntity); + entity.setRealmId(realm.getId()); em.persist(entity); em.flush(); UserModel userModel = new UserAdapter(realm, em, entity); @@ -79,8 +74,7 @@ public class JpaUserProvider implements UserProvider { public boolean removeUser(RealmModel realm, String name) { TypedQuery query = em.createNamedQuery("getRealmUserByUsername", UserEntity.class); query.setParameter("username", name); - RealmEntity realmEntity = em.getReference(RealmEntity.class, realm.getId()); - query.setParameter("realm", realmEntity); + query.setParameter("realmId", realm.getId()); List results = query.getResultList(); if (results.size() == 0) return false; removeUser(results.get(0)); @@ -101,8 +95,7 @@ public class JpaUserProvider implements UserProvider { @Override public void addSocialLink(RealmModel realm, UserModel user, SocialLinkModel socialLink) { SocialLinkEntity entity = new SocialLinkEntity(); - RealmEntity realmEntity = em.getReference(RealmEntity.class, realm.getId()); - entity.setRealm(realmEntity); + entity.setRealmId(realm.getId()); entity.setSocialProvider(socialLink.getSocialProvider()); entity.setSocialUserId(socialLink.getSocialUserId()); entity.setSocialUsername(socialLink.getSocialUsername()); @@ -128,19 +121,18 @@ public class JpaUserProvider implements UserProvider { @Override public void preRemove(RealmModel realm) { - RealmEntity realmEntity = em.getReference(RealmEntity.class, realm.getId()); int num = em.createNamedQuery("deleteUserRoleMappingsByRealm") - .setParameter("realm", realmEntity).executeUpdate(); + .setParameter("realmId", realm.getId()).executeUpdate(); num = em.createNamedQuery("deleteSocialLinkByRealm") - .setParameter("realm", realmEntity).executeUpdate(); + .setParameter("realmId", realm.getId()).executeUpdate(); num = em.createNamedQuery("deleteCredentialsByRealm") - .setParameter("realm", realmEntity).executeUpdate(); + .setParameter("realmId", realm.getId()).executeUpdate(); num = em.createNamedQuery("deleteUserAttributesByRealm") - .setParameter("realm", realmEntity).executeUpdate(); + .setParameter("realmId", realm.getId()).executeUpdate(); num = em.createNamedQuery("deleteAuthenticationLinksByRealm") - .setParameter("realm", realmEntity).executeUpdate(); + .setParameter("realmId", realm.getId()).executeUpdate(); num = em.createNamedQuery("deleteUsersByRealm") - .setParameter("realm", realmEntity).executeUpdate(); + .setParameter("realmId", realm.getId()).executeUpdate(); } @Override @@ -149,35 +141,32 @@ public class JpaUserProvider implements UserProvider { } @Override - public UserModel getUserById(String id, RealmModel realmModel) { + public UserModel getUserById(String id, RealmModel realm) { TypedQuery query = em.createNamedQuery("getRealmUserById", UserEntity.class); query.setParameter("id", id); - RealmEntity realm = em.getReference(RealmEntity.class, realmModel.getId()); - query.setParameter("realm", realm); + query.setParameter("realmId", realm.getId()); List entities = query.getResultList(); if (entities.size() == 0) return null; - return new UserAdapter(realmModel, em, entities.get(0)); + return new UserAdapter(realm, em, entities.get(0)); } @Override - public UserModel getUserByUsername(String username, RealmModel realmModel) { + public UserModel getUserByUsername(String username, RealmModel realm) { TypedQuery query = em.createNamedQuery("getRealmUserByUsername", UserEntity.class); query.setParameter("username", username); - RealmEntity realm = em.getReference(RealmEntity.class, realmModel.getId()); - query.setParameter("realm", realm); + query.setParameter("realmId", realm.getId()); List results = query.getResultList(); if (results.size() == 0) return null; - return new UserAdapter(realmModel, em, results.get(0)); + return new UserAdapter(realm, em, results.get(0)); } @Override - public UserModel getUserByEmail(String email, RealmModel realmModel) { + public UserModel getUserByEmail(String email, RealmModel realm) { TypedQuery query = em.createNamedQuery("getRealmUserByEmail", UserEntity.class); query.setParameter("email", email); - RealmEntity realm = em.getReference(RealmEntity.class, realmModel.getId()); - query.setParameter("realm", realm); + query.setParameter("realmId", realm.getId()); List results = query.getResultList(); - return results.isEmpty() ? null : new UserAdapter(realmModel, em, results.get(0)); + return results.isEmpty() ? null : new UserAdapter(realm, em, results.get(0)); } @Override @@ -187,8 +176,7 @@ public class JpaUserProvider implements UserProvider { @Override public UserModel getUserBySocialLink(SocialLinkModel socialLink, RealmModel realm) { TypedQuery query = em.createNamedQuery("findUserByLinkAndRealm", UserEntity.class); - RealmEntity realmEntity = em.getReference(RealmEntity.class, realm.getId()); - query.setParameter("realm", realmEntity); + query.setParameter("realmId", realm.getId()); query.setParameter("socialProvider", socialLink.getSocialProvider()); query.setParameter("socialUserId", socialLink.getSocialUserId()); List results = query.getResultList(); @@ -210,11 +198,9 @@ public class JpaUserProvider implements UserProvider { @Override public int getUsersCount(RealmModel realm) { - RealmEntity realmEntity = em.getReference(RealmEntity.class, realm.getId()); - // TODO: named query? Object count = em.createNamedQuery("getRealmUserCount") - .setParameter("realm", realmEntity) + .setParameter("realmId", realm.getId()) .getSingleResult(); return ((Number)count).intValue(); } @@ -222,8 +208,7 @@ public class JpaUserProvider implements UserProvider { @Override public List getUsers(RealmModel realm, int firstResult, int maxResults) { TypedQuery query = em.createNamedQuery("getAllUsersByRealm", UserEntity.class); - RealmEntity realmEntity = em.getReference(RealmEntity.class, realm.getId()); - query.setParameter("realm", realmEntity); + query.setParameter("realmId", realm.getId()); if (firstResult != -1) { query.setFirstResult(firstResult); } @@ -244,8 +229,7 @@ public class JpaUserProvider implements UserProvider { @Override public List searchForUser(String search, RealmModel realm, int firstResult, int maxResults) { TypedQuery query = em.createNamedQuery("searchForUser", UserEntity.class); - RealmEntity realmEntity = em.getReference(RealmEntity.class, realm.getId()); - query.setParameter("realm", realmEntity); + query.setParameter("realmId", realm.getId()); query.setParameter("search", "%" + search.toLowerCase() + "%"); if (firstResult != -1) { query.setFirstResult(firstResult); @@ -291,8 +275,7 @@ public class JpaUserProvider implements UserProvider { builder.append(" order by u.username"); String q = builder.toString(); TypedQuery query = em.createQuery(q, UserEntity.class); - RealmEntity realmEntity = em.getReference(RealmEntity.class, realm.getId()); - query.setParameter("realm", realmEntity); + query.setParameter("realmId", realm.getId()); if (firstResult != -1) { query.setFirstResult(firstResult); } diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/AuthenticationLinkEntity.java b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/AuthenticationLinkEntity.java index 1d8b72494f..6075586bac 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/AuthenticationLinkEntity.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/AuthenticationLinkEntity.java @@ -15,7 +15,7 @@ import java.io.Serializable; * @author Marek Posolda */ @NamedQueries({ - @NamedQuery(name="deleteAuthenticationLinksByRealm", query="delete from AuthenticationLinkEntity authLink where authLink.user IN (select u from UserEntity u where realm=:realm)") + @NamedQuery(name="deleteAuthenticationLinksByRealm", query="delete from AuthenticationLinkEntity authLink where authLink.user IN (select u from UserEntity u where u.realmId=:realmId)") }) @Entity @IdClass(AuthenticationLinkEntity.Key.class) diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/CredentialEntity.java b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/CredentialEntity.java index d740d6b58c..98eb5f8295 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/CredentialEntity.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/CredentialEntity.java @@ -16,7 +16,7 @@ import java.io.Serializable; */ @NamedQueries({ @NamedQuery(name="credentialByUserAndType", query="select cred from CredentialEntity cred where cred.user = :user and cred.type = :type"), - @NamedQuery(name="deleteCredentialsByRealm", query="delete from CredentialEntity cred where cred.user IN (select u from UserEntity u where realm=:realm)") + @NamedQuery(name="deleteCredentialsByRealm", query="delete from CredentialEntity cred where cred.user IN (select u from UserEntity u where u.realmId=:realmId)") }) @Entity diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/SocialLinkEntity.java b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/SocialLinkEntity.java index 01af791998..4e5dfad696 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/SocialLinkEntity.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/SocialLinkEntity.java @@ -16,8 +16,8 @@ import java.io.Serializable; @NamedQueries({ @NamedQuery(name="findSocialLinkByUser", query="select link from SocialLinkEntity link where link.user = :user"), @NamedQuery(name="findSocialLinkByUserAndProvider", query="select link from SocialLinkEntity link where link.user = :user and link.socialProvider = :socialProvider"), - @NamedQuery(name="findUserByLinkAndRealm", query="select link.user from SocialLinkEntity link where link.realm = :realm and link.socialProvider = :socialProvider and link.socialUserId = :socialUserId"), - @NamedQuery(name="deleteSocialLinkByRealm", query="delete from SocialLinkEntity social where social.user IN (select u from UserEntity u where realm=:realm)"), + @NamedQuery(name="findUserByLinkAndRealm", query="select link.user from SocialLinkEntity link where link.realmId = :realmId and link.socialProvider = :socialProvider and link.socialUserId = :socialUserId"), + @NamedQuery(name="deleteSocialLinkByRealm", query="delete from SocialLinkEntity social where social.user IN (select u from UserEntity u where realmId=:realmId)"), @NamedQuery(name="deleteSocialLinkByUser", query="delete from SocialLinkEntity social where social.user = :user") }) @Entity @@ -28,8 +28,7 @@ public class SocialLinkEntity { @ManyToOne(fetch = FetchType.LAZY) private UserEntity user; - @ManyToOne(fetch = FetchType.LAZY) - protected RealmEntity realm; + protected String realmId; @Id protected String socialProvider; @@ -68,12 +67,12 @@ public class SocialLinkEntity { this.socialUsername = socialUsername; } - public RealmEntity getRealm() { - return realm; + public String getRealmId() { + return realmId; } - public void setRealm(RealmEntity realm) { - this.realm = realm; + public void setRealmId(String realmId) { + this.realmId = realmId; } public static class Key implements Serializable { diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserAttributeEntity.java b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserAttributeEntity.java index d0c3e65af0..8b027bd4f6 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserAttributeEntity.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserAttributeEntity.java @@ -14,7 +14,7 @@ import java.io.Serializable; * @version $Revision: 1 $ */ @NamedQueries({ - @NamedQuery(name="deleteUserAttributesByRealm", query="delete from UserAttributeEntity attr where attr.user IN (select u from UserEntity u where realm=:realm)") + @NamedQuery(name="deleteUserAttributesByRealm", query="delete from UserAttributeEntity attr where attr.user IN (select u from UserEntity u where u.realmId=:realmId)") }) @Entity @IdClass(UserAttributeEntity.Key.class) diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserEntity.java b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserEntity.java index 85c8fc7d00..fadf3a62b5 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserEntity.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserEntity.java @@ -31,20 +31,20 @@ import java.util.Set; * @version $Revision: 1 $ */ @NamedQueries({ - @NamedQuery(name="getAllUsersByRealm", query="select u from UserEntity u where u.realm = :realm order by u.username"), - @NamedQuery(name="searchForUser", query="select u from UserEntity u where u.realm = :realm and ( lower(u.username) like :search or lower(concat(u.firstName, ' ', u.lastName)) like :search or u.email like :search ) order by u.username"), - @NamedQuery(name="getRealmUserById", query="select u from UserEntity u where u.id = :id and u.realm = :realm"), - @NamedQuery(name="getRealmUserByUsername", query="select u from UserEntity u where u.username = :username and u.realm = :realm"), - @NamedQuery(name="getRealmUserByEmail", query="select u from UserEntity u where u.email = :email and u.realm = :realm"), - @NamedQuery(name="getRealmUserByLastName", query="select u from UserEntity u where u.lastName = :lastName and u.realm = :realm"), - @NamedQuery(name="getRealmUserByFirstLastName", query="select u from UserEntity u where u.firstName = :first and u.lastName = :last and u.realm = :realm"), - @NamedQuery(name="getRealmUserCount", query="select count(u) from UserEntity u where u.realm = :realm"), - @NamedQuery(name="deleteUsersByRealm", query="delete from UserEntity u where u.realm = :realm") + @NamedQuery(name="getAllUsersByRealm", query="select u from UserEntity u where u.realmId = :realmId order by u.username"), + @NamedQuery(name="searchForUser", query="select u from UserEntity u where u.realmId = :realmId and ( lower(u.username) like :search or lower(concat(u.firstName, ' ', u.lastName)) like :search or u.email like :search ) order by u.username"), + @NamedQuery(name="getRealmUserById", query="select u from UserEntity u where u.id = :id and u.realmId = :realmId"), + @NamedQuery(name="getRealmUserByUsername", query="select u from UserEntity u where u.username = :username and u.realmId = :realmId"), + @NamedQuery(name="getRealmUserByEmail", query="select u from UserEntity u where u.email = :email and u.realmId = :realmId"), + @NamedQuery(name="getRealmUserByLastName", query="select u from UserEntity u where u.lastName = :lastName and u.realmId = :realmId"), + @NamedQuery(name="getRealmUserByFirstLastName", query="select u from UserEntity u where u.firstName = :first and u.lastName = :last and u.realmId = :realmId"), + @NamedQuery(name="getRealmUserCount", query="select count(u) from UserEntity u where u.realmId = :realmId"), + @NamedQuery(name="deleteUsersByRealm", query="delete from UserEntity u where u.realmId = :realmId") }) @Entity @Table(uniqueConstraints = { - @UniqueConstraint(columnNames = { "realm", "username" }), - @UniqueConstraint(columnNames = { "realm", "emailConstraint" }) + @UniqueConstraint(columnNames = { "realmId", "username" }), + @UniqueConstraint(columnNames = { "realmId", "emailConstraint" }) }) public class UserEntity { @Id @@ -62,9 +62,7 @@ public class UserEntity { // Hack just to workaround the fact that on MS-SQL you can't have unique constraint with multiple NULL values TODO: Find better solution (like unique index with 'where' but that's proprietary) protected String emailConstraint = KeycloakModelUtils.generateId(); - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "realm") - protected RealmEntity realm; + protected String realmId; @OneToMany(cascade = CascadeType.REMOVE, orphanRemoval = true, mappedBy="user") protected Collection attributes = new ArrayList(); @@ -167,12 +165,12 @@ public class UserEntity { this.requiredActions = requiredActions; } - public RealmEntity getRealm() { - return realm; + public String getRealmId() { + return realmId; } - public void setRealm(RealmEntity realm) { - this.realm = realm; + public void setRealmId(String realmId) { + this.realmId = realmId; } public Collection getCredentials() { diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserRoleMappingEntity.java b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserRoleMappingEntity.java index 3788ef5cf3..d0d040e353 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserRoleMappingEntity.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserRoleMappingEntity.java @@ -18,7 +18,7 @@ import java.io.Serializable; @NamedQuery(name="userHasRole", query="select m from UserRoleMappingEntity m where m.user = :user and m.roleId = :roleId"), @NamedQuery(name="userRoleMappings", query="select m from UserRoleMappingEntity m where m.user = :user"), @NamedQuery(name="userRoleMappingIds", query="select m.roleId from UserRoleMappingEntity m where m.user = :user"), - @NamedQuery(name="deleteUserRoleMappingsByRealm", query="delete from UserRoleMappingEntity mapping where mapping.user IN (select u from UserEntity u where realm=:realm)"), + @NamedQuery(name="deleteUserRoleMappingsByRealm", query="delete from UserRoleMappingEntity mapping where mapping.user IN (select u from UserEntity u where u.realmId=:realmId)"), @NamedQuery(name="deleteUserRoleMappingsByRole", query="delete from UserRoleMappingEntity m where m.roleId = :roleId"), @NamedQuery(name="deleteUserRoleMappingsByUser", query="delete from UserRoleMappingEntity m where m.user = :user")