Search in sources :

Example 11 with ExtensionField

use of io.jans.scim.model.scim2.extensions.ExtensionField in project oxTrust by GluuFederation.

the class LdapFilterListener method enterAttrexp.

@Override
public void enterAttrexp(ScimFilterParser.AttrexpContext ctx) {
    if (StringUtils.isEmpty(error)) {
        log.trace("enterAttrexp.");
        String path = ctx.attrpath().getText();
        ScimFilterParser.CompvalueContext compValueCtx = ctx.compvalue();
        boolean isPrRule = compValueCtx == null && ctx.getChild(1).getText().equals("pr");
        Type attrType = null;
        Attribute attrAnnot = IntrospectUtil.getFieldAnnotation(path, resourceClass, Attribute.class);
        String ldapAttribute = null;
        boolean isNested = false;
        if (attrAnnot == null) {
            ExtensionField field = extService.getFieldOfExtendedAttribute(resourceClass, path);
            if (field == null)
                error = String.format("Attribute path '%s' is not recognized in %s", path, resourceClass.getSimpleName());
            else {
                attrType = field.getAttributeDefinitionType();
                ldapAttribute = path.substring(path.lastIndexOf(":") + 1);
            }
        } else {
            attrType = attrAnnot.type();
            Pair<String, Boolean> pair = FilterUtil.getLdapAttributeOfResourceAttribute(path, resourceClass);
            ldapAttribute = pair.getFirst();
            isNested = pair.getSecond();
        }
        if (error != null)
            // Intentionally left empty
            ;
        else if (attrType == null)
            error = String.format("Could not determine type of attribute path '%s' in %s", path, resourceClass.getSimpleName());
        else if (ldapAttribute == null)
            error = String.format("Could not determine LDAP attribute for path '%s' in %s", path, resourceClass.getSimpleName());
        else {
            String subattr = isNested ? path.substring(path.lastIndexOf(".") + 1) : null;
            String subFilth;
            CompValueType type;
            ScimOperator operator;
            if (isPrRule) {
                type = CompValueType.NULL;
                operator = ScimOperator.NOT_EQUAL;
            } else {
                type = FilterUtil.getCompValueType(compValueCtx);
                operator = ScimOperator.getByValue(ctx.compareop().getText());
            }
            error = FilterUtil.checkFilterConsistency(path, attrType, type, operator);
            if (error == null) {
                subFilth = getSubFilter(subattr, ldapAttribute, isPrRule ? null : compValueCtx.getText(), attrType, type, operator);
                if (subFilth == null) {
                    if (error == null)
                        error = String.format("Operator '%s' is not supported for attribute %s", operator.getValue(), path);
                } else
                    filter.append(subFilth);
            }
        }
    }
}
Also used : Type(org.gluu.oxtrust.model.scim2.AttributeDefinition.Type) CompValueType(org.gluu.oxtrust.service.antlr.scimFilter.enums.CompValueType) ExtensionField(org.gluu.oxtrust.model.scim2.extensions.ExtensionField) Attribute(org.gluu.oxtrust.model.scim2.annotations.Attribute) ScimOperator(org.gluu.oxtrust.service.antlr.scimFilter.enums.ScimOperator) ScimFilterParser(org.gluu.oxtrust.service.antlr.scimFilter.antlr4.ScimFilterParser) CompValueType(org.gluu.oxtrust.service.antlr.scimFilter.enums.CompValueType)

Example 12 with ExtensionField

use of io.jans.scim.model.scim2.extensions.ExtensionField in project oxTrust by GluuFederation.

the class SimpleExpression method evaluate.

public Boolean evaluate(Map<String, Object> item) {
    /*
        There are 3 categories for attribute operators:
        - eq, ne (applicable to all types)
        - co, sw, ew (applicable to STRING, REFERENCE)
        - gt, ge, lt, le (applicable to STRING, DECIMAL, REFERENCE, DATETIME)
         */
    Boolean val = null;
    Type attrType = null;
    log.trace("SimpleExpression.evaluate.");
    String msg = String.format("%s%s", StringUtils.isEmpty(parentAttribute) ? "" : (parentAttribute + "."), resourceClass.getSimpleName());
    Attribute attrAnnot = getAttributeAnnotation();
    if (attrAnnot == null) {
        if (extService != null) {
            ExtensionField field = extService.getFieldOfExtendedAttribute(resourceClass, attribute);
            if (field == null)
                log.error("SimpleExpression.evaluate. Attribute '{}' is not recognized in {}", attribute, msg);
            else
                attrType = field.getAttributeDefinitionType();
        }
    } else
        attrType = attrAnnot.type();
    if (attrType == null) {
        log.error("SimpleExpression.evaluate. Could not determine type of attribute '{}' in {}", attribute, msg);
    } else {
        String errMsg = FilterUtil.checkFilterConsistency(attribute, attrType, type, operator);
        if (errMsg == null) {
            Object currentAttrValue = item.get(attribute);
            if (type.equals(CompValueType.NULL)) {
                // implies attributeValue==null
                log.trace("SimpleExpression.evaluate. Using null as compare value");
                val = operator.equals(ScimOperator.EQUAL) ? currentAttrValue == null : currentAttrValue != null;
            } else if (currentAttrValue == null) {
                // If value is absent, filter won't match against anything (only when comparing with null as in previous case)
                log.trace("SimpleExpression.evaluate. Attribute \"{}\" is absent in resource data", attribute);
                val = false;
            } else if (// check it's a string or reference
            Type.STRING.equals(attrType) || Type.REFERENCE.equals(attrType))
                val = evaluateStringAttribute(attrAnnot != null && attrAnnot.isCaseExact(), currentAttrValue);
            else if (Type.INTEGER.equals(attrType) || Type.DECIMAL.equals(attrType))
                val = evaluateNumericAttribute(attrType, currentAttrValue);
            else if (Type.BOOLEAN.equals(attrType))
                val = evaluateBooleanAttribute(attrType, currentAttrValue);
            else if (Type.DATETIME.equals(attrType))
                val = evaluateDateTimeAttribute(attrType, currentAttrValue);
        } else
            log.error("SimpleExpression.evaluate. {}", errMsg);
    }
    return val;
}
Also used : Type(org.gluu.oxtrust.model.scim2.AttributeDefinition.Type) CompValueType(org.gluu.oxtrust.service.antlr.scimFilter.enums.CompValueType) ExtensionField(org.gluu.oxtrust.model.scim2.extensions.ExtensionField) Attribute(org.gluu.oxtrust.model.scim2.annotations.Attribute)

Example 13 with ExtensionField

use of io.jans.scim.model.scim2.extensions.ExtensionField in project jans by JanssenProject.

the class ScimResourceUtil method attachExtensionInfo.

private static void attachExtensionInfo(Map<String, Object> source, Map<String, Object> destination, List<Extension> extensions, boolean replacing) {
    log.debug("attachExtensionInfo");
    for (Extension extension : extensions) {
        String urn = extension.getUrn();
        Object extendedAttrsObj = source.get(urn);
        if (extendedAttrsObj != null) {
            Map<String, Object> extendedAttrs = IntrospectUtil.strObjMap(extendedAttrsObj);
            Map<String, ExtensionField> fields = extension.getFields();
            Map<String, Object> destMap = destination.get(urn) == null ? new HashMap<>() : IntrospectUtil.strObjMap(destination.get(urn));
            for (String attr : fields.keySet()) {
                Object value = extendedAttrs.get(attr);
                if (value != null) {
                    if (IntrospectUtil.isCollection(value.getClass())) {
                        Collection col = (Collection) value;
                        if (!replacing) {
                            Object destValue = destMap.get(attr);
                            if (destValue != null) {
                                if (!IntrospectUtil.isCollection(destValue.getClass()))
                                    log.warn("Value {} was expected to be a collection", destValue);
                                else
                                    col.addAll((Collection) destMap.get(attr));
                            }
                        }
                        value = col.size() == 0 ? null : col;
                    }
                    destMap.put(attr, value);
                }
            }
            destination.put(urn, destMap);
        }
    }
}
Also used : Extension(io.jans.scim.model.scim2.extensions.Extension) ExtensionField(io.jans.scim.model.scim2.extensions.ExtensionField)

Example 14 with ExtensionField

use of io.jans.scim.model.scim2.extensions.ExtensionField in project jans by JanssenProject.

the class SimpleExpression method evaluate.

public Boolean evaluate(Map<String, Object> item) {
    /*
        There are 3 categories for attribute operators:
        - eq, ne (applicable to all types)
        - co, sw, ew (applicable to STRING, REFERENCE)
        - gt, ge, lt, le (applicable to STRING, DECIMAL, REFERENCE, DATETIME)
         */
    Boolean val = null;
    Type attrType = null;
    log.trace("SimpleExpression.evaluate.");
    String msg = String.format("%s%s", StringUtils.isEmpty(parentAttribute) ? "" : (parentAttribute + "."), resourceClass.getSimpleName());
    Attribute attrAnnot = getAttributeAnnotation();
    if (attrAnnot == null) {
        if (extService != null) {
            ExtensionField field = extService.getFieldOfExtendedAttribute(resourceClass, attribute);
            if (field == null)
                log.error("SimpleExpression.evaluate. Attribute '{}' is not recognized in {}", attribute, msg);
            else
                attrType = field.getAttributeDefinitionType();
        }
    } else
        attrType = attrAnnot.type();
    if (attrType == null) {
        log.error("SimpleExpression.evaluate. Could not determine type of attribute '{}' in {}", attribute, msg);
    } else {
        String errMsg = FilterUtil.checkFilterConsistency(attribute, attrType, type, operator);
        if (errMsg == null) {
            Object currentAttrValue = item.get(attribute);
            if (type.equals(CompValueType.NULL)) {
                // implies attributeValue==null
                log.trace("SimpleExpression.evaluate. Using null as compare value");
                val = operator.equals(ScimOperator.EQUAL) ? currentAttrValue == null : currentAttrValue != null;
            } else if (currentAttrValue == null) {
                // If value is absent, filter won't match against anything (only when comparing with null as in previous case)
                log.trace("SimpleExpression.evaluate. Attribute \"{}\" is absent in resource data", attribute);
                val = false;
            } else if (// check it's a string or reference
            Type.STRING.equals(attrType) || Type.REFERENCE.equals(attrType))
                val = evaluateStringAttribute(attrAnnot != null && attrAnnot.isCaseExact(), currentAttrValue);
            else if (Type.INTEGER.equals(attrType) || Type.DECIMAL.equals(attrType))
                val = evaluateNumericAttribute(attrType, currentAttrValue);
            else if (Type.BOOLEAN.equals(attrType))
                val = evaluateBooleanAttribute(attrType, currentAttrValue);
            else if (Type.DATETIME.equals(attrType))
                val = evaluateDateTimeAttribute(attrType, currentAttrValue);
        } else
            log.error("SimpleExpression.evaluate. {}", errMsg);
    }
    return val;
}
Also used : Type(io.jans.scim.model.scim2.AttributeDefinition.Type) CompValueType(io.jans.scim.service.antlr.scimFilter.enums.CompValueType) ExtensionField(io.jans.scim.model.scim2.extensions.ExtensionField) Attribute(io.jans.scim.model.scim2.annotations.Attribute)

Example 15 with ExtensionField

use of io.jans.scim.model.scim2.extensions.ExtensionField in project jans by JanssenProject.

the class ResourceValidator method validateExtendedAttributes.

/**
 * Inspects the resource passed in the constructor and for every extended attribute (see {@link BaseScimResource#getCustomAttributes()},
 * the attribute's value is checked to see if it complies with the data type it is supposed to belong to. This
 * information is obtained from the list of <code>Extension</code>s passed in the constructor (every {@link ExtensionField}
 * has an associated {@link ExtensionField#getType() type}.
 * <p>When an attribute is {@link ExtensionField#isMultiValued() multi-valued}, every single item inside the collection
 * is validated.</p>
 * @throws SCIMException When any of the validations do not pass or an attribute seems not to be part of a known schema.
 */
public void validateExtendedAttributes() throws SCIMException {
    // Note: throughout this method, we always ignore presence of nulls
    // Gets all extended attributes (see the @JsonAnySetter annotation in BaseScimResource)
    Map<String, Object> extendedAttributes = resource.getCustomAttributes();
    // Iterate over every extension of the resource object (in practice it will be just one at most)
    for (String schema : extendedAttributes.keySet()) {
        // Validate if the schema referenced in the extended attributes is contained in the valid set of extension
        Extension extension = null;
        for (Extension ext : extensions) if (ext.getUrn().equals(schema)) {
            extension = ext;
            break;
        }
        if (extension != null) {
            log.debug("validateExtendedAttributes. Revising attributes under schema {}", schema);
            try {
                // Obtains a generic map consisting of all name/value(s) pairs associated to this schema
                Map<String, Object> attrsMap = IntrospectUtil.strObjMap(extendedAttributes.get(schema));
                for (String attr : attrsMap.keySet()) {
                    Object value = attrsMap.get(attr);
                    if (value != null) {
                        /*
                             Gets the class associated to the value of current attribute. For extended attributes, we
                             should only see coming: String, Integer, Double, boolean, and Collection.
                             Different things will be rejected
                             */
                        Class cls = value.getClass();
                        boolean isCollection = IntrospectUtil.isCollection(cls);
                        // If the attribute coming is unknown, NPE will be thrown and we are covered
                        log.debug("validateExtendedAttributes. Got value(s) for attribute '{}'", attr);
                        // Check if the multivalued custom attribute is consistent with the nature of the value itself
                        if (isCollection == extension.getFields().get(attr).isMultiValued()) {
                            if (isCollection) {
                                for (Object elem : (Collection) value) if (elem != null)
                                    validateDataTypeExtendedAttr(extension, attr, elem);
                            } else
                                validateDataTypeExtendedAttr(extension, attr, value);
                        } else
                            throw new SCIMException(ERROR_PARSING_EXTENDED);
                    }
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                throw new SCIMException(ERROR_PARSING_EXTENDED);
            }
        } else
            throw new SCIMException(String.format(UNKNOWN_EXTENSION, schema));
    }
}
Also used : Extension(io.jans.scim.model.scim2.extensions.Extension) SCIMException(io.jans.scim.model.exception.SCIMException) SCIMException(io.jans.scim.model.exception.SCIMException)

Aggregations

ExtensionField (io.jans.scim.model.scim2.extensions.ExtensionField)8 Extension (io.jans.scim.model.scim2.extensions.Extension)7 ExtensionField (org.gluu.oxtrust.model.scim2.extensions.ExtensionField)7 Extension (org.gluu.oxtrust.model.scim2.extensions.Extension)5 HashMap (java.util.HashMap)3 Type (io.jans.scim.model.scim2.AttributeDefinition.Type)2 Attribute (io.jans.scim.model.scim2.annotations.Attribute)2 CompValueType (io.jans.scim.service.antlr.scimFilter.enums.CompValueType)2 ArrayList (java.util.ArrayList)2 Type (org.gluu.oxtrust.model.scim2.AttributeDefinition.Type)2 Attribute (org.gluu.oxtrust.model.scim2.annotations.Attribute)2 CompValueType (org.gluu.oxtrust.service.antlr.scimFilter.enums.CompValueType)2 GluuAttribute (io.jans.model.GluuAttribute)1 Filter (io.jans.orm.search.filter.Filter)1 SCIMException (io.jans.scim.model.exception.SCIMException)1 AttributeDefinition (io.jans.scim.model.scim2.AttributeDefinition)1 Meta (io.jans.scim.model.scim2.Meta)1 SchemaAttribute (io.jans.scim.model.scim2.provider.schema.SchemaAttribute)1 SchemaResource (io.jans.scim.model.scim2.provider.schema.SchemaResource)1 ScimFilterParser (io.jans.scim.service.antlr.scimFilter.antlr4.ScimFilterParser)1