use of cz.metacentrum.perun.core.api.UserExtSource in project perun by CESNET.
the class RichUser method serializeToString.
@Override
public String serializeToString() {
StringBuilder str = new StringBuilder();
List<UserExtSource> userESOld = getUserExtSources();
List<Attribute> userAttrOld = getUserAttributes();
List<String> userESNew = new ArrayList<String>();
List<String> userAttrNew = new ArrayList<String>();
String sUserESNew;
String sUserAttrNew;
if (getUserExtSources() == null)
sUserESNew = "\\0";
else {
for (UserExtSource u : userESOld) {
userESNew.add(u.serializeToString());
}
sUserESNew = userESNew.toString();
}
if (getUserAttributes() == null)
sUserAttrNew = "\\0";
else {
for (Attribute a : userAttrOld) {
userAttrNew.add(a.serializeToString());
}
sUserAttrNew = userAttrNew.toString();
}
return str.append(this.getClass().getSimpleName()).append(":[").append("id=<").append(getId()).append(">").append(", titleBefore=<").append(getTitleBefore() == null ? "\\0" : BeansUtils.createEscaping(getTitleBefore())).append(">").append(", firstName=<").append(getFirstName() == null ? "\\0" : BeansUtils.createEscaping(getFirstName())).append(">").append(", lastName=<").append(getLastName() == null ? "\\0" : BeansUtils.createEscaping(getLastName())).append(">").append(", middleName=<").append(getMiddleName() == null ? "\\0" : BeansUtils.createEscaping(getMiddleName())).append(">").append(", titleAfter=<").append(getTitleAfter() == null ? "\\0" : BeansUtils.createEscaping(getTitleAfter())).append(">").append(", userExtSources=<").append(sUserESNew).append(">").append(", userAttributes=<").append(sUserAttrNew).append(">").append(']').toString();
}
use of cz.metacentrum.perun.core.api.UserExtSource in project perun by CESNET.
the class ExtSourcesManagerBlImpl method getCandidate.
public Candidate getCandidate(PerunSession perunSession, Map<String, String> subjectData, ExtSource source, String login) throws InternalErrorException, ExtSourceNotExistsException, CandidateNotExistsException, ExtSourceUnsupportedOperationException {
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
Candidate candidate = new Candidate();
// Prepare userExtSource object
UserExtSource userExtSource = new UserExtSource();
userExtSource.setExtSource(source);
userExtSource.setLogin(login);
// Set the userExtSource
candidate.setUserExtSource(userExtSource);
//If first name of candidate is not in format of name, set null instead
candidate.setFirstName(subjectData.get("firstName"));
if (candidate.getFirstName() != null) {
Matcher name = namePattern.matcher(candidate.getFirstName());
if (!name.matches())
candidate.setFirstName(null);
}
//If last name of candidate is not in format of name, set null instead
candidate.setLastName(subjectData.get("lastName"));
if (candidate.getLastName() != null) {
Matcher name = namePattern.matcher(candidate.getLastName());
if (!name.matches())
candidate.setLastName(null);
}
candidate.setMiddleName(subjectData.get("middleName"));
candidate.setTitleAfter(subjectData.get("titleAfter"));
candidate.setTitleBefore(subjectData.get("titleBefore"));
//Set service user
if (subjectData.get("isServiceUser") == null) {
candidate.setServiceUser(false);
} else {
String isServiceUser = subjectData.get("isServiceUser");
if (isServiceUser.equals("true")) {
candidate.setServiceUser(true);
} else {
candidate.setServiceUser(false);
}
}
//Set sponsored user
if (subjectData.get("isSponsoredUser") == null) {
candidate.setSponsoredUser(false);
} else {
String isSponsoredUser = subjectData.get("isSponsoredUser");
if (isSponsoredUser.equals("true")) {
candidate.setSponsoredUser(true);
} else {
candidate.setSponsoredUser(false);
}
}
// Additional userExtSources
List<UserExtSource> additionalUserExtSources = new ArrayList<UserExtSource>();
// Filter attributes
Map<String, String> attributes = new HashMap<String, String>();
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));
} else if (attrName.startsWith(ExtSourcesManagerImpl.USEREXTSOURCEMAPPING)) {
//skip null additional ext sources
if (subjectData.get(attrName) == null)
continue;
// Add additionalUserExtSources
// Entry contains extSourceName|extSourceType|extLogin[|LoA]
String[] userExtSourceRaw = subjectData.get(attrName).split("\\|");
log.debug("Processing additionalUserExtSource {}", subjectData.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 missing some mandatory part of additional user extSource value when processing it - '" + attrName + "'");
}
String additionalExtSourceName = userExtSourceRaw[0];
String additionalExtSourceType = userExtSourceRaw[1];
String additionalExtLogin = userExtSourceRaw[2];
int additionalExtLoa = 0;
//Loa is not mandatory argument
if (userExtSourceRaw.length > 3 && userExtSourceRaw[3] != null) {
try {
additionalExtLoa = Integer.parseInt(userExtSourceRaw[3]);
} catch (NumberFormatException e) {
throw new ParserException("Candidate with login [" + login + "] has wrong LoA '" + userExtSourceRaw[3] + "'.", e, "LoA");
}
}
ExtSource additionalExtSource;
if (additionalExtSourceName == null || additionalExtSourceName.isEmpty() || additionalExtSourceType == null || additionalExtSourceType.isEmpty() || additionalExtLogin == null || additionalExtLogin.isEmpty()) {
log.error("User with login {} has invalid additional userExtSource defined {}.", login, userExtSourceRaw);
} else {
try {
// Try to get extSource, with full extSource object (containg ID)
additionalExtSource = getPerunBl().getExtSourcesManagerBl().getExtSourceByName(perunSession, additionalExtSourceName);
} catch (ExtSourceNotExistsException e) {
try {
// Create new one if not exists
additionalExtSource = new ExtSource(additionalExtSourceName, additionalExtSourceType);
additionalExtSource = getPerunBl().getExtSourcesManagerBl().createExtSource(perunSession, additionalExtSource, null);
} catch (ExtSourceExistsException e1) {
throw new ConsistencyErrorException("Creating existin extSource: " + additionalExtSourceName);
}
}
//add additional user extSource
additionalUserExtSources.add(new UserExtSource(additionalExtSource, additionalExtLoa, additionalExtLogin));
}
}
}
candidate.setAdditionalUserExtSources(additionalUserExtSources);
candidate.setAttributes(attributes);
return candidate;
}
use of cz.metacentrum.perun.core.api.UserExtSource in project perun by CESNET.
the class ExtSourcesManagerBlImpl method getInvalidUsers.
public List<User> getInvalidUsers(PerunSession sess, ExtSource source) throws InternalErrorException {
List<Integer> usersIds;
List<User> invalidUsers = new ArrayList<User>();
// Get all users, who are associated with this extSource
usersIds = getExtSourcesManagerImpl().getAssociatedUsersIdsWithExtSource(sess, source);
List<User> users = getPerunBl().getUsersManagerBl().getUsersByIds(sess, usersIds);
for (User user : users) {
// From user's userExtSources get the login
String userLogin = "";
List<UserExtSource> userExtSources = getPerunBl().getUsersManagerBl().getUserExtSources(sess, user);
for (UserExtSource userExtSource : userExtSources) {
if (userExtSource.getExtSource().equals(source)) {
// It is enough to have at least one login from the extSource
// TODO jak budeme kontrolovat, ze mu zmizel jeden login a zustal jiny, zajima nas to?
userLogin = userExtSource.getLogin();
}
}
// Check if the login is still present in the extSource
try {
((ExtSourceSimpleApi) source).getSubjectByLogin(userLogin);
} catch (SubjectNotExistsException e) {
invalidUsers.add(user);
} catch (ExtSourceUnsupportedOperationException e) {
log.warn("ExtSource {} doesn't support getSubjectByLogin", source.getName());
continue;
}
}
return invalidUsers;
}
use of cz.metacentrum.perun.core.api.UserExtSource in project perun by CESNET.
the class AttributesManagerEntryIntegrationTest method setUserExtSourceAttributeWhenAttributeNotExists.
@Test(expected = AttributeNotExistsException.class)
public void setUserExtSourceAttributeWhenAttributeNotExists() throws Exception {
System.out.println(CLASS_NAME + "setUserExtSourceAttributeWhenAttributeNotExists");
UserExtSource ues = setUpUserExtSourceTest();
attributes = setUpUserExtSourceAttribute();
attributes.get(0).setId(0);
// make valid attribute not existing in DB by setting ID = 0
attributesManager.setAttribute(sess, ues, attributes.get(0));
// shouldn't find attribute
}
use of cz.metacentrum.perun.core.api.UserExtSource in project perun by CESNET.
the class AttributesManagerEntryIntegrationTest method setUserExtSourceAttributeWhenTypeMismatch.
@Test(expected = InternalErrorException.class)
public void setUserExtSourceAttributeWhenTypeMismatch() throws Exception {
System.out.println(CLASS_NAME + "setUserExtSourceAttributeWhenTypeMismatch");
UserExtSource ues = setUpUserExtSourceTest();
attributes = setUpUserExtSourceAttribute();
attributes.get(0).setValue(1);
attributesManager.setAttribute(sess, ues, attributes.get(0));
// shouldn't add attribute with String type and Integer value
}
Aggregations