Merge pull request #614 from patriot1burke/master

add jpa realm attributes
This commit is contained in:
Bill Burke 2014-08-11 12:33:36 -04:00
commit e73bdf9d8c
5 changed files with 280 additions and 88 deletions

View file

@ -7,6 +7,7 @@
<class>org.keycloak.models.jpa.entities.CredentialEntity</class> <class>org.keycloak.models.jpa.entities.CredentialEntity</class>
<class>org.keycloak.models.jpa.entities.OAuthClientEntity</class> <class>org.keycloak.models.jpa.entities.OAuthClientEntity</class>
<class>org.keycloak.models.jpa.entities.RealmEntity</class> <class>org.keycloak.models.jpa.entities.RealmEntity</class>
<class>org.keycloak.models.jpa.entities.RealmAttributeEntity</class>
<class>org.keycloak.models.jpa.entities.RequiredCredentialEntity</class> <class>org.keycloak.models.jpa.entities.RequiredCredentialEntity</class>
<class>org.keycloak.models.jpa.entities.UserFederationProviderEntity</class> <class>org.keycloak.models.jpa.entities.UserFederationProviderEntity</class>
<class>org.keycloak.models.jpa.entities.RoleEntity</class> <class>org.keycloak.models.jpa.entities.RoleEntity</class>

View file

@ -11,10 +11,13 @@ import org.keycloak.models.RequiredCredentialModel;
import org.keycloak.models.RoleModel; import org.keycloak.models.RoleModel;
import org.keycloak.models.UserFederationProviderModel; import org.keycloak.models.UserFederationProviderModel;
import org.keycloak.models.jpa.entities.ApplicationEntity; 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.OAuthClientEntity;
import org.keycloak.models.jpa.entities.RealmAttributeEntity;
import org.keycloak.models.jpa.entities.RealmEntity; import org.keycloak.models.jpa.entities.RealmEntity;
import org.keycloak.models.jpa.entities.RequiredCredentialEntity; import org.keycloak.models.jpa.entities.RequiredCredentialEntity;
import org.keycloak.models.jpa.entities.RoleEntity; 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.jpa.entities.UserFederationProviderEntity;
import org.keycloak.models.utils.KeycloakModelUtils; import org.keycloak.models.utils.KeycloakModelUtils;
@ -127,74 +130,146 @@ public class RealmAdapter implements RealmModel {
em.flush(); 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<RealmAttributeEntity> 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<String, String> getAttributes() {
Map<String, String> result = new HashMap<String, String>();
for (RealmAttributeEntity attr : realm.getAttributes()) {
result.put(attr.getName(), attr.getValue());
}
return result;
}
@Override @Override
public boolean isBruteForceProtected() { public boolean isBruteForceProtected() {
return realm.isBruteForceProtected(); return getAttribute("bruteForceProtected", false);
} }
@Override @Override
public void setBruteForceProtected(boolean value) { public void setBruteForceProtected(boolean value) {
realm.setBruteForceProtected(value); setAttribute("bruteForceProtected", value);
} }
@Override @Override
public int getMaxFailureWaitSeconds() { public int getMaxFailureWaitSeconds() {
return realm.getMaxFailureWaitSeconds(); return getAttribute("maxFailureWaitSeconds", 0);
} }
@Override @Override
public void setMaxFailureWaitSeconds(int val) { public void setMaxFailureWaitSeconds(int val) {
realm.setMaxFailureWaitSeconds(val); setAttribute("maxFailureWaitSeconds", val);
} }
@Override @Override
public int getWaitIncrementSeconds() { public int getWaitIncrementSeconds() {
return realm.getWaitIncrementSeconds(); return getAttribute("waitIncrementSeconds", 0);
} }
@Override @Override
public void setWaitIncrementSeconds(int val) { public void setWaitIncrementSeconds(int val) {
realm.setWaitIncrementSeconds(val); setAttribute("waitIncrementSeconds", val);
} }
@Override @Override
public long getQuickLoginCheckMilliSeconds() { public long getQuickLoginCheckMilliSeconds() {
return realm.getQuickLoginCheckMilliSeconds(); return getAttribute("quickLoginCheckMilliSeconds", 0l);
} }
@Override @Override
public void setQuickLoginCheckMilliSeconds(long val) { public void setQuickLoginCheckMilliSeconds(long val) {
realm.setQuickLoginCheckMilliSeconds(val); setAttribute("quickLoginCheckMilliSeconds", val);
} }
@Override @Override
public int getMinimumQuickLoginWaitSeconds() { public int getMinimumQuickLoginWaitSeconds() {
return realm.getMinimumQuickLoginWaitSeconds(); return getAttribute("minimumQuickLoginWaitSeconds", 0);
} }
@Override @Override
public void setMinimumQuickLoginWaitSeconds(int val) { public void setMinimumQuickLoginWaitSeconds(int val) {
realm.setMinimumQuickLoginWaitSeconds(val); setAttribute("minimumQuickLoginWaitSeconds", val);
} }
@Override @Override
public int getMaxDeltaTimeSeconds() { public int getMaxDeltaTimeSeconds() {
return realm.getMaxDeltaTimeSeconds(); return getAttribute("maxDeltaTimeSeconds", 0);
} }
@Override @Override
public void setMaxDeltaTimeSeconds(int val) { public void setMaxDeltaTimeSeconds(int val) {
realm.setMaxDeltaTimeSeconds(val); setAttribute("maxDeltaTimeSeconds", val);
} }
@Override @Override
public int getFailureFactor() { public int getFailureFactor() {
return realm.getFailureFactor(); return getAttribute("failureFactor", 0);
} }
@Override @Override
public void setFailureFactor(int failureFactor) { public void setFailureFactor(int failureFactor) {
realm.setFailureFactor(failureFactor); setAttribute("failureFactor", failureFactor);
} }
@Override @Override

View file

@ -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 <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
*/
public class AttributeMap {
Map<String, String> attributes = new HashMap<String, String>();
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;
}}

View file

@ -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 <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @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;
}
}
}

View file

@ -58,24 +58,6 @@ public class RealmEntity {
protected boolean social; protected boolean social;
@Column(name="REMEMBER_ME") @Column(name="REMEMBER_ME")
protected boolean rememberMe; 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") @Column(name="UPDATE_PROFILE_ON_SOC_LOGIN")
protected boolean updateProfileOnInitialSocialLogin; protected boolean updateProfileOnInitialSocialLogin;
@Column(name="PASSWORD_POLICY") @Column(name="PASSWORD_POLICY")
@ -108,6 +90,9 @@ public class RealmEntity {
@Column(name="EMAIL_THEME") @Column(name="EMAIL_THEME")
protected String emailTheme; protected String emailTheme;
@OneToMany(cascade ={CascadeType.REMOVE}, orphanRemoval = true, mappedBy = "realm")
Collection<RealmAttributeEntity> attributes = new ArrayList<RealmAttributeEntity>();
@OneToMany(cascade ={CascadeType.REMOVE}, orphanRemoval = true, mappedBy = "realm") @OneToMany(cascade ={CascadeType.REMOVE}, orphanRemoval = true, mappedBy = "realm")
Collection<RequiredCredentialEntity> requiredCredentials = new ArrayList<RequiredCredentialEntity>(); Collection<RequiredCredentialEntity> requiredCredentials = new ArrayList<RequiredCredentialEntity>();
@ -400,62 +385,6 @@ public class RealmEntity {
this.notBefore = notBefore; 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() { public boolean isAuditEnabled() {
return auditEnabled; return auditEnabled;
} }
@ -495,5 +424,13 @@ public class RealmEntity {
public void setUserFederationProviders(List<UserFederationProviderEntity> userFederationProviders) { public void setUserFederationProviders(List<UserFederationProviderEntity> userFederationProviders) {
this.userFederationProviders = userFederationProviders; this.userFederationProviders = userFederationProviders;
} }
public Collection<RealmAttributeEntity> getAttributes() {
return attributes;
}
public void setAttributes(Collection<RealmAttributeEntity> attributes) {
this.attributes = attributes;
}
} }