Search in sources :

Example 6 with ExtensionField

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

the class ExtensionService method getResourceExtensions.

public List<Extension> getResourceExtensions(Class<? extends BaseScimResource> cls) {
    List<Extension> list = new ArrayList<>();
    try {
        // Currently support one extension only for User Resource
        if (cls.equals(UserResource.class)) {
            Map<String, ExtensionField> fields = new HashMap<>();
            for (GluuAttribute attribute : attributeService.getSCIMRelatedAttributes()) {
                if (Optional.ofNullable(attribute.getScimCustomAttr()).orElse(false)) {
                    // first non-null check is needed because certain entries do not have the multivalue attribute set
                    ExtensionField field = new ExtensionField();
                    field.setDescription(attribute.getDescription());
                    field.setType(attribute.getDataType());
                    field.setMultiValued(Optional.ofNullable(attribute.getOxMultiValuedAttribute()).orElse(false));
                    field.setName(attribute.getName());
                    fields.put(attribute.getName(), field);
                }
            }
            String uri = appConfiguration.getUserExtensionSchemaURI();
            if (StringUtils.isEmpty(uri)) {
                uri = USER_EXT_SCHEMA_ID;
            }
            Extension ext = new Extension(uri);
            ext.setFields(fields);
            if (uri.equals(USER_EXT_SCHEMA_ID)) {
                ext.setName(USER_EXT_SCHEMA_NAME);
                ext.setDescription(USER_EXT_SCHEMA_DESCRIPTION);
            }
            list.add(ext);
        }
    } catch (Exception e) {
        log.error("An error ocurred when building extension for {}", cls.getName());
        log.error(e.getMessage(), e);
    }
    return list;
}
Also used : Extension(io.jans.scim.model.scim2.extensions.Extension) ExtensionField(io.jans.scim.model.scim2.extensions.ExtensionField) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GluuAttribute(io.jans.model.GluuAttribute)

Example 7 with ExtensionField

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

the class Scim2UserService method transferExtendedAttributesToPerson.

/**
 * Takes all extended attributes found in the SCIM resource and copies them to a
 * ScimCustomPerson This method is called after validations take place (see
 * associated decorator for User Service), so all inputs are OK and can go
 * straight to LDAP with no runtime surprises
 *
 * @param resource
 *            A SCIM resource used as origin of data
 * @param person
 *            a ScimCustomPerson used as destination
 */
private void transferExtendedAttributesToPerson(BaseScimResource resource, ScimCustomPerson person) {
    try {
        // Gets all the extended attributes for this resource
        Map<String, Object> extendedAttrs = resource.getCustomAttributes();
        // Iterates over all extensions this type of resource might have
        for (Extension extension : extService.getResourceExtensions(resource.getClass())) {
            Object val = extendedAttrs.get(extension.getUrn());
            if (val != null) {
                // Obtains the attribute/value(s) pairs in the current extension
                Map<String, Object> attrsMap = IntrospectUtil.strObjMap(val);
                for (String attribute : attrsMap.keySet()) {
                    Object value = attrsMap.get(attribute);
                    if (value == null) {
                        // Attribute was unassigned in this resource: drop it from destination too
                        log.debug("transferExtendedAttributesToPerson. Flushing attribute {}", attribute);
                        person.setAttribute(attribute, (String) null);
                    } else {
                        ExtensionField field = extension.getFields().get(attribute);
                        if (field.isMultiValued()) {
                            person.setCustomAttribute(attribute, extService.getAttributeValues(field, (Collection) value, ldapBackend));
                        } else {
                            person.setCustomAttribute(attribute, extService.getAttributeValue(field, value, ldapBackend));
                        }
                        log.debug("transferExtendedAttributesToPerson. Setting attribute '{}' with values {}", attribute, person.getTypedAttribute(attribute).getDisplayValue());
                    }
                }
            }
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
}
Also used : Extension(io.jans.scim.model.scim2.extensions.Extension) ExtensionField(io.jans.scim.model.scim2.extensions.ExtensionField) Collection(java.util.Collection) WebApplicationException(javax.ws.rs.WebApplicationException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException)

Example 8 with ExtensionField

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

the class Scim2UserService method transferExtendedAttributesToResource.

private void transferExtendedAttributesToResource(ScimCustomPerson person, BaseScimResource resource) {
    log.debug("transferExtendedAttributesToResource of type {}", ScimResourceUtil.getType(resource.getClass()));
    // Gets the list of extensions associated to the resource passed. In practice,
    // this will be at most a singleton list
    List<Extension> extensions = extService.getResourceExtensions(resource.getClass());
    // resource
    for (Extension extension : extensions) {
        Map<String, ExtensionField> fields = extension.getFields();
        // Create empty map to store the values of the extended attributes found for
        // current extension in object person
        Map<String, Object> map = new HashMap<>();
        log.debug("transferExtendedAttributesToResource. Revising attributes of extension '{}'", extension.getUrn());
        // Iterate over every attribute part of this extension
        for (String attr : fields.keySet()) {
            // Gets the values associated to this attribute that were found in LDAP
            String[] values = person.getAttributes(attr);
            if (values != null) {
                log.debug("transferExtendedAttributesToResource. Copying to resource the value(s) for attribute '{}'", attr);
                ExtensionField field = fields.get(attr);
                List<Object> convertedValues = extService.convertValues(field, values, ldapBackend);
                if (convertedValues.size() > 0) {
                    map.put(attr, field.isMultiValued() ? convertedValues : convertedValues.get(0));
                }
            }
        }
        // Stores all extended attributes (with their values) in the resource object
        if (map.size() > 0) {
            resource.addCustomAttributes(extension.getUrn(), map);
        }
    }
    for (String urn : resource.getCustomAttributes().keySet()) {
        resource.getSchemas().add(urn);
    }
}
Also used : Extension(io.jans.scim.model.scim2.extensions.Extension) ExtensionField(io.jans.scim.model.scim2.extensions.ExtensionField) HashMap(java.util.HashMap)

Example 9 with ExtensionField

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

the class FilterListener 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;
        Boolean multiValued = 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();
                multiValued = field.isMultiValued();
                ldapAttribute = path.substring(path.lastIndexOf(":") + 1);
            }
        } else {
            attrType = attrAnnot.type();
            Pair<String, Boolean> pair = FilterUtil.getLdapAttributeOfResourceAttribute(path, resourceClass);
            ldapAttribute = pair.getFirst();
            isNested = pair.getSecond();
            multiValued = computeMultivaluedForCoreAttribute(path, attrAnnot, ldapAttribute);
        }
        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;
            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) {
                Pair<Filter, String> subf = subFilterGenerator.build(subattr, ldapAttribute, isPrRule ? null : compValueCtx.getText(), attrType, type, operator, multiValued);
                Filter subFilth = subf.getFirst();
                error = subf.getSecond();
                if (subFilth == null) {
                    if (error == null) {
                        error = String.format("Operator '%s' is not supported for attribute %s", operator.getValue(), path);
                    }
                } else {
                    filter.push(subFilth);
                }
            }
        }
    }
}
Also used : Attribute(io.jans.scim.model.scim2.annotations.Attribute) ScimOperator(io.jans.scim.service.antlr.scimFilter.enums.ScimOperator) Type(io.jans.scim.model.scim2.AttributeDefinition.Type) NullType(javax.lang.model.type.NullType) CompValueType(io.jans.scim.service.antlr.scimFilter.enums.CompValueType) ExtensionField(io.jans.scim.model.scim2.extensions.ExtensionField) Filter(io.jans.orm.search.filter.Filter) ScimFilterParser(io.jans.scim.service.antlr.scimFilter.antlr4.ScimFilterParser) CompValueType(io.jans.scim.service.antlr.scimFilter.enums.CompValueType)

Example 10 with ExtensionField

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

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<String, Object>() : 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(org.gluu.oxtrust.model.scim2.extensions.Extension) ExtensionField(org.gluu.oxtrust.model.scim2.extensions.ExtensionField)

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