Merge pull request #3153 from cargosoft/KEYCLOAK-3327
KEYCLOAK-3327 Make realm attributes accessible via the RealmModel
This commit is contained in:
commit
1f27fc9e4b
11 changed files with 242 additions and 8 deletions
|
@ -123,6 +123,8 @@ public class RealmRepresentation {
|
|||
protected String resetCredentialsFlow;
|
||||
protected String clientAuthenticationFlow;
|
||||
|
||||
protected Map<String, String> attributes;
|
||||
|
||||
protected String keycloakVersion;
|
||||
|
||||
@Deprecated
|
||||
|
@ -864,4 +866,12 @@ public class RealmRepresentation {
|
|||
return identityProviders != null && !identityProviders.isEmpty();
|
||||
}
|
||||
|
||||
public void setAttributes(Map<String, String> attributes) {
|
||||
this.attributes = attributes;
|
||||
}
|
||||
|
||||
public Map<String, String> getAttributes() {
|
||||
return attributes;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1465,4 +1465,64 @@ public class RealmAdapter implements RealmModel {
|
|||
if (isUpdated()) return updated.getComponent(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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -152,6 +152,8 @@ public class CachedRealm extends AbstractRevisioned {
|
|||
protected MultivaluedHashMap<String, IdentityProviderMapperModel> identityProviderMappers = new MultivaluedHashMap<>();
|
||||
protected Set<IdentityProviderMapperModel> identityProviderMapperSet;
|
||||
|
||||
protected Map<String, String> attributes;
|
||||
|
||||
public CachedRealm(Long revision, RealmModel model) {
|
||||
super(revision, model.getId());
|
||||
name = model.getName();
|
||||
|
@ -231,10 +233,10 @@ public class CachedRealm extends AbstractRevisioned {
|
|||
eventsExpiration = model.getEventsExpiration();
|
||||
eventsListeners = model.getEventsListeners();
|
||||
enabledEventTypes = model.getEnabledEventTypes();
|
||||
|
||||
|
||||
adminEventsEnabled = model.isAdminEventsEnabled();
|
||||
adminEventsDetailsEnabled = model.isAdminEventsDetailsEnabled();
|
||||
|
||||
|
||||
defaultRoles = model.getDefaultRoles();
|
||||
ClientModel masterAdminClient = model.getMasterAdminClient();
|
||||
this.masterAdminClient = (masterAdminClient != null) ? masterAdminClient.getId() : null;
|
||||
|
@ -285,6 +287,11 @@ public class CachedRealm extends AbstractRevisioned {
|
|||
components.put(component.getId(), component);
|
||||
}
|
||||
|
||||
try {
|
||||
attributes = model.getAttributes();
|
||||
} catch (UnsupportedOperationException ex) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected void cacheClientTemplates(RealmModel model) {
|
||||
|
@ -475,7 +482,7 @@ public class CachedRealm extends AbstractRevisioned {
|
|||
public Set<String> getEventsListeners() {
|
||||
return eventsListeners;
|
||||
}
|
||||
|
||||
|
||||
public Set<String> getEnabledEventTypes() {
|
||||
return enabledEventTypes;
|
||||
}
|
||||
|
@ -619,4 +626,28 @@ public class CachedRealm extends AbstractRevisioned {
|
|||
public Map<String, ComponentModel> getComponents() {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -182,6 +182,7 @@ public class RealmAdapter implements RealmModel, JpaModel<RealmEntity> {
|
|||
em.flush();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, String value) {
|
||||
for (RealmAttributeEntity attr : realm.getAttributes()) {
|
||||
if (attr.getName().equals(name)) {
|
||||
|
@ -197,18 +198,22 @@ public class RealmAdapter implements RealmModel, JpaModel<RealmEntity> {
|
|||
realm.getAttributes().add(attr);
|
||||
}
|
||||
|
||||
@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) {
|
||||
Iterator<RealmAttributeEntity> it = realm.getAttributes().iterator();
|
||||
while (it.hasNext()) {
|
||||
|
@ -220,6 +225,7 @@ public class RealmAdapter implements RealmModel, JpaModel<RealmEntity> {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAttribute(String name) {
|
||||
for (RealmAttributeEntity attr : realm.getAttributes()) {
|
||||
if (attr.getName().equals(name)) {
|
||||
|
@ -229,24 +235,28 @@ public class RealmAdapter implements RealmModel, JpaModel<RealmEntity> {
|
|||
return null;
|
||||
}
|
||||
|
||||
@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() {
|
||||
// should always return a copy
|
||||
Map<String, String> result = new HashMap<String, String>();
|
||||
|
@ -255,6 +265,7 @@ public class RealmAdapter implements RealmModel, JpaModel<RealmEntity> {
|
|||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBruteForceProtected() {
|
||||
return getAttribute("bruteForceProtected", false);
|
||||
|
|
|
@ -1230,7 +1230,7 @@ public class RealmAdapter extends AbstractMongoAdapter<MongoRealmEntity> impleme
|
|||
}
|
||||
updateRealm();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isAdminEventsEnabled() {
|
||||
return realm.isAdminEventsEnabled();
|
||||
|
@ -1240,7 +1240,7 @@ public class RealmAdapter extends AbstractMongoAdapter<MongoRealmEntity> impleme
|
|||
public void setAdminEventsEnabled(boolean enabled) {
|
||||
realm.setAdminEventsEnabled(enabled);
|
||||
updateRealm();
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1253,7 +1253,7 @@ public class RealmAdapter extends AbstractMongoAdapter<MongoRealmEntity> impleme
|
|||
realm.setAdminEventsDetailsEnabled(enabled);
|
||||
updateRealm();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ClientModel getMasterAdminClient() {
|
||||
MongoClientEntity appData = getMongoStore().loadEntity(MongoClientEntity.class, realm.getMasterAdminClient(), invocationContext);
|
||||
|
@ -2181,4 +2181,62 @@ public class RealmAdapter extends AbstractMongoAdapter<MongoRealmEntity> impleme
|
|||
}
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -110,6 +110,17 @@ public interface RealmModel extends RoleContainerModel {
|
|||
|
||||
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
|
||||
boolean isBruteForceProtected();
|
||||
void setBruteForceProtected(boolean value);
|
||||
|
|
|
@ -94,6 +94,8 @@ public class RealmEntity extends AbstractIdentifiableEntity {
|
|||
private Map<String, String> smtpConfig = new HashMap<String, String>();
|
||||
private Map<String, String> socialConfig = new HashMap<String, String>();
|
||||
|
||||
private Map<String, String> attributes = new HashMap<>();
|
||||
|
||||
private boolean eventsEnabled;
|
||||
private long eventsExpiration;
|
||||
private List<String> eventsListeners = new ArrayList<String>();
|
||||
|
@ -692,6 +694,13 @@ public class RealmEntity extends AbstractIdentifiableEntity {
|
|||
public void setComponentEntities(List<ComponentEntity> componentEntities) {
|
||||
this.componentEntities = componentEntities;
|
||||
}
|
||||
|
||||
public Map<String, String> getAttributes() {
|
||||
return attributes;
|
||||
}
|
||||
|
||||
public void setAttributes(Map<String, String> attributes) {
|
||||
this.attributes = attributes;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -390,6 +390,10 @@ public class ModelToRepresentation {
|
|||
exportRequiredActions(realm, rep);
|
||||
exportGroups(realm, rep);
|
||||
}
|
||||
|
||||
Map<String, String> attributes = realm.getAttributes();
|
||||
rep.setAttributes(attributes);
|
||||
|
||||
return rep;
|
||||
}
|
||||
|
||||
|
|
|
@ -373,6 +373,15 @@ public class RepresentationToModel {
|
|||
if(rep.getDefaultLocale() != null){
|
||||
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) {
|
||||
|
@ -821,6 +830,13 @@ public class RepresentationToModel {
|
|||
if (rep.getClientAuthenticationFlow() != null) {
|
||||
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
|
||||
|
|
|
@ -57,6 +57,7 @@ import java.util.LinkedList;
|
|||
import java.util.List;
|
||||
import java.util.HashSet;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
@ -237,6 +238,16 @@ public class RealmTest extends AbstractAdminTest {
|
|||
assertEquals(Boolean.FALSE, rep.isRegistrationAllowed());
|
||||
assertEquals(Boolean.FALSE, rep.isRegistrationEmailAsUsername());
|
||||
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
|
||||
|
@ -393,6 +404,13 @@ public class RealmTest extends AbstractAdminTest {
|
|||
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
|
||||
|
|
|
@ -95,5 +95,11 @@
|
|||
"roles": ["customer-user"]
|
||||
}
|
||||
]
|
||||
},
|
||||
"attributes": {
|
||||
"string-attr": "foo",
|
||||
"int-attr": "123",
|
||||
"long-attr": "1234567890123456",
|
||||
"bool-attr": "true"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue