Search in sources :

Example 6 with PlainAttrValue

use of org.apache.syncope.core.persistence.api.entity.PlainAttrValue in project syncope by apache.

the class ConfigurationDataBinderImpl method fillAttr.

private void fillAttr(final List<String> values, final PlainSchema schema, final CPlainAttr attr, final SyncopeClientException invalidValues) {
    // if schema is multivalue, all values are considered for addition;
    // otherwise only the fist one - if provided - is considered
    List<String> valuesProvided = schema.isMultivalue() ? values : (values.isEmpty() ? Collections.<String>emptyList() : Collections.singletonList(values.iterator().next()));
    if (valuesProvided.isEmpty()) {
        JexlContext jexlContext = new MapContext();
        JexlUtils.addPlainAttrsToContext(confDAO.get().getPlainAttrs(), jexlContext);
        if (!schema.isReadonly() && Boolean.parseBoolean(JexlUtils.evaluate(schema.getMandatoryCondition(), jexlContext))) {
            LOG.error("Mandatory schema " + schema.getKey() + " not provided with values");
            SyncopeClientException reqValMissing = SyncopeClientException.build(ClientExceptionType.RequiredValuesMissing);
            reqValMissing.getElements().add(schema.getKey());
            throw reqValMissing;
        }
    }
    for (String value : valuesProvided) {
        if (value == null || value.isEmpty()) {
            LOG.debug("Null value for {}, ignoring", schema.getKey());
        } else {
            try {
                PlainAttrValue attrValue;
                if (schema.isUniqueConstraint()) {
                    attrValue = entityFactory.newEntity(CPlainAttrUniqueValue.class);
                    ((PlainAttrUniqueValue) attrValue).setSchema(schema);
                } else {
                    attrValue = entityFactory.newEntity(CPlainAttrValue.class);
                }
                attr.add(value, attrValue);
            } catch (InvalidPlainAttrValueException e) {
                LOG.warn("Invalid value for attribute " + schema.getKey() + ": " + value, e);
                invalidValues.getElements().add(schema.getKey() + ": " + value + " - " + e.getMessage());
            }
        }
    }
}
Also used : CPlainAttrUniqueValue(org.apache.syncope.core.persistence.api.entity.conf.CPlainAttrUniqueValue) PlainAttrUniqueValue(org.apache.syncope.core.persistence.api.entity.PlainAttrUniqueValue) CPlainAttrValue(org.apache.syncope.core.persistence.api.entity.conf.CPlainAttrValue) CPlainAttrUniqueValue(org.apache.syncope.core.persistence.api.entity.conf.CPlainAttrUniqueValue) JexlContext(org.apache.commons.jexl3.JexlContext) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) PlainAttrValue(org.apache.syncope.core.persistence.api.entity.PlainAttrValue) CPlainAttrValue(org.apache.syncope.core.persistence.api.entity.conf.CPlainAttrValue) MapContext(org.apache.commons.jexl3.MapContext) InvalidPlainAttrValueException(org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidPlainAttrValueException)

Example 7 with PlainAttrValue

use of org.apache.syncope.core.persistence.api.entity.PlainAttrValue in project syncope by apache.

the class AbstractAnySearchDAO method check.

protected Triple<PlainSchema, PlainAttrValue, AnyCond> check(final AnyCond cond, final AnyTypeKind kind) {
    AnyCond condClone = SerializationUtils.clone(cond);
    AnyUtils attrUtils = anyUtilsFactory.getInstance(kind);
    // Keeps track of difference between entity's getKey() and JPA @Id fields
    if ("key".equals(condClone.getSchema())) {
        condClone.setSchema("id");
    }
    Field anyField = ReflectionUtils.findField(attrUtils.anyClass(), condClone.getSchema());
    if (anyField == null) {
        LOG.warn("Ignoring invalid schema '{}'", condClone.getSchema());
        throw new IllegalArgumentException();
    }
    PlainSchema schema = new JPAPlainSchema();
    schema.setKey(anyField.getName());
    for (AttrSchemaType attrSchemaType : AttrSchemaType.values()) {
        if (anyField.getType().isAssignableFrom(attrSchemaType.getType())) {
            schema.setType(attrSchemaType);
        }
    }
    // Deal with any Integer fields logically mapping to boolean values
    boolean foundBooleanMin = false;
    boolean foundBooleanMax = false;
    if (Integer.class.equals(anyField.getType())) {
        for (Annotation annotation : anyField.getAnnotations()) {
            if (Min.class.equals(annotation.annotationType())) {
                foundBooleanMin = ((Min) annotation).value() == 0;
            } else if (Max.class.equals(annotation.annotationType())) {
                foundBooleanMax = ((Max) annotation).value() == 1;
            }
        }
    }
    if (foundBooleanMin && foundBooleanMax) {
        schema.setType(AttrSchemaType.Boolean);
    }
    // Deal with any fields representing relationships to other entities
    if (anyField.getType().getAnnotation(Entity.class) != null) {
        Method relMethod = null;
        try {
            relMethod = ClassUtils.getPublicMethod(anyField.getType(), "getKey", new Class<?>[0]);
        } catch (Exception e) {
            LOG.error("Could not find {}#getKey", anyField.getType(), e);
        }
        if (relMethod != null && String.class.isAssignableFrom(relMethod.getReturnType())) {
            condClone.setSchema(condClone.getSchema() + "_id");
            schema.setType(AttrSchemaType.String);
        }
    }
    PlainAttrValue attrValue = attrUtils.newPlainAttrValue();
    if (condClone.getType() != AttributeCond.Type.LIKE && condClone.getType() != AttributeCond.Type.ILIKE && condClone.getType() != AttributeCond.Type.ISNULL && condClone.getType() != AttributeCond.Type.ISNOTNULL) {
        try {
            ((JPAPlainSchema) schema).validator().validate(condClone.getExpression(), attrValue);
        } catch (ValidationException e) {
            LOG.error("Could not validate expression '" + condClone.getExpression() + "'", e);
            throw new IllegalArgumentException();
        }
    }
    return Triple.of(schema, attrValue, condClone);
}
Also used : Entity(javax.persistence.Entity) ValidationException(javax.validation.ValidationException) Max(javax.validation.constraints.Max) JPAPlainSchema(org.apache.syncope.core.persistence.jpa.entity.JPAPlainSchema) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) ValidationException(javax.validation.ValidationException) Field(java.lang.reflect.Field) Min(javax.validation.constraints.Min) AttrSchemaType(org.apache.syncope.common.lib.types.AttrSchemaType) PlainAttrValue(org.apache.syncope.core.persistence.api.entity.PlainAttrValue) JPAPlainSchema(org.apache.syncope.core.persistence.jpa.entity.JPAPlainSchema) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) AnyCond(org.apache.syncope.core.persistence.api.dao.search.AnyCond) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils)

Example 8 with PlainAttrValue

use of org.apache.syncope.core.persistence.api.entity.PlainAttrValue in project syncope by apache.

the class AbstractAnySearchDAO method check.

protected Pair<PlainSchema, PlainAttrValue> check(final AttributeCond cond, final AnyTypeKind kind) {
    AnyUtils attrUtils = anyUtilsFactory.getInstance(kind);
    PlainSchema schema = schemaDAO.find(cond.getSchema());
    if (schema == null) {
        LOG.warn("Ignoring invalid schema '{}'", cond.getSchema());
        throw new IllegalArgumentException();
    }
    PlainAttrValue attrValue = attrUtils.newPlainAttrValue();
    try {
        if (cond.getType() != AttributeCond.Type.LIKE && cond.getType() != AttributeCond.Type.ILIKE && cond.getType() != AttributeCond.Type.ISNULL && cond.getType() != AttributeCond.Type.ISNOTNULL) {
            ((JPAPlainSchema) schema).validator().validate(cond.getExpression(), attrValue);
        }
    } catch (ValidationException e) {
        LOG.error("Could not validate expression '" + cond.getExpression() + "'", e);
        throw new IllegalArgumentException();
    }
    return Pair.of(schema, attrValue);
}
Also used : ValidationException(javax.validation.ValidationException) PlainAttrValue(org.apache.syncope.core.persistence.api.entity.PlainAttrValue) JPAPlainSchema(org.apache.syncope.core.persistence.jpa.entity.JPAPlainSchema) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils)

Example 9 with PlainAttrValue

use of org.apache.syncope.core.persistence.api.entity.PlainAttrValue in project syncope by apache.

the class SAML2UserManager method findMatchingUser.

@Transactional(readOnly = true)
public List<String> findMatchingUser(final String keyValue, final String idpKey) {
    List<String> result = new ArrayList<>();
    SAML2IdP idp = idpDAO.find(idpKey);
    if (idp == null) {
        LOG.warn("Invalid IdP: {}", idpKey);
        return result;
    }
    String transformed = keyValue;
    for (ItemTransformer transformer : MappingUtils.getItemTransformers(idp.getConnObjectKeyItem().get())) {
        List<Object> output = transformer.beforePull(null, null, Collections.<Object>singletonList(transformed));
        if (output != null && !output.isEmpty()) {
            transformed = output.get(0).toString();
        }
    }
    IntAttrName intAttrName;
    try {
        intAttrName = intAttrNameParser.parse(idp.getConnObjectKeyItem().get().getIntAttrName(), AnyTypeKind.USER);
    } catch (ParseException e) {
        LOG.error("Invalid intAttrName '{}' specified, ignoring", idp.getConnObjectKeyItem().get().getIntAttrName(), e);
        return result;
    }
    if (intAttrName.getField() != null) {
        switch(intAttrName.getField()) {
            case "key":
                User byKey = userDAO.find(transformed);
                if (byKey != null) {
                    result.add(byKey.getUsername());
                }
                break;
            case "username":
                User byUsername = userDAO.findByUsername(transformed);
                if (byUsername != null) {
                    result.add(byUsername.getUsername());
                }
                break;
            default:
        }
    } else if (intAttrName.getSchemaType() != null) {
        switch(intAttrName.getSchemaType()) {
            case PLAIN:
                PlainAttrValue value = entityFactory.newEntity(UPlainAttrValue.class);
                PlainSchema schema = plainSchemaDAO.find(intAttrName.getSchemaName());
                if (schema == null) {
                    value.setStringValue(transformed);
                } else {
                    try {
                        value.parseValue(schema, transformed);
                    } catch (ParsingValidationException e) {
                        LOG.error("While parsing provided key value {}", transformed, e);
                        value.setStringValue(transformed);
                    }
                }
                result.addAll(userDAO.findByPlainAttrValue(intAttrName.getSchemaName(), value).stream().map(user -> user.getUsername()).collect(Collectors.toList()));
                break;
            case DERIVED:
                result.addAll(userDAO.findByDerAttrValue(intAttrName.getSchemaName(), transformed).stream().map(user -> user.getUsername()).collect(Collectors.toList()));
                break;
            default:
        }
    }
    return result;
}
Also used : ItemTransformer(org.apache.syncope.core.provisioning.api.data.ItemTransformer) AttrTO(org.apache.syncope.common.lib.to.AttrTO) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) SerializationUtils(org.apache.commons.lang3.SerializationUtils) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) UserPatch(org.apache.syncope.common.lib.patch.UserPatch) SAML2IdP(org.apache.syncope.core.persistence.api.entity.SAML2IdP) ArrayList(java.util.ArrayList) AnyTypeKind(org.apache.syncope.common.lib.types.AnyTypeKind) UPlainAttrValue(org.apache.syncope.core.persistence.api.entity.user.UPlainAttrValue) Pair(org.apache.commons.lang3.tuple.Pair) Propagation(org.springframework.transaction.annotation.Propagation) UserDataBinder(org.apache.syncope.core.provisioning.api.data.UserDataBinder) SAML2LoginResponseTO(org.apache.syncope.common.lib.to.SAML2LoginResponseTO) PropagationStatus(org.apache.syncope.common.lib.to.PropagationStatus) SAML2IdPDAO(org.apache.syncope.core.persistence.api.dao.SAML2IdPDAO) ParseException(java.text.ParseException) TemplateUtils(org.apache.syncope.core.provisioning.java.utils.TemplateUtils) SyncopeConstants(org.apache.syncope.common.lib.SyncopeConstants) Logger(org.slf4j.Logger) UserDAO(org.apache.syncope.core.persistence.api.dao.UserDAO) PlainSchemaDAO(org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO) User(org.apache.syncope.core.persistence.api.entity.user.User) PlainAttrValue(org.apache.syncope.core.persistence.api.entity.PlainAttrValue) Collectors(java.util.stream.Collectors) MappingUtils(org.apache.syncope.core.provisioning.java.utils.MappingUtils) EntityFactory(org.apache.syncope.core.persistence.api.entity.EntityFactory) List(java.util.List) Component(org.springframework.stereotype.Component) SAML2IdPActions(org.apache.syncope.core.provisioning.api.SAML2IdPActions) UserProvisioningManager(org.apache.syncope.core.provisioning.api.UserProvisioningManager) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) Optional(java.util.Optional) IntAttrNameParser(org.apache.syncope.core.provisioning.java.IntAttrNameParser) UserTO(org.apache.syncope.common.lib.to.UserTO) ApplicationContextProvider(org.apache.syncope.core.spring.ApplicationContextProvider) Collections(java.util.Collections) AnyOperations(org.apache.syncope.common.lib.AnyOperations) ParsingValidationException(org.apache.syncope.core.persistence.api.attrvalue.validation.ParsingValidationException) IntAttrName(org.apache.syncope.core.provisioning.api.IntAttrName) Transactional(org.springframework.transaction.annotation.Transactional) UPlainAttrValue(org.apache.syncope.core.persistence.api.entity.user.UPlainAttrValue) User(org.apache.syncope.core.persistence.api.entity.user.User) ItemTransformer(org.apache.syncope.core.provisioning.api.data.ItemTransformer) ArrayList(java.util.ArrayList) IntAttrName(org.apache.syncope.core.provisioning.api.IntAttrName) ParsingValidationException(org.apache.syncope.core.persistence.api.attrvalue.validation.ParsingValidationException) SAML2IdP(org.apache.syncope.core.persistence.api.entity.SAML2IdP) UPlainAttrValue(org.apache.syncope.core.persistence.api.entity.user.UPlainAttrValue) PlainAttrValue(org.apache.syncope.core.persistence.api.entity.PlainAttrValue) ParseException(java.text.ParseException) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with PlainAttrValue

use of org.apache.syncope.core.persistence.api.entity.PlainAttrValue in project syncope by apache.

the class MappingManagerImpl method getConnObjectKeyValue.

@Transactional(readOnly = true)
@Override
public Optional<String> getConnObjectKeyValue(final Any<?> any, final Provision provision) {
    MappingItem mapItem = provision.getMapping().getConnObjectKeyItem().get();
    List<PlainAttrValue> values;
    try {
        values = getIntValues(provision, mapItem, intAttrNameParser.parse(mapItem.getIntAttrName(), provision.getAnyType().getKind()), any);
    } catch (ParseException e) {
        LOG.error("Invalid intAttrName '{}' specified, ignoring", mapItem.getIntAttrName(), e);
        values = Collections.emptyList();
    }
    return Optional.ofNullable(values.isEmpty() ? null : values.get(0).getValueAsString());
}
Also used : MappingItem(org.apache.syncope.core.persistence.api.entity.resource.MappingItem) PlainAttrValue(org.apache.syncope.core.persistence.api.entity.PlainAttrValue) ParseException(java.text.ParseException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

PlainAttrValue (org.apache.syncope.core.persistence.api.entity.PlainAttrValue)12 PlainSchema (org.apache.syncope.core.persistence.api.entity.PlainSchema)8 ParseException (java.text.ParseException)7 ArrayList (java.util.ArrayList)7 IntAttrName (org.apache.syncope.core.provisioning.api.IntAttrName)6 ParsingValidationException (org.apache.syncope.core.persistence.api.attrvalue.validation.ParsingValidationException)5 User (org.apache.syncope.core.persistence.api.entity.user.User)5 AttrSchemaType (org.apache.syncope.common.lib.types.AttrSchemaType)4 AnyUtils (org.apache.syncope.core.persistence.api.entity.AnyUtils)4 AnyObject (org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject)4 MappingItem (org.apache.syncope.core.persistence.api.entity.resource.MappingItem)4 ItemTransformer (org.apache.syncope.core.provisioning.api.data.ItemTransformer)4 Attribute (org.identityconnectors.framework.common.objects.Attribute)4 Collections (java.util.Collections)3 List (java.util.List)3 Optional (java.util.Optional)3 Pair (org.apache.commons.lang3.tuple.Pair)3 AttrTO (org.apache.syncope.common.lib.to.AttrTO)3 UserTO (org.apache.syncope.common.lib.to.UserTO)3 DerSchema (org.apache.syncope.core.persistence.api.entity.DerSchema)3