Search in sources :

Example 1 with URelationship

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

the class UserDataBinderImpl method create.

@Override
public void create(final User user, final UserTO userTO, final boolean storePassword) {
    SyncopeClientCompositeException scce = SyncopeClientException.buildComposite();
    // set username
    user.setUsername(userTO.getUsername());
    // set password
    if (StringUtils.isBlank(userTO.getPassword()) || !storePassword) {
        LOG.debug("Password was not provided or not required to be stored");
    } else {
        setPassword(user, userTO.getPassword(), scce);
    }
    user.setMustChangePassword(userTO.isMustChangePassword());
    // security question / answer
    if (userTO.getSecurityQuestion() != null) {
        SecurityQuestion securityQuestion = securityQuestionDAO.find(userTO.getSecurityQuestion());
        if (securityQuestion != null) {
            user.setSecurityQuestion(securityQuestion);
        }
    }
    user.setSecurityAnswer(userTO.getSecurityAnswer());
    // roles
    userTO.getRoles().forEach(roleKey -> {
        Role role = roleDAO.find(roleKey);
        if (role == null) {
            LOG.warn("Ignoring unknown role with id {}", roleKey);
        } else {
            user.add(role);
        }
    });
    // realm
    Realm realm = realmDAO.findByFullPath(userTO.getRealm());
    if (realm == null) {
        SyncopeClientException noRealm = SyncopeClientException.build(ClientExceptionType.InvalidRealm);
        noRealm.getElements().add("Invalid or null realm specified: " + userTO.getRealm());
        scce.addException(noRealm);
    }
    user.setRealm(realm);
    AnyUtils anyUtils = anyUtilsFactory.getInstance(AnyTypeKind.USER);
    if (user.getRealm() != null) {
        // relationships
        userTO.getRelationships().forEach(relationshipTO -> {
            AnyObject otherEnd = anyObjectDAO.find(relationshipTO.getOtherEndKey());
            if (otherEnd == null) {
                LOG.debug("Ignoring invalid anyObject " + relationshipTO.getOtherEndKey());
            } else if (user.getRealm().getFullPath().startsWith(otherEnd.getRealm().getFullPath())) {
                RelationshipType relationshipType = relationshipTypeDAO.find(relationshipTO.getType());
                if (relationshipType == null) {
                    LOG.debug("Ignoring invalid relationship type {}", relationshipTO.getType());
                } else {
                    URelationship relationship = entityFactory.newEntity(URelationship.class);
                    relationship.setType(relationshipType);
                    relationship.setRightEnd(otherEnd);
                    relationship.setLeftEnd(user);
                    user.add(relationship);
                }
            } else {
                LOG.error("{} cannot be assigned to {}", otherEnd, user);
                SyncopeClientException unassignabled = SyncopeClientException.build(ClientExceptionType.InvalidRelationship);
                unassignabled.getElements().add("Cannot be assigned: " + otherEnd);
                scce.addException(unassignabled);
            }
        });
        // memberships
        userTO.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 (user.getRealm().getFullPath().startsWith(group.getRealm().getFullPath())) {
                UMembership membership = entityFactory.newEntity(UMembership.class);
                membership.setRightEnd(group);
                membership.setLeftEnd(user);
                user.add(membership);
                // membership attributes
                fill(user, membership, membershipTO, anyUtils, scce);
            } else {
                LOG.error("{} cannot be assigned to {}", group, user);
                SyncopeClientException unassignable = SyncopeClientException.build(ClientExceptionType.InvalidMembership);
                unassignable.getElements().add("Cannot be assigned: " + group);
                scce.addException(unassignable);
            }
        });
    }
    // attributes and resources
    fill(user, userTO, anyUtils, scce);
    // Throw composite exception if there is at least one element set in the composing exceptions
    if (scce.hasExceptions()) {
        throw scce;
    }
}
Also used : Role(org.apache.syncope.core.persistence.api.entity.Role) 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) UMembership(org.apache.syncope.core.persistence.api.entity.user.UMembership) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) RelationshipType(org.apache.syncope.core.persistence.api.entity.RelationshipType) Realm(org.apache.syncope.core.persistence.api.entity.Realm) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils) URelationship(org.apache.syncope.core.persistence.api.entity.user.URelationship) SecurityQuestion(org.apache.syncope.core.persistence.api.entity.user.SecurityQuestion)

Example 2 with URelationship

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

the class UserDataBinderImpl method update.

@Override
public PropagationByResource update(final User toBeUpdated, final UserPatch userPatch) {
    // Re-merge any pending change from workflow tasks
    User user = userDAO.save(toBeUpdated);
    PropagationByResource propByRes = new PropagationByResource();
    SyncopeClientCompositeException scce = SyncopeClientException.buildComposite();
    AnyUtils anyUtils = anyUtilsFactory.getInstance(AnyTypeKind.USER);
    Collection<String> currentResources = userDAO.findAllResourceKeys(user.getKey());
    // fetch connObjectKeys before update
    Map<String, String> oldConnObjectKeys = getConnObjectKeys(user, anyUtils);
    // realm
    setRealm(user, userPatch);
    // password
    if (userPatch.getPassword() != null && StringUtils.isNotBlank(userPatch.getPassword().getValue())) {
        if (userPatch.getPassword().isOnSyncope()) {
            setPassword(user, userPatch.getPassword().getValue(), scce);
            user.setChangePwdDate(new Date());
        }
        propByRes.addAll(ResourceOperation.UPDATE, userPatch.getPassword().getResources());
    }
    // username
    if (userPatch.getUsername() != null && StringUtils.isNotBlank(userPatch.getUsername().getValue())) {
        String oldUsername = user.getUsername();
        user.setUsername(userPatch.getUsername().getValue());
        if (oldUsername.equals(AuthContextUtils.getUsername())) {
            AuthContextUtils.updateUsername(userPatch.getUsername().getValue());
        }
        AccessToken accessToken = accessTokenDAO.findByOwner(oldUsername);
        if (accessToken != null) {
            accessToken.setOwner(userPatch.getUsername().getValue());
            accessTokenDAO.save(accessToken);
        }
        propByRes.addAll(ResourceOperation.UPDATE, currentResources);
    }
    // security question / answer:
    if (userPatch.getSecurityQuestion() != null) {
        if (userPatch.getSecurityQuestion().getValue() == null) {
            user.setSecurityQuestion(null);
            user.setSecurityAnswer(null);
        } else {
            SecurityQuestion securityQuestion = securityQuestionDAO.find(userPatch.getSecurityQuestion().getValue());
            if (securityQuestion != null) {
                user.setSecurityQuestion(securityQuestion);
                user.setSecurityAnswer(userPatch.getSecurityAnswer().getValue());
            }
        }
    }
    if (userPatch.getMustChangePassword() != null) {
        user.setMustChangePassword(userPatch.getMustChangePassword().getValue());
    }
    // roles
    for (StringPatchItem patch : userPatch.getRoles()) {
        Role role = roleDAO.find(patch.getValue());
        if (role == null) {
            LOG.warn("Ignoring unknown role with key {}", patch.getValue());
        } else {
            switch(patch.getOperation()) {
                case ADD_REPLACE:
                    user.add(role);
                    break;
                case DELETE:
                default:
                    user.getRoles().remove(role);
            }
        }
    }
    // attributes and resources
    propByRes.merge(fill(user, userPatch, anyUtils, scce));
    // relationships
    userPatch.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 URelationship> relationship = user.getRelationship(relationshipType, patch.getRelationshipTO().getOtherEndKey());
            if (relationship.isPresent()) {
                user.getRelationships().remove(relationship.get());
                relationship.get().setLeftEnd(null);
            }
            if (patch.getOperation() == PatchOperation.ADD_REPLACE) {
                AnyObject otherEnd = anyObjectDAO.find(patch.getRelationshipTO().getOtherEndKey());
                if (otherEnd == null) {
                    LOG.debug("Ignoring invalid any object {}", patch.getRelationshipTO().getOtherEndKey());
                } else if (user.getRealm().getFullPath().startsWith(otherEnd.getRealm().getFullPath())) {
                    URelationship newRelationship = entityFactory.newEntity(URelationship.class);
                    newRelationship.setType(relationshipType);
                    newRelationship.setRightEnd(otherEnd);
                    newRelationship.setLeftEnd(user);
                    user.add(newRelationship);
                } else {
                    LOG.error("{} cannot be assigned to {}", otherEnd, user);
                    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 = userDAO.findAllResources(user);
    Map<String, Set<String>> reasons = new HashMap<>();
    user.getResources().forEach(resource -> {
        reasons.put(resource.getKey(), new HashSet<>(Collections.singleton(user.getKey())));
    });
    userDAO.findAllGroupKeys(user).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
    userPatch.getMemberships().stream().filter(membPatch -> membPatch.getGroup() != null).forEachOrdered((membPatch) -> {
        Optional<? extends UMembership> membership = user.getMembership(membPatch.getGroup());
        if (membership.isPresent()) {
            user.getMemberships().remove(membership.get());
            membership.get().setLeftEnd(null);
            user.getPlainAttrs(membership.get()).forEach(attr -> {
                user.remove(attr);
                attr.setOwner(null);
                attr.setMembership(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 (user.getRealm().getFullPath().startsWith(group.getRealm().getFullPath())) {
                UMembership newMembership = entityFactory.newEntity(UMembership.class);
                newMembership.setRightEnd(group);
                newMembership.setLeftEnd(user);
                user.add(newMembership);
                membPatch.getPlainAttrs().forEach(attrTO -> {
                    PlainSchema schema = getPlainSchema(attrTO.getSchema());
                    if (schema == null) {
                        LOG.debug("Invalid " + PlainSchema.class.getSimpleName() + "{}, ignoring...", attrTO.getSchema());
                    } else {
                        UPlainAttr attr = user.getPlainAttr(schema.getKey(), newMembership).orElse(null);
                        if (attr == null) {
                            LOG.debug("No plain attribute found for {} and membership of {}", schema, newMembership.getRightEnd());
                            attr = anyUtils.newPlainAttr();
                            attr.setOwner(user);
                            attr.setMembership(newMembership);
                            attr.setSchema(schema);
                            user.add(attr);
                            AttrPatch patch = new AttrPatch.Builder().attrTO(attrTO).build();
                            processAttrPatch(user, patch, schema, attr, anyUtils, resources, propByRes, invalidValues);
                        }
                    }
                });
                if (!invalidValues.isEmpty()) {
                    scce.addException(invalidValues);
                }
                toBeProvisioned.addAll(groupDAO.findAllResourceKeys(group.getKey()));
                // ensure that they are counted for password propagation
                if (toBeUpdated.canDecodePassword()) {
                    if (userPatch.getPassword() == null) {
                        userPatch.setPassword(new PasswordPatch());
                    }
                    group.getResources().stream().filter(resource -> isPasswordMapped(resource)).forEachOrdered(resource -> {
                        userPatch.getPassword().getResources().add(resource.getKey());
                    });
                }
            } else {
                LOG.error("{} cannot be assigned to {}", group, user);
                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(user, 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 = userDAO.saveAndGetDynGroupMembs(user);
    // 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 : StringPatchItem(org.apache.syncope.common.lib.patch.StringPatchItem) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) SecurityQuestionDAO(org.apache.syncope.core.persistence.api.dao.SecurityQuestionDAO) Date(java.util.Date) Realm(org.apache.syncope.core.persistence.api.entity.Realm) Autowired(org.springframework.beans.factory.annotation.Autowired) UserPatch(org.apache.syncope.common.lib.patch.UserPatch) Entity(org.apache.syncope.core.persistence.api.entity.Entity) ResourceOperation(org.apache.syncope.common.lib.types.ResourceOperation) StringUtils(org.apache.commons.lang3.StringUtils) RoleDAO(org.apache.syncope.core.persistence.api.dao.RoleDAO) AnyTypeKind(org.apache.syncope.common.lib.types.AnyTypeKind) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) UserDataBinder(org.apache.syncope.core.provisioning.api.data.UserDataBinder) PropagationByResource(org.apache.syncope.core.provisioning.api.PropagationByResource) AuthContextUtils(org.apache.syncope.core.spring.security.AuthContextUtils) Role(org.apache.syncope.core.persistence.api.entity.Role) Collection(java.util.Collection) Resource(javax.annotation.Resource) Set(java.util.Set) Collectors(java.util.stream.Collectors) AnyTypeDAO(org.apache.syncope.core.persistence.api.dao.AnyTypeDAO) List(java.util.List) Provision(org.apache.syncope.core.persistence.api.entity.resource.Provision) AttrPatch(org.apache.syncope.common.lib.patch.AttrPatch) Group(org.apache.syncope.core.persistence.api.entity.group.Group) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) Optional(java.util.Optional) ConfDAO(org.apache.syncope.core.persistence.api.dao.ConfDAO) UPlainAttr(org.apache.syncope.core.persistence.api.entity.user.UPlainAttr) AccessToken(org.apache.syncope.core.persistence.api.entity.AccessToken) HashMap(java.util.HashMap) BooleanUtils(org.apache.commons.lang3.BooleanUtils) BeanUtils(org.apache.syncope.core.spring.BeanUtils) URelationship(org.apache.syncope.core.persistence.api.entity.user.URelationship) HashSet(java.util.HashSet) SyncopeClientCompositeException(org.apache.syncope.common.lib.SyncopeClientCompositeException) ClientExceptionType(org.apache.syncope.common.lib.types.ClientExceptionType) UMembership(org.apache.syncope.core.persistence.api.entity.user.UMembership) MembershipTO(org.apache.syncope.common.lib.to.MembershipTO) CipherAlgorithm(org.apache.syncope.common.lib.types.CipherAlgorithm) Encryptor(org.apache.syncope.core.spring.security.Encryptor) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) User(org.apache.syncope.core.persistence.api.entity.user.User) AccessTokenDAO(org.apache.syncope.core.persistence.api.dao.AccessTokenDAO) VirSchema(org.apache.syncope.core.persistence.api.entity.VirSchema) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) Component(org.springframework.stereotype.Component) PasswordPatch(org.apache.syncope.common.lib.patch.PasswordPatch) PatchOperation(org.apache.syncope.common.lib.types.PatchOperation) RelationshipType(org.apache.syncope.core.persistence.api.entity.RelationshipType) UserTO(org.apache.syncope.common.lib.to.UserTO) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils) Collections(java.util.Collections) SecurityQuestion(org.apache.syncope.core.persistence.api.entity.user.SecurityQuestion) 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) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) RelationshipType(org.apache.syncope.core.persistence.api.entity.RelationshipType) PropagationByResource(org.apache.syncope.core.provisioning.api.PropagationByResource) SecurityQuestion(org.apache.syncope.core.persistence.api.entity.user.SecurityQuestion) UMembership(org.apache.syncope.core.persistence.api.entity.user.UMembership) AccessToken(org.apache.syncope.core.persistence.api.entity.AccessToken) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) UPlainAttr(org.apache.syncope.core.persistence.api.entity.user.UPlainAttr) URelationship(org.apache.syncope.core.persistence.api.entity.user.URelationship) HashSet(java.util.HashSet) SyncopeClientCompositeException(org.apache.syncope.common.lib.SyncopeClientCompositeException) PasswordPatch(org.apache.syncope.common.lib.patch.PasswordPatch) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) Date(java.util.Date) AttrPatch(org.apache.syncope.common.lib.patch.AttrPatch) Role(org.apache.syncope.core.persistence.api.entity.Role) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) StringPatchItem(org.apache.syncope.common.lib.patch.StringPatchItem) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils)

Example 3 with URelationship

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

the class UserReportlet method doExtract.

private void doExtract(final ContentHandler handler, final List<User> users) throws SAXException {
    AttributesImpl atts = new AttributesImpl();
    for (User user : users) {
        atts.clear();
        for (Feature feature : conf.getFeatures()) {
            String type = null;
            String value = null;
            switch(feature) {
                case key:
                    type = ReportXMLConst.XSD_STRING;
                    value = user.getKey();
                    break;
                case username:
                    type = ReportXMLConst.XSD_STRING;
                    value = user.getUsername();
                    break;
                case workflowId:
                    type = ReportXMLConst.XSD_STRING;
                    value = user.getWorkflowId();
                    break;
                case status:
                    type = ReportXMLConst.XSD_STRING;
                    value = user.getStatus();
                    break;
                case creationDate:
                    type = ReportXMLConst.XSD_DATETIME;
                    value = user.getCreationDate() == null ? "" : FormatUtils.format(user.getCreationDate());
                    break;
                case lastLoginDate:
                    type = ReportXMLConst.XSD_DATETIME;
                    value = user.getLastLoginDate() == null ? "" : FormatUtils.format(user.getLastLoginDate());
                    break;
                case changePwdDate:
                    type = ReportXMLConst.XSD_DATETIME;
                    value = user.getChangePwdDate() == null ? "" : FormatUtils.format(user.getChangePwdDate());
                    break;
                case passwordHistorySize:
                    type = ReportXMLConst.XSD_INT;
                    value = String.valueOf(user.getPasswordHistory().size());
                    break;
                case failedLoginCount:
                    type = ReportXMLConst.XSD_INT;
                    value = String.valueOf(user.getFailedLogins());
                    break;
                default:
            }
            if (type != null && value != null) {
                atts.addAttribute("", "", feature.name(), type, value);
            }
        }
        handler.startElement("", "", "user", atts);
        // Using UserTO for attribute values, since the conversion logic of
        // values to String is already encapsulated there
        UserTO userTO = userDataBinder.getUserTO(user, true);
        doExtractAttributes(handler, userTO, conf.getPlainAttrs(), conf.getDerAttrs(), conf.getVirAttrs());
        if (conf.getFeatures().contains(Feature.relationships)) {
            handler.startElement("", "", "relationships", null);
            for (RelationshipTO rel : userTO.getRelationships()) {
                atts.clear();
                atts.addAttribute("", "", "anyObjectKey", ReportXMLConst.XSD_STRING, rel.getOtherEndKey());
                handler.startElement("", "", "relationship", atts);
                if (conf.getFeatures().contains(Feature.resources)) {
                    for (URelationship actualRel : user.getRelationships(rel.getOtherEndKey())) {
                        doExtractResources(handler, anyObjectDataBinder.getAnyObjectTO(actualRel.getRightEnd(), true));
                    }
                }
                handler.endElement("", "", "relationship");
            }
            handler.endElement("", "", "relationships");
        }
        if (conf.getFeatures().contains(Feature.memberships)) {
            handler.startElement("", "", "memberships", null);
            for (MembershipTO memb : userTO.getMemberships()) {
                atts.clear();
                atts.addAttribute("", "", "groupKey", ReportXMLConst.XSD_STRING, memb.getGroupKey());
                atts.addAttribute("", "", "groupName", ReportXMLConst.XSD_STRING, memb.getGroupName());
                handler.startElement("", "", "membership", atts);
                if (conf.getFeatures().contains(Feature.resources)) {
                    UMembership actualMemb = user.getMembership(memb.getGroupKey()).orElse(null);
                    if (actualMemb == null) {
                        LOG.warn("Unexpected: cannot find membership for group {} for user {}", memb.getGroupKey(), user);
                    } else {
                        doExtractResources(handler, groupDataBinder.getGroupTO(actualMemb.getRightEnd(), true));
                    }
                }
                handler.endElement("", "", "membership");
            }
            handler.endElement("", "", "memberships");
        }
        if (conf.getFeatures().contains(Feature.resources)) {
            doExtractResources(handler, userTO);
        }
        handler.endElement("", "", "user");
    }
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) User(org.apache.syncope.core.persistence.api.entity.user.User) RelationshipTO(org.apache.syncope.common.lib.to.RelationshipTO) UMembership(org.apache.syncope.core.persistence.api.entity.user.UMembership) UserTO(org.apache.syncope.common.lib.to.UserTO) MembershipTO(org.apache.syncope.common.lib.to.MembershipTO) Feature(org.apache.syncope.common.lib.report.UserReportletConf.Feature) URelationship(org.apache.syncope.core.persistence.api.entity.user.URelationship)

Example 4 with URelationship

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

the class UserTest method ships.

@Test
public void ships() {
    User user = userDAO.findByUsername("bellini");
    assertNotNull(user);
    assertEquals(1, user.getMemberships().size());
    assertEquals("bf825fe1-7320-4a54-bd64-143b5c18ab97", user.getMemberships().get(0).getRightEnd().getKey());
    user.getMemberships().remove(0);
    UMembership newM = entityFactory.newEntity(UMembership.class);
    newM.setLeftEnd(user);
    newM.setRightEnd(groupDAO.find("ba9ed509-b1f5-48ab-a334-c8530a6422dc"));
    user.add(newM);
    userDAO.save(user);
    userDAO.flush();
    user = userDAO.findByUsername("bellini");
    assertEquals(1, user.getMemberships().size());
    assertEquals("ba9ed509-b1f5-48ab-a334-c8530a6422dc", user.getMemberships().get(0).getRightEnd().getKey());
    assertEquals(1, user.getRelationships().size());
    assertEquals("fc6dbc3a-6c07-4965-8781-921e7401a4a5", user.getRelationships().get(0).getRightEnd().getKey());
    user.getRelationships().remove(0);
    URelationship newR = entityFactory.newEntity(URelationship.class);
    newR.setType(relationshipTypeDAO.find("neighborhood"));
    newR.setLeftEnd(user);
    newR.setRightEnd(anyObjectDAO.find("8559d14d-58c2-46eb-a2d4-a7d35161e8f8"));
    user.add(newR);
    userDAO.save(user);
    userDAO.flush();
    user = userDAO.findByUsername("bellini");
    assertEquals(1, user.getRelationships().size());
    assertEquals("8559d14d-58c2-46eb-a2d4-a7d35161e8f8", user.getRelationships().get(0).getRightEnd().getKey());
}
Also used : User(org.apache.syncope.core.persistence.api.entity.user.User) UMembership(org.apache.syncope.core.persistence.api.entity.user.UMembership) URelationship(org.apache.syncope.core.persistence.api.entity.user.URelationship) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Example 5 with URelationship

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

the class JPARelationshipTypeDAO method delete.

@Override
public void delete(final String key) {
    RelationshipType type = find(key);
    if (type == null) {
        return;
    }
    findRelationshipsByType(type).stream().map(relationship -> {
        if (relationship instanceof URelationship) {
            ((URelationship) relationship).getLeftEnd().getRelationships().remove((URelationship) relationship);
        } else if (relationship instanceof UMembership) {
            ((UMembership) relationship).getLeftEnd().getMemberships().remove((UMembership) relationship);
        } else if (relationship instanceof ARelationship) {
            ((ARelationship) relationship).getLeftEnd().getRelationships().remove((ARelationship) relationship);
        } else if (relationship instanceof AMembership) {
            ((AMembership) relationship).getLeftEnd().getMemberships().remove((AMembership) relationship);
        }
        relationship.setLeftEnd(null);
        return relationship;
    }).forEachOrdered(relationship -> entityManager().remove(relationship));
    entityManager().remove(type);
}
Also used : RelationshipTypeDAO(org.apache.syncope.core.persistence.api.dao.RelationshipTypeDAO) Collection(java.util.Collection) AMembership(org.apache.syncope.core.persistence.api.entity.anyobject.AMembership) JPAURelationship(org.apache.syncope.core.persistence.jpa.entity.user.JPAURelationship) URelationship(org.apache.syncope.core.persistence.api.entity.user.URelationship) TypedQuery(javax.persistence.TypedQuery) ArrayList(java.util.ArrayList) List(java.util.List) ARelationship(org.apache.syncope.core.persistence.api.entity.anyobject.ARelationship) Relationship(org.apache.syncope.core.persistence.api.entity.Relationship) JPAARelationship(org.apache.syncope.core.persistence.jpa.entity.anyobject.JPAARelationship) JPARelationshipType(org.apache.syncope.core.persistence.jpa.entity.JPARelationshipType) RelationshipType(org.apache.syncope.core.persistence.api.entity.RelationshipType) Repository(org.springframework.stereotype.Repository) UMembership(org.apache.syncope.core.persistence.api.entity.user.UMembership) UMembership(org.apache.syncope.core.persistence.api.entity.user.UMembership) AMembership(org.apache.syncope.core.persistence.api.entity.anyobject.AMembership) JPARelationshipType(org.apache.syncope.core.persistence.jpa.entity.JPARelationshipType) RelationshipType(org.apache.syncope.core.persistence.api.entity.RelationshipType) JPAURelationship(org.apache.syncope.core.persistence.jpa.entity.user.JPAURelationship) URelationship(org.apache.syncope.core.persistence.api.entity.user.URelationship) ARelationship(org.apache.syncope.core.persistence.api.entity.anyobject.ARelationship) JPAARelationship(org.apache.syncope.core.persistence.jpa.entity.anyobject.JPAARelationship)

Aggregations

UMembership (org.apache.syncope.core.persistence.api.entity.user.UMembership)5 URelationship (org.apache.syncope.core.persistence.api.entity.user.URelationship)5 RelationshipType (org.apache.syncope.core.persistence.api.entity.RelationshipType)3 User (org.apache.syncope.core.persistence.api.entity.user.User)3 Collection (java.util.Collection)2 List (java.util.List)2 SyncopeClientCompositeException (org.apache.syncope.common.lib.SyncopeClientCompositeException)2 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)2 MembershipTO (org.apache.syncope.common.lib.to.MembershipTO)2 UserTO (org.apache.syncope.common.lib.to.UserTO)2 AnyUtils (org.apache.syncope.core.persistence.api.entity.AnyUtils)2 Realm (org.apache.syncope.core.persistence.api.entity.Realm)2 Role (org.apache.syncope.core.persistence.api.entity.Role)2 AnyObject (org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject)2 Group (org.apache.syncope.core.persistence.api.entity.group.Group)2 SecurityQuestion (org.apache.syncope.core.persistence.api.entity.user.SecurityQuestion)2 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1