Search in sources :

Example 6 with DelegateType

use of org.kuali.kfs.kim.impl.common.delegate.DelegateType in project cu-kfs by CU-CommunityApps.

the class RoleServiceImpl method getRoleMembers.

protected List<RoleMembership> getRoleMembers(List<String> roleIds, Map<String, String> qualification, boolean followDelegations, Set<String> foundRoleTypeMembers) {
    List<RoleMembership> results = new ArrayList<>();
    Set<String> allRoleIds = new HashSet<>();
    for (String roleId : roleIds) {
        if (getProxiedRoleService().isRoleActive(roleId)) {
            allRoleIds.add(roleId);
        }
    }
    // short-circuit if no roles match
    if (allRoleIds.isEmpty()) {
        return Collections.emptyList();
    }
    Set<String> matchingRoleIds = new HashSet<>(allRoleIds.size());
    // for efficiency, retrieve all roles and store in a map
    Map<String, RoleLite> roles = getRoleLiteMap(allRoleIds);
    List<String> copyRoleIds = new ArrayList<>(allRoleIds);
    List<RoleMember> rms = new ArrayList<>();
    for (String roleId : allRoleIds) {
        RoleTypeService roleTypeService = getRoleTypeService(roleId);
        if (roleTypeService != null) {
            List<String> attributesForExactMatch = roleTypeService.getQualifiersForExactMatch();
            if (CollectionUtils.isNotEmpty(attributesForExactMatch)) {
                copyRoleIds.remove(roleId);
                rms.addAll(getStoredRoleMembersForRoleIds(Collections.singletonList(roleId), null, populateQualifiersForExactMatch(qualification, attributesForExactMatch)));
            }
        }
    }
    if (CollectionUtils.isNotEmpty(copyRoleIds)) {
        rms.addAll(getStoredRoleMembersForRoleIds(copyRoleIds, null, null));
    }
    // build a map of role ID to membership information
    // this will be used for later qualification checks
    Map<String, List<RoleMembership>> roleIdToMembershipMap = new HashMap<>();
    for (RoleMember roleMember : rms) {
        RoleMembership mi = RoleMembership.Builder.create(roleMember.getRoleId(), roleMember.getId(), roleMember.getMemberId(), roleMember.getType(), roleMember.getAttributes()).build();
        // if the qualification check does not need to be made, just add the result
        if (qualification == null || qualification.isEmpty()) {
            if (MemberType.ROLE.equals(roleMember.getType())) {
                // if a role member type, do a non-recursive role member check to obtain the group and principal
                // members of that role given the qualification
                Map<String, String> nestedRoleQualification = qualification;
                RoleTypeService roleTypeService = getRoleTypeService(roleMember.getRoleId());
                if (roleTypeService != null) {
                    // get the member role object
                    RoleLite memberRole = getRoleLite(mi.getMemberId());
                    nestedRoleQualification = roleTypeService.convertQualificationForMemberRoles(roles.get(roleMember.getRoleId()).getNamespaceCode(), roles.get(roleMember.getRoleId()).getName(), memberRole.getNamespaceCode(), memberRole.getName(), qualification);
                }
                if (getProxiedRoleService().isRoleActive(roleMember.getRoleId())) {
                    Collection<RoleMembership> nestedRoleMembers = getNestedRoleMembers(nestedRoleQualification, mi, foundRoleTypeMembers);
                    if (!nestedRoleMembers.isEmpty()) {
                        results.addAll(nestedRoleMembers);
                        matchingRoleIds.add(roleMember.getRoleId());
                    }
                }
            } else {
                results.add(mi);
                matchingRoleIds.add(roleMember.getRoleId());
            }
            matchingRoleIds.add(roleMember.getRoleId());
        } else {
            List<RoleMembership> lrmi = roleIdToMembershipMap.computeIfAbsent(mi.getRoleId(), k -> new ArrayList<>());
            lrmi.add(mi);
        }
    }
    // entries
    if (!roleIdToMembershipMap.isEmpty()) {
        // for evaluation, the service will return those which match
        for (Map.Entry<String, List<RoleMembership>> entry : roleIdToMembershipMap.entrySet()) {
            // can't be guaranteed that it is up and working, so using a try/catch to catch this possibility.
            try {
                RoleTypeService roleTypeService = getRoleTypeService(entry.getKey());
                List<RoleMembership> matchingMembers = roleTypeService.getMatchingRoleMemberships(qualification, entry.getValue());
                // loop over the matching entries, adding them to the results
                for (RoleMembership roleMemberships : matchingMembers) {
                    if (MemberType.ROLE.equals(roleMemberships.getType())) {
                        // if a role member type, do a non-recursive role member check to obtain the group and
                        // principal members of that role given the qualification get the member role object
                        RoleLite memberRole = getRoleLite(roleMemberships.getMemberId());
                        if (memberRole.isActive()) {
                            Map<String, String> nestedRoleQualification = roleTypeService.convertQualificationForMemberRoles(roles.get(roleMemberships.getRoleId()).getNamespaceCode(), roles.get(roleMemberships.getRoleId()).getName(), memberRole.getNamespaceCode(), memberRole.getName(), qualification);
                            Collection<RoleMembership> nestedRoleMembers = getNestedRoleMembers(nestedRoleQualification, roleMemberships, foundRoleTypeMembers);
                            if (!nestedRoleMembers.isEmpty()) {
                                results.addAll(nestedRoleMembers);
                                matchingRoleIds.add(roleMemberships.getRoleId());
                            }
                        }
                    } else {
                        results.add(roleMemberships);
                        matchingRoleIds.add(roleMemberships.getRoleId());
                    }
                }
            } catch (Exception ex) {
                LOG.warn("Not able to retrieve RoleTypeService from remote system for role Id: " + entry.getKey(), ex);
            }
        }
    }
    // handle derived roles
    for (String roleId : allRoleIds) {
        RoleTypeService roleTypeService = getRoleTypeService(roleId);
        RoleLite role = roles.get(roleId);
        // check if a derived role
        try {
            if (isDerivedRoleType(roleTypeService)) {
                // for each derived role, get the list of principals and groups which are in that role given the
                // qualification (per the role type service)
                List<RoleMembership> roleMembers = roleTypeService.getRoleMembersFromDerivedRole(role.getNamespaceCode(), role.getName(), qualification);
                if (!roleMembers.isEmpty()) {
                    matchingRoleIds.add(roleId);
                }
                for (RoleMembership rm : roleMembers) {
                    RoleMembership.Builder builder = RoleMembership.Builder.create(rm);
                    builder.setRoleId(roleId);
                    builder.setId("*");
                    results.add(builder.build());
                }
            }
        } catch (Exception ex) {
            LOG.warn("Not able to retrieve RoleTypeService from remote system for role Id: " + roleId, ex);
        }
    }
    if (followDelegations && !matchingRoleIds.isEmpty()) {
        // we have a list of RoleMembershipInfo objects
        // need to get delegations for distinct list of roles in that list
        Map<String, DelegateType> delegationIdToDelegationMap = getStoredDelegationImplMapFromRoleIds(matchingRoleIds);
        if (!delegationIdToDelegationMap.isEmpty()) {
            List<RoleMembership.Builder> membershipsWithDelegations = applyDelegationsToRoleMembers(results, delegationIdToDelegationMap.values(), qualification);
            resolveDelegationMemberRoles(membershipsWithDelegations, qualification, foundRoleTypeMembers);
            results = ModelObjectUtils.buildImmutableCopy(membershipsWithDelegations);
        }
    }
    // all the matching role members
    if (results.size() > 1) {
        // if a single role: easy case
        if (matchingRoleIds.size() == 1) {
            String roleId = matchingRoleIds.iterator().next();
            RoleTypeService roleTypeService = getRoleTypeService(roleId);
            // can't be guaranteed that it is up and working, so using a try/catch to catch this possibility.
            try {
                if (roleTypeService != null) {
                    results = roleTypeService.sortRoleMembers(results);
                }
            } catch (Exception ex) {
                LOG.warn("Not able to retrieve RoleTypeService from remote system for role Id: " + roleId, ex);
            }
        } else if (matchingRoleIds.size() > 1) {
            // if more than one, check if there is only a single role type service
            String prevServiceName = null;
            boolean multipleServices = false;
            for (String roleId : matchingRoleIds) {
                String serviceName = KimApiServiceLocator.getKimTypeInfoService().getKimType(getRoleWithoutMembers(roleId).getKimTypeId()).getServiceName();
                if (prevServiceName != null && !StringUtils.equals(prevServiceName, serviceName)) {
                    multipleServices = true;
                    break;
                }
                prevServiceName = serviceName;
            }
            if (!multipleServices) {
                String roleId = matchingRoleIds.iterator().next();
                // it can't be guaranteed that it is up and working, so using a try/catch to catch this possibility.
                try {
                    RoleTypeService kimRoleTypeService = getRoleTypeService(roleId);
                    if (kimRoleTypeService != null) {
                        results = kimRoleTypeService.sortRoleMembers(results);
                    }
                } catch (Exception ex) {
                    LOG.warn("Not able to retrieve RoleTypeService from remote system for role Id: " + roleId, ex);
                }
            } else {
                LOG.warn("Did not sort role members - multiple role type services found.  Role Ids: " + matchingRoleIds);
            }
        }
    }
    return Collections.unmodifiableList(results);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) DelegateType(org.kuali.kfs.kim.impl.common.delegate.DelegateType) HashSet(java.util.HashSet) RoleTypeService(org.kuali.kfs.kim.framework.role.RoleTypeService) RoleMembership(org.kuali.kfs.kim.api.role.RoleMembership) Map(java.util.Map) HashMap(java.util.HashMap) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap)

Example 7 with DelegateType

use of org.kuali.kfs.kim.impl.common.delegate.DelegateType in project cu-kfs by CU-CommunityApps.

the class RoleServiceImpl method applyDelegationsToRoleMembers.

/**
 * Checks each of the result records to determine if there are potentially applicable delegation members for that
 * role membership. If there are, applicable delegations and members will be linked to the RoleMemberships in the
 * given list. An updated list will be returned from this method which includes the appropriate linked delegations.
 */
protected List<RoleMembership.Builder> applyDelegationsToRoleMembers(List<RoleMembership> roleMemberships, Collection<DelegateType> delegations, Map<String, String> qualification) {
    MultiValueMap<String, String> roleIdToRoleMembershipIds = new LinkedMultiValueMap<>();
    Map<String, RoleMembership.Builder> roleMembershipIdToBuilder = new HashMap<>();
    List<RoleMembership.Builder> roleMembershipBuilders = new ArrayList<>();
    // builders
    for (RoleMembership roleMembership : roleMemberships) {
        roleIdToRoleMembershipIds.add(roleMembership.getRoleId(), roleMembership.getId());
        RoleMembership.Builder builder = RoleMembership.Builder.create(roleMembership);
        roleMembershipBuilders.add(builder);
        roleMembershipIdToBuilder.put(roleMembership.getId(), builder);
    }
    for (DelegateType delegation : delegations) {
        // determine the candidate role memberships where this delegation can be mapped
        List<String> candidateRoleMembershipIds = roleIdToRoleMembershipIds.get(delegation.getRoleId());
        if (CollectionUtils.isNotEmpty(candidateRoleMembershipIds)) {
            DelegationTypeService delegationTypeService = getDelegationTypeService(delegation.getDelegationId());
            for (DelegateMember delegationMember : delegation.getMembers()) {
                // Make sure that the delegation member is active
                if (delegationMember.isActive(DateTime.now()) && (delegationTypeService == null || delegationTypeService.doesDelegationQualifierMatchQualification(qualification, delegationMember.getQualifier()))) {
                    // memberships on the role
                    if (StringUtils.isBlank(delegationMember.getRoleMemberId())) {
                        RoleTypeService roleTypeService = getRoleTypeService(delegation.getRoleId());
                        for (String roleMembershipId : candidateRoleMembershipIds) {
                            RoleMembership.Builder roleMembershipBuilder = roleMembershipIdToBuilder.get(roleMembershipId);
                            if (roleTypeService == null || roleTypeService.doesRoleQualifierMatchQualification(roleMembershipBuilder.getQualifier(), delegationMember.getQualifier())) {
                                linkDelegateToRoleMembership(delegation, delegationMember, roleMembershipBuilder);
                            }
                        }
                    } else if (candidateRoleMembershipIds.contains(delegationMember.getRoleMemberId())) {
                        RoleMembership.Builder roleMembershipBuilder = roleMembershipIdToBuilder.get(delegationMember.getRoleMemberId());
                        linkDelegateToRoleMembership(delegation, delegationMember, roleMembershipBuilder);
                    }
                }
            }
        }
    }
    return roleMembershipBuilders;
}
Also used : LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) HashMap(java.util.HashMap) DelegationTypeService(org.kuali.kfs.kim.framework.common.delegate.DelegationTypeService) ArrayList(java.util.ArrayList) RoleTypeService(org.kuali.kfs.kim.framework.role.RoleTypeService) DelegateMember(org.kuali.kfs.kim.impl.common.delegate.DelegateMember) RoleMembership(org.kuali.kfs.kim.api.role.RoleMembership) DelegateType(org.kuali.kfs.kim.impl.common.delegate.DelegateType)

Example 8 with DelegateType

use of org.kuali.kfs.kim.impl.common.delegate.DelegateType in project cu-kfs by CU-CommunityApps.

the class OrgReviewRoleServiceImpl method populateOrgReviewRoleFromDelegationMember.

@Override
public void populateOrgReviewRoleFromDelegationMember(OrgReviewRole orr, String roleMemberId, String delegationMemberId) {
    RoleMember roleMember = null;
    if (StringUtils.isNotBlank(roleMemberId)) {
        roleMember = getRoleMemberFromKimRoleService(roleMemberId);
    }
    RoleService roleService = KimApiServiceLocator.getRoleService();
    DelegateMember delegationMember = roleService.getDelegationMemberById(delegationMemberId);
    DelegateType delegation = roleService.getDelegateTypeByDelegationId(delegationMember.getDelegationId());
    orr.setDelegationTypeCode(delegation.getDelegationType().getCode());
    orr.setDelegateMember(roleMember, delegationMember);
    orr.setRoleRspActions(roleService.getRoleMemberResponsibilityActions(delegationMember.getRoleMemberId()));
    populateObjectExtras(orr);
}
Also used : OrgReviewRoleService(org.kuali.kfs.coa.service.OrgReviewRoleService) RoleService(org.kuali.kfs.kim.api.role.RoleService) KfsKimDocDelegateMember(org.kuali.kfs.coa.identity.KfsKimDocDelegateMember) DelegateMember(org.kuali.kfs.kim.impl.common.delegate.DelegateMember) DelegateType(org.kuali.kfs.kim.impl.common.delegate.DelegateType) RoleMember(org.kuali.kfs.kim.impl.role.RoleMember)

Example 9 with DelegateType

use of org.kuali.kfs.kim.impl.common.delegate.DelegateType in project cu-kfs by CU-CommunityApps.

the class OrgReviewRoleServiceImpl method saveDelegateMemberToKim.

protected void saveDelegateMemberToKim(OrgReviewRole orr) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Saving delegate member from OrgReviewRole: " + orr);
    }
    RoleService roleService = KimApiServiceLocator.getRoleService();
    // Save delegation(s)
    List<KfsKimDocDelegateMember> delegationMembers = getDelegationMembersToSave(orr);
    for (KfsKimDocDelegateMember dm : delegationMembers) {
        // retrieve the delegate type so it can be updated
        DelegationType delegationType = dm.getDelegationType();
        DelegateType delegateType = roleService.getDelegateTypeByRoleIdAndDelegateTypeCode(orr.getRoleId(), delegationType);
        if (shouldCreateNewDelegateType(delegateType)) {
            DelegateType newDelegateType = new DelegateType();
            newDelegateType.setRoleId(orr.getRoleId());
            newDelegateType.setDelegationType(delegationType);
            newDelegateType.setDelegationMembers(new ArrayList<>(1));
            // ensure this is set (for new delegation types)
            newDelegateType.setKimTypeId(orr.getKimTypeId());
            delegateType = roleService.createDelegateType(newDelegateType);
            if (LOG.isDebugEnabled()) {
                LOG.debug("No DelegateType in KIM.  Created new one: " + delegateType);
            }
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Pulled DelegateType from KIM: " + delegateType);
            }
        }
        boolean foundExistingMember = false;
        DelegateMember addedMember = null;
        // check for an existing delegation member given its unique ID if found, update that record
        if (StringUtils.isNotBlank(dm.getDelegationMemberId())) {
            DelegateMember member = roleService.getDelegationMemberById(dm.getDelegationMemberId());
            if (member != null) {
                foundExistingMember = true;
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Found existing delegate member - updating existing record. " + member);
                }
                // KFSMI-9628 : fixing issue with the delegate switch from primary to secondary
                // IN this case, we need to delete the member from the "other" delegate type
                // need to determine what the "existing" type was
                DelegateType originalDelegateType = roleService.getDelegateTypeByDelegationId(member.getDelegationId());
                // if they are the same, we can just update the existing record
                if (originalDelegateType.getDelegationType().equals(dm.getDelegationType())) {
                    updateDelegateMemberFromDocDelegateMember(member, dm);
                    addedMember = roleService.updateDelegateMember(member);
                } else {
                    // Otherwise, we need to remove the old one and add a new one
                    // Remove old
                    roleService.removeDelegateMembers(Collections.singletonList(member));
                    // add new
                    DelegateMember newMember = new DelegateMember();
                    newMember.setDelegationId(delegateType.getDelegationId());
                    updateDelegateMemberFromDocDelegateMember(newMember, dm);
                    addedMember = roleService.createDelegateMember(newMember);
                }
            }
        }
        // if we did not find one, then we need to create a new member
        if (!foundExistingMember) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("No existing delegate member found, adding as a new delegate: " + dm);
            }
            DelegateMember newMember = new DelegateMember();
            newMember.setDelegationId(delegateType.getDelegationId());
            updateDelegateMemberFromDocDelegateMember(newMember, dm);
            addedMember = roleService.createDelegateMember(newMember);
        }
        if (addedMember != null) {
            orr.setDelegationMemberId(addedMember.getDelegationMemberId());
        }
    }
}
Also used : OrgReviewRoleService(org.kuali.kfs.coa.service.OrgReviewRoleService) RoleService(org.kuali.kfs.kim.api.role.RoleService) KfsKimDocDelegateMember(org.kuali.kfs.coa.identity.KfsKimDocDelegateMember) DelegateMember(org.kuali.kfs.kim.impl.common.delegate.DelegateMember) DelegationType(org.kuali.kfs.core.api.delegation.DelegationType) DelegateType(org.kuali.kfs.kim.impl.common.delegate.DelegateType) KfsKimDocDelegateMember(org.kuali.kfs.coa.identity.KfsKimDocDelegateMember)

Example 10 with DelegateType

use of org.kuali.kfs.kim.impl.common.delegate.DelegateType in project cu-kfs by CU-CommunityApps.

the class PersonImpl method populateDelegateMembers.

private void populateDelegateMembers() {
    Map<String, String> criteria = new HashMap<>(2);
    criteria.put(KIMPropertyConstants.DelegationMember.MEMBER_ID, principalId);
    criteria.put(KIMPropertyConstants.DelegationMember.MEMBER_TYPE_CODE, MemberType.PRINCIPAL.getCode());
    delegateMembers = (List<DelegateMember>) getBusinessObjectService().findMatching(DelegateMember.class, criteria);
    criteria.clear();
    for (DelegateMember dmb : delegateMembers) {
        criteria.put(KIMPropertyConstants.Delegation.DELEGATION_ID, dmb.getDelegationId());
        DelegateType delegate = getBusinessObjectService().findByPrimaryKey(DelegateType.class, criteria);
        dmb.setDelegationType(delegate.getDelegationTypeCode());
    }
}
Also used : HashMap(java.util.HashMap) DelegateMember(org.kuali.kfs.kim.impl.common.delegate.DelegateMember) DelegateType(org.kuali.kfs.kim.impl.common.delegate.DelegateType)

Aggregations

DelegateType (org.kuali.kfs.kim.impl.common.delegate.DelegateType)13 DelegateMember (org.kuali.kfs.kim.impl.common.delegate.DelegateMember)7 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)4 RoleTypeService (org.kuali.kfs.kim.framework.role.RoleTypeService)4 RoleMembership (org.kuali.kfs.kim.api.role.RoleMembership)3 DelegationTypeService (org.kuali.kfs.kim.framework.common.delegate.DelegationTypeService)3 List (java.util.List)2 KfsKimDocDelegateMember (org.kuali.kfs.coa.identity.KfsKimDocDelegateMember)2 OrgReviewRoleService (org.kuali.kfs.coa.service.OrgReviewRoleService)2 RoleService (org.kuali.kfs.kim.api.role.RoleService)2 DelegateMemberAttributeData (org.kuali.kfs.kim.impl.common.delegate.DelegateMemberAttributeData)2 CacheEvict (org.springframework.cache.annotation.CacheEvict)2 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)2 Timestamp (java.sql.Timestamp)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 DelegationType (org.kuali.kfs.core.api.delegation.DelegationType)1 KimTypeService (org.kuali.kfs.kim.framework.type.KimTypeService)1