Merge pull request #3153 from cargosoft/KEYCLOAK-3327

KEYCLOAK-3327 Make realm attributes accessible via the RealmModel
This commit is contained in:
Stian Thorgersen 2016-09-08 08:00:14 +02:00 committed by GitHub
commit 1f27fc9e4b
11 changed files with 242 additions and 8 deletions

View file

@ -123,6 +123,8 @@ public class RealmRepresentation {
protected String resetCredentialsFlow; protected String resetCredentialsFlow;
protected String clientAuthenticationFlow; protected String clientAuthenticationFlow;
protected Map<String, String> attributes;
protected String keycloakVersion; protected String keycloakVersion;
@Deprecated @Deprecated
@ -864,4 +866,12 @@ public class RealmRepresentation {
return identityProviders != null && !identityProviders.isEmpty(); return identityProviders != null && !identityProviders.isEmpty();
} }
public void setAttributes(Map<String, String> attributes) {
this.attributes = attributes;
}
public Map<String, String> getAttributes() {
return attributes;
}
} }

View file

@ -1465,4 +1465,64 @@ public class RealmAdapter implements RealmModel {
if (isUpdated()) return updated.getComponent(id); if (isUpdated()) return updated.getComponent(id);
return cached.getComponents().get(id); return cached.getComponents().get(id);
} }
public void setAttribute(String name, String value) {
getDelegateForUpdate();
updated.setAttribute(name, value);
}
@Override
public void setAttribute(String name, Boolean value) {
getDelegateForUpdate();
updated.setAttribute(name, value);
}
@Override
public void setAttribute(String name, Integer value) {
getDelegateForUpdate();
updated.setAttribute(name, value);
}
@Override
public void setAttribute(String name, Long value) {
getDelegateForUpdate();
updated.setAttribute(name, value);
}
@Override
public void removeAttribute(String name) {
getDelegateForUpdate();
updated.removeAttribute(name);
}
@Override
public String getAttribute(String name) {
if (isUpdated()) return updated.getAttribute(name);
return cached.getAttribute(name);
}
@Override
public Integer getAttribute(String name, Integer defaultValue) {
if (isUpdated()) return updated.getAttribute(name, defaultValue);
return cached.getAttribute(name, defaultValue);
}
@Override
public Long getAttribute(String name, Long defaultValue) {
if (isUpdated()) return updated.getAttribute(name, defaultValue);
return cached.getAttribute(name, defaultValue);
}
@Override
public Boolean getAttribute(String name, Boolean defaultValue) {
if (isUpdated()) return updated.getAttribute(name, defaultValue);
return cached.getAttribute(name, defaultValue);
}
@Override
public Map<String, String> getAttributes() {
if (isUpdated()) return updated.getAttributes();
return cached.getAttributes();
}
} }

View file

@ -152,6 +152,8 @@ public class CachedRealm extends AbstractRevisioned {
protected MultivaluedHashMap<String, IdentityProviderMapperModel> identityProviderMappers = new MultivaluedHashMap<>(); protected MultivaluedHashMap<String, IdentityProviderMapperModel> identityProviderMappers = new MultivaluedHashMap<>();
protected Set<IdentityProviderMapperModel> identityProviderMapperSet; protected Set<IdentityProviderMapperModel> identityProviderMapperSet;
protected Map<String, String> attributes;
public CachedRealm(Long revision, RealmModel model) { public CachedRealm(Long revision, RealmModel model) {
super(revision, model.getId()); super(revision, model.getId());
name = model.getName(); name = model.getName();
@ -231,10 +233,10 @@ public class CachedRealm extends AbstractRevisioned {
eventsExpiration = model.getEventsExpiration(); eventsExpiration = model.getEventsExpiration();
eventsListeners = model.getEventsListeners(); eventsListeners = model.getEventsListeners();
enabledEventTypes = model.getEnabledEventTypes(); enabledEventTypes = model.getEnabledEventTypes();
adminEventsEnabled = model.isAdminEventsEnabled(); adminEventsEnabled = model.isAdminEventsEnabled();
adminEventsDetailsEnabled = model.isAdminEventsDetailsEnabled(); adminEventsDetailsEnabled = model.isAdminEventsDetailsEnabled();
defaultRoles = model.getDefaultRoles(); defaultRoles = model.getDefaultRoles();
ClientModel masterAdminClient = model.getMasterAdminClient(); ClientModel masterAdminClient = model.getMasterAdminClient();
this.masterAdminClient = (masterAdminClient != null) ? masterAdminClient.getId() : null; this.masterAdminClient = (masterAdminClient != null) ? masterAdminClient.getId() : null;
@ -285,6 +287,11 @@ public class CachedRealm extends AbstractRevisioned {
components.put(component.getId(), component); components.put(component.getId(), component);
} }
try {
attributes = model.getAttributes();
} catch (UnsupportedOperationException ex) {
}
} }
protected void cacheClientTemplates(RealmModel model) { protected void cacheClientTemplates(RealmModel model) {
@ -475,7 +482,7 @@ public class CachedRealm extends AbstractRevisioned {
public Set<String> getEventsListeners() { public Set<String> getEventsListeners() {
return eventsListeners; return eventsListeners;
} }
public Set<String> getEnabledEventTypes() { public Set<String> getEnabledEventTypes() {
return enabledEventTypes; return enabledEventTypes;
} }
@ -619,4 +626,28 @@ public class CachedRealm extends AbstractRevisioned {
public Map<String, ComponentModel> getComponents() { public Map<String, ComponentModel> getComponents() {
return components; return components;
} }
public String getAttribute(String name) {
return attributes != null ? attributes.get(name) : 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() {
return attributes;
}
} }

View file

@ -182,6 +182,7 @@ public class RealmAdapter implements RealmModel, JpaModel<RealmEntity> {
em.flush(); em.flush();
} }
@Override
public void setAttribute(String name, String value) { public void setAttribute(String name, String value) {
for (RealmAttributeEntity attr : realm.getAttributes()) { for (RealmAttributeEntity attr : realm.getAttributes()) {
if (attr.getName().equals(name)) { if (attr.getName().equals(name)) {
@ -197,18 +198,22 @@ public class RealmAdapter implements RealmModel, JpaModel<RealmEntity> {
realm.getAttributes().add(attr); realm.getAttributes().add(attr);
} }
@Override
public void setAttribute(String name, Boolean value) { public void setAttribute(String name, Boolean value) {
setAttribute(name, value.toString()); setAttribute(name, value.toString());
} }
@Override
public void setAttribute(String name, Integer value) { public void setAttribute(String name, Integer value) {
setAttribute(name, value.toString()); setAttribute(name, value.toString());
} }
@Override
public void setAttribute(String name, Long value) { public void setAttribute(String name, Long value) {
setAttribute(name, value.toString()); setAttribute(name, value.toString());
} }
@Override
public void removeAttribute(String name) { public void removeAttribute(String name) {
Iterator<RealmAttributeEntity> it = realm.getAttributes().iterator(); Iterator<RealmAttributeEntity> it = realm.getAttributes().iterator();
while (it.hasNext()) { while (it.hasNext()) {
@ -220,6 +225,7 @@ public class RealmAdapter implements RealmModel, JpaModel<RealmEntity> {
} }
} }
@Override
public String getAttribute(String name) { public String getAttribute(String name) {
for (RealmAttributeEntity attr : realm.getAttributes()) { for (RealmAttributeEntity attr : realm.getAttributes()) {
if (attr.getName().equals(name)) { if (attr.getName().equals(name)) {
@ -229,24 +235,28 @@ public class RealmAdapter implements RealmModel, JpaModel<RealmEntity> {
return null; return null;
} }
@Override
public Integer getAttribute(String name, Integer defaultValue) { public Integer getAttribute(String name, Integer defaultValue) {
String v = getAttribute(name); String v = getAttribute(name);
return v != null ? Integer.parseInt(v) : defaultValue; return v != null ? Integer.parseInt(v) : defaultValue;
} }
@Override
public Long getAttribute(String name, Long defaultValue) { public Long getAttribute(String name, Long defaultValue) {
String v = getAttribute(name); String v = getAttribute(name);
return v != null ? Long.parseLong(v) : defaultValue; return v != null ? Long.parseLong(v) : defaultValue;
} }
@Override
public Boolean getAttribute(String name, Boolean defaultValue) { public Boolean getAttribute(String name, Boolean defaultValue) {
String v = getAttribute(name); String v = getAttribute(name);
return v != null ? Boolean.parseBoolean(v) : defaultValue; return v != null ? Boolean.parseBoolean(v) : defaultValue;
} }
@Override
public Map<String, String> getAttributes() { public Map<String, String> getAttributes() {
// should always return a copy // should always return a copy
Map<String, String> result = new HashMap<String, String>(); Map<String, String> result = new HashMap<String, String>();
@ -255,6 +265,7 @@ public class RealmAdapter implements RealmModel, JpaModel<RealmEntity> {
} }
return result; return result;
} }
@Override @Override
public boolean isBruteForceProtected() { public boolean isBruteForceProtected() {
return getAttribute("bruteForceProtected", false); return getAttribute("bruteForceProtected", false);

View file

@ -1230,7 +1230,7 @@ public class RealmAdapter extends AbstractMongoAdapter<MongoRealmEntity> impleme
} }
updateRealm(); updateRealm();
} }
@Override @Override
public boolean isAdminEventsEnabled() { public boolean isAdminEventsEnabled() {
return realm.isAdminEventsEnabled(); return realm.isAdminEventsEnabled();
@ -1240,7 +1240,7 @@ public class RealmAdapter extends AbstractMongoAdapter<MongoRealmEntity> impleme
public void setAdminEventsEnabled(boolean enabled) { public void setAdminEventsEnabled(boolean enabled) {
realm.setAdminEventsEnabled(enabled); realm.setAdminEventsEnabled(enabled);
updateRealm(); updateRealm();
} }
@Override @Override
@ -1253,7 +1253,7 @@ public class RealmAdapter extends AbstractMongoAdapter<MongoRealmEntity> impleme
realm.setAdminEventsDetailsEnabled(enabled); realm.setAdminEventsDetailsEnabled(enabled);
updateRealm(); updateRealm();
} }
@Override @Override
public ClientModel getMasterAdminClient() { public ClientModel getMasterAdminClient() {
MongoClientEntity appData = getMongoStore().loadEntity(MongoClientEntity.class, realm.getMasterAdminClient(), invocationContext); MongoClientEntity appData = getMongoStore().loadEntity(MongoClientEntity.class, realm.getMasterAdminClient(), invocationContext);
@ -2181,4 +2181,62 @@ public class RealmAdapter extends AbstractMongoAdapter<MongoRealmEntity> impleme
} }
return null; return null;
} }
@Override
public void setAttribute(String name, String value) {
realm.getAttributes().put(name, value);
updateRealm();
}
@Override
public void setAttribute(String name, Boolean value) {
setAttribute(name, value.toString());
}
@Override
public void setAttribute(String name, Integer value) {
setAttribute(name, value.toString());
}
@Override
public void setAttribute(String name, Long value) {
setAttribute(name, value.toString());
}
@Override
public void removeAttribute(String name) {
realm.getAttributes().remove(name);
updateRealm();
}
@Override
public String getAttribute(String name) {
return realm.getAttributes().get(name);
}
@Override
public Integer getAttribute(String name, Integer defaultValue) {
String v = getAttribute(name);
return v != null ? Integer.parseInt(v) : defaultValue;
}
@Override
public Long getAttribute(String name, Long defaultValue) {
String v = getAttribute(name);
return v != null ? Long.parseLong(v) : defaultValue;
}
@Override
public Boolean getAttribute(String name, Boolean defaultValue) {
String v = getAttribute(name);
return v != null ? Boolean.parseBoolean(v) : defaultValue;
}
@Override
public Map<String, String> getAttributes() {
Map<String, String> attributes = new HashMap<>();
attributes.putAll(realm.getAttributes());
return attributes;
}
} }

View file

@ -110,6 +110,17 @@ public interface RealmModel extends RoleContainerModel {
void setEditUsernameAllowed(boolean editUsernameAllowed); void setEditUsernameAllowed(boolean editUsernameAllowed);
void setAttribute(String name, String value);
void setAttribute(String name, Boolean value);
void setAttribute(String name, Integer value);
void setAttribute(String name, Long value);
void removeAttribute(String name);
String getAttribute(String name);
Integer getAttribute(String name, Integer defaultValue);
Long getAttribute(String name, Long defaultValue);
Boolean getAttribute(String name, Boolean defaultValue);
Map<String, String> getAttributes();
//--- brute force settings //--- brute force settings
boolean isBruteForceProtected(); boolean isBruteForceProtected();
void setBruteForceProtected(boolean value); void setBruteForceProtected(boolean value);

View file

@ -94,6 +94,8 @@ public class RealmEntity extends AbstractIdentifiableEntity {
private Map<String, String> smtpConfig = new HashMap<String, String>(); private Map<String, String> smtpConfig = new HashMap<String, String>();
private Map<String, String> socialConfig = new HashMap<String, String>(); private Map<String, String> socialConfig = new HashMap<String, String>();
private Map<String, String> attributes = new HashMap<>();
private boolean eventsEnabled; private boolean eventsEnabled;
private long eventsExpiration; private long eventsExpiration;
private List<String> eventsListeners = new ArrayList<String>(); private List<String> eventsListeners = new ArrayList<String>();
@ -692,6 +694,13 @@ public class RealmEntity extends AbstractIdentifiableEntity {
public void setComponentEntities(List<ComponentEntity> componentEntities) { public void setComponentEntities(List<ComponentEntity> componentEntities) {
this.componentEntities = componentEntities; this.componentEntities = componentEntities;
} }
public Map<String, String> getAttributes() {
return attributes;
}
public void setAttributes(Map<String, String> attributes) {
this.attributes = attributes;
}
} }

View file

@ -390,6 +390,10 @@ public class ModelToRepresentation {
exportRequiredActions(realm, rep); exportRequiredActions(realm, rep);
exportGroups(realm, rep); exportGroups(realm, rep);
} }
Map<String, String> attributes = realm.getAttributes();
rep.setAttributes(attributes);
return rep; return rep;
} }

View file

@ -373,6 +373,15 @@ public class RepresentationToModel {
if(rep.getDefaultLocale() != null){ if(rep.getDefaultLocale() != null){
newRealm.setDefaultLocale(rep.getDefaultLocale()); newRealm.setDefaultLocale(rep.getDefaultLocale());
} }
// import attributes
if (rep.getAttributes() != null) {
for (Map.Entry<String, String> attr : rep.getAttributes().entrySet()) {
newRealm.setAttribute(attr.getKey(), attr.getValue());
}
}
} }
protected static void importComponents(RealmModel newRealm, MultivaluedHashMap<String, ComponentExportRepresentation> components, String parentId) { protected static void importComponents(RealmModel newRealm, MultivaluedHashMap<String, ComponentExportRepresentation> components, String parentId) {
@ -821,6 +830,13 @@ public class RepresentationToModel {
if (rep.getClientAuthenticationFlow() != null) { if (rep.getClientAuthenticationFlow() != null) {
realm.setClientAuthenticationFlow(realm.getFlowByAlias(rep.getClientAuthenticationFlow())); realm.setClientAuthenticationFlow(realm.getFlowByAlias(rep.getClientAuthenticationFlow()));
} }
if (rep.getAttributes() != null) {
for (Map.Entry<String, String> entry : rep.getAttributes().entrySet()) {
realm.setAttribute(entry.getKey(), entry.getValue());
}
}
} }
// Basic realm stuff // Basic realm stuff

View file

@ -57,6 +57,7 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.HashSet; import java.util.HashSet;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -237,6 +238,16 @@ public class RealmTest extends AbstractAdminTest {
assertEquals(Boolean.FALSE, rep.isRegistrationAllowed()); assertEquals(Boolean.FALSE, rep.isRegistrationAllowed());
assertEquals(Boolean.FALSE, rep.isRegistrationEmailAsUsername()); assertEquals(Boolean.FALSE, rep.isRegistrationEmailAsUsername());
assertEquals(Boolean.FALSE, rep.isEditUsernameAllowed()); assertEquals(Boolean.FALSE, rep.isEditUsernameAllowed());
// attributes
rep.getAttributes().put("foo", "bar");
realm.update(rep);
assertAdminEvents.assertEvent(realmId, OperationType.UPDATE, Matchers.nullValue(String.class), rep, ResourceType.REALM);
rep = realm.toRepresentation();
assertEquals("bar", rep.getAttributes().get("foo"));
} }
@Test @Test
@ -393,6 +404,13 @@ public class RealmTest extends AbstractAdminTest {
assertEquals(realm.getBrowserSecurityHeaders(), storedRealm.getBrowserSecurityHeaders()); assertEquals(realm.getBrowserSecurityHeaders(), storedRealm.getBrowserSecurityHeaders());
} }
if (realm.getAttributes() != null) {
HashMap<String, String> attributes = new HashMap<>();
attributes.putAll(storedRealm.getAttributes());
attributes.entrySet().retainAll(realm.getAttributes().entrySet());
assertEquals(realm.getAttributes(), attributes);
}
} }
@Test @Test

View file

@ -95,5 +95,11 @@
"roles": ["customer-user"] "roles": ["customer-user"]
} }
] ]
},
"attributes": {
"string-attr": "foo",
"int-attr": "123",
"long-attr": "1234567890123456",
"bool-attr": "true"
} }
} }