From 8d1ff1d49c0d734b29b32113bc47e59282cf6ab3 Mon Sep 17 00:00:00 2001 From: Bill Burke Date: Mon, 11 Aug 2014 12:33:17 -0400 Subject: [PATCH] add jpa realm attributes --- .../main/resources/META-INF/persistence.xml | 1 + .../org/keycloak/models/jpa/RealmAdapter.java | 103 ++++++++++++++--- .../models/jpa/entities/AttributeMap.java | 74 ++++++++++++ .../jpa/entities/RealmAttributeEntity.java | 105 ++++++++++++++++++ .../models/jpa/entities/RealmEntity.java | 85 ++------------ 5 files changed, 280 insertions(+), 88 deletions(-) create mode 100755 model/jpa/src/main/java/org/keycloak/models/jpa/entities/AttributeMap.java create mode 100755 model/jpa/src/main/java/org/keycloak/models/jpa/entities/RealmAttributeEntity.java diff --git a/connections/jpa/src/main/resources/META-INF/persistence.xml b/connections/jpa/src/main/resources/META-INF/persistence.xml index 94f1fd049b..dde200e23a 100755 --- a/connections/jpa/src/main/resources/META-INF/persistence.xml +++ b/connections/jpa/src/main/resources/META-INF/persistence.xml @@ -7,6 +7,7 @@ org.keycloak.models.jpa.entities.CredentialEntity org.keycloak.models.jpa.entities.OAuthClientEntity org.keycloak.models.jpa.entities.RealmEntity + org.keycloak.models.jpa.entities.RealmAttributeEntity org.keycloak.models.jpa.entities.RequiredCredentialEntity org.keycloak.models.jpa.entities.UserFederationProviderEntity org.keycloak.models.jpa.entities.RoleEntity diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/RealmAdapter.java b/model/jpa/src/main/java/org/keycloak/models/jpa/RealmAdapter.java index 4f0da3cbc2..4b44180354 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/RealmAdapter.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/RealmAdapter.java @@ -11,10 +11,13 @@ import org.keycloak.models.RequiredCredentialModel; import org.keycloak.models.RoleModel; import org.keycloak.models.UserFederationProviderModel; import org.keycloak.models.jpa.entities.ApplicationEntity; +import org.keycloak.models.jpa.entities.AttributeMap; import org.keycloak.models.jpa.entities.OAuthClientEntity; +import org.keycloak.models.jpa.entities.RealmAttributeEntity; import org.keycloak.models.jpa.entities.RealmEntity; import org.keycloak.models.jpa.entities.RequiredCredentialEntity; import org.keycloak.models.jpa.entities.RoleEntity; +import org.keycloak.models.jpa.entities.UserAttributeEntity; import org.keycloak.models.jpa.entities.UserFederationProviderEntity; import org.keycloak.models.utils.KeycloakModelUtils; @@ -127,74 +130,146 @@ public class RealmAdapter implements RealmModel { em.flush(); } + public void setAttribute(String name, String value) { + for (RealmAttributeEntity attr : realm.getAttributes()) { + if (attr.getName().equals(name)) { + attr.setValue(value); + return; + } + } + RealmAttributeEntity attr = new RealmAttributeEntity(); + attr.setName(name); + attr.setValue(value); + attr.setRealm(realm); + em.persist(attr); + realm.getAttributes().add(attr); + } + + public void setAttribute(String name, Boolean value) { + setAttribute(name, value.toString()); + } + + public void setAttribute(String name, Integer value) { + setAttribute(name, value.toString()); + } + + public void setAttribute(String name, Long value) { + setAttribute(name, value.toString()); + } + + public void removeAttribute(String name) { + Iterator it = realm.getAttributes().iterator(); + while (it.hasNext()) { + RealmAttributeEntity attr = it.next(); + if (attr.getName().equals(name)) { + it.remove(); + em.remove(attr); + } + } + } + + public String getAttribute(String name) { + for (RealmAttributeEntity attr : realm.getAttributes()) { + if (attr.getName().equals(name)) { + return attr.getValue(); + } + } + return null; + } + + public Integer getAttribute(String name, Integer defaultValue) { + String v = getAttribute(name); + return v != null ? Integer.parseInt(v) : defaultValue; + + } + + public Long getAttribute(String name, Long defaultValue) { + String v = getAttribute(name); + return v != null ? Long.parseLong(v) : defaultValue; + + } + + public Boolean getAttribute(String name, Boolean defaultValue) { + String v = getAttribute(name); + return v != null ? Boolean.parseBoolean(v) : defaultValue; + + } + + public Map getAttributes() { + Map result = new HashMap(); + for (RealmAttributeEntity attr : realm.getAttributes()) { + result.put(attr.getName(), attr.getValue()); + } + return result; + } @Override public boolean isBruteForceProtected() { - return realm.isBruteForceProtected(); + return getAttribute("bruteForceProtected", false); } @Override public void setBruteForceProtected(boolean value) { - realm.setBruteForceProtected(value); + setAttribute("bruteForceProtected", value); } @Override public int getMaxFailureWaitSeconds() { - return realm.getMaxFailureWaitSeconds(); + return getAttribute("maxFailureWaitSeconds", 0); } @Override public void setMaxFailureWaitSeconds(int val) { - realm.setMaxFailureWaitSeconds(val); + setAttribute("maxFailureWaitSeconds", val); } @Override public int getWaitIncrementSeconds() { - return realm.getWaitIncrementSeconds(); + return getAttribute("waitIncrementSeconds", 0); } @Override public void setWaitIncrementSeconds(int val) { - realm.setWaitIncrementSeconds(val); + setAttribute("waitIncrementSeconds", val); } @Override public long getQuickLoginCheckMilliSeconds() { - return realm.getQuickLoginCheckMilliSeconds(); + return getAttribute("quickLoginCheckMilliSeconds", 0l); } @Override public void setQuickLoginCheckMilliSeconds(long val) { - realm.setQuickLoginCheckMilliSeconds(val); + setAttribute("quickLoginCheckMilliSeconds", val); } @Override public int getMinimumQuickLoginWaitSeconds() { - return realm.getMinimumQuickLoginWaitSeconds(); + return getAttribute("minimumQuickLoginWaitSeconds", 0); } @Override public void setMinimumQuickLoginWaitSeconds(int val) { - realm.setMinimumQuickLoginWaitSeconds(val); + setAttribute("minimumQuickLoginWaitSeconds", val); } @Override public int getMaxDeltaTimeSeconds() { - return realm.getMaxDeltaTimeSeconds(); + return getAttribute("maxDeltaTimeSeconds", 0); } @Override public void setMaxDeltaTimeSeconds(int val) { - realm.setMaxDeltaTimeSeconds(val); + setAttribute("maxDeltaTimeSeconds", val); } @Override public int getFailureFactor() { - return realm.getFailureFactor(); + return getAttribute("failureFactor", 0); } @Override public void setFailureFactor(int failureFactor) { - realm.setFailureFactor(failureFactor); + setAttribute("failureFactor", failureFactor); } @Override diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/AttributeMap.java b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/AttributeMap.java new file mode 100755 index 0000000000..5a4e28b19f --- /dev/null +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/AttributeMap.java @@ -0,0 +1,74 @@ +package org.keycloak.models.jpa.entities; + +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @author Bill Burke + * @version $Revision: 1 $ + */ +public class AttributeMap { + Map attributes = new HashMap(); + + public void set(String key, String value) { + attributes.put(key, value); + } + + public void set(String key, Boolean value) { + attributes.put(key, value.toString()); + } + + public void set(String key, Integer value) { + attributes.put(key, value.toString()); + } + + public String get(String key) { + return attributes.get(key); + } + + public String get(String key, String defaultValue) { + String value = attributes.get(key); + return value == null ? defaultValue : value; + } + + public String[] getArray(String key) { + String value = get(key); + if (value != null) { + String[] a = value.split(","); + for (int i = 0; i < a.length; i++) { + a[i] = a[i].trim(); + } + return a; + } else { + return null; + } + } + + public Integer getInt(String key) { + return getInt(key, null); + } + + public Integer getInt(String key, Integer defaultValue) { + String v = get(key, null); + return v != null ? Integer.parseInt(v) : defaultValue; + } + + public Long getLong(String key) { + return getLong(key, null); + } + + public Long getLong(String key, Long defaultValue) { + String v = get(key, null); + return v != null ? Long.parseLong(v) : defaultValue; + } + + public Boolean getBoolean(String key) { + return getBoolean(key, null); + } + + public Boolean getBoolean(String key, Boolean defaultValue) { + String v = get(key, null); + return v != null ? Boolean.parseBoolean(v) : defaultValue; + }} diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/RealmAttributeEntity.java b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/RealmAttributeEntity.java new file mode 100755 index 0000000000..2e025cc344 --- /dev/null +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/RealmAttributeEntity.java @@ -0,0 +1,105 @@ +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; + +/** + * @author Bill Burke + * @version $Revision: 1 $ + */ +@NamedQueries({ + @NamedQuery(name="deleteRealmAttributesByRealm", query="delete from RealmAttributeEntity attr where attr.realm = :realm") +}) +@Table(name="REALM_ATTRIBUTE") +@Entity +@IdClass(RealmAttributeEntity.Key.class) +public class RealmAttributeEntity { + + @Id + @ManyToOne(fetch= FetchType.LAZY) + @JoinColumn(name = "REALM_ID") + protected RealmEntity realm; + + @Id + @Column(name = "NAME") + protected String name; + @Column(name = "VALUE") + protected String value; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public RealmEntity getRealm() { + return realm; + } + + public void setRealm(RealmEntity realm) { + this.realm = realm; + } + + public static class Key implements Serializable { + + protected RealmEntity realm; + + protected String name; + + public Key() { + } + + public Key(RealmEntity user, String name) { + this.realm = user; + this.name = name; + } + + public RealmEntity getRealm() { + return realm; + } + + 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 (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 + (name != null ? name.hashCode() : 0); + return result; + } + } + +} diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/RealmEntity.java b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/RealmEntity.java index 39e35c16c3..de46f3716d 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/RealmEntity.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/RealmEntity.java @@ -58,24 +58,6 @@ public class RealmEntity { 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="UPDATE_PROFILE_ON_SOC_LOGIN") protected boolean updateProfileOnInitialSocialLogin; @Column(name="PASSWORD_POLICY") @@ -108,6 +90,9 @@ public class RealmEntity { @Column(name="EMAIL_THEME") protected String emailTheme; + @OneToMany(cascade ={CascadeType.REMOVE}, orphanRemoval = true, mappedBy = "realm") + Collection attributes = new ArrayList(); + @OneToMany(cascade ={CascadeType.REMOVE}, orphanRemoval = true, mappedBy = "realm") Collection requiredCredentials = new ArrayList(); @@ -400,62 +385,6 @@ public class RealmEntity { this.notBefore = notBefore; } - public boolean isBruteForceProtected() { - return bruteForceProtected; - } - - public void setBruteForceProtected(boolean bruteForceProtected) { - this.bruteForceProtected = bruteForceProtected; - } - - public int getMaxFailureWaitSeconds() { - return maxFailureWaitSeconds; - } - - public void setMaxFailureWaitSeconds(int maxFailureWaitSeconds) { - this.maxFailureWaitSeconds = maxFailureWaitSeconds; - } - - public int getMinimumQuickLoginWaitSeconds() { - return minimumQuickLoginWaitSeconds; - } - - public void setMinimumQuickLoginWaitSeconds(int minimumQuickLoginWaitSeconds) { - this.minimumQuickLoginWaitSeconds = minimumQuickLoginWaitSeconds; - } - - public int getWaitIncrementSeconds() { - return waitIncrementSeconds; - } - - public void setWaitIncrementSeconds(int waitIncrementSeconds) { - this.waitIncrementSeconds = waitIncrementSeconds; - } - - public long getQuickLoginCheckMilliSeconds() { - return quickLoginCheckMilliSeconds; - } - - public void setQuickLoginCheckMilliSeconds(long quickLoginCheckMilliSeconds) { - this.quickLoginCheckMilliSeconds = quickLoginCheckMilliSeconds; - } - - public int getMaxDeltaTimeSeconds() { - return maxDeltaTimeSeconds; - } - - public void setMaxDeltaTimeSeconds(int maxDeltaTimeSeconds) { - this.maxDeltaTimeSeconds = maxDeltaTimeSeconds; - } - - public int getFailureFactor() { - return failureFactor; - } - - public void setFailureFactor(int failureFactor) { - this.failureFactor = failureFactor; - } - public boolean isAuditEnabled() { return auditEnabled; } @@ -495,5 +424,13 @@ public class RealmEntity { public void setUserFederationProviders(List userFederationProviders) { this.userFederationProviders = userFederationProviders; } + + public Collection getAttributes() { + return attributes; + } + + public void setAttributes(Collection attributes) { + this.attributes = attributes; + } }