Search in sources :

Example 1 with PlainAttr

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

the class MappingManagerImpl method getIntValues.

@Transactional(readOnly = true)
@Override
public List<PlainAttrValue> getIntValues(final Provision provision, final Item mapItem, final IntAttrName intAttrName, final Any<?> any) {
    LOG.debug("Get internal values for {} as '{}' on {}", any, mapItem.getIntAttrName(), provision.getResource());
    Any<?> reference = null;
    Membership<?> membership = null;
    if (intAttrName.getEnclosingGroup() == null && intAttrName.getRelatedAnyObject() == null) {
        reference = any;
    }
    if (any instanceof GroupableRelatable) {
        GroupableRelatable<?, ?, ?, ?, ?> groupableRelatable = (GroupableRelatable<?, ?, ?, ?, ?>) any;
        if (intAttrName.getEnclosingGroup() != null) {
            Group group = groupDAO.findByName(intAttrName.getEnclosingGroup());
            if (group == null || !groupableRelatable.getMembership(group.getKey()).isPresent()) {
                LOG.warn("No membership for {} in {}, ignoring", intAttrName.getEnclosingGroup(), groupableRelatable);
            } else {
                reference = group;
            }
        } else if (intAttrName.getRelatedAnyObject() != null) {
            AnyObject anyObject = anyObjectDAO.findByName(intAttrName.getRelatedAnyObject());
            if (anyObject == null || groupableRelatable.getRelationships(anyObject.getKey()).isEmpty()) {
                LOG.warn("No relationship for {} in {}, ignoring", intAttrName.getRelatedAnyObject(), groupableRelatable);
            } else {
                reference = anyObject;
            }
        } else if (intAttrName.getMembershipOfGroup() != null) {
            Group group = groupDAO.findByName(intAttrName.getMembershipOfGroup());
            membership = groupableRelatable.getMembership(group.getKey()).orElse(null);
        }
    }
    if (reference == null) {
        LOG.warn("Could not determine the reference instance for {}", mapItem.getIntAttrName());
        return Collections.emptyList();
    }
    List<PlainAttrValue> values = new ArrayList<>();
    boolean transform = true;
    AnyUtils anyUtils = anyUtilsFactory.getInstance(reference);
    if (intAttrName.getField() != null) {
        PlainAttrValue attrValue = anyUtils.newPlainAttrValue();
        switch(intAttrName.getField()) {
            case "key":
                attrValue.setStringValue(reference.getKey());
                values.add(attrValue);
                break;
            case "realm":
                attrValue.setStringValue(reference.getRealm().getFullPath());
                values.add(attrValue);
                break;
            case "password":
                // ignore
                break;
            case "userOwner":
            case "groupOwner":
                Mapping uMapping = provision.getAnyType().equals(anyTypeDAO.findUser()) ? provision.getMapping() : null;
                Mapping gMapping = provision.getAnyType().equals(anyTypeDAO.findGroup()) ? provision.getMapping() : null;
                if (reference instanceof Group) {
                    Group group = (Group) reference;
                    String groupOwnerValue = null;
                    if (group.getUserOwner() != null && uMapping != null) {
                        groupOwnerValue = getGroupOwnerValue(provision, group.getUserOwner());
                    }
                    if (group.getGroupOwner() != null && gMapping != null) {
                        groupOwnerValue = getGroupOwnerValue(provision, group.getGroupOwner());
                    }
                    if (StringUtils.isNotBlank(groupOwnerValue)) {
                        attrValue.setStringValue(groupOwnerValue);
                        values.add(attrValue);
                    }
                }
                break;
            case "suspended":
                if (reference instanceof User) {
                    attrValue.setBooleanValue(((User) reference).isSuspended());
                    values.add(attrValue);
                }
                break;
            case "mustChangePassword":
                if (reference instanceof User) {
                    attrValue.setBooleanValue(((User) reference).isMustChangePassword());
                    values.add(attrValue);
                }
                break;
            default:
                try {
                    Object fieldValue = FieldUtils.readField(reference, intAttrName.getField(), true);
                    if (fieldValue instanceof Date) {
                        // needed because ConnId does not natively supports the Date type
                        attrValue.setStringValue(DateFormatUtils.ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT.format((Date) fieldValue));
                    } else if (Boolean.TYPE.isInstance(fieldValue)) {
                        attrValue.setBooleanValue((Boolean) fieldValue);
                    } else if (Double.TYPE.isInstance(fieldValue) || Float.TYPE.isInstance(fieldValue)) {
                        attrValue.setDoubleValue((Double) fieldValue);
                    } else if (Long.TYPE.isInstance(fieldValue) || Integer.TYPE.isInstance(fieldValue)) {
                        attrValue.setLongValue((Long) fieldValue);
                    } else {
                        attrValue.setStringValue(fieldValue.toString());
                    }
                    values.add(attrValue);
                } catch (Exception e) {
                    LOG.error("Could not read value of '{}' from {}", intAttrName.getField(), reference, e);
                }
        }
    } else if (intAttrName.getSchemaType() != null) {
        switch(intAttrName.getSchemaType()) {
            case PLAIN:
                PlainAttr<?> attr;
                if (membership == null) {
                    attr = reference.getPlainAttr(intAttrName.getSchemaName()).orElse(null);
                } else {
                    attr = ((GroupableRelatable<?, ?, ?, ?, ?>) reference).getPlainAttr(intAttrName.getSchemaName(), membership).orElse(null);
                }
                if (attr == null) {
                    LOG.warn("Invalid PlainSchema {} or PlainAttr not found for {}", intAttrName.getSchemaName(), reference);
                } else {
                    if (attr.getUniqueValue() != null) {
                        values.add(anyUtils.clonePlainAttrValue(attr.getUniqueValue()));
                    } else if (attr.getValues() != null) {
                        attr.getValues().forEach(value -> values.add(anyUtils.clonePlainAttrValue(value)));
                    }
                }
                break;
            case DERIVED:
                DerSchema derSchema = derSchemaDAO.find(intAttrName.getSchemaName());
                if (derSchema == null) {
                    LOG.warn("Invalid DerSchema: {}", intAttrName.getSchemaName());
                } else {
                    String derValue = membership == null ? derAttrHandler.getValue(reference, derSchema) : derAttrHandler.getValue(reference, membership, derSchema);
                    if (derValue != null) {
                        PlainAttrValue attrValue = anyUtils.newPlainAttrValue();
                        attrValue.setStringValue(derValue);
                        values.add(attrValue);
                    }
                }
                break;
            case VIRTUAL:
                // virtual attributes don't get transformed
                transform = false;
                VirSchema virSchema = virSchemaDAO.find(intAttrName.getSchemaName());
                if (virSchema == null) {
                    LOG.warn("Invalid VirSchema: {}", intAttrName.getSchemaName());
                } else {
                    LOG.debug("Expire entry cache {}-{}", reference, intAttrName.getSchemaName());
                    virAttrCache.expire(reference.getType().getKey(), reference.getKey(), intAttrName.getSchemaName());
                    List<String> virValues = membership == null ? virAttrHandler.getValues(reference, virSchema) : virAttrHandler.getValues(reference, membership, virSchema);
                    virValues.forEach(virValue -> {
                        PlainAttrValue attrValue = anyUtils.newPlainAttrValue();
                        attrValue.setStringValue(virValue);
                        values.add(attrValue);
                    });
                }
                break;
            default:
        }
    } else if (intAttrName.getPrivilegesOfApplication() != null && reference instanceof User) {
        Application application = applicationDAO.find(intAttrName.getPrivilegesOfApplication());
        if (application == null) {
            LOG.warn("Invalid application: {}", intAttrName.getPrivilegesOfApplication());
        } else {
            userDAO.findAllRoles((User) reference).stream().flatMap(role -> role.getPrivileges(application).stream()).forEach(privilege -> {
                PlainAttrValue attrValue = anyUtils.newPlainAttrValue();
                attrValue.setStringValue(privilege.getKey());
                values.add(attrValue);
            });
        }
    }
    LOG.debug("Internal values: {}", values);
    List<PlainAttrValue> transformed = values;
    if (transform) {
        for (ItemTransformer transformer : MappingUtils.getItemTransformers(mapItem)) {
            transformed = transformer.beforePropagation(mapItem, any, transformed);
        }
        LOG.debug("Transformed values: {}", values);
    } else {
        LOG.debug("No transformation occurred");
    }
    return transformed;
}
Also used : Date(java.util.Date) Realm(org.apache.syncope.core.persistence.api.entity.Realm) PlainAttr(org.apache.syncope.core.persistence.api.entity.PlainAttr) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Schema(org.apache.syncope.core.persistence.api.entity.Schema) InvalidPasswordRuleConf(org.apache.syncope.core.provisioning.api.utils.policy.InvalidPasswordRuleConf) StringUtils(org.apache.commons.lang3.StringUtils) Attribute(org.identityconnectors.framework.common.objects.Attribute) GroupDAO(org.apache.syncope.core.persistence.api.dao.GroupDAO) Pair(org.apache.commons.lang3.tuple.Pair) AttrSchemaType(org.apache.syncope.common.lib.types.AttrSchemaType) AnyObjectDAO(org.apache.syncope.core.persistence.api.dao.AnyObjectDAO) ConnObjectUtils(org.apache.syncope.core.provisioning.java.utils.ConnObjectUtils) GroupableRelatableTO(org.apache.syncope.common.lib.to.GroupableRelatableTO) OrgUnit(org.apache.syncope.core.persistence.api.entity.resource.OrgUnit) ParseException(java.text.ParseException) OperationalAttributes(org.identityconnectors.framework.common.objects.OperationalAttributes) UserDAO(org.apache.syncope.core.persistence.api.dao.UserDAO) OrgUnitItem(org.apache.syncope.core.persistence.api.entity.resource.OrgUnitItem) DerAttrHandler(org.apache.syncope.core.provisioning.api.DerAttrHandler) Set(java.util.Set) PlainAttrValue(org.apache.syncope.core.persistence.api.entity.PlainAttrValue) GroupTO(org.apache.syncope.common.lib.to.GroupTO) AnyTypeDAO(org.apache.syncope.core.persistence.api.dao.AnyTypeDAO) MappingItem(org.apache.syncope.core.persistence.api.entity.resource.MappingItem) List(java.util.List) DerSchemaDAO(org.apache.syncope.core.persistence.api.dao.DerSchemaDAO) Provision(org.apache.syncope.core.persistence.api.entity.resource.Provision) AttributeUtil(org.identityconnectors.framework.common.objects.AttributeUtil) AttributeBuilder(org.identityconnectors.framework.common.objects.AttributeBuilder) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) Group(org.apache.syncope.core.persistence.api.entity.group.Group) Optional(java.util.Optional) IntAttrName(org.apache.syncope.core.provisioning.api.IntAttrName) VirAttrCache(org.apache.syncope.core.provisioning.api.cache.VirAttrCache) ApplicationDAO(org.apache.syncope.core.persistence.api.dao.ApplicationDAO) ItemTransformer(org.apache.syncope.core.provisioning.api.data.ItemTransformer) AttrTO(org.apache.syncope.common.lib.to.AttrTO) AnyUtilsFactory(org.apache.syncope.core.persistence.api.entity.AnyUtilsFactory) RealmTO(org.apache.syncope.common.lib.to.RealmTO) GroupableRelatable(org.apache.syncope.core.persistence.api.entity.GroupableRelatable) AnyTO(org.apache.syncope.common.lib.to.AnyTO) FrameworkUtil(org.identityconnectors.framework.common.FrameworkUtil) BooleanUtils(org.apache.commons.lang3.BooleanUtils) PasswordGenerator(org.apache.syncope.core.spring.security.PasswordGenerator) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Application(org.apache.syncope.core.persistence.api.entity.Application) DerSchema(org.apache.syncope.core.persistence.api.entity.DerSchema) MappingManager(org.apache.syncope.core.provisioning.api.MappingManager) FieldUtils(org.apache.commons.lang3.reflect.FieldUtils) Item(org.apache.syncope.core.persistence.api.entity.resource.Item) RealmDAO(org.apache.syncope.core.persistence.api.dao.RealmDAO) MembershipTO(org.apache.syncope.common.lib.to.MembershipTO) Mapping(org.apache.syncope.core.persistence.api.entity.resource.Mapping) Encryptor(org.apache.syncope.core.spring.security.Encryptor) Logger(org.slf4j.Logger) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) PlainSchemaDAO(org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO) VirAttrHandler(org.apache.syncope.core.provisioning.api.VirAttrHandler) User(org.apache.syncope.core.persistence.api.entity.user.User) Membership(org.apache.syncope.core.persistence.api.entity.Membership) Name(org.identityconnectors.framework.common.objects.Name) VirSchema(org.apache.syncope.core.persistence.api.entity.VirSchema) MappingUtils(org.apache.syncope.core.provisioning.java.utils.MappingUtils) Component(org.springframework.stereotype.Component) VirSchemaDAO(org.apache.syncope.core.persistence.api.dao.VirSchemaDAO) UserTO(org.apache.syncope.common.lib.to.UserTO) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils) Collections(java.util.Collections) Any(org.apache.syncope.core.persistence.api.entity.Any) DateFormatUtils(org.apache.commons.lang3.time.DateFormatUtils) AnyObjectTO(org.apache.syncope.common.lib.to.AnyObjectTO) ParsingValidationException(org.apache.syncope.core.persistence.api.attrvalue.validation.ParsingValidationException) Transactional(org.springframework.transaction.annotation.Transactional) Group(org.apache.syncope.core.persistence.api.entity.group.Group) User(org.apache.syncope.core.persistence.api.entity.user.User) PlainAttr(org.apache.syncope.core.persistence.api.entity.PlainAttr) ItemTransformer(org.apache.syncope.core.provisioning.api.data.ItemTransformer) ArrayList(java.util.ArrayList) Mapping(org.apache.syncope.core.persistence.api.entity.resource.Mapping) GroupableRelatable(org.apache.syncope.core.persistence.api.entity.GroupableRelatable) PlainAttrValue(org.apache.syncope.core.persistence.api.entity.PlainAttrValue) List(java.util.List) ArrayList(java.util.ArrayList) DerSchema(org.apache.syncope.core.persistence.api.entity.DerSchema) VirSchema(org.apache.syncope.core.persistence.api.entity.VirSchema) Date(java.util.Date) ParseException(java.text.ParseException) ParsingValidationException(org.apache.syncope.core.persistence.api.attrvalue.validation.ParsingValidationException) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils) Application(org.apache.syncope.core.persistence.api.entity.Application) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with PlainAttr

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

the class AbstractAnyDataBinder method fill.

@SuppressWarnings({ "unchecked", "rawtypes" })
protected PropagationByResource fill(final Any any, final AnyPatch anyPatch, final AnyUtils anyUtils, final SyncopeClientCompositeException scce) {
    PropagationByResource propByRes = new PropagationByResource();
    // 1. anyTypeClasses
    for (StringPatchItem patch : anyPatch.getAuxClasses()) {
        AnyTypeClass auxClass = anyTypeClassDAO.find(patch.getValue());
        if (auxClass == null) {
            LOG.debug("Invalid " + AnyTypeClass.class.getSimpleName() + " {}, ignoring...", patch.getValue());
        } else {
            switch(patch.getOperation()) {
                case ADD_REPLACE:
                    any.add(auxClass);
                    break;
                case DELETE:
                default:
                    any.getAuxClasses().remove(auxClass);
            }
        }
    }
    // 2. resources
    for (StringPatchItem patch : anyPatch.getResources()) {
        ExternalResource resource = resourceDAO.find(patch.getValue());
        if (resource == null) {
            LOG.debug("Invalid " + ExternalResource.class.getSimpleName() + " {}, ignoring...", patch.getValue());
        } else {
            switch(patch.getOperation()) {
                case ADD_REPLACE:
                    propByRes.add(ResourceOperation.CREATE, resource.getKey());
                    any.add(resource);
                    break;
                case DELETE:
                default:
                    propByRes.add(ResourceOperation.DELETE, resource.getKey());
                    any.getResources().remove(resource);
            }
        }
    }
    Set<ExternalResource> resources = anyUtils.getAllResources(any);
    SyncopeClientException invalidValues = SyncopeClientException.build(ClientExceptionType.InvalidValues);
    // 3. plain attributes
    anyPatch.getPlainAttrs().stream().filter(patch -> patch.getAttrTO() != null).forEach(patch -> {
        PlainSchema schema = getPlainSchema(patch.getAttrTO().getSchema());
        if (schema == null) {
            LOG.debug("Invalid " + PlainSchema.class.getSimpleName() + " {}, ignoring...", patch.getAttrTO().getSchema());
        } else {
            PlainAttr<?> attr = (PlainAttr<?>) any.getPlainAttr(schema.getKey()).orElse(null);
            if (attr == null) {
                LOG.debug("No plain attribute found for schema {}", schema);
                if (patch.getOperation() == PatchOperation.ADD_REPLACE) {
                    attr = anyUtils.newPlainAttr();
                    ((PlainAttr) attr).setOwner(any);
                    attr.setSchema(schema);
                    any.add(attr);
                }
            }
            if (attr != null) {
                processAttrPatch(any, patch, schema, attr, anyUtils, resources, propByRes, invalidValues);
            }
        }
    });
    if (!invalidValues.isEmpty()) {
        scce.addException(invalidValues);
    }
    SyncopeClientException requiredValuesMissing = checkMandatory(any, anyUtils);
    if (!requiredValuesMissing.isEmpty()) {
        scce.addException(requiredValuesMissing);
    }
    requiredValuesMissing = checkMandatoryOnResources(any, resources);
    if (!requiredValuesMissing.isEmpty()) {
        scce.addException(requiredValuesMissing);
    }
    return propByRes;
}
Also used : StringPatchItem(org.apache.syncope.common.lib.patch.StringPatchItem) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) Realm(org.apache.syncope.core.persistence.api.entity.Realm) PlainAttr(org.apache.syncope.core.persistence.api.entity.PlainAttr) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) InvalidPlainAttrValueException(org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidPlainAttrValueException) ResourceOperation(org.apache.syncope.common.lib.types.ResourceOperation) StringUtils(org.apache.commons.lang3.StringUtils) AllowedSchemas(org.apache.syncope.core.persistence.api.dao.AllowedSchemas) JexlUtils(org.apache.syncope.core.provisioning.java.jexl.JexlUtils) GroupDAO(org.apache.syncope.core.persistence.api.dao.GroupDAO) AnyObjectDAO(org.apache.syncope.core.persistence.api.dao.AnyObjectDAO) Map(java.util.Map) SchemaDataBinder(org.apache.syncope.core.provisioning.api.data.SchemaDataBinder) PropagationByResource(org.apache.syncope.core.provisioning.api.PropagationByResource) ParseException(java.text.ParseException) AnyTypeClass(org.apache.syncope.core.persistence.api.entity.AnyTypeClass) AnyPatch(org.apache.syncope.common.lib.patch.AnyPatch) RelationshipTypeDAO(org.apache.syncope.core.persistence.api.dao.RelationshipTypeDAO) UserDAO(org.apache.syncope.core.persistence.api.dao.UserDAO) Collection(java.util.Collection) DerAttrHandler(org.apache.syncope.core.provisioning.api.DerAttrHandler) Set(java.util.Set) PlainAttrValue(org.apache.syncope.core.persistence.api.entity.PlainAttrValue) Collectors(java.util.stream.Collectors) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) MappingItem(org.apache.syncope.core.persistence.api.entity.resource.MappingItem) EntityFactory(org.apache.syncope.core.persistence.api.entity.EntityFactory) List(java.util.List) Provision(org.apache.syncope.core.persistence.api.entity.resource.Provision) AttrPatch(org.apache.syncope.common.lib.patch.AttrPatch) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) Optional(java.util.Optional) ExternalResourceDAO(org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO) IntAttrName(org.apache.syncope.core.provisioning.api.IntAttrName) AttrTO(org.apache.syncope.common.lib.to.AttrTO) AnyUtilsFactory(org.apache.syncope.core.persistence.api.entity.AnyUtilsFactory) GroupableRelatable(org.apache.syncope.core.persistence.api.entity.GroupableRelatable) AnyTO(org.apache.syncope.common.lib.to.AnyTO) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DerSchema(org.apache.syncope.core.persistence.api.entity.DerSchema) MappingManager(org.apache.syncope.core.provisioning.api.MappingManager) SyncopeClientCompositeException(org.apache.syncope.common.lib.SyncopeClientCompositeException) ClientExceptionType(org.apache.syncope.common.lib.types.ClientExceptionType) RealmDAO(org.apache.syncope.core.persistence.api.dao.RealmDAO) MembershipTO(org.apache.syncope.common.lib.to.MembershipTO) Logger(org.slf4j.Logger) PlainSchemaDAO(org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO) VirAttrHandler(org.apache.syncope.core.provisioning.api.VirAttrHandler) Membership(org.apache.syncope.core.persistence.api.entity.Membership) PlainAttrValueDAO(org.apache.syncope.core.persistence.api.dao.PlainAttrValueDAO) VirSchema(org.apache.syncope.core.persistence.api.entity.VirSchema) MappingUtils(org.apache.syncope.core.provisioning.java.utils.MappingUtils) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) RelationshipTO(org.apache.syncope.common.lib.to.RelationshipTO) PatchOperation(org.apache.syncope.common.lib.types.PatchOperation) IntAttrNameParser(org.apache.syncope.core.provisioning.java.IntAttrNameParser) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils) Collections(java.util.Collections) AnyTypeClassDAO(org.apache.syncope.core.persistence.api.dao.AnyTypeClassDAO) Any(org.apache.syncope.core.persistence.api.entity.Any) PlainAttrDAO(org.apache.syncope.core.persistence.api.dao.PlainAttrDAO) GroupablePlainAttr(org.apache.syncope.core.persistence.api.entity.GroupablePlainAttr) PlainAttr(org.apache.syncope.core.persistence.api.entity.PlainAttr) GroupablePlainAttr(org.apache.syncope.core.persistence.api.entity.GroupablePlainAttr) StringPatchItem(org.apache.syncope.common.lib.patch.StringPatchItem) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) PropagationByResource(org.apache.syncope.core.provisioning.api.PropagationByResource) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) AnyTypeClass(org.apache.syncope.core.persistence.api.entity.AnyTypeClass) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource)

Example 3 with PlainAttr

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

the class ElasticsearchUtils method builder.

/**
 * Returns the builder specialized with content from the provided any.
 *
 * @param any user, group or any object to index
 * @return builder specialized with content from the provided any
 * @throws IOException in case of errors
 */
@Transactional
public XContentBuilder builder(final Any<?> any) throws IOException {
    XContentBuilder builder = XContentFactory.jsonBuilder().startObject().field("id", any.getKey()).field("realm", any.getRealm().getFullPath()).field("anyType", any.getType().getKey()).field("creationDate", any.getCreationDate()).field("creator", any.getCreator()).field("lastChangeDate", any.getLastChangeDate()).field("lastModified", any.getLastModifier()).field("status", any.getStatus()).field("resources", any instanceof User ? userDAO.findAllResourceKeys(any.getKey()) : any instanceof AnyObject ? anyObjectDAO.findAllResourceKeys(any.getKey()) : groupDAO.findAllResourceKeys(any.getKey())).field("dynRealms", any instanceof User ? userDAO.findDynRealms(any.getKey()) : any instanceof AnyObject ? anyObjectDAO.findDynRealms(any.getKey()) : groupDAO.findDynRealms(any.getKey()));
    if (any instanceof AnyObject) {
        AnyObject anyObject = ((AnyObject) any);
        builder = builder.field("name", anyObject.getName());
        List<Object> memberships = new ArrayList<>(anyObjectDAO.findAllGroupKeys(anyObject));
        builder = builder.field("memberships", memberships);
        List<Object> relationships = new ArrayList<>();
        List<Object> relationshipTypes = new ArrayList<>();
        anyObjectDAO.findAllRelationships(anyObject).forEach(relationship -> {
            relationships.add(relationship.getRightEnd().getKey());
            relationshipTypes.add(relationship.getType().getKey());
        });
        builder = builder.field("relationships", relationships);
        builder = builder.field("relationshipTypes", relationshipTypes);
    } else if (any instanceof Group) {
        Group group = ((Group) any);
        builder = builder.field("name", group.getName());
        if (group.getUserOwner() != null) {
            builder = builder.field("userOwner", group.getUserOwner().getKey());
        }
        if (group.getGroupOwner() != null) {
            builder = builder.field("groupOwner", group.getGroupOwner().getKey());
        }
        List<Object> members = groupDAO.findUMemberships(group).stream().map(membership -> membership.getLeftEnd().getKey()).collect(Collectors.toList());
        members.add(groupDAO.findUDynMembers(group));
        members.addAll(groupDAO.findAMemberships(group).stream().map(membership -> membership.getLeftEnd().getKey()).collect(Collectors.toList()));
        members.add(groupDAO.findADynMembers(group));
        builder = builder.field("members", members);
    } else if (any instanceof User) {
        User user = ((User) any);
        builder = builder.field("username", user.getUsername()).field("lastLoginDate", user.getLastLoginDate()).field("lastRecertification", user.getLastRecertification()).field("lastRecertificator", user.getLastRecertificator());
        List<Object> roles = userDAO.findAllRoles(user).stream().map(r -> r.getKey()).collect(Collectors.toList());
        builder = builder.field("roles", roles);
        Set<Object> privileges = userDAO.findAllRoles(user).stream().flatMap(role -> role.getPrivileges().stream()).map(Entity::getKey).collect(Collectors.toSet());
        builder = builder.field("privileges", privileges);
        List<Object> memberships = new ArrayList<>(userDAO.findAllGroupKeys(user));
        builder = builder.field("memberships", memberships);
        List<Object> relationships = new ArrayList<>();
        Set<Object> relationshipTypes = new HashSet<>();
        user.getRelationships().stream().map(relationship -> {
            relationships.add(relationship.getRightEnd().getKey());
            return relationship;
        }).forEachOrdered(relationship -> {
            relationshipTypes.add(relationship.getType().getKey());
        });
        builder = builder.field("relationships", relationships);
        builder = builder.field("relationshipTypes", relationshipTypes);
    }
    if (any.getPlainAttrs() != null) {
        for (PlainAttr<?> plainAttr : any.getPlainAttrs()) {
            List<Object> values = plainAttr.getValues().stream().map(value -> value.getValue()).collect(Collectors.toList());
            if (plainAttr.getUniqueValue() != null) {
                values.add(plainAttr.getUniqueValue().getValue());
            }
            builder = builder.field(plainAttr.getSchema().getKey(), values);
        }
    }
    builder = builder.endObject();
    return builder;
}
Also used : XContentFactory(org.elasticsearch.common.xcontent.XContentFactory) UserDAO(org.apache.syncope.core.persistence.api.dao.UserDAO) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) PlainAttr(org.apache.syncope.core.persistence.api.entity.PlainAttr) Set(java.util.Set) User(org.apache.syncope.core.persistence.api.entity.user.User) Autowired(org.springframework.beans.factory.annotation.Autowired) IOException(java.io.IOException) Entity(org.apache.syncope.core.persistence.api.entity.Entity) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) GroupDAO(org.apache.syncope.core.persistence.api.dao.GroupDAO) AnyObjectDAO(org.apache.syncope.core.persistence.api.dao.AnyObjectDAO) Group(org.apache.syncope.core.persistence.api.entity.group.Group) Any(org.apache.syncope.core.persistence.api.entity.Any) Transactional(org.springframework.transaction.annotation.Transactional) Group(org.apache.syncope.core.persistence.api.entity.group.Group) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) User(org.apache.syncope.core.persistence.api.entity.user.User) ArrayList(java.util.ArrayList) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) ArrayList(java.util.ArrayList) List(java.util.List) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with PlainAttr

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

the class AbstractAnyDataBinder method fill.

@SuppressWarnings({ "unchecked", "rawtypes" })
protected void fill(final Any any, final AnyTO anyTO, final AnyUtils anyUtils, final SyncopeClientCompositeException scce) {
    // 0. aux classes
    any.getAuxClasses().clear();
    anyTO.getAuxClasses().stream().map(className -> anyTypeClassDAO.find(className)).forEachOrdered(auxClass -> {
        if (auxClass == null) {
            LOG.debug("Invalid " + AnyTypeClass.class.getSimpleName() + " {}, ignoring...", auxClass);
        } else {
            any.add(auxClass);
        }
    });
    // 1. attributes
    SyncopeClientException invalidValues = SyncopeClientException.build(ClientExceptionType.InvalidValues);
    anyTO.getPlainAttrs().stream().filter(attrTO -> !attrTO.getValues().isEmpty()).forEach(attrTO -> {
        PlainSchema schema = getPlainSchema(attrTO.getSchema());
        if (schema != null) {
            PlainAttr<?> attr = (PlainAttr<?>) any.getPlainAttr(schema.getKey()).orElse(null);
            if (attr == null) {
                attr = anyUtils.newPlainAttr();
                ((PlainAttr) attr).setOwner(any);
                attr.setSchema(schema);
            }
            fillAttr(attrTO.getValues(), anyUtils, schema, attr, invalidValues);
            if (attr.getValuesAsStrings().isEmpty()) {
                attr.setOwner(null);
            } else {
                any.add(attr);
            }
        }
    });
    if (!invalidValues.isEmpty()) {
        scce.addException(invalidValues);
    }
    SyncopeClientException requiredValuesMissing = checkMandatory(any, anyUtils);
    if (!requiredValuesMissing.isEmpty()) {
        scce.addException(requiredValuesMissing);
    }
    // 2. resources
    anyTO.getResources().forEach(resourceKey -> {
        ExternalResource resource = resourceDAO.find(resourceKey);
        if (resource == null) {
            LOG.debug("Invalid " + ExternalResource.class.getSimpleName() + " {}, ignoring...", resourceKey);
        } else {
            any.add(resource);
        }
    });
    requiredValuesMissing = checkMandatoryOnResources(any, anyUtils.getAllResources(any));
    if (!requiredValuesMissing.isEmpty()) {
        scce.addException(requiredValuesMissing);
    }
}
Also used : StringPatchItem(org.apache.syncope.common.lib.patch.StringPatchItem) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) Realm(org.apache.syncope.core.persistence.api.entity.Realm) PlainAttr(org.apache.syncope.core.persistence.api.entity.PlainAttr) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) InvalidPlainAttrValueException(org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidPlainAttrValueException) ResourceOperation(org.apache.syncope.common.lib.types.ResourceOperation) StringUtils(org.apache.commons.lang3.StringUtils) AllowedSchemas(org.apache.syncope.core.persistence.api.dao.AllowedSchemas) JexlUtils(org.apache.syncope.core.provisioning.java.jexl.JexlUtils) GroupDAO(org.apache.syncope.core.persistence.api.dao.GroupDAO) AnyObjectDAO(org.apache.syncope.core.persistence.api.dao.AnyObjectDAO) Map(java.util.Map) SchemaDataBinder(org.apache.syncope.core.provisioning.api.data.SchemaDataBinder) PropagationByResource(org.apache.syncope.core.provisioning.api.PropagationByResource) ParseException(java.text.ParseException) AnyTypeClass(org.apache.syncope.core.persistence.api.entity.AnyTypeClass) AnyPatch(org.apache.syncope.common.lib.patch.AnyPatch) RelationshipTypeDAO(org.apache.syncope.core.persistence.api.dao.RelationshipTypeDAO) UserDAO(org.apache.syncope.core.persistence.api.dao.UserDAO) Collection(java.util.Collection) DerAttrHandler(org.apache.syncope.core.provisioning.api.DerAttrHandler) Set(java.util.Set) PlainAttrValue(org.apache.syncope.core.persistence.api.entity.PlainAttrValue) Collectors(java.util.stream.Collectors) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) MappingItem(org.apache.syncope.core.persistence.api.entity.resource.MappingItem) EntityFactory(org.apache.syncope.core.persistence.api.entity.EntityFactory) List(java.util.List) Provision(org.apache.syncope.core.persistence.api.entity.resource.Provision) AttrPatch(org.apache.syncope.common.lib.patch.AttrPatch) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) Optional(java.util.Optional) ExternalResourceDAO(org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO) IntAttrName(org.apache.syncope.core.provisioning.api.IntAttrName) AttrTO(org.apache.syncope.common.lib.to.AttrTO) AnyUtilsFactory(org.apache.syncope.core.persistence.api.entity.AnyUtilsFactory) GroupableRelatable(org.apache.syncope.core.persistence.api.entity.GroupableRelatable) AnyTO(org.apache.syncope.common.lib.to.AnyTO) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DerSchema(org.apache.syncope.core.persistence.api.entity.DerSchema) MappingManager(org.apache.syncope.core.provisioning.api.MappingManager) SyncopeClientCompositeException(org.apache.syncope.common.lib.SyncopeClientCompositeException) ClientExceptionType(org.apache.syncope.common.lib.types.ClientExceptionType) RealmDAO(org.apache.syncope.core.persistence.api.dao.RealmDAO) MembershipTO(org.apache.syncope.common.lib.to.MembershipTO) Logger(org.slf4j.Logger) PlainSchemaDAO(org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO) VirAttrHandler(org.apache.syncope.core.provisioning.api.VirAttrHandler) Membership(org.apache.syncope.core.persistence.api.entity.Membership) PlainAttrValueDAO(org.apache.syncope.core.persistence.api.dao.PlainAttrValueDAO) VirSchema(org.apache.syncope.core.persistence.api.entity.VirSchema) MappingUtils(org.apache.syncope.core.provisioning.java.utils.MappingUtils) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) RelationshipTO(org.apache.syncope.common.lib.to.RelationshipTO) PatchOperation(org.apache.syncope.common.lib.types.PatchOperation) IntAttrNameParser(org.apache.syncope.core.provisioning.java.IntAttrNameParser) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils) Collections(java.util.Collections) AnyTypeClassDAO(org.apache.syncope.core.persistence.api.dao.AnyTypeClassDAO) Any(org.apache.syncope.core.persistence.api.entity.Any) PlainAttrDAO(org.apache.syncope.core.persistence.api.dao.PlainAttrDAO) GroupablePlainAttr(org.apache.syncope.core.persistence.api.entity.GroupablePlainAttr) PlainAttr(org.apache.syncope.core.persistence.api.entity.PlainAttr) GroupablePlainAttr(org.apache.syncope.core.persistence.api.entity.GroupablePlainAttr) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) AnyTypeClass(org.apache.syncope.core.persistence.api.entity.AnyTypeClass) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource)

Aggregations

ArrayList (java.util.ArrayList)4 List (java.util.List)4 Set (java.util.Set)4 AnyObjectDAO (org.apache.syncope.core.persistence.api.dao.AnyObjectDAO)4 GroupDAO (org.apache.syncope.core.persistence.api.dao.GroupDAO)4 UserDAO (org.apache.syncope.core.persistence.api.dao.UserDAO)4 Any (org.apache.syncope.core.persistence.api.entity.Any)4 PlainAttr (org.apache.syncope.core.persistence.api.entity.PlainAttr)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 ParseException (java.text.ParseException)3 Collections (java.util.Collections)3 Optional (java.util.Optional)3 StringUtils (org.apache.commons.lang3.StringUtils)3 AnyTO (org.apache.syncope.common.lib.to.AnyTO)3 AttrTO (org.apache.syncope.common.lib.to.AttrTO)3 MembershipTO (org.apache.syncope.common.lib.to.MembershipTO)3 PlainSchemaDAO (org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO)3 RealmDAO (org.apache.syncope.core.persistence.api.dao.RealmDAO)3 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2