Skip to content

Commit

Permalink
Merged in override-fixes (pull request #312)
Browse files Browse the repository at this point in the history
override fixes
  • Loading branch information
Jonathan Johnson committed Mar 7, 2019
2 parents 2a1711e + 77359f8 commit e74cbc5
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class JsonSchemaBuilderService {
description: it['helpText'],
type : it['displayType']]
if (it['displayType'] == 'boolean') {
property['default'] = (Boolean)(it['defaultValue'])
property['default'] = Boolean.valueOf(it['defaultValue'])
} else {
property['default'] = it['defaultValue']
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ private void rebuildAttributes() {
@PostLoad
public void intoTransientRepresentation() {
this.attributeRelease = getAttributeReleaseListFromAttributeList(this.attributes);
this.relyingPartyOverrides = getRelyingPartyOverridesRepresentationFromAttributeList(attributes);
this.relyingPartyOverrides = getRelyingPartyOverridesRepresentationFromAttributeList(this.attributes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import java.util.stream.Collectors;

import static edu.internet2.tier.shibboleth.admin.util.ModelRepresentationConversions.getStringListOfAttributeValues;
import static edu.internet2.tier.shibboleth.admin.util.ModelRepresentationConversions.getValueFromXMLObject;

/**
* Default implementation of {@link EntityDescriptorService}
Expand Down Expand Up @@ -675,27 +676,15 @@ public EntityDescriptorRepresentation createRepresentationFromDescriptor(org.ope
}
}

// TODO: fix this; there is a problem with the way that defaults are working and the processing from the front end
ModelRepresentationConversions.completeMe(relyingPartyOverrides);

representation.setRelyingPartyOverrides(relyingPartyOverrides);
}

return representation;
}

// TODO: remove
private String getValueFromXMLObject(XMLObject xmlObject) {
String objectType = xmlObject.getClass().getSimpleName();
switch (objectType) {
case "XSAny":
return ((XSAny)xmlObject).getTextContent();
case "XSString":
return ((XSString)xmlObject).getValue();
case "XSBoolean":
return ((XSBoolean)xmlObject).getStoredValue();
default:
throw new RuntimeException(String.format("Unsupported XML Object type [%s]", objectType));
}
return ModelRepresentationConversions.getValueFromXMLObject(xmlObject);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.util.List;
import java.util.Map;

import static edu.internet2.tier.shibboleth.admin.util.ModelRepresentationConversions.getAttributeFromObjectAndRelyingPartyOverrideProperty;

public class JPAEntityServiceImpl implements EntityService {

@Autowired
Expand Down Expand Up @@ -64,95 +66,16 @@ public List<Attribute> getAttributeListFromEntityRepresentation(EntityDescriptor

@Override
public edu.internet2.tier.shibboleth.admin.ui.domain.Attribute getAttributeFromAttributeReleaseList(List<String> attributeReleaseList) {
edu.internet2.tier.shibboleth.admin.ui.domain.Attribute attribute = ((AttributeBuilder) openSamlObjects
.getBuilderFactory()
.getBuilder(edu.internet2.tier.shibboleth.admin.ui.domain.Attribute.DEFAULT_ELEMENT_NAME))
.buildObject();

attribute.setName(MDDCConstants.RELEASE_ATTRIBUTES);

attributeReleaseList.forEach(attributeRelease -> {
XSString xsString = (XSString) openSamlObjects
.getBuilderFactory()
.getBuilder(XSString.TYPE_NAME)
.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSString.TYPE_NAME);
xsString.setValue(attributeRelease);
attribute.getAttributeValues().add(xsString);
});

return attribute;
return (edu.internet2.tier.shibboleth.admin.ui.domain.Attribute) ModelRepresentationConversions.getAttributeListFromAttributeReleaseList(attributeReleaseList).get(0);
}

@Override
public List<Attribute> getAttributeListFromAttributeReleaseList(List<String> attributeReleaseList) {
List<edu.internet2.tier.shibboleth.admin.ui.domain.Attribute> attributeList = new ArrayList<>();

if (attributeReleaseList != null && attributeReleaseList.size() > 0) {
attributeList.add(attributeUtility.createAttributeWithStringValues(MDDCConstants.RELEASE_ATTRIBUTES, attributeReleaseList));
}

return (List<Attribute>)(List<? extends Attribute>)attributeList;
}

edu.internet2.tier.shibboleth.admin.ui.domain.Attribute getAttributeFromObjectAndRelyingPartyOverrideProperty(Object o, RelyingPartyOverrideProperty overrideProperty) {
switch (ModelRepresentationConversions.AttributeTypes.valueOf(overrideProperty.getDisplayType().toUpperCase())) {
case BOOLEAN:
if ((o instanceof Boolean && ((Boolean)o)) ||
(o instanceof String) && Boolean.valueOf((String)o)) {
if (overrideProperty.getPersistType() != null &&
!overrideProperty.getPersistType().equalsIgnoreCase("boolean")) {
return attributeUtility.createAttributeWithStringValues(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
overrideProperty.getPersistValue());
} else {
if (o instanceof String) {
return attributeUtility.createAttributeWithBooleanValue(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
Boolean.valueOf((String) o));
} else {
Boolean value = Boolean.valueOf(overrideProperty.getInvert()) ^ (Boolean)o;
return attributeUtility.createAttributeWithBooleanValue(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
value);
}
}
}
return null;
case INTEGER:
return attributeUtility.createAttributeWithIntegerValue(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
Integer.valueOf((String) o));
case STRING:
return attributeUtility.createAttributeWithStringValues(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
(String) o);
case SET:
return attributeUtility.createAttributeWithStringValues(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
(List<String>) o);
case LIST:
return attributeUtility.createAttributeWithStringValues(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
(List<String>) o);
default:
throw new UnsupportedOperationException("getAttributeListFromRelyingPartyOverridesRepresentation was called with an unsupported type (" + overrideProperty.getDisplayType() + ")!");
}
return ModelRepresentationConversions.getAttributeListFromAttributeReleaseList(attributeReleaseList);
}

@Override
public List<Attribute> getAttributeListFromRelyingPartyOverridesRepresentation(Map<String, Object> relyingPartyOverridesRepresentation) {
List<RelyingPartyOverrideProperty> overridePropertyList = customPropertiesConfiguration.getOverrides();
List<edu.internet2.tier.shibboleth.admin.ui.domain.Attribute> list = new ArrayList<>();

for (Map.Entry entry : relyingPartyOverridesRepresentation.entrySet()) {
String key = (String) entry.getKey();
RelyingPartyOverrideProperty overrideProperty = overridePropertyList.stream().filter(op -> op.getName().equals(key)).findFirst().get();
edu.internet2.tier.shibboleth.admin.ui.domain.Attribute attribute = getAttributeFromObjectAndRelyingPartyOverrideProperty(entry.getValue(), overrideProperty);
if (attribute != null) {
list.add(attribute);
}
}

return (List<org.opensaml.saml.saml2.core.Attribute>) (List<? extends org.opensaml.saml.saml2.core.Attribute>) list;
return ModelRepresentationConversions.getAttributeListFromRelyingPartyOverridesRepresentation(relyingPartyOverridesRepresentation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,6 @@ public static Map<String, Object> getRelyingPartyOverridesRepresentationFromAttr
return relyingPartyOverrides;
}

// TODO: fix this; currently there is a problem with not returning a value
public static Map<String,Object> completeMe(Map<String, Object> relyingPartyOverrides) {
customPropertiesConfiguration
.getOverrides()
.stream()
.filter(o -> !relyingPartyOverrides.containsKey(o.getName()))
.filter(o -> o.getDisplayType().equals("boolean"))
.forEach(p -> relyingPartyOverrides.put(p.getName(), getDefaultValueFromProperty(p)));
return relyingPartyOverrides;
}

private static Object getDefaultValueFromProperty(RelyingPartyOverrideProperty property) {
switch (property.getDisplayType()) {
case "boolean":
Expand All @@ -127,9 +116,9 @@ public static Object getOverrideFromAttribute(Attribute attribute) {
case BOOLEAN:
if (relyingPartyOverrideProperty.getPersistType() != null
&& (!relyingPartyOverrideProperty.getPersistType().equalsIgnoreCase("boolean"))) {
return true;
return relyingPartyOverrideProperty.getPersistValue().equals(getValueFromXMLObject(attributeValues.get(0)));
} else {
return Boolean.valueOf(((XSBoolean) attributeValues.get(0)).getStoredValue());
return Boolean.valueOf(relyingPartyOverrideProperty.getInvert()) ^ Boolean.valueOf(((XSBoolean) attributeValues.get(0)).getStoredValue());
}
case INTEGER:
return ((XSInteger) attributeValues.get(0)).getValue();
Expand All @@ -147,6 +136,20 @@ public static Object getOverrideFromAttribute(Attribute attribute) {
}
}

public static String getValueFromXMLObject(XMLObject xmlObject) {
String objectType = xmlObject.getClass().getSimpleName();
switch (objectType) {
case "XSAny":
return ((XSAny)xmlObject).getTextContent();
case "XSString":
return ((XSString)xmlObject).getValue();
case "XSBoolean":
return ((XSBoolean)xmlObject).getStoredValue();
default:
throw new RuntimeException(String.format("Unsupported XML Object type [%s]", objectType));
}
}

public static List<org.opensaml.saml.saml2.core.Attribute> getAttributeListFromAttributeReleaseList(List<String> attributeReleaseList) {
List<edu.internet2.tier.shibboleth.admin.ui.domain.Attribute> attributeList = new ArrayList<>();

Expand All @@ -166,62 +169,61 @@ public static List<org.opensaml.saml.saml2.core.Attribute> getAttributeListFromA
for (Map.Entry entry : relyingPartyOverridesRepresentation.entrySet()) {
String key = (String) entry.getKey();
RelyingPartyOverrideProperty overrideProperty = overridePropertyList.stream().filter(op -> op.getName().equals(key)).findFirst().get();
switch (AttributeTypes.valueOf(overrideProperty.getDisplayType().toUpperCase())) {
case BOOLEAN:
if (overrideProperty.getPersistType() != null &&
!overrideProperty.getPersistType().equals(overrideProperty.getDisplayType())) {
list.add(ATTRIBUTE_UTILITY.createAttributeWithStringValues(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
overrideProperty.getPersistValue()));
} else {
list.add(ATTRIBUTE_UTILITY.createAttributeWithBooleanValue(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
(Boolean) entry.getValue()));
}
break;
case INTEGER:
list.add(ATTRIBUTE_UTILITY.createAttributeWithIntegerValue(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
(Integer) entry.getValue()));
break;
case STRING:
list.add(ATTRIBUTE_UTILITY.createAttributeWithStringValues(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
(String) entry.getValue()));
break;
case SET:
Set<String> setValues;
if (entry.getValue() instanceof Set) {
setValues = (Set<String>) entry.getValue();
} else if (entry.getValue() instanceof List) {
setValues = new HashSet<>();
setValues.addAll((List<String>) entry.getValue());
} else {
throw new UnsupportedOperationException("The collection passed from the UI is neither a Set or List. This shouldn't happen. Fix this!");
}
if (setValues.size() > 0) {
list.add(ATTRIBUTE_UTILITY.createAttributeWithStringValues(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
new ArrayList<>(setValues)));
}
break;
case LIST:
List<String> listValues = (List<String>) entry.getValue();
if (listValues.size() > 0) {
list.add(ATTRIBUTE_UTILITY.createAttributeWithStringValues(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
listValues));
}
break;
default:
throw new UnsupportedOperationException("getAttributeListFromRelyingPartyOverridesRepresentation was called with an unsupported type (" + overrideProperty.getDisplayType() + ")!");
Attribute attribute = getAttributeFromObjectAndRelyingPartyOverrideProperty(entry.getValue(), overrideProperty);
if (attribute != null) {
list.add(attribute);
}
}
}

return (List<org.opensaml.saml.saml2.core.Attribute>) (List<? extends org.opensaml.saml.saml2.core.Attribute>) list;
}

public static Attribute getAttributeFromObjectAndRelyingPartyOverrideProperty(Object o, RelyingPartyOverrideProperty overrideProperty) {
switch (ModelRepresentationConversions.AttributeTypes.valueOf(overrideProperty.getDisplayType().toUpperCase())) {
case BOOLEAN:
if ((o instanceof Boolean && ((Boolean)o)) ||
(o instanceof String) && Boolean.valueOf((String)o)) {
if (overrideProperty.getPersistType() != null &&
!overrideProperty.getPersistType().equalsIgnoreCase("boolean")) {
return ATTRIBUTE_UTILITY.createAttributeWithStringValues(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
overrideProperty.getPersistValue());
} else {
if (o instanceof String) {
return ATTRIBUTE_UTILITY.createAttributeWithBooleanValue(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
Boolean.valueOf((String) o));
} else {
Boolean value = Boolean.valueOf(overrideProperty.getInvert()) ^ (Boolean)o;
return ATTRIBUTE_UTILITY.createAttributeWithBooleanValue(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
value);
}
}
}
return null;
case INTEGER:
return ATTRIBUTE_UTILITY.createAttributeWithIntegerValue(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
Integer.valueOf((String) o));
case STRING:
return ATTRIBUTE_UTILITY.createAttributeWithStringValues(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
(String) o);
case SET:
return ATTRIBUTE_UTILITY.createAttributeWithStringValues(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
(List<String>) o);
case LIST:
return ATTRIBUTE_UTILITY.createAttributeWithStringValues(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
(List<String>) o);
default:
throw new UnsupportedOperationException("getAttributeListFromRelyingPartyOverridesRepresentation was called with an unsupported type (" + overrideProperty.getDisplayType() + ")!");
}
}


public enum AttributeTypes {
BOOLEAN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import edu.internet2.tier.shibboleth.admin.ui.domain.RelyingPartyOverridePropert
import edu.internet2.tier.shibboleth.admin.ui.domain.XSBoolean
import edu.internet2.tier.shibboleth.admin.ui.opensaml.OpenSamlObjects
import edu.internet2.tier.shibboleth.admin.util.AttributeUtility
import edu.internet2.tier.shibboleth.admin.util.ModelRepresentationConversions
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll
Expand Down Expand Up @@ -33,7 +34,7 @@ class AuxiliaryJPAEntityServiceTests extends Specification {
displayType: 'boolean',
invert: 'true'
)
Attribute att = jpaEntityService.getAttributeFromObjectAndRelyingPartyOverrideProperty(input, overrideProperty)
Attribute att = ModelRepresentationConversions.getAttributeFromObjectAndRelyingPartyOverrideProperty(input, overrideProperty)

expect:
assert att && att.getAttributeValues()[0] instanceof XSBoolean && ((XSBoolean) att.getAttributeValues()[0]).value.value == output
Expand Down
Loading

0 comments on commit e74cbc5

Please sign in to comment.