Search in sources :

Example 1 with APlainAttr

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

the class AnyObjectDataBinderImpl method update.

@Override
public PropagationByResource update(final AnyObject toBeUpdated, final AnyObjectPatch anyObjectPatch) {
    // Re-merge any pending change from workflow tasks
    AnyObject anyObject = anyObjectDAO.save(toBeUpdated);
    PropagationByResource propByRes = new PropagationByResource();
    SyncopeClientCompositeException scce = SyncopeClientException.buildComposite();
    AnyUtils anyUtils = anyUtilsFactory.getInstance(AnyTypeKind.ANY_OBJECT);
    Collection<String> currentResources = anyObjectDAO.findAllResourceKeys(anyObject.getKey());
    // fetch connObjectKeys before update
    Map<String, String> oldConnObjectKeys = getConnObjectKeys(anyObject, anyUtils);
    // realm
    setRealm(anyObject, anyObjectPatch);
    // name
    if (anyObjectPatch.getName() != null && StringUtils.isNotBlank(anyObjectPatch.getName().getValue())) {
        propByRes.addAll(ResourceOperation.UPDATE, anyObjectDAO.findAllResourceKeys(anyObject.getKey()));
        anyObject.setName(anyObjectPatch.getName().getValue());
    }
    // attributes and resources
    propByRes.merge(fill(anyObject, anyObjectPatch, anyUtils, scce));
    // relationships
    anyObjectPatch.getRelationships().stream().filter(patch -> patch.getRelationshipTO() != null).forEachOrdered((patch) -> {
        RelationshipType relationshipType = relationshipTypeDAO.find(patch.getRelationshipTO().getType());
        if (relationshipType == null) {
            LOG.debug("Ignoring invalid relationship type {}", patch.getRelationshipTO().getType());
        } else {
            Optional<? extends ARelationship> relationship = anyObject.getRelationship(relationshipType, patch.getRelationshipTO().getOtherEndKey());
            if (relationship.isPresent()) {
                anyObject.getRelationships().remove(relationship.get());
                relationship.get().setLeftEnd(null);
            }
            if (patch.getOperation() == PatchOperation.ADD_REPLACE) {
                if (StringUtils.isBlank(patch.getRelationshipTO().getOtherEndType()) || AnyTypeKind.USER.name().equals(patch.getRelationshipTO().getOtherEndType()) || AnyTypeKind.GROUP.name().equals(patch.getRelationshipTO().getOtherEndType())) {
                    SyncopeClientException invalidAnyType = SyncopeClientException.build(ClientExceptionType.InvalidAnyType);
                    invalidAnyType.getElements().add(AnyType.class.getSimpleName() + " not allowed for relationship: " + patch.getRelationshipTO().getOtherEndType());
                    scce.addException(invalidAnyType);
                } else {
                    AnyObject otherEnd = anyObjectDAO.find(patch.getRelationshipTO().getOtherEndKey());
                    if (otherEnd == null) {
                        LOG.debug("Ignoring invalid any object {}", patch.getRelationshipTO().getOtherEndKey());
                    } else if (anyObject.getRealm().getFullPath().startsWith(otherEnd.getRealm().getFullPath())) {
                        ARelationship newRelationship = entityFactory.newEntity(ARelationship.class);
                        newRelationship.setType(relationshipType);
                        newRelationship.setRightEnd(otherEnd);
                        newRelationship.setLeftEnd(anyObject);
                        anyObject.add(newRelationship);
                    } else {
                        LOG.error("{} cannot be assigned to {}", otherEnd, anyObject);
                        SyncopeClientException unassignable = SyncopeClientException.build(ClientExceptionType.InvalidRelationship);
                        unassignable.getElements().add("Cannot be assigned: " + otherEnd);
                        scce.addException(unassignable);
                    }
                }
            }
        }
    });
    // prepare for membership-related resource management
    Collection<ExternalResource> resources = anyObjectDAO.findAllResources(anyObject);
    Map<String, Set<String>> reasons = new HashMap<>();
    anyObject.getResources().forEach(resource -> {
        reasons.put(resource.getKey(), new HashSet<>(Collections.singleton(anyObject.getKey())));
    });
    anyObjectDAO.findAllGroupKeys(anyObject).forEach(group -> {
        groupDAO.findAllResourceKeys(group).forEach(resource -> {
            if (!reasons.containsKey(resource)) {
                reasons.put(resource, new HashSet<>());
            }
            reasons.get(resource).add(group);
        });
    });
    Set<String> toBeDeprovisioned = new HashSet<>();
    Set<String> toBeProvisioned = new HashSet<>();
    SyncopeClientException invalidValues = SyncopeClientException.build(ClientExceptionType.InvalidValues);
    // memberships
    anyObjectPatch.getMemberships().stream().filter((membPatch) -> (membPatch.getGroup() != null)).forEachOrdered(membPatch -> {
        Optional<? extends AMembership> membership = anyObject.getMembership(membPatch.getGroup());
        if (membership.isPresent()) {
            anyObject.getMemberships().remove(membership.get());
            membership.get().setLeftEnd(null);
            anyObject.getPlainAttrs(membership.get()).forEach(attr -> {
                anyObject.remove(attr);
                attr.setOwner(null);
            });
            if (membPatch.getOperation() == PatchOperation.DELETE) {
                groupDAO.findAllResourceKeys(membership.get().getRightEnd().getKey()).stream().filter(resource -> reasons.containsKey(resource)).forEach(resource -> {
                    reasons.get(resource).remove(membership.get().getRightEnd().getKey());
                    toBeProvisioned.add(resource);
                });
            }
        }
        if (membPatch.getOperation() == PatchOperation.ADD_REPLACE) {
            Group group = groupDAO.find(membPatch.getGroup());
            if (group == null) {
                LOG.debug("Ignoring invalid group {}", membPatch.getGroup());
            } else if (anyObject.getRealm().getFullPath().startsWith(group.getRealm().getFullPath())) {
                AMembership newMembership = entityFactory.newEntity(AMembership.class);
                newMembership.setRightEnd(group);
                newMembership.setLeftEnd(anyObject);
                anyObject.add(newMembership);
                membPatch.getPlainAttrs().forEach(attrTO -> {
                    PlainSchema schema = getPlainSchema(attrTO.getSchema());
                    if (schema == null) {
                        LOG.debug("Invalid " + PlainSchema.class.getSimpleName() + "{}, ignoring...", attrTO.getSchema());
                    } else {
                        Optional<? extends APlainAttr> attr = anyObject.getPlainAttr(schema.getKey(), newMembership);
                        if (!attr.isPresent()) {
                            LOG.debug("No plain attribute found for {} and membership of {}", schema, newMembership.getRightEnd());
                            APlainAttr newAttr = anyUtils.newPlainAttr();
                            newAttr.setOwner(anyObject);
                            newAttr.setMembership(newMembership);
                            newAttr.setSchema(schema);
                            anyObject.add(newAttr);
                            AttrPatch patch = new AttrPatch.Builder().attrTO(attrTO).build();
                            processAttrPatch(anyObject, patch, schema, newAttr, anyUtils, resources, propByRes, invalidValues);
                        }
                    }
                });
                if (!invalidValues.isEmpty()) {
                    scce.addException(invalidValues);
                }
                toBeProvisioned.addAll(groupDAO.findAllResourceKeys(group.getKey()));
            } else {
                LOG.error("{} cannot be assigned to {}", group, anyObject);
                SyncopeClientException unassignabled = SyncopeClientException.build(ClientExceptionType.InvalidMembership);
                unassignabled.getElements().add("Cannot be assigned: " + group);
                scce.addException(unassignabled);
            }
        }
    });
    // finalize resource management
    reasons.entrySet().stream().filter(entry -> entry.getValue().isEmpty()).forEach(entry -> toBeDeprovisioned.add(entry.getKey()));
    propByRes.addAll(ResourceOperation.DELETE, toBeDeprovisioned);
    propByRes.addAll(ResourceOperation.UPDATE, toBeProvisioned);
    // attribute values.
    if (!toBeDeprovisioned.isEmpty() || !toBeProvisioned.isEmpty()) {
        currentResources.removeAll(toBeDeprovisioned);
        propByRes.addAll(ResourceOperation.UPDATE, currentResources);
    }
    // check if some connObjectKey was changed by the update above
    Map<String, String> newcCnnObjectKeys = getConnObjectKeys(anyObject, anyUtils);
    oldConnObjectKeys.entrySet().stream().filter(entry -> newcCnnObjectKeys.containsKey(entry.getKey()) && !entry.getValue().equals(newcCnnObjectKeys.get(entry.getKey()))).forEach(entry -> {
        propByRes.addOldConnObjectKey(entry.getKey(), entry.getValue());
        propByRes.add(ResourceOperation.UPDATE, entry.getKey());
    });
    Pair<Set<String>, Set<String>> dynGroupMembs = anyObjectDAO.saveAndGetDynGroupMembs(anyObject);
    // finally check if any resource assignment is to be processed due to dynamic group membership change
    dynGroupMembs.getLeft().stream().filter(group -> !dynGroupMembs.getRight().contains(group)).forEach(delete -> {
        groupDAO.find(delete).getResources().stream().filter(resource -> !propByRes.contains(resource.getKey())).forEach(resource -> {
            propByRes.add(ResourceOperation.DELETE, resource.getKey());
        });
    });
    dynGroupMembs.getLeft().stream().filter(group -> dynGroupMembs.getRight().contains(group)).forEach(update -> {
        groupDAO.find(update).getResources().stream().filter(resource -> !propByRes.contains(resource.getKey())).forEach(resource -> {
            propByRes.add(ResourceOperation.UPDATE, resource.getKey());
        });
    });
    dynGroupMembs.getRight().stream().filter(group -> !dynGroupMembs.getLeft().contains(group)).forEach(create -> {
        groupDAO.find(create).getResources().stream().filter(resource -> !propByRes.contains(resource.getKey())).forEach(resource -> {
            propByRes.add(ResourceOperation.CREATE, resource.getKey());
        });
    });
    // Throw composite exception if there is at least one element set in the composing exceptions
    if (scce.hasExceptions()) {
        throw scce;
    }
    return propByRes;
}
Also used : SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) Realm(org.apache.syncope.core.persistence.api.entity.Realm) AnyObjectPatch(org.apache.syncope.common.lib.patch.AnyObjectPatch) AnyType(org.apache.syncope.core.persistence.api.entity.AnyType) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) ResourceOperation(org.apache.syncope.common.lib.types.ResourceOperation) BeanUtils(org.apache.syncope.core.spring.BeanUtils) StringUtils(org.apache.commons.lang3.StringUtils) HashSet(java.util.HashSet) AnyTypeKind(org.apache.syncope.common.lib.types.AnyTypeKind) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) PropagationByResource(org.apache.syncope.core.provisioning.api.PropagationByResource) SyncopeClientCompositeException(org.apache.syncope.common.lib.SyncopeClientCompositeException) ClientExceptionType(org.apache.syncope.common.lib.types.ClientExceptionType) MembershipTO(org.apache.syncope.common.lib.to.MembershipTO) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) Collection(java.util.Collection) AMembership(org.apache.syncope.core.persistence.api.entity.anyobject.AMembership) Set(java.util.Set) Collectors(java.util.stream.Collectors) AnyTypeDAO(org.apache.syncope.core.persistence.api.dao.AnyTypeDAO) VirSchema(org.apache.syncope.core.persistence.api.entity.VirSchema) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) List(java.util.List) ARelationship(org.apache.syncope.core.persistence.api.entity.anyobject.ARelationship) Component(org.springframework.stereotype.Component) AttrPatch(org.apache.syncope.common.lib.patch.AttrPatch) PatchOperation(org.apache.syncope.common.lib.types.PatchOperation) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) Group(org.apache.syncope.core.persistence.api.entity.group.Group) Optional(java.util.Optional) RelationshipType(org.apache.syncope.core.persistence.api.entity.RelationshipType) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils) AnyObjectDataBinder(org.apache.syncope.core.provisioning.api.data.AnyObjectDataBinder) Collections(java.util.Collections) AnyObjectTO(org.apache.syncope.common.lib.to.AnyObjectTO) APlainAttr(org.apache.syncope.core.persistence.api.entity.anyobject.APlainAttr) Transactional(org.springframework.transaction.annotation.Transactional) Group(org.apache.syncope.core.persistence.api.entity.group.Group) HashSet(java.util.HashSet) Set(java.util.Set) APlainAttr(org.apache.syncope.core.persistence.api.entity.anyobject.APlainAttr) HashMap(java.util.HashMap) RelationshipType(org.apache.syncope.core.persistence.api.entity.RelationshipType) PropagationByResource(org.apache.syncope.core.provisioning.api.PropagationByResource) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) AnyType(org.apache.syncope.core.persistence.api.entity.AnyType) HashSet(java.util.HashSet) SyncopeClientCompositeException(org.apache.syncope.common.lib.SyncopeClientCompositeException) Optional(java.util.Optional) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) ARelationship(org.apache.syncope.core.persistence.api.entity.anyobject.ARelationship) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) AttrPatch(org.apache.syncope.common.lib.patch.AttrPatch) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) AMembership(org.apache.syncope.core.persistence.api.entity.anyobject.AMembership) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils)

Example 2 with APlainAttr

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

the class GroupTest method adynMembership.

@Test
public void adynMembership() {
    // 0. create any object matching the condition below
    AnyObject anyObject = entityFactory.newEntity(AnyObject.class);
    anyObject.setName("name");
    anyObject.setType(anyTypeDAO.find("PRINTER"));
    anyObject.setRealm(realmDAO.findByFullPath("/even/two"));
    APlainAttr attr = entityFactory.newEntity(APlainAttr.class);
    attr.setOwner(anyObject);
    attr.setSchema(plainSchemaDAO.find("model"));
    attr.add("Canon MFC8030", anyUtilsFactory.getInstance(AnyTypeKind.ANY_OBJECT));
    anyObject.add(attr);
    anyObject = anyObjectDAO.save(anyObject);
    String newAnyObjectKey = anyObject.getKey();
    assertNotNull(newAnyObjectKey);
    // 1. create group with dynamic membership
    Group group = entityFactory.newEntity(Group.class);
    group.setRealm(realmDAO.getRoot());
    group.setName("new");
    ADynGroupMembership dynMembership = entityFactory.newEntity(ADynGroupMembership.class);
    dynMembership.setAnyType(anyTypeDAO.find("PRINTER"));
    dynMembership.setFIQLCond("model==Canon MFC8030");
    dynMembership.setGroup(group);
    group.add(dynMembership);
    Group actual = groupDAO.save(group);
    assertNotNull(actual);
    groupDAO.flush();
    // 2. verify that dynamic membership is there
    actual = groupDAO.find(actual.getKey());
    assertNotNull(actual);
    assertNotNull(actual.getADynMembership(anyTypeDAO.find("PRINTER")).get());
    assertNotNull(actual.getADynMembership(anyTypeDAO.find("PRINTER")).get().getKey());
    assertEquals(actual, actual.getADynMembership(anyTypeDAO.find("PRINTER")).get().getGroup());
    // 3. verify that expected any objects have the created group dynamically assigned
    List<String> members = groupDAO.findADynMembers(actual).stream().filter(object -> "PRINTER".equals(anyObjectDAO.find(object).getType().getKey())).collect(Collectors.toList());
    assertEquals(2, members.size());
    assertEquals(new HashSet<>(Arrays.asList("fc6dbc3a-6c07-4965-8781-921e7401a4a5", newAnyObjectKey)), new HashSet<>(members));
    anyObject = anyObjectDAO.find("fc6dbc3a-6c07-4965-8781-921e7401a4a5");
    assertNotNull(anyObject);
    Collection<Group> dynGroupMemberships = findDynGroups(anyObject);
    assertEquals(1, dynGroupMemberships.size());
    assertTrue(dynGroupMemberships.contains(actual.getADynMembership(anyTypeDAO.find("PRINTER")).get().getGroup()));
    // 4. delete the new any object and verify that dynamic membership was updated
    anyObjectDAO.delete(newAnyObjectKey);
    anyObjectDAO.flush();
    actual = groupDAO.find(actual.getKey());
    members = groupDAO.findADynMembers(actual).stream().filter(object -> "PRINTER".equals(anyObjectDAO.find(object).getType().getKey())).collect(Collectors.toList());
    assertEquals(1, members.size());
    assertEquals("fc6dbc3a-6c07-4965-8781-921e7401a4a5", members.get(0));
    // 5. delete group and verify that dynamic membership was also removed
    String dynMembershipKey = actual.getADynMembership(anyTypeDAO.find("PRINTER")).get().getKey();
    groupDAO.delete(actual);
    groupDAO.flush();
    assertNull(entityManager().find(JPAADynGroupMembership.class, dynMembershipKey));
    dynGroupMemberships = findDynGroups(anyObject);
    assertTrue(dynGroupMemberships.isEmpty());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) UDynGroupMembership(org.apache.syncope.core.persistence.api.entity.user.UDynGroupMembership) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Autowired(org.springframework.beans.factory.annotation.Autowired) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AnyTypeKind(org.apache.syncope.common.lib.types.AnyTypeKind) GPlainAttrValue(org.apache.syncope.core.persistence.api.entity.group.GPlainAttrValue) GroupDAO(org.apache.syncope.core.persistence.api.dao.GroupDAO) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AnyObjectDAO(org.apache.syncope.core.persistence.api.dao.AnyObjectDAO) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) RealmDAO(org.apache.syncope.core.persistence.api.dao.RealmDAO) JPAADynGroupMembership(org.apache.syncope.core.persistence.jpa.entity.anyobject.JPAADynGroupMembership) SyncopeConstants(org.apache.syncope.common.lib.SyncopeConstants) TypeExtension(org.apache.syncope.core.persistence.api.entity.group.TypeExtension) UserDAO(org.apache.syncope.core.persistence.api.dao.UserDAO) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) Collection(java.util.Collection) PlainSchemaDAO(org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO) ADynGroupMembership(org.apache.syncope.core.persistence.api.entity.anyobject.ADynGroupMembership) User(org.apache.syncope.core.persistence.api.entity.user.User) GPlainAttr(org.apache.syncope.core.persistence.api.entity.group.GPlainAttr) JPAUDynGroupMembership(org.apache.syncope.core.persistence.jpa.entity.user.JPAUDynGroupMembership) JPAGroupDAO(org.apache.syncope.core.persistence.jpa.dao.JPAGroupDAO) Collectors(java.util.stream.Collectors) AnyTypeDAO(org.apache.syncope.core.persistence.api.dao.AnyTypeDAO) PlainAttrValueDAO(org.apache.syncope.core.persistence.api.dao.PlainAttrValueDAO) InvalidEntityException(org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException) Test(org.junit.jupiter.api.Test) List(java.util.List) Query(javax.persistence.Query) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Group(org.apache.syncope.core.persistence.api.entity.group.Group) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest) AnyTypeClassDAO(org.apache.syncope.core.persistence.api.dao.AnyTypeClassDAO) PlainAttrDAO(org.apache.syncope.core.persistence.api.dao.PlainAttrDAO) APlainAttr(org.apache.syncope.core.persistence.api.entity.anyobject.APlainAttr) UPlainAttr(org.apache.syncope.core.persistence.api.entity.user.UPlainAttr) Transactional(org.springframework.transaction.annotation.Transactional) JPAADynGroupMembership(org.apache.syncope.core.persistence.jpa.entity.anyobject.JPAADynGroupMembership) Group(org.apache.syncope.core.persistence.api.entity.group.Group) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) APlainAttr(org.apache.syncope.core.persistence.api.entity.anyobject.APlainAttr) JPAADynGroupMembership(org.apache.syncope.core.persistence.jpa.entity.anyobject.JPAADynGroupMembership) ADynGroupMembership(org.apache.syncope.core.persistence.api.entity.anyobject.ADynGroupMembership) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Aggregations

Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 AnyTypeKind (org.apache.syncope.common.lib.types.AnyTypeKind)2 AnyTypeDAO (org.apache.syncope.core.persistence.api.dao.AnyTypeDAO)2 APlainAttr (org.apache.syncope.core.persistence.api.entity.anyobject.APlainAttr)2 AnyObject (org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject)2 Group (org.apache.syncope.core.persistence.api.entity.group.Group)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Query (javax.persistence.Query)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Pair (org.apache.commons.lang3.tuple.Pair)1 SyncopeClientCompositeException (org.apache.syncope.common.lib.SyncopeClientCompositeException)1