Search in sources :

Example 1 with AnyType

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

the class AbstractAnyLogic method beforeCreate.

protected Pair<TO, List<LogicActions>> beforeCreate(final TO input) {
    Realm realm = realmDAO.findByFullPath(input.getRealm());
    if (realm == null) {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidRealm);
        sce.getElements().add(input.getRealm());
        throw sce;
    }
    AnyType anyType = input instanceof UserTO ? anyTypeDAO.findUser() : input instanceof GroupTO ? anyTypeDAO.findGroup() : anyTypeDAO.find(input.getType());
    if (anyType == null) {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidAnyType);
        sce.getElements().add(input.getType());
        throw sce;
    }
    TO any = input;
    templateUtils.apply(any, realm.getTemplate(anyType));
    List<LogicActions> actions = getActions(realm);
    for (LogicActions action : actions) {
        any = action.beforeCreate(any);
    }
    LOG.debug("Input: {}\nOutput: {}\n", input, any);
    return ImmutablePair.of(any, actions);
}
Also used : UserTO(org.apache.syncope.common.lib.to.UserTO) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) AnyTO(org.apache.syncope.common.lib.to.AnyTO) GroupTO(org.apache.syncope.common.lib.to.GroupTO) UserTO(org.apache.syncope.common.lib.to.UserTO) Realm(org.apache.syncope.core.persistence.api.entity.Realm) AnyType(org.apache.syncope.core.persistence.api.entity.AnyType) LogicActions(org.apache.syncope.core.provisioning.api.LogicActions) GroupTO(org.apache.syncope.common.lib.to.GroupTO)

Example 2 with AnyType

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

the class NotificationManagerImpl method createTasks.

@Override
public List<NotificationTask> createTasks(final AuditElements.EventCategoryType type, final String category, final String subcategory, final String event, final Result condition, final Object before, final Object output, final Object... input) {
    Any<?> any = null;
    if (before instanceof UserTO) {
        any = userDAO.find(((UserTO) before).getKey());
    } else if (output instanceof UserTO) {
        any = userDAO.find(((UserTO) output).getKey());
    } else if (output instanceof Pair && ((Pair) output).getRight() instanceof UserTO) {
        any = userDAO.find(((UserTO) ((Pair) output).getRight()).getKey());
    } else if (output instanceof ProvisioningResult && ((ProvisioningResult) output).getEntity() instanceof UserTO) {
        any = userDAO.find(((ProvisioningResult) output).getEntity().getKey());
    } else if (before instanceof AnyObjectTO) {
        any = anyObjectDAO.find(((AnyObjectTO) before).getKey());
    } else if (output instanceof AnyObjectTO) {
        any = anyObjectDAO.find(((AnyObjectTO) output).getKey());
    } else if (output instanceof ProvisioningResult && ((ProvisioningResult) output).getEntity() instanceof AnyObjectTO) {
        any = anyObjectDAO.find(((ProvisioningResult) output).getEntity().getKey());
    } else if (before instanceof GroupTO) {
        any = groupDAO.find(((GroupTO) before).getKey());
    } else if (output instanceof GroupTO) {
        any = groupDAO.find(((GroupTO) output).getKey());
    } else if (output instanceof ProvisioningResult && ((ProvisioningResult) output).getEntity() instanceof GroupTO) {
        any = groupDAO.find(((ProvisioningResult) output).getEntity().getKey());
    }
    AnyType anyType = any == null ? null : any.getType();
    LOG.debug("Search notification for [{}]{}", anyType, any);
    List<NotificationTask> notifications = new ArrayList<>();
    for (Notification notification : notificationDAO.findAll()) {
        if (LOG.isDebugEnabled()) {
            notification.getAbouts().forEach(about -> {
                LOG.debug("Notification about {} defined: {}", about.getAnyType(), about.get());
            });
        }
        if (notification.isActive()) {
            String currentEvent = AuditLoggerName.buildEvent(type, category, subcategory, event, condition);
            if (!notification.getEvents().contains(currentEvent)) {
                LOG.debug("No events found about {}", any);
            } else if (anyType == null || any == null || !notification.getAbout(anyType).isPresent() || searchDAO.matches(any, SearchCondConverter.convert(notification.getAbout(anyType).get().get()))) {
                LOG.debug("Creating notification task for event {} about {}", currentEvent, any);
                final Map<String, Object> model = new HashMap<>();
                model.put("type", type);
                model.put("category", category);
                model.put("subcategory", subcategory);
                model.put("event", event);
                model.put("condition", condition);
                model.put("before", before);
                model.put("output", output);
                model.put("input", input);
                if (any instanceof User) {
                    model.put("user", userDataBinder.getUserTO((User) any, true));
                } else if (any instanceof Group) {
                    model.put("group", groupDataBinder.getGroupTO((Group) any, true));
                } else if (any instanceof AnyObject) {
                    model.put("group", anyObjectDataBinder.getAnyObjectTO((AnyObject) any, true));
                }
                NotificationTask notificationTask = getNotificationTask(notification, any, model);
                notificationTask = taskDAO.save(notificationTask);
                notifications.add(notificationTask);
            }
        } else {
            LOG.debug("Notification {} is not active, task will not be created", notification.getKey());
        }
    }
    return notifications;
}
Also used : Group(org.apache.syncope.core.persistence.api.entity.group.Group) NotificationTask(org.apache.syncope.core.persistence.api.entity.task.NotificationTask) User(org.apache.syncope.core.persistence.api.entity.user.User) ProvisioningResult(org.apache.syncope.common.lib.to.ProvisioningResult) ArrayList(java.util.ArrayList) Notification(org.apache.syncope.core.persistence.api.entity.Notification) GroupTO(org.apache.syncope.common.lib.to.GroupTO) AnyObjectTO(org.apache.syncope.common.lib.to.AnyObjectTO) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) UserTO(org.apache.syncope.common.lib.to.UserTO) AnyType(org.apache.syncope.core.persistence.api.entity.AnyType) Map(java.util.Map) HashMap(java.util.HashMap) Pair(org.apache.commons.lang3.tuple.Pair)

Example 3 with AnyType

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

the class SchemaDataBinderImpl method fill.

// --------------- VIRTUAL -----------------
private VirSchema fill(final VirSchema schema, final VirSchemaTO schemaTO) {
    BeanUtils.copyProperties(schemaTO, schema, IGNORE_PROPERTIES);
    if (schemaTO.getAnyTypeClass() != null && (schema.getAnyTypeClass() == null || !schemaTO.getAnyTypeClass().equals(schema.getAnyTypeClass().getKey()))) {
        AnyTypeClass anyTypeClass = anyTypeClassDAO.find(schemaTO.getAnyTypeClass());
        if (anyTypeClass == null) {
            LOG.debug("Invalid " + AnyTypeClass.class.getSimpleName() + "{}, ignoring...", schemaTO.getAnyTypeClass());
        } else {
            anyTypeClass.add(schema);
            schema.setAnyTypeClass(anyTypeClass);
        }
    } else if (schemaTO.getAnyTypeClass() == null && schema.getAnyTypeClass() != null) {
        schema.getAnyTypeClass().getVirSchemas().remove(schema);
        schema.setAnyTypeClass(null);
    }
    ExternalResource resource = resourceDAO.find(schemaTO.getResource());
    if (resource == null) {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidSchemaDefinition);
        sce.getElements().add("Resource " + schemaTO.getResource() + " not found");
        throw sce;
    }
    AnyType anyType = anyTypeDAO.find(schemaTO.getAnyType());
    if (anyType == null) {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidSchemaDefinition);
        sce.getElements().add("AnyType " + schemaTO.getAnyType() + " not found");
        throw sce;
    }
    Provision provision = resource.getProvision(anyType).orElse(null);
    if (provision == null) {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidSchemaDefinition);
        sce.getElements().add("Provision for AnyType" + schemaTO.getAnyType() + " not found in " + schemaTO.getResource());
        throw sce;
    }
    schema.setProvision(provision);
    return virSchemaDAO.save(schema);
}
Also used : Provision(org.apache.syncope.core.persistence.api.entity.resource.Provision) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) AnyTypeClass(org.apache.syncope.core.persistence.api.entity.AnyTypeClass) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) AnyType(org.apache.syncope.core.persistence.api.entity.AnyType)

Example 4 with AnyType

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

the class AnyObjectDataBinderImpl method create.

@Override
public void create(final AnyObject anyObject, final AnyObjectTO anyObjectTO) {
    AnyType type = anyTypeDAO.find(anyObjectTO.getType());
    if (type == null) {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidAnyType);
        sce.getElements().add(anyObjectTO.getType());
        throw sce;
    }
    anyObject.setType(type);
    SyncopeClientCompositeException scce = SyncopeClientException.buildComposite();
    // name
    SyncopeClientException invalidGroups = SyncopeClientException.build(ClientExceptionType.InvalidGroup);
    if (anyObjectTO.getName() == null) {
        LOG.error("No name specified for this anyObject");
        invalidGroups.getElements().add("No name specified for this anyObject");
    } else {
        anyObject.setName(anyObjectTO.getName());
    }
    // realm
    Realm realm = realmDAO.findByFullPath(anyObjectTO.getRealm());
    if (realm == null) {
        SyncopeClientException noRealm = SyncopeClientException.build(ClientExceptionType.InvalidRealm);
        noRealm.getElements().add("Invalid or null realm specified: " + anyObjectTO.getRealm());
        scce.addException(noRealm);
    }
    anyObject.setRealm(realm);
    AnyUtils anyUtils = anyUtilsFactory.getInstance(AnyTypeKind.ANY_OBJECT);
    if (anyObject.getRealm() != null) {
        // relationships
        anyObjectTO.getRelationships().forEach(relationshipTO -> {
            if (StringUtils.isBlank(relationshipTO.getOtherEndType()) || AnyTypeKind.USER.name().equals(relationshipTO.getOtherEndType()) || AnyTypeKind.GROUP.name().equals(relationshipTO.getOtherEndType())) {
                SyncopeClientException invalidAnyType = SyncopeClientException.build(ClientExceptionType.InvalidAnyType);
                invalidAnyType.getElements().add(AnyType.class.getSimpleName() + " not allowed for relationship: " + relationshipTO.getOtherEndType());
                scce.addException(invalidAnyType);
            } else {
                AnyObject otherEnd = anyObjectDAO.find(relationshipTO.getOtherEndKey());
                if (otherEnd == null) {
                    LOG.debug("Ignoring invalid anyObject " + relationshipTO.getOtherEndKey());
                } else if (anyObject.getRealm().getFullPath().startsWith(otherEnd.getRealm().getFullPath())) {
                    RelationshipType relationshipType = relationshipTypeDAO.find(relationshipTO.getType());
                    if (relationshipType == null) {
                        LOG.debug("Ignoring invalid relationship type {}", relationshipTO.getType());
                    } else {
                        ARelationship relationship = entityFactory.newEntity(ARelationship.class);
                        relationship.setType(relationshipType);
                        relationship.setRightEnd(otherEnd);
                        relationship.setLeftEnd(anyObject);
                        anyObject.add(relationship);
                    }
                } else {
                    LOG.error("{} cannot be assigned to {}", otherEnd, anyObject);
                    SyncopeClientException unassignabled = SyncopeClientException.build(ClientExceptionType.InvalidRelationship);
                    unassignabled.getElements().add("Cannot be assigned: " + otherEnd);
                    scce.addException(unassignabled);
                }
            }
        });
        // memberships
        anyObjectTO.getMemberships().forEach(membershipTO -> {
            Group group = membershipTO.getGroupKey() == null ? groupDAO.findByName(membershipTO.getGroupName()) : groupDAO.find(membershipTO.getGroupKey());
            if (group == null) {
                LOG.debug("Ignoring invalid group " + membershipTO.getGroupKey() + " / " + membershipTO.getGroupName());
            } else if (anyObject.getRealm().getFullPath().startsWith(group.getRealm().getFullPath())) {
                AMembership membership = entityFactory.newEntity(AMembership.class);
                membership.setRightEnd(group);
                membership.setLeftEnd(anyObject);
                anyObject.add(membership);
                // membership attributes
                fill(anyObject, membership, membershipTO, anyUtils, scce);
            } else {
                LOG.error("{} cannot be assigned to {}", group, anyObject);
                SyncopeClientException unassignable = SyncopeClientException.build(ClientExceptionType.InvalidMembership);
                unassignable.getElements().add("Cannot be assigned: " + group);
                scce.addException(unassignable);
            }
        });
    }
    // attributes and resources
    fill(anyObject, anyObjectTO, anyUtils, scce);
    // Throw composite exception if there is at least one element set in the composing exceptions
    if (scce.hasExceptions()) {
        throw scce;
    }
}
Also used : Group(org.apache.syncope.core.persistence.api.entity.group.Group) SyncopeClientCompositeException(org.apache.syncope.common.lib.SyncopeClientCompositeException) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) AMembership(org.apache.syncope.core.persistence.api.entity.anyobject.AMembership) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) RelationshipType(org.apache.syncope.core.persistence.api.entity.RelationshipType) AnyType(org.apache.syncope.core.persistence.api.entity.AnyType) Realm(org.apache.syncope.core.persistence.api.entity.Realm) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils) ARelationship(org.apache.syncope.core.persistence.api.entity.anyobject.ARelationship)

Example 5 with AnyType

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

the class GroupDataBinderImpl method create.

@Override
public void create(final Group group, final GroupTO groupTO) {
    SyncopeClientCompositeException scce = SyncopeClientException.buildComposite();
    // name
    SyncopeClientException invalidGroups = SyncopeClientException.build(ClientExceptionType.InvalidGroup);
    if (groupTO.getName() == null) {
        LOG.error("No name specified for this group");
        invalidGroups.getElements().add("No name specified for this group");
    } else {
        group.setName(groupTO.getName());
    }
    // realm
    Realm realm = realmDAO.findByFullPath(groupTO.getRealm());
    if (realm == null) {
        SyncopeClientException noRealm = SyncopeClientException.build(ClientExceptionType.InvalidRealm);
        noRealm.getElements().add("Invalid or null realm specified: " + groupTO.getRealm());
        scce.addException(noRealm);
    }
    group.setRealm(realm);
    // attributes and resources
    fill(group, groupTO, anyUtilsFactory.getInstance(AnyTypeKind.GROUP), scce);
    // owner
    if (groupTO.getUserOwner() != null) {
        User owner = userDAO.find(groupTO.getUserOwner());
        if (owner == null) {
            LOG.warn("Ignoring invalid user specified as owner: {}", groupTO.getUserOwner());
        } else {
            group.setUserOwner(owner);
        }
    }
    if (groupTO.getGroupOwner() != null) {
        Group owner = groupDAO.find(groupTO.getGroupOwner());
        if (owner == null) {
            LOG.warn("Ignoring invalid group specified as owner: {}", groupTO.getGroupOwner());
        } else {
            group.setGroupOwner(owner);
        }
    }
    // dynamic membership
    if (groupTO.getUDynMembershipCond() != null) {
        setDynMembership(group, anyTypeDAO.findUser(), groupTO.getUDynMembershipCond());
    }
    groupTO.getADynMembershipConds().forEach((type, fiql) -> {
        AnyType anyType = anyTypeDAO.find(type);
        if (anyType == null) {
            LOG.warn("Ignoring invalid {}: {}", AnyType.class.getSimpleName(), type);
        } else {
            setDynMembership(group, anyType, fiql);
        }
    });
    // type extensions
    groupTO.getTypeExtensions().forEach(typeExtTO -> {
        AnyType anyType = anyTypeDAO.find(typeExtTO.getAnyType());
        if (anyType == null) {
            LOG.warn("Ignoring invalid {}: {}", AnyType.class.getSimpleName(), typeExtTO.getAnyType());
        } else {
            TypeExtension typeExt = entityFactory.newEntity(TypeExtension.class);
            typeExt.setAnyType(anyType);
            typeExt.setGroup(group);
            group.add(typeExt);
            typeExtTO.getAuxClasses().forEach(name -> {
                AnyTypeClass anyTypeClass = anyTypeClassDAO.find(name);
                if (anyTypeClass == null) {
                    LOG.warn("Ignoring invalid {}: {}", AnyTypeClass.class.getSimpleName(), name);
                } else {
                    typeExt.add(anyTypeClass);
                }
            });
            if (typeExt.getAuxClasses().isEmpty()) {
                group.getTypeExtensions().remove(typeExt);
                typeExt.setGroup(null);
            }
        }
    });
    // Throw composite exception if there is at least one element set in the composing exceptions
    if (scce.hasExceptions()) {
        throw scce;
    }
}
Also used : Group(org.apache.syncope.core.persistence.api.entity.group.Group) SyncopeClientCompositeException(org.apache.syncope.common.lib.SyncopeClientCompositeException) User(org.apache.syncope.core.persistence.api.entity.user.User) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) TypeExtension(org.apache.syncope.core.persistence.api.entity.group.TypeExtension) Realm(org.apache.syncope.core.persistence.api.entity.Realm) AnyType(org.apache.syncope.core.persistence.api.entity.AnyType) AnyTypeClass(org.apache.syncope.core.persistence.api.entity.AnyTypeClass)

Aggregations

AnyType (org.apache.syncope.core.persistence.api.entity.AnyType)35 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)13 NotFoundException (org.apache.syncope.core.persistence.api.dao.NotFoundException)9 ExternalResource (org.apache.syncope.core.persistence.api.entity.resource.ExternalResource)9 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)7 Collectors (java.util.stream.Collectors)6 AnyTypeDAO (org.apache.syncope.core.persistence.api.dao.AnyTypeDAO)6 AnyTypeClass (org.apache.syncope.core.persistence.api.entity.AnyTypeClass)6 Autowired (org.springframework.beans.factory.annotation.Autowired)6 Component (org.springframework.stereotype.Component)6 ArrayList (java.util.ArrayList)5 StringUtils (org.apache.commons.lang3.StringUtils)5 AnyTypeKind (org.apache.syncope.common.lib.types.AnyTypeKind)5 Realm (org.apache.syncope.core.persistence.api.entity.Realm)5 AnyObject (org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject)5 Group (org.apache.syncope.core.persistence.api.entity.group.Group)5 Provision (org.apache.syncope.core.persistence.api.entity.resource.Provision)5 AbstractTest (org.apache.syncope.core.persistence.jpa.AbstractTest)5 Test (org.junit.jupiter.api.Test)5 Map (java.util.Map)4