Search in sources :

Example 1 with DerSchema

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

the class JPADerSchemaDAO method delete.

@Override
public void delete(final String key) {
    final DerSchema schema = find(key);
    if (schema == null) {
        return;
    }
    resourceDAO.deleteMapping(key);
    if (schema.getAnyTypeClass() != null) {
        schema.getAnyTypeClass().getDerSchemas().remove(schema);
    }
    entityManager().remove(schema);
}
Also used : JPADerSchema(org.apache.syncope.core.persistence.jpa.entity.JPADerSchema) DerSchema(org.apache.syncope.core.persistence.api.entity.DerSchema)

Example 2 with DerSchema

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

the class DerAttrTest method derAttrFromSpecialAttrs.

@Test
public void derAttrFromSpecialAttrs() {
    DerSchema info = derSchemaDAO.find("info");
    assertEquals("username + ' - ' + creationDate + '[' + failedLogins + ']'", info.getExpression());
    User user = userDAO.findByUsername("vivaldi");
    assertNotNull(user);
    String value = derAttrHandler.getValue(user, info);
    assertNotNull(value);
    assertFalse(value.isEmpty());
    assertTrue(value.startsWith("vivaldi - 2010-10-20"));
    assertTrue(value.endsWith("[0]"));
}
Also used : DerSchema(org.apache.syncope.core.persistence.api.entity.DerSchema) User(org.apache.syncope.core.persistence.api.entity.user.User) Test(org.junit.jupiter.api.Test)

Example 3 with DerSchema

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

the class DerAttrHandlerImpl method getValues.

private Map<DerSchema, String> getValues(final GroupableRelatable<?, ?, ?, ?, ?> any, final Membership<?> membership, final Set<DerSchema> schemas) {
    Map<DerSchema, String> result = new HashMap<>(schemas.size());
    for (DerSchema schema : schemas) {
        JexlContext jexlContext = new MapContext();
        JexlUtils.addPlainAttrsToContext(any.getPlainAttrs(membership), jexlContext);
        JexlUtils.addFieldsToContext(any, jexlContext);
        result.put(schema, JexlUtils.evaluate(schema.getExpression(), jexlContext));
    }
    return result;
}
Also used : DerSchema(org.apache.syncope.core.persistence.api.entity.DerSchema) HashMap(java.util.HashMap) JexlContext(org.apache.commons.jexl3.JexlContext) MapContext(org.apache.commons.jexl3.MapContext)

Example 4 with DerSchema

use of org.apache.syncope.core.persistence.api.entity.DerSchema 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 5 with DerSchema

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

the class GroupDataBinderImpl method getGroupTO.

@Transactional(readOnly = true)
@Override
public GroupTO getGroupTO(final Group group, final boolean details) {
    GroupTO groupTO = new GroupTO();
    // set sys info
    groupTO.setCreator(group.getCreator());
    groupTO.setCreationDate(group.getCreationDate());
    groupTO.setLastModifier(group.getLastModifier());
    groupTO.setLastChangeDate(group.getLastChangeDate());
    groupTO.setKey(group.getKey());
    groupTO.setName(group.getName());
    if (group.getUserOwner() != null) {
        groupTO.setUserOwner(group.getUserOwner().getKey());
    }
    if (group.getGroupOwner() != null) {
        groupTO.setGroupOwner(group.getGroupOwner().getKey());
    }
    Map<DerSchema, String> derAttrValues = derAttrHandler.getValues(group);
    Map<VirSchema, List<String>> virAttrValues = details ? virAttrHandler.getValues(group) : Collections.<VirSchema, List<String>>emptyMap();
    fillTO(groupTO, group.getRealm().getFullPath(), group.getAuxClasses(), group.getPlainAttrs(), derAttrValues, virAttrValues, group.getResources(), details);
    if (details) {
        // dynamic realms
        groupTO.getDynRealms().addAll(groupDAO.findDynRealms(group.getKey()));
    }
    // Static user and AnyType membership counts
    groupTO.setStaticUserMembershipCount(groupDAO.countUMembers(group));
    groupTO.setStaticAnyObjectMembershipCount(groupDAO.countAMembers(group));
    // Dynamic user and AnyType membership counts
    groupTO.setDynamicUserMembershipCount(groupDAO.countUDynMembers(group));
    groupTO.setDynamicAnyObjectMembershipCount(groupDAO.countADynMembers(group));
    if (group.getUDynMembership() != null) {
        groupTO.setUDynMembershipCond(group.getUDynMembership().getFIQLCond());
    }
    group.getADynMemberships().forEach(memb -> {
        groupTO.getADynMembershipConds().put(memb.getAnyType().getKey(), memb.getFIQLCond());
    });
    group.getTypeExtensions().forEach(typeExt -> {
        groupTO.getTypeExtensions().add(getTypeExtensionTO(typeExt));
    });
    return groupTO;
}
Also used : DerSchema(org.apache.syncope.core.persistence.api.entity.DerSchema) VirSchema(org.apache.syncope.core.persistence.api.entity.VirSchema) List(java.util.List) GroupTO(org.apache.syncope.common.lib.to.GroupTO) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

DerSchema (org.apache.syncope.core.persistence.api.entity.DerSchema)22 VirSchema (org.apache.syncope.core.persistence.api.entity.VirSchema)10 PlainSchema (org.apache.syncope.core.persistence.api.entity.PlainSchema)8 Test (org.junit.jupiter.api.Test)6 AbstractTest (org.apache.syncope.core.persistence.jpa.AbstractTest)5 ArrayList (java.util.ArrayList)4 AnyTypeClass (org.apache.syncope.core.persistence.api.entity.AnyTypeClass)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 List (java.util.List)3 DerSchemaTO (org.apache.syncope.common.lib.to.DerSchemaTO)3 PlainSchemaTO (org.apache.syncope.common.lib.to.PlainSchemaTO)3 VirSchemaTO (org.apache.syncope.common.lib.to.VirSchemaTO)3 User (org.apache.syncope.core.persistence.api.entity.user.User)3 Transactional (org.springframework.transaction.annotation.Transactional)3 ParseException (java.text.ParseException)2 HashMap (java.util.HashMap)2 Optional (java.util.Optional)2 JexlContext (org.apache.commons.jexl3.JexlContext)2 MapContext (org.apache.commons.jexl3.MapContext)2 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)2