Search in sources :

Example 91 with Candidate

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

the class RegistrarManagerImplUnitTest method testParseWithoutFedInfo.

@Test
public void testParseWithoutFedInfo() {
    Candidate candidate = new Candidate();
    Map<String, String> attributes = new HashMap<>();
    attributes.put(URN_USER_DISPLAY_NAME, "Mgr. Bc. Vojtech Sassmann Dis. Csc.");
    Map<String, String> fedData = new HashMap<>();
    registrarManager.parseNamesFromDisplayNameAndFedInfo(candidate, attributes, fedData);
    assertThat(candidate.getTitleBefore()).isEqualTo("Mgr. Bc.");
    assertThat(candidate.getTitleAfter()).isEqualTo("Dis. Csc.");
    assertThat(candidate.getFirstName()).isEqualTo("Vojtech");
    assertThat(candidate.getLastName()).isEqualTo("Sassmann");
}
Also used : Candidate(cz.metacentrum.perun.core.api.Candidate) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 92 with Candidate

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

the class RegistrarManagerImplUnitTest method testDisplayNameWithoutOtherAttributes.

@Test
public void testDisplayNameWithoutOtherAttributes() {
    Candidate candidate = new Candidate();
    Map<String, String> attributes = new HashMap<>();
    attributes.put(URN_USER_DISPLAY_NAME, "Bc. Vojtech Sassmann");
    registrarManager.parseNamesFromDisplayNameAndFedInfo(candidate, attributes, null);
    assertThat(candidate.getTitleBefore()).isEqualTo("Bc.");
    assertThat(candidate.getTitleAfter()).isEqualTo(null);
    assertThat(candidate.getFirstName()).isEqualTo("Vojtech");
    assertThat(candidate.getLastName()).isEqualTo("Sassmann");
}
Also used : Candidate(cz.metacentrum.perun.core.api.Candidate) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 93 with Candidate

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

the class VosManagerBlImpl method findCandidates.

public List<Candidate> findCandidates(PerunSession sess, Group group, String searchString, List<ExtSource> extSources, boolean filterExistingMembers) {
    List<Candidate> candidates = new ArrayList<>();
    try {
        // Iterate through given extSources
        for (ExtSource source : extSources) {
            try {
                // Info if this is only simple ext source, change behavior if not
                boolean simpleExtSource = true;
                // Get potential subjects from the extSource
                List<Map<String, String>> subjects;
                try {
                    if (source instanceof ExtSourceApi) {
                        // find subjects with all their properties
                        subjects = ((ExtSourceApi) source).findSubjects(searchString);
                        simpleExtSource = false;
                    } else {
                        // find subjects only with logins - they then must be retrieved by login
                        subjects = ((ExtSourceSimpleApi) source).findSubjectsLogins(searchString);
                    }
                } catch (ExtSourceUnsupportedOperationException e1) {
                    log.warn("ExtSource {} doesn't support findSubjects", source.getName());
                    continue;
                } catch (InternalErrorException e) {
                    log.error("Error occurred on ExtSource {},  Exception {}.", source.getName(), e);
                    continue;
                } finally {
                    try {
                        ((ExtSourceSimpleApi) source).close();
                    } catch (ExtSourceUnsupportedOperationException e) {
                    // ExtSource doesn't support that functionality, so silently skip it.
                    } catch (InternalErrorException e) {
                        log.error("Can't close extSource connection.", e);
                    }
                }
                Set<String> uniqueLogins = new HashSet<>();
                for (Map<String, String> s : subjects) {
                    // Check if the user has unique identifier within extSource
                    if ((s.get("login") == null) || (s.get("login") != null && s.get("login").isEmpty())) {
                        log.error("User '{}' cannot be added, because he/she doesn't have a unique identifier (login)", s);
                        // Skip to another user
                        continue;
                    }
                    String extLogin = s.get("login");
                    // check uniqueness of every login in extSource
                    if (uniqueLogins.contains(extLogin)) {
                        throw new InternalErrorException("There are more than 1 login '" + extLogin + "' getting from extSource '" + source + "'");
                    } else {
                        uniqueLogins.add(extLogin);
                    }
                    // Get Candidate
                    Candidate candidate;
                    try {
                        if (simpleExtSource) {
                            // retrieve data about subjects from ext source based on ext. login
                            candidate = new Candidate(getPerunBl().getExtSourcesManagerBl().getCandidate(sess, source, extLogin));
                        } else {
                            // retrieve data about subjects from subjects we already have locally
                            candidate = new Candidate(getPerunBl().getExtSourcesManagerBl().getCandidate(sess, s, source, extLogin));
                        }
                    } catch (CandidateNotExistsException e) {
                        throw new ConsistencyErrorException("findSubjects returned that candidate, but getCandidate cannot find him using login " + extLogin, e);
                    } catch (ExtSourceUnsupportedOperationException e) {
                        throw new InternalErrorException("extSource supports findSubjects but not getCandidate???", e);
                    }
                    if (filterExistingMembers) {
                        try {
                            Vo vo = getPerunBl().getVosManagerBl().getVoById(sess, group.getVoId());
                            getPerunBl().getMembersManagerBl().getMemberByUserExtSources(sess, vo, candidate.getUserExtSources());
                            // Candidate is already a member of the VO, so do not add him to the list of candidates
                            continue;
                        } catch (VoNotExistsException e) {
                            throw new InternalErrorException(e);
                        } catch (MemberNotExistsException e) {
                        // This is OK
                        }
                    }
                    // Add candidate to the list of candidates
                    log.debug("findCandidates: returning candidate: {}", candidate);
                    candidates.add(candidate);
                }
            } catch (InternalErrorException e) {
                log.error("Failed to get candidates from ExtSource: {}", source);
            } finally {
                if (source instanceof ExtSourceSimpleApi) {
                    try {
                        ((ExtSourceSimpleApi) source).close();
                    } catch (ExtSourceUnsupportedOperationException e) {
                    // silently skip
                    } catch (Exception e) {
                        log.error("Failed to close connection to extsource", e);
                    }
                }
            }
        }
        log.debug("Returning {} potential members for group {}", candidates.size(), group);
        return candidates;
    } catch (RuntimeException e) {
        throw new InternalErrorException(e);
    }
}
Also used : Candidate(cz.metacentrum.perun.core.api.Candidate) MemberCandidate(cz.metacentrum.perun.core.api.MemberCandidate) ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) MemberNotExistsException(cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException) ArrayList(java.util.ArrayList) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ExtSourceApi(cz.metacentrum.perun.core.implApi.ExtSourceApi) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) MemberNotExistsException(cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException) GroupExistsException(cz.metacentrum.perun.core.api.exceptions.GroupExistsException) UserNotAdminException(cz.metacentrum.perun.core.api.exceptions.UserNotAdminException) LoginNotExistsException(cz.metacentrum.perun.core.api.exceptions.LoginNotExistsException) RelationExistsException(cz.metacentrum.perun.core.api.exceptions.RelationExistsException) CandidateNotExistsException(cz.metacentrum.perun.core.api.exceptions.CandidateNotExistsException) VoExistsException(cz.metacentrum.perun.core.api.exceptions.VoExistsException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) RoleCannotBeManagedException(cz.metacentrum.perun.core.api.exceptions.RoleCannotBeManagedException) MemberNotSponsoredException(cz.metacentrum.perun.core.api.exceptions.MemberNotSponsoredException) AlreadySponsorException(cz.metacentrum.perun.core.api.exceptions.AlreadySponsorException) ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) GroupNotAdminException(cz.metacentrum.perun.core.api.exceptions.GroupNotAdminException) ExtSourceUnsupportedOperationException(cz.metacentrum.perun.core.api.exceptions.ExtSourceUnsupportedOperationException) NotGroupMemberException(cz.metacentrum.perun.core.api.exceptions.NotGroupMemberException) AlreadyAdminException(cz.metacentrum.perun.core.api.exceptions.AlreadyAdminException) UserNotInRoleException(cz.metacentrum.perun.core.api.exceptions.UserNotInRoleException) BanNotExistsException(cz.metacentrum.perun.core.api.exceptions.BanNotExistsException) VoNotExistsException(cz.metacentrum.perun.core.api.exceptions.VoNotExistsException) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) PerunException(cz.metacentrum.perun.core.api.exceptions.PerunException) VoNotExistsException(cz.metacentrum.perun.core.api.exceptions.VoNotExistsException) Vo(cz.metacentrum.perun.core.api.Vo) BanOnVo(cz.metacentrum.perun.core.api.BanOnVo) ExtSource(cz.metacentrum.perun.core.api.ExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) ExtSourceUnsupportedOperationException(cz.metacentrum.perun.core.api.exceptions.ExtSourceUnsupportedOperationException) Map(java.util.Map) HashMap(java.util.HashMap) ExtSourceSimpleApi(cz.metacentrum.perun.core.implApi.ExtSourceSimpleApi) HashSet(java.util.HashSet) CandidateNotExistsException(cz.metacentrum.perun.core.api.exceptions.CandidateNotExistsException)

Example 94 with Candidate

use of cz.metacentrum.perun.core.api.Candidate 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 95 with Candidate

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

the class GroupAndGroupStructureSynchronizationIntegrationTest method synchronizeGroupAddMissingMember.

@Test
public void synchronizeGroupAddMissingMember() throws Exception {
    System.out.println(CLASS_NAME + "synchronizeGroupAddMissingMember");
    when(extSourceManagerBl.getExtSourceByName(sess, ExtSourcesManager.EXTSOURCE_NAME_PERUN)).thenReturn(extSourceForUserCreation);
    Attribute attr = attributesManagerBl.getAttribute(sess, group, GroupsManager.GROUPEXTSOURCE_ATTRNAME);
    attr.setValue(extSource.getName());
    attributesManagerBl.setAttribute(sess, group, attr);
    List<Map<String, String>> subjects = new ArrayList<>();
    Map<String, String> attributes = new HashMap<>();
    attributes.put("login", "metodej");
    subjects.add(attributes);
    Candidate candidate = setUpCandidate();
    when(extSourceManagerBl.getCandidate(sess, attributes, (ExtSourceLdap) essa, "metodej")).thenReturn(new CandidateSync(candidate));
    when(essa.getGroupSubjects(anyMap())).thenReturn(subjects);
    assertEquals(0, groupsManagerBl.getGroupMembers(sess, group).size());
    groupsManagerBl.synchronizeGroup(sess, group);
    assertEquals(1, groupsManagerBl.getGroupMembers(sess, group).size());
}
Also used : Candidate(cz.metacentrum.perun.core.api.Candidate) Attribute(cz.metacentrum.perun.core.api.Attribute) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) CandidateSync(cz.metacentrum.perun.core.api.CandidateSync) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) LinkedHashMap(java.util.LinkedHashMap) AbstractPerunIntegrationTest(cz.metacentrum.perun.core.AbstractPerunIntegrationTest) Test(org.junit.Test)

Aggregations

Candidate (cz.metacentrum.perun.core.api.Candidate)100 UserExtSource (cz.metacentrum.perun.core.api.UserExtSource)52 Test (org.junit.Test)41 Member (cz.metacentrum.perun.core.api.Member)37 AbstractPerunIntegrationTest (cz.metacentrum.perun.core.AbstractPerunIntegrationTest)30 ExtSource (cz.metacentrum.perun.core.api.ExtSource)25 RichMember (cz.metacentrum.perun.core.api.RichMember)24 User (cz.metacentrum.perun.core.api.User)23 HashMap (java.util.HashMap)23 ArrayList (java.util.ArrayList)21 Group (cz.metacentrum.perun.core.api.Group)15 Attribute (cz.metacentrum.perun.core.api.Attribute)14 Map (java.util.Map)12 MemberCandidate (cz.metacentrum.perun.core.api.MemberCandidate)11 RichUser (cz.metacentrum.perun.core.api.RichUser)11 Vo (cz.metacentrum.perun.core.api.Vo)11 LinkedHashMap (java.util.LinkedHashMap)11 RichUserExtSource (cz.metacentrum.perun.core.api.RichUserExtSource)9 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)9 CandidateNotExistsException (cz.metacentrum.perun.core.api.exceptions.CandidateNotExistsException)8