Skip to content

Commit

Permalink
[NOISSUE]
Browse files Browse the repository at this point in the history
allow inverted storage of attributes
  • Loading branch information
jj committed Mar 5, 2019
1 parent 9bcaf38 commit cd289d1
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class RelyingPartyOverrideProperty {
private String persistValue;
private String attributeName;
private String attributeFriendlyName;
private String invert;

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,51 @@ public List<Attribute> getAttributeListFromAttributeReleaseList(List<String> att
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) || (!(Boolean)o && Boolean.valueOf(overrideProperty.getInvert()))) ||
(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 : (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() + ")!");
}
}

@Override
public List<Attribute> getAttributeListFromRelyingPartyOverridesRepresentation(Map<String, Object> relyingPartyOverridesRepresentation) {
List<RelyingPartyOverrideProperty> overridePropertyList = customPropertiesConfiguration.getOverrides();
Expand All @@ -102,50 +147,9 @@ public List<Attribute> getAttributeListFromRelyingPartyOverridesRepresentation(M
for (Map.Entry entry : relyingPartyOverridesRepresentation.entrySet()) {
String key = (String) entry.getKey();
RelyingPartyOverrideProperty overrideProperty = overridePropertyList.stream().filter(op -> op.getName().equals(key)).findFirst().get();
switch (ModelRepresentationConversions.AttributeTypes.valueOf(overrideProperty.getDisplayType().toUpperCase())) {
case BOOLEAN:
if ((entry.getValue() instanceof Boolean && (Boolean)entry.getValue()) ||
((entry.getValue() instanceof String) && Boolean.valueOf((String)entry.getValue()))) {
if (overrideProperty.getPersistType() != null &&
!overrideProperty.getPersistType().equalsIgnoreCase("boolean")) {
list.add(attributeUtility.createAttributeWithStringValues(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
overrideProperty.getPersistValue()));
} else {
if (entry.getValue() instanceof String) {
list.add(attributeUtility.createAttributeWithBooleanValue(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
Boolean.valueOf((String) entry.getValue())));
} else {
list.add(attributeUtility.createAttributeWithBooleanValue(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
(Boolean) entry.getValue()));
}
}
}
break;
case INTEGER:
list.add(attributeUtility.createAttributeWithIntegerValue(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
Integer.valueOf((String) entry.getValue())));
break;
case STRING:
list.add(attributeUtility.createAttributeWithStringValues(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
(String) entry.getValue()));
break;
case SET:
list.add(attributeUtility.createAttributeWithStringValues(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
(List<String>) entry.getValue()));
break;
case LIST:
list.add(attributeUtility.createAttributeWithStringValues(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
(List<String>) entry.getValue()));
break;
default:
throw new UnsupportedOperationException("getAttributeListFromRelyingPartyOverridesRepresentation was called with an unsupported type (" + overrideProperty.getDisplayType() + ")!");
edu.internet2.tier.shibboleth.admin.ui.domain.Attribute attribute = getAttributeFromObjectAndRelyingPartyOverrideProperty(entry.getValue(), overrideProperty);
if (attribute != null) {
list.add(attribute);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package edu.internet2.tier.shibboleth.admin.ui.service

import edu.internet2.tier.shibboleth.admin.ui.domain.Attribute
import edu.internet2.tier.shibboleth.admin.ui.domain.RelyingPartyOverrideProperty
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 spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll

class AuxiliaryJPAEntityServiceTests extends Specification {
@Shared
JPAEntityServiceImpl jpaEntityService

def setup() {
def openSamlObjects = new OpenSamlObjects().with {
it.init()
it
}

def attributeUtility = new AttributeUtility(openSamlObjects)

jpaEntityService = new JPAEntityServiceImpl(openSamlObjects, attributeUtility)
}

@Unroll
def "test invert #input"() {
setup:
RelyingPartyOverrideProperty overrideProperty = new RelyingPartyOverrideProperty(
attributeName: 'name',
attributeFriendlyName: 'friendlyName',
displayType: 'boolean',
invert: 'true'
)
Attribute att = jpaEntityService.getAttributeFromObjectAndRelyingPartyOverrideProperty(input, overrideProperty)

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

where:
input | output
true | false
false | true
}
}

0 comments on commit cd289d1

Please sign in to comment.