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;
}
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;
}
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);
}
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))))));
}
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<>())));
}
Aggregations