Search in sources :

Example 6 with RichUserExtSource

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

the class UtilsIntegrationTest method extractAdditionalUserExtSourcesWithAttributeTest.

@Test
public void extractAdditionalUserExtSourcesWithAttributeTest() throws Exception {
    System.out.println("Utils.extractAdditionalUserExtSourcesWithAttributeTest");
    Map<String, String> map = new HashMap<>();
    map.put("additionalues_b", extSourceName2 + "|cz.metacentrum.perun.core.impl.ExtSourceInternal|" + extLogin2 + ";urn:perun:ues:attribute-def:def:eppn=" + extLogin2 + "|2");
    AttributeDefinition attributeDefinition = new AttributeDefinition();
    attributeDefinition.setNamespace("urn:perun:ues:attribute-def:def");
    attributeDefinition.setFriendlyName("eppn");
    attributeDefinition.setDescription("login value");
    attributeDefinition.setType(String.class.getName());
    sess.getPerun().getAttributesManager().createAttribute(sess, attributeDefinition);
    List<RichUserExtSource> list = Utils.extractAdditionalUserExtSources(sess, map);
    assertEquals(list.size(), 1);
    assertTrue(list.contains(new RichUserExtSource(userExtSource2, Arrays.asList(new Attribute(attributeDefinition, extLogin2)))));
}
Also used : RichUserExtSource(cz.metacentrum.perun.core.api.RichUserExtSource) HashMap(java.util.HashMap) Attribute(cz.metacentrum.perun.core.api.Attribute) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) AbstractPerunIntegrationTest(cz.metacentrum.perun.core.AbstractPerunIntegrationTest) Test(org.junit.Test)

Example 7 with RichUserExtSource

use of cz.metacentrum.perun.core.api.RichUserExtSource 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)

Aggregations

RichUserExtSource (cz.metacentrum.perun.core.api.RichUserExtSource)7 HashMap (java.util.HashMap)5 Attribute (cz.metacentrum.perun.core.api.Attribute)4 ArrayList (java.util.ArrayList)4 AbstractPerunIntegrationTest (cz.metacentrum.perun.core.AbstractPerunIntegrationTest)3 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)3 UserExtSource (cz.metacentrum.perun.core.api.UserExtSource)3 Test (org.junit.Test)3 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)2 GroupCreatedInVo (cz.metacentrum.perun.audit.events.GroupManagerEvents.GroupCreatedInVo)1 Candidate (cz.metacentrum.perun.core.api.Candidate)1 CandidateSync (cz.metacentrum.perun.core.api.CandidateSync)1 RichMember (cz.metacentrum.perun.core.api.RichMember)1 RichUser (cz.metacentrum.perun.core.api.RichUser)1 User (cz.metacentrum.perun.core.api.User)1 Vo (cz.metacentrum.perun.core.api.Vo)1 ParserException (cz.metacentrum.perun.core.api.exceptions.ParserException)1 UserExtSourceNotExistsException (cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException)1 UserNotExistsException (cz.metacentrum.perun.core.api.exceptions.UserNotExistsException)1 WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)1