Search in sources :

Example 6 with NotGroupMemberException

use of cz.metacentrum.perun.core.api.exceptions.NotGroupMemberException in project perun by CESNET.

the class GroupsManagerEntry method getGroupRichMembersByIds.

@Override
public List<RichMember> getGroupRichMembersByIds(PerunSession sess, int groupId, List<Integer> memberIds, List<String> attrNames) throws GroupNotExistsException, PrivilegeException, AttributeNotExistsException {
    Utils.checkPerunSession(sess);
    Group group = groupsManagerBl.getGroupById(sess, groupId);
    getGroupsManagerBl().checkGroupExists(sess, group);
    // Authorization
    if (!AuthzResolver.authorizedInternal(sess, "getGroupRichMembersByIds_int_List<Integer>_List<String>_policy", group)) {
        throw new PrivilegeException(sess, "getGroupRichMembersByIds");
    }
    List<AttributeDefinition> attributeDefinitions = new ArrayList<>();
    for (String attrName : attrNames) {
        attributeDefinitions.add(getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, attrName));
    }
    List<Member> members = new ArrayList<>();
    for (Integer memberId : memberIds) {
        try {
            members.add(groupsManagerBl.getGroupMemberById(sess, group, memberId));
        } catch (NotGroupMemberException e) {
        // silently skip
        }
    }
    members.removeIf(member -> !AuthzResolver.authorizedInternal(sess, "filter-getGroupRichMembersByIds_int_List<Integer>_List<String>_policy", member));
    List<RichMember> richMembers = perunBl.getMembersManagerBl().getRichMembersWithAttributes(sess, group, members, attributeDefinitions);
    return getPerunBl().getMembersManagerBl().filterOnlyAllowedAttributes(sess, richMembers, group, true);
}
Also used : NotGroupMemberException(cz.metacentrum.perun.core.api.exceptions.NotGroupMemberException) Group(cz.metacentrum.perun.core.api.Group) RichGroup(cz.metacentrum.perun.core.api.RichGroup) PrivilegeException(cz.metacentrum.perun.core.api.exceptions.PrivilegeException) ArrayList(java.util.ArrayList) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) RichMember(cz.metacentrum.perun.core.api.RichMember) Member(cz.metacentrum.perun.core.api.Member) RichMember(cz.metacentrum.perun.core.api.RichMember)

Example 7 with NotGroupMemberException

use of cz.metacentrum.perun.core.api.exceptions.NotGroupMemberException in project perun by CESNET.

the class VosManagerBlImpl method createMemberCandidates.

/**
 * Creates MemberCandidates for given RichUsers, vo, group and candidates. If the given group is not null
 * then to all members who are in this group is assigned the sourceGroupId of the given group.
 * The given group can be null.
 *
 * @param sess session
 * @param users users
 * @param group group
 * @param candidates candidates
 * @param attrNames names of attributes that will be returned
 * @return list of MemberCandidates for given RichUsers, group and candidates
 * @throws InternalErrorException internal error
 */
public List<MemberCandidate> createMemberCandidates(PerunSession sess, List<RichUser> users, Vo vo, Group group, List<Candidate> candidates, List<String> attrNames) {
    List<MemberCandidate> memberCandidates = new ArrayList<>();
    Set<Integer> allUsersIds = new HashSet<>();
    int userId;
    // try to find matching RichUser for candidates
    for (Candidate candidate : candidates) {
        MemberCandidate mc = new MemberCandidate();
        try {
            User user = getPerunBl().getUsersManagerBl().getUserByUserExtSources(sess, candidate.getUserExtSources());
            userId = user.getId();
            // check if user already exists in the list
            if (!allUsersIds.contains(userId)) {
                RichUser richUser = getPerunBl().getUsersManagerBl().convertUserToRichUserWithAttributesByNames(sess, user, attrNames);
                mc.setRichUser(richUser);
                memberCandidates.add(mc);
            }
            allUsersIds.add(userId);
        } catch (UserNotExistsException ignored) {
            // no matching user was found
            mc.setCandidate(candidate);
            memberCandidates.add(mc);
        }
    }
    List<RichUser> foundRichUsers = memberCandidates.stream().map(MemberCandidate::getRichUser).collect(Collectors.toList());
    // create MemberCandidates for RichUsers without candidate
    for (RichUser richUser : users) {
        if (!foundRichUsers.contains(richUser)) {
            MemberCandidate mc = new MemberCandidate();
            mc.setRichUser(richUser);
            memberCandidates.add(mc);
        }
    }
    // try to find member for MemberCandidates with not null RichUser
    for (MemberCandidate memberCandidate : memberCandidates) {
        if (memberCandidate.getRichUser() != null) {
            Member member = null;
            try {
                member = getPerunBl().getMembersManagerBl().getMemberByUser(sess, vo, memberCandidate.getRichUser());
                if (group != null) {
                    member = getPerunBl().getGroupsManagerBl().getGroupMemberById(sess, group, member.getId());
                }
            } catch (MemberNotExistsException ignored) {
            // no matching VO member was found
            } catch (NotGroupMemberException e) {
            // not matching Group member was found
            }
            // put null or matching member
            memberCandidate.setMember(member);
        }
    }
    return memberCandidates;
}
Also used : Candidate(cz.metacentrum.perun.core.api.Candidate) MemberCandidate(cz.metacentrum.perun.core.api.MemberCandidate) User(cz.metacentrum.perun.core.api.User) RichUser(cz.metacentrum.perun.core.api.RichUser) MemberNotExistsException(cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) RichUser(cz.metacentrum.perun.core.api.RichUser) ArrayList(java.util.ArrayList) NotGroupMemberException(cz.metacentrum.perun.core.api.exceptions.NotGroupMemberException) MemberCandidate(cz.metacentrum.perun.core.api.MemberCandidate) Member(cz.metacentrum.perun.core.api.Member) HashSet(java.util.HashSet)

Example 8 with NotGroupMemberException

use of cz.metacentrum.perun.core.api.exceptions.NotGroupMemberException in project perun by CESNET.

the class urn_perun_member_group_attribute_def_virt_groupStatus method getAttributeValue.

@Override
public Attribute getAttributeValue(PerunSessionImpl sess, Member member, Group group, AttributeDefinition attribute) {
    // Source member object can't be trusted to hold correct group membership status, since we don't know in which context
    // it was originally retrieved. Hence we get member of a group once more.
    Attribute newAttribute = new Attribute(attribute);
    try {
        Member retrievedMember = sess.getPerunBl().getGroupsManagerBl().getGroupMemberById(sess, group, member.getId());
        MemberGroupStatus result = retrievedMember.getGroupStatus();
        newAttribute.setValue((result != null) ? result.toString() : null);
        return newAttribute;
    } catch (NotGroupMemberException e) {
        log.warn("{} is not member of a {} when retrieving member_group:virt:groupStatus attribute.", member, group);
    }
    return newAttribute;
}
Also used : NotGroupMemberException(cz.metacentrum.perun.core.api.exceptions.NotGroupMemberException) Attribute(cz.metacentrum.perun.core.api.Attribute) Member(cz.metacentrum.perun.core.api.Member) MemberGroupStatus(cz.metacentrum.perun.core.api.MemberGroupStatus)

Aggregations

NotGroupMemberException (cz.metacentrum.perun.core.api.exceptions.NotGroupMemberException)8 Member (cz.metacentrum.perun.core.api.Member)5 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)4 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)4 RichMember (cz.metacentrum.perun.core.api.RichMember)3 Group (cz.metacentrum.perun.core.api.Group)2 GroupNotExistsException (cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException)2 MemberNotExistsException (cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException)2 WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)2 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)2 ArrayList (java.util.ArrayList)2 MemberDeleted (cz.metacentrum.perun.audit.events.MembersManagerEvents.MemberDeleted)1 Attribute (cz.metacentrum.perun.core.api.Attribute)1 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)1 BanOnResource (cz.metacentrum.perun.core.api.BanOnResource)1 BanOnVo (cz.metacentrum.perun.core.api.BanOnVo)1 Candidate (cz.metacentrum.perun.core.api.Candidate)1 MemberCandidate (cz.metacentrum.perun.core.api.MemberCandidate)1 MemberGroupStatus (cz.metacentrum.perun.core.api.MemberGroupStatus)1 Resource (cz.metacentrum.perun.core.api.Resource)1