Search in sources :

Example 61 with Candidate

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

the class UsersManagerEntryIntegrationTest method convertAttributesToJSON.

@Test
public void convertAttributesToJSON() {
    System.out.println(CLASS_NAME + "convertAttributesToJSON");
    Candidate candidate = new Candidate(user, userExtSource);
    candidate.setAttributes(Collections.singletonMap(perun.getAttributesManager().NS_USER_ATTR + ":attribute", "value"));
    JSONObject jsonObject = candidate.convertAttributesToJSON();
    assertEquals(8, jsonObject.length());
    assertEquals("value", jsonObject.getJSONArray(perun.getAttributesManager().NS_USER_ATTR + ":attribute").getString(0));
    assertEquals(userFirstName, jsonObject.getJSONArray(perun.getAttributesManager().NS_USER_ATTR_CORE + ":firstName").getString(0));
}
Also used : Candidate(cz.metacentrum.perun.core.api.Candidate) JSONObject(org.json.JSONObject) AbstractPerunIntegrationTest(cz.metacentrum.perun.core.AbstractPerunIntegrationTest) Test(org.junit.Test)

Example 62 with Candidate

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

the class UsersManagerEntryIntegrationTest method convertAttributesWithNullToJSON.

@Test
public void convertAttributesWithNullToJSON() {
    System.out.println(CLASS_NAME + "convertAttributesWithNullToJSON");
    Candidate candidate = new Candidate(user, userExtSource);
    candidate.setAttributes(Collections.singletonMap(perun.getAttributesManager().NS_USER_ATTR + ":attribute", null));
    JSONObject jsonObject = candidate.convertAttributesToJSON();
    assertEquals(8, jsonObject.length());
    assertTrue(jsonObject.getJSONArray(perun.getAttributesManager().NS_USER_ATTR + ":attribute").isNull(0));
}
Also used : Candidate(cz.metacentrum.perun.core.api.Candidate) JSONObject(org.json.JSONObject) AbstractPerunIntegrationTest(cz.metacentrum.perun.core.AbstractPerunIntegrationTest) Test(org.junit.Test)

Example 63 with Candidate

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

the class ExpirationNotifSchedulerTest method setUpMember.

private Member setUpMember() throws Exception {
    Candidate candidate = new Candidate();
    candidate.setFirstName(Long.toHexString(Double.doubleToLongBits(Math.random())));
    candidate.setId(0);
    candidate.setMiddleName("");
    candidate.setLastName(Long.toHexString(Double.doubleToLongBits(Math.random())));
    candidate.setTitleBefore("");
    candidate.setTitleAfter("");
    final UserExtSource userExtSource = new UserExtSource(extSource, Long.toHexString(Double.doubleToLongBits(Math.random())));
    candidate.setUserExtSource(userExtSource);
    candidate.setAttributes(new HashMap<>());
    return perun.getMembersManagerBl().createMemberSync(session, vo, candidate);
}
Also used : Candidate(cz.metacentrum.perun.core.api.Candidate) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource)

Example 64 with Candidate

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

the class GroupsManagerBlImpl method categorizeMembersForLightweightSynchronization.

/**
 * For lightweight synchronization prepare candidate to add and members to remove.
 *
 * Get all subjects from loginSource and try to find users in Perun by their login and this ExtSource.
 * If found, look if this user is already in synchronized Group. If yes skip him, if not add him to candidateToAdd
 * If not found in vo of the group, skip him.
 *
 * Rest of former members need to be add to membersToRemove to remove them from group.
 *
 * This method fill 2 member structures which get as parameters:
 * 1. candidateToAdd - New members of the group
 * 2. membersToRemove - Former members who are not in synchronized ExtSource now
 *
 * @param sess
 * @param group
 * @param loginSource
 * @param memberSource
 * @param groupMembers
 * @param candidatesToAdd
 * @param membersToRemove
 * @param skippedMembers
 */
private void categorizeMembersForLightweightSynchronization(PerunSession sess, Group group, ExtSource loginSource, ExtSource memberSource, List<RichMember> groupMembers, List<Candidate> candidatesToAdd, List<RichMember> membersToRemove, List<String> skippedMembers) {
    // Get subjects from loginSource
    List<Map<String, String>> subjects = getSubjectsFromExtSource(sess, loginSource, group);
    // Prepare structure of userIds with richMembers to better work with actual members
    Map<Integer, RichMember> idsOfUsersInGroup = new HashMap<>();
    for (RichMember richMember : groupMembers) {
        idsOfUsersInGroup.put(richMember.getUserId(), richMember);
    }
    // try to find users by login and loginSource
    for (Map<String, String> subjectFromLoginSource : subjects) {
        if (subjectFromLoginSource == null) {
            log.error("Null value in the subjects list. Skipping.");
            continue;
        }
        String login = subjectFromLoginSource.get("login");
        // Skip subjects, which doesn't have login
        if (login == null || login.isEmpty()) {
            log.debug("Subject {} doesn't contain attribute login, skipping.", subjectFromLoginSource);
            skippedMembers.add("MemberEntry:[" + subjectFromLoginSource + "] was skipped because login is missing");
            continue;
        }
        // try to find user from perun by login and member extSource (need to use memberSource because loginSource is not saved by synchronization)
        User user = null;
        List<UserExtSource> userExtSources = new ArrayList<>();
        try {
            UserExtSource userExtSource = getPerunBl().getUsersManagerBl().getUserExtSourceByExtLogin(sess, memberSource, login);
            userExtSources.add(userExtSource);
        } catch (UserExtSourceNotExistsException e) {
        // skipping, this extSource does not exist and thus won't be in the list
        }
        Vo groupVo = getVo(sess, group);
        List<UserExtSource> additionalUserExtSources = Utils.extractAdditionalUserExtSources(sess, subjectFromLoginSource).stream().map(RichUserExtSource::asUserExtSource).collect(toList());
        userExtSources.addAll(additionalUserExtSources);
        for (UserExtSource source : userExtSources) {
            try {
                user = getPerunBl().getUsersManagerBl().getUserByUserExtSource(sess, source);
                // check if user is already member of group's vo
                if (getPerunBl().getUsersManagerBl().getVosWhereUserIsMember(sess, user).contains(groupVo)) {
                    if (idsOfUsersInGroup.containsKey(user.getId())) {
                        // we can skip this one, because he is already in group, and remove him from the map
                        // but first we need to also validate him if he was disabled before (invalidate and then validate)
                        RichMember richMember = idsOfUsersInGroup.get(user.getId());
                        if (richMember != null && Status.DISABLED.equals(richMember.getStatus())) {
                            getPerunBl().getMembersManagerBl().invalidateMember(sess, richMember);
                            try {
                                getPerunBl().getMembersManagerBl().validateMember(sess, richMember);
                            } catch (WrongAttributeValueException | WrongReferenceAttributeValueException e) {
                                log.info("Switching member id {} into INVALID state from DISABLED, because there was problem with attributes {}.", richMember.getId(), e);
                            }
                        }
                        idsOfUsersInGroup.remove(user.getId());
                    } else {
                        // he is not yet in group, so we need to create a candidate
                        Candidate candidate = new Candidate(user, source);
                        // for lightweight synchronization we want to skip all update of attributes
                        candidate.setAttributes(new HashMap<>());
                        candidatesToAdd.add(candidate);
                    }
                    break;
                }
            } catch (UserNotExistsException e) {
            // skip because the user from this ExtSource does not exist so we can continue
            }
        }
        // If user not found in group's vo, skip him and log it
        if (user == null) {
            log.debug("Subject {} with login {} was skipped during lightweight synchronization of group {} because he is not in vo of the group yet.", subjectFromLoginSource, login, group);
        }
    }
    // Rest of them need to be removed
    membersToRemove.addAll(idsOfUsersInGroup.values());
}
Also used : Candidate(cz.metacentrum.perun.core.api.Candidate) User(cz.metacentrum.perun.core.api.User) RichUser(cz.metacentrum.perun.core.api.RichUser) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) UserExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException) ArrayList(java.util.ArrayList) RichMember(cz.metacentrum.perun.core.api.RichMember) RichUserExtSource(cz.metacentrum.perun.core.api.RichUserExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) Vo(cz.metacentrum.perun.core.api.Vo) GroupCreatedInVo(cz.metacentrum.perun.audit.events.GroupManagerEvents.GroupCreatedInVo) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap)

Example 65 with Candidate

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

the class GroupsManagerEntryIntegrationTest method setUpMemberWithDifferentParam.

private Member setUpMemberWithDifferentParam(Vo vo, int i) throws Exception {
    // List<Candidate> candidates = perun.getVosManager().findCandidates(sess, vo, extLogin);
    // find candidates from ext source based on extLogin
    // assertTrue(candidates.size() > 0);
    Candidate candidate = setUpCandidate(i);
    Member member = perun.getMembersManagerBl().createMemberSync(sess, vo, candidate);
    // set first candidate as member of test VO
    assertNotNull("No member created", member);
    usersForDeletion.add(perun.getUsersManager().getUserByMember(sess, member));
    // save user for deletion after test
    return member;
}
Also used : Candidate(cz.metacentrum.perun.core.api.Candidate) RichMember(cz.metacentrum.perun.core.api.RichMember) Member(cz.metacentrum.perun.core.api.Member)

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