Merge pull request #536 from patriot1burke/master

realmId and rroleId
This commit is contained in:
Bill Burke 2014-07-17 16:34:27 -04:00
commit 14af840c3a
18 changed files with 246 additions and 186 deletions

View file

@ -145,8 +145,8 @@ public class ApplicationAdapter extends ClientAdapter implements ApplicationMode
applicationEntity.getRoles().remove(role);
applicationEntity.getDefaultRoles().remove(role);
em.createNativeQuery("delete from CompositeRole where childRole = :role").setParameter("role", role).executeUpdate();
em.createQuery("delete from " + ScopeMappingEntity.class.getSimpleName() + " where role = :role").setParameter("role", role).executeUpdate();
em.createNativeQuery("delete from COMPOSITE_ROLE where CHILD_ROLE = :role").setParameter("role", role).executeUpdate();
em.createNamedQuery("deleteScopeMappingByRole").setParameter("role", role).executeUpdate();
role.setApplication(null);
em.flush();
em.remove(role);

View file

@ -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<UserEntity> 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<UserEntity> results = query.getResultList();
if (results.size() == 0) return false;
removeUser(results.get(0));
@ -88,8 +82,8 @@ public class JpaUserProvider implements UserProvider {
}
private void removeUser(UserEntity user) {
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();
em.createNamedQuery("deleteUserRoleMappingsByUser").setParameter("user", user).executeUpdate();
em.createNamedQuery("deleteSocialLinkByUser").setParameter("user", user).executeUpdate();
if (user.getAuthenticationLink() != null) {
for (AuthenticationLinkEntity l : user.getAuthenticationLink()) {
em.remove(l);
@ -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,57 +121,52 @@ 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
public void preRemove(RoleModel role) {
RoleEntity roleEntity = em.getReference(RoleEntity.class, role.getId());
em.createQuery("delete from " + UserRoleMappingEntity.class.getSimpleName() + " where role = :role").setParameter("role", roleEntity).executeUpdate();
em.createNamedQuery("deleteUserRoleMappingsByRole").setParameter("roleId", role.getId()).executeUpdate();
}
@Override
public UserModel getUserById(String id, RealmModel realmModel) {
public UserModel getUserById(String id, RealmModel realm) {
TypedQuery<UserEntity> 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<UserEntity> 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<UserEntity> 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<UserEntity> 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<UserEntity> 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<UserEntity> 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
@ -188,8 +176,7 @@ public class JpaUserProvider implements UserProvider {
@Override
public UserModel getUserBySocialLink(SocialLinkModel socialLink, RealmModel realm) {
TypedQuery<UserEntity> 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<UserEntity> results = query.getResultList();
@ -211,20 +198,17 @@ 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.createQuery("select count(u) from UserEntity u where u.realm = :realm")
.setParameter("realm", realmEntity)
Object count = em.createNamedQuery("getRealmUserCount")
.setParameter("realmId", realm.getId())
.getSingleResult();
return ((Number)count).intValue();
}
@Override
public List<UserModel> getUsers(RealmModel realm, int firstResult, int maxResults) {
TypedQuery<UserEntity> query = em.createQuery("select u from UserEntity u where u.realm = :realm order by u.username", UserEntity.class);
RealmEntity realmEntity = em.getReference(RealmEntity.class, realm.getId());
query.setParameter("realm", realmEntity);
TypedQuery<UserEntity> query = em.createNamedQuery("getAllUsersByRealm", UserEntity.class);
query.setParameter("realmId", realm.getId());
if (firstResult != -1) {
query.setFirstResult(firstResult);
}
@ -244,9 +228,8 @@ public class JpaUserProvider implements UserProvider {
@Override
public List<UserModel> searchForUser(String search, RealmModel realm, int firstResult, int maxResults) {
TypedQuery<UserEntity> query = em.createQuery("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", UserEntity.class);
RealmEntity realmEntity = em.getReference(RealmEntity.class, realm.getId());
query.setParameter("realm", realmEntity);
TypedQuery<UserEntity> query = em.createNamedQuery("searchForUser", UserEntity.class);
query.setParameter("realmId", realm.getId());
query.setParameter("search", "%" + search.toLowerCase() + "%");
if (firstResult != -1) {
query.setFirstResult(firstResult);
@ -292,8 +275,7 @@ public class JpaUserProvider implements UserProvider {
builder.append(" order by u.username");
String q = builder.toString();
TypedQuery<UserEntity> 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);
}

View file

@ -548,7 +548,7 @@ public class RealmAdapter implements RealmModel {
return false;
}
em.remove(applicationEntity);
em.createQuery("delete from " + ScopeMappingEntity.class.getSimpleName() + " where client = :client").setParameter("client", applicationEntity).executeUpdate();
em.createNamedQuery("deleteScopeMappingByClient").setParameter("client", applicationEntity).executeUpdate();
em.flush();
return true;
@ -608,7 +608,7 @@ public class RealmAdapter implements RealmModel {
OAuthClientModel oauth = getOAuthClientById(id);
if (oauth == null) return false;
OAuthClientEntity client = em.getReference(OAuthClientEntity.class, oauth.getId());
em.createQuery("delete from " + ScopeMappingEntity.class.getSimpleName() + " where client = :client").setParameter("client", client).executeUpdate();
em.createNamedQuery("deleteScopeMappingByClient").setParameter("client", client).executeUpdate();
em.remove(client);
return true;
}
@ -769,8 +769,8 @@ public class RealmAdapter implements RealmModel {
realm.getRoles().remove(role);
realm.getDefaultRoles().remove(role);
em.createNativeQuery("delete from CompositeRole where childRole = :role").setParameter("role", roleEntity).executeUpdate();
em.createQuery("delete from " + ScopeMappingEntity.class.getSimpleName() + " where role = :role").setParameter("role", roleEntity).executeUpdate();
em.createNativeQuery("delete from COMPOSITE_ROLE where CHILD_ROLE = :role").setParameter("role", roleEntity).executeUpdate();
em.createNamedQuery("deleteScopeMappingByRole").setParameter("role", roleEntity).executeUpdate();
em.remove(roleEntity);

View file

@ -11,11 +11,11 @@ import org.keycloak.models.UserCredentialValueModel;
import org.keycloak.models.UserModel;
import org.keycloak.models.jpa.entities.AuthenticationLinkEntity;
import org.keycloak.models.jpa.entities.CredentialEntity;
import org.keycloak.models.jpa.entities.RoleEntity;
import org.keycloak.models.jpa.entities.UserAttributeEntity;
import org.keycloak.models.jpa.entities.UserEntity;
import org.keycloak.models.jpa.entities.UserRequiredActionEntity;
import org.keycloak.models.jpa.entities.UserRoleMappingEntity;
import org.keycloak.models.utils.KeycloakModelUtils;
import org.keycloak.models.utils.Pbkdf2PasswordEncoder;
import javax.persistence.EntityManager;
@ -216,6 +216,7 @@ public class UserAdapter implements UserModel {
if (credentialEntity == null) {
credentialEntity = new CredentialEntity();
credentialEntity.setId(KeycloakModelUtils.generateId());
credentialEntity.setType(cred.getType());
credentialEntity.setDevice(cred.getDevice());
credentialEntity.setUser(user);
@ -277,6 +278,7 @@ public class UserAdapter implements UserModel {
if (credentialEntity == null) {
credentialEntity = new CredentialEntity();
credentialEntity.setId(KeycloakModelUtils.generateId());
credentialEntity.setType(credModel.getType());
credentialEntity.setUser(user);
em.persist(credentialEntity);
@ -305,8 +307,7 @@ public class UserAdapter implements UserModel {
protected TypedQuery<UserRoleMappingEntity> getUserRoleMappingEntityTypedQuery(RoleModel role) {
TypedQuery<UserRoleMappingEntity> query = em.createNamedQuery("userHasRole", UserRoleMappingEntity.class);
query.setParameter("user", getUser());
RoleEntity roleEntity = em.getReference(RoleEntity.class, role.getId());
query.setParameter("role", roleEntity);
query.setParameter("roleId", role.getId());
return query;
}
@ -315,8 +316,7 @@ public class UserAdapter implements UserModel {
if (hasRole(role)) return;
UserRoleMappingEntity entity = new UserRoleMappingEntity();
entity.setUser(getUser());
RoleEntity roleEntity = em.getReference(RoleEntity.class, role.getId());
entity.setRole(roleEntity);
entity.setRoleId(role.getId());
em.persist(entity);
em.flush();
em.detach(entity);

View file

@ -1,10 +1,12 @@
package org.keycloak.models.jpa.entities;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.Collection;
@ -15,16 +17,23 @@ import java.util.Collection;
@Entity
public class ApplicationEntity extends ClientEntity {
@Column(name="SURROGATE_AUTH_REQUIRED")
private boolean surrogateAuthRequired;
@Column(name="BASE_URL")
private String baseUrl;
@Column(name="MANAGEMENT_URL")
private String managementUrl;
@Column(name="BEARER_ONLY")
private boolean bearerOnly;
@OneToMany(fetch = FetchType.EAGER, cascade ={CascadeType.REMOVE}, orphanRemoval = true, mappedBy = "application")
Collection<RoleEntity> roles = new ArrayList<RoleEntity>();
@OneToMany(fetch = FetchType.LAZY, cascade ={CascadeType.REMOVE}, orphanRemoval = true)
@JoinTable(name="ApplicationDefaultRoles")
@JoinTable(name="APPLICATION_DEFAULT_ROLES")
Collection<RoleEntity> defaultRoles = new ArrayList<RoleEntity>();
public boolean isSurrogateAuthRequired() {

View file

@ -1,5 +1,6 @@
package org.keycloak.models.jpa.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
@ -9,20 +10,24 @@ import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.io.Serializable;
/**
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/
@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)")
})
@Table(name="AUTHENTICATION_LINK")
@Entity
@IdClass(AuthenticationLinkEntity.Key.class)
public class AuthenticationLinkEntity {
@Id
@Column(name="AUTH_PROVIDER")
protected String authProvider;
@Column(name="AUTH_USER_ID")
protected String authUserId;
// NOTE: @OnetoOne creates a constraint race condition if the join column is on AuthenticationLinkEntity.
@ -30,7 +35,7 @@ public class AuthenticationLinkEntity {
// 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")
@JoinColumn(name="USER_ID")
protected UserEntity user;
public String getAuthProvider() {

View file

@ -18,23 +18,27 @@ import java.util.Map;
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/
@Entity
@Table(name="AuthProviderEntity")
@Table(name="AUTH_PROVIDER_ENTITY")
@IdClass(AuthenticationProviderEntity.Key.class)
public class AuthenticationProviderEntity {
@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "REALM_ID")
protected RealmEntity realm;
@Id
@Column(name="PROVIDER_NAME")
private String providerName;
@Column(name="PASSWORD_UPDATE_SUPPORTED")
private boolean passwordUpdateSupported;
@Column(name="PRIORITY")
private int priority;
@ElementCollection
@MapKeyColumn(name="name")
@Column(name="value")
@CollectionTable(name="AuthProviderEntity_cfg")
@CollectionTable(name="AUTH_PROVIDER_CONFIG")
private Map<String, String> config;
public RealmEntity getRealm() {

View file

@ -21,28 +21,34 @@ import java.util.Set;
*/
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(uniqueConstraints = {@UniqueConstraint(columnNames = {"realm", "name"})})
@Table(name="CLIENT", uniqueConstraints = {@UniqueConstraint(columnNames = {"REALM_ID", "NAME"})})
public abstract class ClientEntity {
@Id
@Column(length = 36)
@Column(name="ID", length = 36)
private String id;
@Column(name = "name")
@Column(name = "NAME")
private String name;
@Column(name="ENABLED")
private boolean enabled;
@Column(name="SECRET")
private String secret;
@Column(name="ALLOWED_CLAIMS_MASK")
private long allowedClaimsMask;
@Column(name="NOT_BEFORE")
private int notBefore;
@Column(name="PUBLIC_CLIENT")
private boolean publicClient;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "realm")
@JoinColumn(name = "REALM_ID")
protected RealmEntity realm;
@ElementCollection
@CollectionTable
@CollectionTable(name = "WEB_ORIGINS")
protected Set<String> webOrigins = new HashSet<String>();
@ElementCollection
@CollectionTable
@CollectionTable(name = "REDIRECT_URIS")
protected Set<String> redirectUris = new HashSet<String>();
public RealmEntity getRealm() {

View file

@ -1,5 +1,6 @@
package org.keycloak.models.jpa.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
@ -8,6 +9,7 @@ import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import java.io.Serializable;
/**
@ -16,25 +18,39 @@ 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)")
})
@Table(name="CREDENTIAL")
@Entity
@IdClass(CredentialEntity.Key.class)
public class CredentialEntity {
@Id
@Column(length = 36)
protected String id;
@Column(name="TYPE")
protected String type;
@Column(name="VALUE")
protected String value;
@Column(name="DEVICE")
protected String device;
@Column(name="SALT")
protected byte[] salt;
@Column(name="HASH_ITERATIONS")
protected int hashIterations;
@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="userId")
@JoinColumn(name="USER_ID")
protected UserEntity user;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getValue() {
return value;
}
@ -82,48 +98,4 @@ 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,6 +1,8 @@
package org.keycloak.models.jpa.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
@ -15,6 +17,7 @@ import javax.persistence.NamedQuery;
})
@Entity
public class OAuthClientEntity extends ClientEntity {
@Column(name="DIRECT_GRANTS_ONLY")
protected boolean directGrantsOnly;
public boolean isDirectGrantsOnly() {

View file

@ -8,12 +8,14 @@ import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.MapKeyColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
@ -26,6 +28,7 @@ import java.util.Set;
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
*/
@Table(name="REALM")
@Entity
@NamedQueries({
@NamedQuery(name="getAllRealms", query="select realm from RealmEntity realm"),
@ -33,59 +36,85 @@ import java.util.Set;
})
public class RealmEntity {
@Id
@Column(length = 36)
@Column(name="ID", length = 36)
protected String id;
@Column(unique = true)
@Column(name="NAME", unique = true)
protected String name;
@Column(name="ENABLED")
protected boolean enabled;
@Column(name="SSL_NOT_REQUIRED")
protected boolean sslNotRequired;
@Column(name="REGISTRATION_ALLOWED")
protected boolean registrationAllowed;
@Column(name="PASSWORD_CRED_GRANT_ALLOWED")
protected boolean passwordCredentialGrantAllowed;
@Column(name="VERIFY_EMAIL")
protected boolean verifyEmail;
@Column(name="RESET_PASSWORD_ALLOWED")
protected boolean resetPasswordAllowed;
@Column(name="SOCIAL")
protected boolean social;
@Column(name="REMEMBER_ME")
protected boolean rememberMe;
//--- brute force settings
@Column(name="BRUTE_FORCE_PROTECTED")
protected boolean bruteForceProtected;
@Column(name="MAX_FAILURE_WAIT")
protected int maxFailureWaitSeconds;
@Column(name="MINIMUM_QUICK_LOGIN_WAIT")
protected int minimumQuickLoginWaitSeconds;
@Column(name="WAIT_INCREMENT_SECONDS")
protected int waitIncrementSeconds;
@Column(name="QUICK_LOGIN_CHECK")
protected long quickLoginCheckMilliSeconds;
@Column(name="MAX_DELTA_TIME")
protected int maxDeltaTimeSeconds;
@Column(name="FAILURE_FACTOR")
protected int failureFactor;
//--- end brute force settings
@Column(name="updateProfileOnInitSocLogin")
@Column(name="UPDATE_PROFILE_ON_SOC_LOGIN")
protected boolean updateProfileOnInitialSocialLogin;
@Column(name="PASSWORD_POLICY")
protected String passwordPolicy;
@Column(name="SSO_IDLE_TIMEOUT")
private int ssoSessionIdleTimeout;
@Column(name="SSO_MAX_LIFESPAN")
private int ssoSessionMaxLifespan;
@Column(name="ACCESS_TOKEN_LIFESPAN")
protected int accessTokenLifespan;
@Column(name="ACCESS_CODE_LIFESPAN")
protected int accessCodeLifespan;
@Column(name="USER_ACTION_LIFESPAN")
protected int accessCodeLifespanUserAction;
@Column(name="NOT_BEFORE")
protected int notBefore;
@Column(length = 2048)
@Column(name="PUBLIC_KEY", length = 2048)
protected String publicKeyPem;
@Column(length = 2048)
@Column(name="PRIVATE_KEY", length = 2048)
protected String privateKeyPem;
@Column(name="LOGIN_THEME")
protected String loginTheme;
@Column(name="ACCOUNT_THEME")
protected String accountTheme;
@Column(name="ADMIN_THEME")
protected String adminTheme;
@Column(name="EMAIL_THEME")
protected String emailTheme;
@OneToMany(cascade ={CascadeType.REMOVE}, orphanRemoval = true)
@JoinTable(name="User_RequiredCreds")
@JoinTable(name="USER_REQUIRED_CREDS")
Collection<RequiredCredentialEntity> requiredCredentials = new ArrayList<RequiredCredentialEntity>();
@OneToMany(cascade ={CascadeType.REMOVE}, orphanRemoval = true)
@JoinTable(name="AuthProviders")
@JoinTable(name="AUTH_PROVIDERS")
List<AuthenticationProviderEntity> authenticationProviders = new ArrayList<AuthenticationProviderEntity>();
@OneToMany(fetch = FetchType.LAZY, cascade ={CascadeType.REMOVE}, orphanRemoval = true)
@ -97,32 +126,36 @@ public class RealmEntity {
@ElementCollection
@MapKeyColumn(name="name")
@Column(name="value")
@CollectionTable
@CollectionTable(name="REALM_SMTP_CONFIG")
protected Map<String, String> smtpConfig = new HashMap<String, String>();
@ElementCollection
@MapKeyColumn(name="name")
@Column(name="value")
@CollectionTable
@CollectionTable(name="REALM_SOCIAL_CONFIG")
protected Map<String, String> socialConfig = new HashMap<String, String>();
@ElementCollection
@MapKeyColumn(name="name")
@Column(name="value")
@CollectionTable
@CollectionTable(name="REALM_LDAP_CONFIG")
protected Map<String, String> ldapServerConfig = new HashMap<String, String>();
@OneToMany(fetch = FetchType.LAZY, cascade ={CascadeType.REMOVE}, orphanRemoval = true)
@JoinTable(name="RealmDefaultRoles")
@JoinTable(name="REALM_DEFAULT_ROLES")
protected Collection<RoleEntity> defaultRoles = new ArrayList<RoleEntity>();
@Column(name="AUDIT_ENABLED")
protected boolean auditEnabled;
@Column(name="AUDIT_EXPIRATION")
protected long auditExpiration;
@ElementCollection
@CollectionTable(name="REALM_AUDIT_LISTENERS")
protected Set<String> auditListeners= new HashSet<String>();
@OneToOne
@JoinColumn(name="MASTER_ADMIN_APP")
protected ApplicationEntity masterAdminApp;
public String getId() {

View file

@ -1,30 +1,37 @@
package org.keycloak.models.jpa.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import java.io.Serializable;
/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
*/
@Table(name="REALM_REQUIRED_CREDENTIAL")
@Entity
@IdClass(RequiredCredentialEntity.Key.class)
public class RequiredCredentialEntity {
@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "realm")
@JoinColumn(name = "REALM_ID")
protected RealmEntity realm;
@Id
@Column(name = "TYPE")
protected String type;
@Column(name = "INPUT")
protected boolean input;
@Column(name = "SECRET")
protected boolean secret;
@Column(name = "FORM_LABEL")
protected String formLabel;
public RealmEntity getRealm() {

View file

@ -20,8 +20,8 @@ import java.util.Collection;
* @version $Revision: 1 $
*/
@Entity
@Table(uniqueConstraints = {
@UniqueConstraint(columnNames = { "name", "appRealmConstraint" })
@Table(name="KEYCLOAK_ROLE", uniqueConstraints = {
@UniqueConstraint(columnNames = { "NAME", "APP_REALM_CONSTRAINT" })
})
@NamedQueries({
@NamedQuery(name="getAppRoleByName", query="select role from RoleEntity role where role.name = :name and role.application = :application"),
@ -33,29 +33,32 @@ public class RoleEntity {
@Column(name="id", length = 36)
private String id;
@Column(name = "NAME")
private String name;
@Column(name = "DESCRIPTION")
private String description;
// hax! couldn't get constraint to work properly
@Column(name = "REALM_ID")
private String realmId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "realm")
@JoinColumn(name = "REALM")
private RealmEntity realm;
@Column(name="applicationRole")
@Column(name="APPLICATION_ROLE")
private boolean applicationRole;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "application")
@JoinColumn(name = "APPLICATION")
private ApplicationEntity application;
// Hack to ensure that either name+application or name+realm are unique. Needed due to MS-SQL as it don't allow multiple NULL values in the column, which is part of constraint
@Column(length = 36)
@Column(name="APP_REALM_CONSTRAINT", length = 36)
private String appRealmConstraint;
@ManyToMany(fetch = FetchType.LAZY, cascade = {})
@JoinTable(name = "CompositeRole", joinColumns = @JoinColumn(name = "composite"), inverseJoinColumns = @JoinColumn(name = "childRole"))
@JoinTable(name = "COMPOSITE_ROLE", joinColumns = @JoinColumn(name = "COMPOSITE"), inverseJoinColumns = @JoinColumn(name = "CHILD_ROLE"))
private Collection<RoleEntity> compositeRoles = new ArrayList<RoleEntity>();
public String getId() {

View file

@ -1,5 +1,6 @@
package org.keycloak.models.jpa.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
@ -8,6 +9,7 @@ import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import java.io.Serializable;
/**
@ -17,19 +19,23 @@ import java.io.Serializable;
@NamedQueries({
@NamedQuery(name="hasScope", query="select m from ScopeMappingEntity m where m.client = :client and m.role = :role"),
@NamedQuery(name="clientScopeMappings", query="select m from ScopeMappingEntity m where m.client = :client"),
@NamedQuery(name="clientScopeMappingIds", query="select m.role.id from ScopeMappingEntity m where m.client = :client")
@NamedQuery(name="clientScopeMappingIds", query="select m.role.id from ScopeMappingEntity m where m.client = :client"),
@NamedQuery(name="deleteScopeMappingByRole", query="delete from ScopeMappingEntity where role = :role"),
@NamedQuery(name="deleteScopeMappingByClient", query="delete from ScopeMappingEntity where client = :client")
})
@Table(name="SCOPE_MAPPING")
@Entity
@IdClass(ScopeMappingEntity.Key.class)
public class ScopeMappingEntity {
@Id
@ManyToOne(fetch= FetchType.LAZY)
@JoinColumn(name = "CLIENT_ID")
protected ClientEntity client;
@Id
@ManyToOne(fetch= FetchType.LAZY)
@JoinColumn(name="roleId")
@JoinColumn(name="ROLE_ID")
protected RoleEntity role;
public ClientEntity getClient() {

View file

@ -1,12 +1,15 @@
package org.keycloak.models.jpa.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
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.Table;
import java.io.Serializable;
/**
@ -16,23 +19,29 @@ 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")
})
@Table(name="USER_SOCIAL_LINK")
@Entity
@IdClass(SocialLinkEntity.Key.class)
public class SocialLinkEntity {
@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "USER_ID")
private UserEntity user;
@ManyToOne(fetch = FetchType.LAZY)
protected RealmEntity realm;
@Column(name = "REALM_ID")
protected String realmId;
@Id
@Column(name = "SOCIAL_PROVIDER")
protected String socialProvider;
@Column(name = "SOCIAL_USER_ID")
protected String socialUserId;
@Column(name = "SOCIAL_USERNAME")
protected String socialUsername;
public UserEntity getUser() {
@ -67,12 +76,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 {

View file

@ -1,12 +1,15 @@
package org.keycloak.models.jpa.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
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.Table;
import java.io.Serializable;
/**
@ -14,18 +17,22 @@ 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)")
})
@Table(name="USER_ATTRIBUTE")
@Entity
@IdClass(UserAttributeEntity.Key.class)
public class UserAttributeEntity {
@Id
@ManyToOne(fetch= FetchType.LAZY)
@JoinColumn(name = "USER_ID")
protected UserEntity user;
@Id
@Column(name = "NAME")
protected String name;
@Column(name = "VALUE")
protected String value;
public String getName() {

View file

@ -31,37 +31,47 @@ import java.util.Set;
* @version $Revision: 1 $
*/
@NamedQueries({
@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="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" })
@Table(name="USER", uniqueConstraints = {
@UniqueConstraint(columnNames = { "REALM_ID", "USERNAME" }),
@UniqueConstraint(columnNames = { "REALM_ID", "EMAIL_CONSTRAINT" })
})
public class UserEntity {
@Id
@Column(length = 36)
@Column(name="ID", length = 36)
protected String id;
@Column(name = "USERNAME")
protected String username;
@Column(name = "FIRST_NAME")
protected String firstName;
@Column(name = "LAST_NAME")
protected String lastName;
@Column(name = "EMAIL")
protected String email;
@Column(name = "ENABLED")
protected boolean enabled;
@Column(name = "TOTP")
protected boolean totp;
@Column(name = "EMAIL_VERIFIED")
protected boolean emailVerified;
// 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)
@Column(name = "EMAIL_CONSTRAINT")
protected String emailConstraint = KeycloakModelUtils.generateId();
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "realm")
protected RealmEntity realm;
@Column(name = "REALM_ID")
protected String realmId;
@OneToMany(cascade = CascadeType.REMOVE, orphanRemoval = true, mappedBy="user")
protected Collection<UserAttributeEntity> attributes = new ArrayList<UserAttributeEntity>();
@ -164,12 +174,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<CredentialEntity> getCredentials() {

View file

@ -1,5 +1,6 @@
package org.keycloak.models.jpa.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
@ -8,6 +9,7 @@ import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import java.io.Serializable;
/**
@ -15,25 +17,27 @@ import java.io.Serializable;
* @version $Revision: 1 $
*/
@NamedQueries({
@NamedQuery(name="userHasRole", query="select m from UserRoleMappingEntity m where m.user = :user and m.role = :role"),
@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.role.id 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="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 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")
})
@Table(name="USER_ROLE_MAPPING")
@Entity
@IdClass(UserRoleMappingEntity.Key.class)
public class UserRoleMappingEntity {
@Id
@ManyToOne(fetch= FetchType.LAZY)
@JoinColumn(name="userId")
@JoinColumn(name="USER_ID")
protected UserEntity user;
@Id
@ManyToOne(fetch= FetchType.LAZY)
@JoinColumn(name="roleId")
protected RoleEntity role;
@Column(name = "ROLE_ID")
protected String roleId;
public UserEntity getUser() {
return user;
@ -43,34 +47,35 @@ public class UserRoleMappingEntity {
this.user = user;
}
public RoleEntity getRole() {
return role;
public String getRoleId() {
return roleId;
}
public void setRole(RoleEntity role) {
this.role = role;
public void setRoleId(String roleId) {
this.roleId = roleId;
}
public static class Key implements Serializable {
protected UserEntity user;
protected RoleEntity role;
protected String roleId;
public Key() {
}
public Key(UserEntity user, RoleEntity role) {
public Key(UserEntity user, String roleId) {
this.user = user;
this.role = role;
this.roleId = roleId;
}
public UserEntity getUser() {
return user;
}
public RoleEntity getRole() {
return role;
public String getRoleId() {
return roleId;
}
@Override
@ -80,18 +85,17 @@ public class UserRoleMappingEntity {
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;
if (!roleId.equals(key.roleId)) return false;
if (!user.equals(key.user)) 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);
int result = user.hashCode();
result = 31 * result + roleId.hashCode();
return result;
}
}
}