Search in sources :

Example 1 with RichUserExtSource

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

the class ExtSourcesManagerBlImpl method getCandidate.

@Override
public CandidateSync getCandidate(PerunSession perunSession, Map<String, String> subjectData, ExtSource source, String login) {
    if (login == null || login.isEmpty())
        throw new InternalErrorException("Login can't be empty or null.");
    if (subjectData == null || subjectData.isEmpty())
        throw new InternalErrorException("Subject data can't be null or empty, at least login there must exists.");
    // New Canddate
    CandidateSync candidateSync = new CandidateSync();
    // Prepare userExtSource object
    UserExtSource userExtSource = new UserExtSource();
    userExtSource.setExtSource(source);
    userExtSource.setLogin(login);
    // If first name of candidate is not in format of name, set null instead
    candidateSync.setFirstName(subjectData.get("firstName"));
    if (candidateSync.getFirstName() != null) {
        Matcher name = namePattern.matcher(candidateSync.getFirstName());
        if (!name.matches())
            candidateSync.setFirstName(null);
    }
    // If last name of candidate is not in format of name, set null instead
    candidateSync.setLastName(subjectData.get("lastName"));
    if (candidateSync.getLastName() != null) {
        Matcher name = namePattern.matcher(candidateSync.getLastName());
        if (!name.matches())
            candidateSync.setLastName(null);
    }
    candidateSync.setMiddleName(subjectData.get("middleName"));
    candidateSync.setTitleAfter(subjectData.get("titleAfter"));
    candidateSync.setTitleBefore(subjectData.get("titleBefore"));
    // Set service user
    if (subjectData.get("isServiceUser") == null) {
        candidateSync.setServiceUser(false);
    } else {
        String isServiceUser = subjectData.get("isServiceUser");
        candidateSync.setServiceUser(isServiceUser.equals("true"));
    }
    // Set sponsored user
    if (subjectData.get("isSponsoredUser") == null) {
        candidateSync.setSponsoredUser(false);
    } else {
        String isSponsoredUser = subjectData.get("isSponsoredUser");
        candidateSync.setSponsoredUser(isSponsoredUser.equals("true"));
    }
    // Filter attributes
    Map<String, String> attributes = new HashMap<>();
    for (String attrName : subjectData.keySet()) {
        // FIXME volat metody z attributesManagera nez kontrolovat na zacatek jmena
        if (attrName.startsWith(AttributesManager.NS_MEMBER_ATTR) || attrName.startsWith(AttributesManager.NS_USER_ATTR)) {
            attributes.put(attrName, subjectData.get(attrName));
        }
    }
    candidateSync.setRichUserExtSource(new RichUserExtSource(userExtSource, new ArrayList<>()));
    candidateSync.setAdditionalRichUserExtSources(Utils.extractAdditionalUserExtSources(perunSession, subjectData));
    candidateSync.setAttributes(attributes);
    return candidateSync;
}
Also used : RichUserExtSource(cz.metacentrum.perun.core.api.RichUserExtSource) CandidateSync(cz.metacentrum.perun.core.api.CandidateSync) RichUserExtSource(cz.metacentrum.perun.core.api.RichUserExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 2 with RichUserExtSource

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

the class Utils method extractAdditionalUserExtSources.

/**
 * Returns additionalUserExtSources from the subject. It's used for synchronization from different ExtSources. subjectFromExtSource was obtained from the ExtSource.
 * This additional userExtSource has 3 required parts: name of extSource, type of extSource, login of extsource with optional ues attributes and their values.
 * And 1 optional part: LoA.
 * Expected format of additional userExtSource is: extSourceName|extSourceType|extLogin;uesAttribute1=value1,value2;uesAttribute2=value1|LoA
 *
 * @param sess perun session
 * @param subjectFromExtSource map with the subject
 * @return List<RichUserExtSource> all additional ExtSources (and possibly their attributes) from the subject, returned list will never contain null value
 */
public static List<RichUserExtSource> extractAdditionalUserExtSources(PerunSession sess, Map<String, String> subjectFromExtSource) {
    List<RichUserExtSource> additionalUserExtSources = new ArrayList<>();
    for (String attrName : subjectFromExtSource.keySet()) {
        if (attrName != null && subjectFromExtSource.get(attrName) != null && attrName.startsWith(ExtSourcesManagerImpl.USEREXTSOURCEMAPPING)) {
            String login = subjectFromExtSource.get("login");
            // Entry contains extSourceName|extSourceType|extLogin;uesAttribute=value1,value2[|LoA]
            String[] userExtSourceRaw = subjectFromExtSource.get(attrName).split("\\|");
            log.debug("Processing additionalUserExtSource {}", subjectFromExtSource.get(attrName));
            // Check if the array has at least 3 parts, this is protection against outOfBoundException
            if (userExtSourceRaw.length < 3) {
                throw new InternalErrorException("There is a missing mandatory part of additional user extSource value when processing it - '" + attrName + "'");
            }
            try {
                // Get additional ues
                UserExtSource additionalUserExtSource = parseAdditionalUserExtSource(sess, userExtSourceRaw);
                // Get ues attributes
                List<Attribute> uesAttributes = parseUESAttributes(sess, login, userExtSourceRaw[2]);
                // Create richUserExtSource from ues and its attributes
                RichUserExtSource richUserExtSource = new RichUserExtSource(additionalUserExtSource, uesAttributes);
                // Add additional rich user extSource
                additionalUserExtSources.add(richUserExtSource);
            } catch (ParserException e) {
                log.error("User with login {} has invalid additional userExtSource defined {}.", login, userExtSourceRaw);
            }
        }
    }
    return additionalUserExtSources;
}
Also used : RichUserExtSource(cz.metacentrum.perun.core.api.RichUserExtSource) ParserException(cz.metacentrum.perun.core.api.exceptions.ParserException) RichUserExtSource(cz.metacentrum.perun.core.api.RichUserExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) Attribute(cz.metacentrum.perun.core.api.Attribute) ArrayList(java.util.ArrayList) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 3 with RichUserExtSource

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

the class UsersManagerEntryIntegrationTest method testGetRichUserExtSourceAttributes.

// PRIVATE METHODS -------------------------------------------------------------
/**
 * This method is used to test attributes of returned richUserExtSource from given call.
 *
 * First, this method creates attributes for given names. Then the method executes
 * the given getRichUserExtSourceCall and finds the tested rues. Then calls the ruesValidation.
 *
 * @param getRichUserExtSourceCall call that returns richUserExtSources
 * @param ruesValidation validation of returned richUserExtSource
 * @param attrNamesToSetup names of ues attributes that will be set up for the tested ues
 * @throws Exception any exception
 */
private void testGetRichUserExtSourceAttributes(TestSupplier<List<RichUserExtSource>> getRichUserExtSourceCall, TestConsumer<RichUserExtSource> ruesValidation, String... attrNamesToSetup) throws Exception {
    // set up ues attributes
    for (String attrName : attrNamesToSetup) {
        Attribute attribute = createUserExtSourceAttribute(attrName);
        perun.getAttributesManagerBl().setAttribute(sess, userExtSource, attribute);
    }
    // get richUserExtSources and find the one with set attribute
    RichUserExtSource desiredRues = null;
    List<RichUserExtSource> richUserExtSources = getRichUserExtSourceCall.getThrows();
    for (RichUserExtSource richUserExtSource : richUserExtSources) {
        if (richUserExtSource.asUserExtSource().equals(userExtSource)) {
            desiredRues = richUserExtSource;
        }
    }
    // validate assertions
    ruesValidation.acceptThrows(desiredRues);
}
Also used : RichUserExtSource(cz.metacentrum.perun.core.api.RichUserExtSource) Attribute(cz.metacentrum.perun.core.api.Attribute)

Example 4 with RichUserExtSource

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

the class UtilsIntegrationTest method extractAdditionalUserExtSourcesWithAttributeListTest.

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

Example 5 with RichUserExtSource

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

the class UtilsIntegrationTest method extractAdditionalUserExtSourcesWithAttributeWrongValueTest.

@Test
public void extractAdditionalUserExtSourcesWithAttributeWrongValueTest() throws Exception {
    System.out.println("Utils.extractAdditionalUserExtSourcesWithAttributeWrongValueTest");
    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");
    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, new ArrayList<>())));
}
Also used : RichUserExtSource(cz.metacentrum.perun.core.api.RichUserExtSource) HashMap(java.util.HashMap) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) AbstractPerunIntegrationTest(cz.metacentrum.perun.core.AbstractPerunIntegrationTest) Test(org.junit.Test)

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