Removed long ids and replaced with composite ids

This commit is contained in:
Stian Thorgersen 2014-07-17 11:16:10 +01:00
parent 9abe357754
commit 46c04561ff
23 changed files with 523 additions and 208 deletions

View file

@ -6,10 +6,7 @@ import org.keycloak.audit.AuditProviderFactory;
import org.keycloak.audit.EventType;
import org.keycloak.connections.jpa.JpaConnectionProvider;
import org.keycloak.models.KeycloakSession;
import org.keycloak.util.JpaUtils;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.util.HashSet;
import java.util.Set;

View file

@ -2,11 +2,11 @@ package org.keycloak.connections.jpa;
import org.keycloak.Config;
import org.keycloak.models.KeycloakSession;
import org.keycloak.util.JpaUtils;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.util.Properties;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
@ -47,10 +47,22 @@ public class DefaultJpaConnectionProviderFactory implements JpaConnectionProvide
if (emf == null) {
synchronized (this) {
if (emf == null) {
emf = Persistence.createEntityManagerFactory(unitName, JpaUtils.getHibernateProperties());
emf = Persistence.createEntityManagerFactory(unitName, getHibernateProperties());
}
}
}
}
private Properties getHibernateProperties() {
Properties result = new Properties();
for (Object property : System.getProperties().keySet()) {
if (property.toString().startsWith("hibernate.")) {
String propValue = System.getProperty(property.toString());
result.put(property, propValue);
}
}
return result;
}
}

View file

@ -10,7 +10,6 @@ import org.keycloak.connections.mongo.api.MongoStore;
import org.keycloak.connections.mongo.impl.MongoStoreImpl;
import org.keycloak.connections.mongo.impl.context.TransactionMongoStoreInvocationContext;
import org.keycloak.models.KeycloakSession;
import org.keycloak.util.JpaUtils;
import java.util.Collections;

View file

@ -1,22 +0,0 @@
package org.keycloak.util;
import java.util.Properties;
/**
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/
public class JpaUtils {
// Allows to override some properties in persistence.xml by system properties
public static Properties getHibernateProperties() {
Properties result = new Properties();
for (Object property : System.getProperties().keySet()) {
if (property.toString().startsWith("hibernate.")) {
String propValue = System.getProperty(property.toString());
result.put(property, propValue);
}
}
return result;
}
}

View file

@ -8,6 +8,7 @@ import org.keycloak.models.SocialLinkModel;
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;
@ -90,7 +91,9 @@ public class JpaUserProvider implements UserProvider {
em.createQuery("delete from " + UserRoleMappingEntity.class.getSimpleName() + " where user = :user").setParameter("user", user).executeUpdate();
em.createQuery("delete from " + SocialLinkEntity.class.getSimpleName() + " where user = :user").setParameter("user", user).executeUpdate();
if (user.getAuthenticationLink() != null) {
em.remove(user.getAuthenticationLink());
for (AuthenticationLinkEntity l : user.getAuthenticationLink()) {
em.remove(l);
}
}
em.remove(user);
}

View file

@ -353,6 +353,7 @@ public class RealmAdapter implements RealmModel {
public void addRequiredCredential(RequiredCredentialModel model) {
RequiredCredentialEntity entity = new RequiredCredentialEntity();
entity.setRealm(realm);
entity.setInput(model.isInput());
entity.setSecret(model.isSecret());
entity.setType(model.getType());
@ -548,6 +549,7 @@ public class RealmAdapter implements RealmModel {
}
em.remove(applicationEntity);
em.createQuery("delete from " + ScopeMappingEntity.class.getSimpleName() + " where client = :client").setParameter("client", applicationEntity).executeUpdate();
em.flush();
return true;
}
@ -701,6 +703,7 @@ public class RealmAdapter implements RealmModel {
int counter = 1;
for (AuthenticationProviderModel model : authenticationProviders) {
AuthenticationProviderEntity entity = new AuthenticationProviderEntity();
entity.setRealm(realm);
entity.setProviderName(model.getProviderName());
entity.setPasswordUpdateSupported(model.isPasswordUpdateSupported());
entity.setConfig(model.getConfig());
@ -716,6 +719,8 @@ public class RealmAdapter implements RealmModel {
em.remove(apToRemove);
}
em.flush();
// Now create all new providers
for (AuthenticationProviderEntity apToAdd : newEntities) {
existing.add(apToAdd);

View file

@ -21,9 +21,11 @@ import org.keycloak.models.utils.Pbkdf2PasswordEncoder;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -383,8 +385,12 @@ public class UserAdapter implements UserModel {
@Override
public AuthenticationLinkModel getAuthenticationLink() {
AuthenticationLinkEntity authLinkEntity = user.getAuthenticationLink();
return authLinkEntity == null ? null : new AuthenticationLinkModel(authLinkEntity.getAuthProvider(), authLinkEntity.getAuthUserId());
Collection<AuthenticationLinkEntity> col = user.getAuthenticationLink();
if (col == null || col.isEmpty()) {
return null;
}
AuthenticationLinkEntity authLinkEntity = col.iterator().next();
return new AuthenticationLinkModel(authLinkEntity.getAuthProvider(), authLinkEntity.getAuthUserId());
}
@Override
@ -394,15 +400,16 @@ public class UserAdapter implements UserModel {
entity.setAuthUserId(authenticationLink.getAuthUserId());
entity.setUser(user);
if (user.getAuthenticationLink() != null) {
AuthenticationLinkEntity old = user.getAuthenticationLink();
old.setUser(null);
if (user.getAuthenticationLink() == null) {
user.setAuthenticationLink(new LinkedList<AuthenticationLinkEntity>());
} else if (!user.getAuthenticationLink().isEmpty()) {
AuthenticationLinkEntity old = user.getAuthenticationLink().iterator().next();
user.getAuthenticationLink().clear();
em.remove(old);
user.setAuthenticationLink(null);
em.flush();
}
user.getAuthenticationLink().add(entity);
em.persist(entity);
user.setAuthenticationLink(entity);
em.flush();
}

View file

@ -1,17 +1,15 @@
package org.keycloak.models.jpa.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.OneToMany;
import java.io.Serializable;
/**
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
@ -20,31 +18,21 @@ import org.hibernate.annotations.GenericGenerator;
@NamedQuery(name="deleteAuthenticationLinksByRealm", query="delete from AuthenticationLinkEntity authLink where authLink.user IN (select u from UserEntity u where realm=:realm)")
})
@Entity
@IdClass(AuthenticationLinkEntity.Key.class)
public class AuthenticationLinkEntity {
@Id
@GeneratedValue
protected long id;
protected String authProvider;
protected String authUserId;
// NOTE: @OnetoOne creates a constraint race condition if the join column is on AuthenticationLinkEntity.
// The race is that user gets loaded concurrently, creates link concurrently, and sets it. Therefore, we have
// a @ManyToOne on both sides. Broken yes, but, I think we're going to replace AuthenticationLinkEntity anyways.
@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="userId")
protected UserEntity user;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getAuthProvider() {
return authProvider;
}
@ -68,4 +56,48 @@ public class AuthenticationLinkEntity {
public void setUser(UserEntity user) {
this.user = user;
}
public static class Key implements Serializable {
protected UserEntity user;
protected String authProvider;
public Key() {
}
public Key(UserEntity user, String authProvider) {
this.user = user;
this.authProvider = authProvider;
}
public UserEntity getUser() {
return user;
}
public String getAuthProvider() {
return authProvider;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Key key = (Key) o;
if (authProvider != null ? !authProvider.equals(key.authProvider) : key.authProvider != null) return false;
if (user != null ? !user.getId().equals(key.user != null ? key.user.getId() : null) : key.user != null) return false;
return true;
}
@Override
public int hashCode() {
int result = user != null ? user.getId().hashCode() : 0;
result = 31 * result + (authProvider != null ? authProvider.hashCode() : 0);
return result;
}
}
}

View file

@ -1,16 +1,17 @@
package org.keycloak.models.jpa.entities;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MapKeyColumn;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Map;
/**
@ -18,12 +19,14 @@ import java.util.Map;
*/
@Entity
@Table(name="AuthProviderEntity")
@IdClass(AuthenticationProviderEntity.Key.class)
public class AuthenticationProviderEntity {
@Id
@GeneratedValue
protected long id;
@ManyToOne(fetch = FetchType.LAZY)
protected RealmEntity realm;
@Id
private String providerName;
private boolean passwordUpdateSupported;
private int priority;
@ -31,17 +34,15 @@ public class AuthenticationProviderEntity {
@ElementCollection
@MapKeyColumn(name="name")
@Column(name="value")
@CollectionTable(name="AuthProviderEntity_cfg", joinColumns = {
@JoinColumn(name = "AuthProviderEntity_id")
})
@CollectionTable(name="AuthProviderEntity_cfg")
private Map<String, String> config;
public long getId() {
return id;
public RealmEntity getRealm() {
return realm;
}
public void setId(long id) {
this.id = id;
public void setRealm(RealmEntity realm) {
this.realm = realm;
}
public String getProviderName() {
@ -75,4 +76,48 @@ public class AuthenticationProviderEntity {
public void setConfig(Map<String, String> config) {
this.config = config;
}
public static class Key implements Serializable {
protected RealmEntity realm;
protected String providerName;
public Key() {
}
public Key(RealmEntity realm, String providerName) {
this.realm = realm;
this.providerName = providerName;
}
public RealmEntity getRealm() {
return realm;
}
public String getProviderName() {
return providerName;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Key key = (Key) o;
if (providerName != null ? !providerName.equals(key.providerName) : key.providerName != null) return false;
if (realm != null ? !realm.getId().equals(key.realm != null ? key.realm.getId() : null) : key.realm != null) return false;
return true;
}
@Override
public int hashCode() {
int result = realm != null ? realm.getId().hashCode() : 0;
result = 31 * result + (providerName != null ? providerName.hashCode() : 0);
return result;
}
}
}

View file

@ -1,16 +1,14 @@
package org.keycloak.models.jpa.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import org.hibernate.annotations.GenericGenerator;
import java.io.Serializable;
/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
@ -22,29 +20,21 @@ import org.hibernate.annotations.GenericGenerator;
})
@Entity
@IdClass(CredentialEntity.Key.class)
public class CredentialEntity {
@Id
@GeneratedValue
protected long id;
@Id
protected String type;
protected String value;
protected String device;
protected byte[] salt;
protected int hashIterations;
@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="userId")
protected UserEntity user;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getValue() {
return value;
}
@ -92,4 +82,48 @@ public class CredentialEntity {
public void setHashIterations(int hashIterations) {
this.hashIterations = hashIterations;
}
public static class Key implements Serializable {
protected UserEntity user;
protected String type;
public Key() {
}
public Key(UserEntity user, String type) {
this.user = user;
this.type = type;
}
public UserEntity getUser() {
return user;
}
public String getType() {
return type;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Key key = (Key) o;
if (type != null ? !type.equals(key.type) : key.type != null) return false;
if (user != null ? !user.getId().equals(key.user != null ? key.user.getId() : null) : key.user != null) return false;
return true;
}
@Override
public int hashCode() {
int result = user != null ? user.getId().hashCode() : 0;
result = 31 * result + (type != null ? type.hashCode() : 0);
return result;
}
}
}

View file

@ -1,33 +1,38 @@
package org.keycloak.models.jpa.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.FetchType;
import javax.persistence.Id;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import java.io.Serializable;
/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
*/
@Entity
@IdClass(RequiredCredentialEntity.Key.class)
public class RequiredCredentialEntity {
@Id
@GeneratedValue
protected long id;
@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "realm")
protected RealmEntity realm;
@Id
protected String type;
protected boolean input;
protected boolean secret;
protected String formLabel;
public long getId() {
return id;
public RealmEntity getRealm() {
return realm;
}
public void setId(long id) {
this.id = id;
public void setRealm(RealmEntity realm) {
this.realm = realm;
}
public String getType() {
@ -61,4 +66,48 @@ public class RequiredCredentialEntity {
public void setFormLabel(String formLabel) {
this.formLabel = formLabel;
}
public static class Key implements Serializable {
protected RealmEntity realm;
protected String type;
public Key() {
}
public Key(RealmEntity realm, String type) {
this.realm = realm;
this.type = type;
}
public RealmEntity getRealm() {
return realm;
}
public String getType() {
return type;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Key key = (Key) o;
if (realm != null ? !realm.getId().equals(key.realm != null ? key.realm.getId() : null) : key.realm != null) return false;
if (type != null ? !type.equals(key.type) : key.type != null) return false;
return true;
}
@Override
public int hashCode() {
int result = realm != null ? realm.getId().hashCode() : 0;
result = 31 * result + (type != null ? type.hashCode() : 0);
return result;
}
}
}

View file

@ -1,16 +1,14 @@
package org.keycloak.models.jpa.entities;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import java.io.Serializable;
/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
@ -22,26 +20,18 @@ import javax.persistence.NamedQuery;
@NamedQuery(name="clientScopeMappingIds", query="select m.role.id from ScopeMappingEntity m where m.client = :client")
})
@Entity
@IdClass(ScopeMappingEntity.Key.class)
public class ScopeMappingEntity {
@Id
@GeneratedValue
protected long id;
@Id
@ManyToOne(fetch= FetchType.LAZY)
protected ClientEntity client;
@Id
@ManyToOne(fetch= FetchType.LAZY)
@JoinColumn(name="roleId")
protected RoleEntity role;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public ClientEntity getClient() {
return client;
}
@ -58,4 +48,47 @@ public class ScopeMappingEntity {
this.role = role;
}
public static class Key implements Serializable {
protected ClientEntity client;
protected RoleEntity role;
public Key() {
}
public Key(ClientEntity client, RoleEntity role) {
this.client = client;
this.role = role;
}
public ClientEntity getClient() {
return client;
}
public RoleEntity getRole() {
return role;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Key key = (Key) o;
if (client != null ? !client.getId().equals(key.client != null ? key.client.getId() : null) : key.client != null) return false;
if (role != null ? !role.getId().equals(key.role != null ? key.role.getId() : null) : key.role != null) return false;
return true;
}
@Override
public int hashCode() {
int result = client != null ? client.getId().hashCode() : 0;
result = 31 * result + (role != null ? role.getId().hashCode() : 0);
return result;
}
}
}

View file

@ -1,16 +1,13 @@
package org.keycloak.models.jpa.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.IdClass;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import org.hibernate.annotations.GenericGenerator;
import java.io.Serializable;
/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
@ -23,29 +20,21 @@ import org.hibernate.annotations.GenericGenerator;
@NamedQuery(name="deleteSocialLinkByRealm", query="delete from SocialLinkEntity social where social.user IN (select u from UserEntity u where realm=:realm)")
})
@Entity
@IdClass(SocialLinkEntity.Key.class)
public class SocialLinkEntity {
@Id
@GeneratedValue
protected long id;
@Id
@ManyToOne(fetch = FetchType.LAZY)
private UserEntity user;
@ManyToOne(fetch = FetchType.LAZY)
protected RealmEntity realm;
@Id
protected String socialProvider;
protected String socialUserId;
protected String socialUsername;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public UserEntity getUser() {
return user;
}
@ -85,4 +74,49 @@ public class SocialLinkEntity {
public void setRealm(RealmEntity realm) {
this.realm = realm;
}
public static class Key implements Serializable {
protected UserEntity user;
protected String socialProvider;
public Key() {
}
public Key(UserEntity user, String socialProvider) {
this.user = user;
this.socialProvider = socialProvider;
}
public UserEntity getUser() {
return user;
}
public String getSocialProvider() {
return socialProvider;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Key key = (Key) o;
if (socialProvider != null ? !socialProvider.equals(key.socialProvider) : key.socialProvider != null)
return false;
if (user != null ? !user.getId().equals(key.user != null ? key.user.getId() : null) : key.user != null) return false;
return true;
}
@Override
public int hashCode() {
int result = user != null ? user.getId().hashCode() : 0;
result = 31 * result + (socialProvider != null ? socialProvider.hashCode() : 0);
return result;
}
}
}

View file

@ -1,13 +1,10 @@
package org.keycloak.models.jpa.entities;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.IdClass;
import javax.persistence.ManyToOne;
import javax.persistence.MapsId;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import java.io.Serializable;
@ -20,25 +17,17 @@ import java.io.Serializable;
@NamedQuery(name="deleteUserAttributesByRealm", query="delete from UserAttributeEntity attr where attr.user IN (select u from UserEntity u where realm=:realm)")
})
@Entity
@IdClass(UserAttributeEntity.Key.class)
public class UserAttributeEntity {
@Id
@GeneratedValue
protected long id;
@Id
@ManyToOne(fetch= FetchType.LAZY)
protected UserEntity user;
@Id
protected String name;
protected String value;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
@ -63,4 +52,47 @@ public class UserAttributeEntity {
this.user = user;
}
public static class Key implements Serializable {
protected UserEntity user;
protected String name;
public Key() {
}
public Key(UserEntity user, String name) {
this.user = user;
this.name = name;
}
public UserEntity getUser() {
return user;
}
public String getName() {
return name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Key key = (Key) o;
if (name != null ? !name.equals(key.name) : key.name != null) return false;
if (user != null ? !user.getId().equals(key.user != null ? key.user.getId() : null) : key.user != null) return false;
return true;
}
@Override
public int hashCode() {
int result = user != null ? user.getId().hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}
}
}

View file

@ -72,9 +72,8 @@ public class UserEntity {
@OneToMany(cascade = CascadeType.REMOVE, orphanRemoval = true, mappedBy="user")
protected Collection<CredentialEntity> credentials = new ArrayList<CredentialEntity>();
@ManyToOne
@JoinColumn(name="link_id")
protected AuthenticationLinkEntity authenticationLink;
@OneToMany(cascade = CascadeType.REMOVE, orphanRemoval = true, mappedBy="user")
protected Collection<AuthenticationLinkEntity> authenticationLink;
public String getId() {
return id;
@ -181,11 +180,11 @@ public class UserEntity {
this.credentials = credentials;
}
public AuthenticationLinkEntity getAuthenticationLink() {
public Collection<AuthenticationLinkEntity> getAuthenticationLink() {
return authenticationLink;
}
public void setAuthenticationLink(AuthenticationLinkEntity authenticationLink) {
public void setAuthenticationLink(Collection<AuthenticationLinkEntity> authenticationLink) {
this.authenticationLink = authenticationLink;
}

View file

@ -4,12 +4,13 @@ import org.keycloak.models.UserModel;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import java.io.Serializable;
/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
@ -19,21 +20,17 @@ import javax.persistence.NamedQuery;
@NamedQuery(name="deleteUserRequiredActionsByRealm", query="delete from UserRequiredActionEntity action where action.user IN (select u from UserEntity u where realm=:realm)")
})
@Entity
@IdClass(UserRequiredActionEntity.Key.class)
public class UserRequiredActionEntity {
@Id
@GeneratedValue
protected long id;
@Id
@ManyToOne(fetch= FetchType.LAZY)
@JoinColumn(name="userId")
protected UserEntity user;
@Id
protected UserModel.RequiredAction action;
public long getId() {
return id;
}
public UserModel.RequiredAction getAction() {
return action;
}
@ -50,4 +47,47 @@ public class UserRequiredActionEntity {
this.user = user;
}
public static class Key implements Serializable {
protected UserEntity user;
protected UserModel.RequiredAction action;
public Key() {
}
public Key(UserEntity user, UserModel.RequiredAction action) {
this.user = user;
this.action = action;
}
public UserEntity getUser() {
return user;
}
public UserModel.RequiredAction getAction() {
return action;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Key key = (Key) o;
if (action != key.action) return false;
if (user != null ? !user.getId().equals(key.user != null ? key.user.getId() : null) : key.user != null) return false;
return true;
}
@Override
public int hashCode() {
int result = user != null ? user.getId().hashCode() : 0;
result = 31 * result + (action != null ? action.hashCode() : 0);
return result;
}
}
}

View file

@ -1,16 +1,14 @@
package org.keycloak.models.jpa.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import org.hibernate.annotations.GenericGenerator;
import java.io.Serializable;
/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
@ -24,27 +22,19 @@ import org.hibernate.annotations.GenericGenerator;
})
@Entity
@IdClass(UserRoleMappingEntity.Key.class)
public class UserRoleMappingEntity {
@Id
@GeneratedValue
protected long id;
@Id
@ManyToOne(fetch= FetchType.LAZY)
@JoinColumn(name="userId")
protected UserEntity user;
@Id
@ManyToOne(fetch= FetchType.LAZY)
@JoinColumn(name="roleId")
protected RoleEntity role;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public UserEntity getUser() {
return user;
}
@ -61,4 +51,47 @@ public class UserRoleMappingEntity {
this.role = role;
}
public static class Key implements Serializable {
protected UserEntity user;
protected RoleEntity role;
public Key() {
}
public Key(UserEntity user, RoleEntity role) {
this.user = user;
this.role = role;
}
public UserEntity getUser() {
return user;
}
public RoleEntity getRole() {
return role;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Key key = (Key) o;
if (role != null ? !role.getId().equals(key.role != null ? key.role.getId() : null) : key.role != null) return false;
if (user != null ? !user.getId().equals(key.user != null ? key.user.getId() : null) : key.user != null) return false;
return true;
}
@Override
public int hashCode() {
int result = user != null ? user.getId().hashCode() : 0;
result = 31 * result + (role != null ? role.getId().hashCode() : 0);
return result;
}
}
}

View file

@ -1,19 +0,0 @@
package org.keycloak.models.jpa.utils;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.id.IdentifierGenerator;
import org.keycloak.models.utils.KeycloakModelUtils;
import java.io.Serializable;
/**
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/
public class JpaIdGenerator implements IdentifierGenerator {
@Override
public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
return KeycloakModelUtils.generateId();
}
}

View file

@ -9,6 +9,7 @@ import org.keycloak.models.UserSessionProvider;
import org.keycloak.models.UsernameLoginFailureModel;
import org.keycloak.models.sessions.jpa.entities.UserSessionEntity;
import org.keycloak.models.sessions.jpa.entities.UsernameLoginFailureEntity;
import org.keycloak.models.utils.KeycloakModelUtils;
import org.keycloak.util.Time;
import javax.persistence.EntityManager;
@ -64,6 +65,7 @@ public class JpaUserSessionProvider implements UserSessionProvider {
@Override
public UserSessionModel createUserSession(RealmModel realm, UserModel user, String ipAddress) {
UserSessionEntity entity = new UserSessionEntity();
entity.setId(KeycloakModelUtils.generateId());
entity.setRealmId(realm.getId());
entity.setUserId(user.getId());
entity.setIpAddress(ipAddress);

View file

@ -79,7 +79,7 @@ public class ClientUserSessionAssociationEntity {
Key key = (Key) o;
if (clientId != null ? !clientId.equals(key.clientId) : key.clientId != null) return false;
if (session != null ? !session.equals(key.session) : key.session != null) return false;
if (session != null ? !session.getId().equals(key.session != null ? key.session.getId() : null) : key.session != null) return false;
return true;
}
@ -87,7 +87,7 @@ public class ClientUserSessionAssociationEntity {
@Override
public int hashCode() {
int result = clientId != null ? clientId.hashCode() : 0;
result = 31 * result + (session != null ? session.hashCode() : 0);
result = 31 * result + (session != null ? session.getId().hashCode() : 0);
return result;
}
}

View file

@ -3,6 +3,7 @@ package org.keycloak.models.sessions.jpa.entities;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
@ -28,8 +29,7 @@ import java.util.Collection;
public class UserSessionEntity {
@Id
@GenericGenerator(name="uuid_generator", strategy="org.keycloak.models.sessions.jpa.utils.JpaIdGenerator")
@GeneratedValue(generator = "uuid_generator")
@Column(length = 36)
protected String id;
protected String userId;

View file

@ -104,6 +104,25 @@ public class UsernameLoginFailureEntity {
return username;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Key key = (Key) o;
if (realmId != null ? !realmId.equals(key.realmId) : key.realmId != null) return false;
if (username != null ? !username.equals(key.username) : key.username != null) return false;
return true;
}
@Override
public int hashCode() {
int result = realmId != null ? realmId.hashCode() : 0;
result = 31 * result + (username != null ? username.hashCode() : 0);
return result;
}
}
}

View file

@ -1,19 +0,0 @@
package org.keycloak.models.sessions.jpa.utils;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.id.IdentifierGenerator;
import org.keycloak.models.utils.KeycloakModelUtils;
import java.io.Serializable;
/**
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/
public class JpaIdGenerator implements IdentifierGenerator {
@Override
public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
return KeycloakModelUtils.generateId();
}
}