use of cz.metacentrum.perun.core.api.exceptions.SubjectNotExistsException in project perun by CESNET.
the class ExtSourcesManagerBlImpl method getInvalidUsers.
@Override
public List<User> getInvalidUsers(PerunSession sess, ExtSource source) {
List<Integer> usersIds;
List<User> invalidUsers = new ArrayList<>();
// 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());
} finally {
if (source instanceof ExtSourceSimpleApi) {
try {
((ExtSourceSimpleApi) source).close();
} catch (ExtSourceUnsupportedOperationException e) {
// silently skip
} catch (Exception e) {
log.error("Failed to close connection to extsource", e);
}
}
}
}
return invalidUsers;
}
use of cz.metacentrum.perun.core.api.exceptions.SubjectNotExistsException in project perun by CESNET.
the class MembersManagerBlImpl method createMember.
@Override
public Member createMember(PerunSession sess, Vo vo, ExtSource extSource, String login, List<Group> groups) throws WrongAttributeValueException, WrongReferenceAttributeValueException, AlreadyMemberException, ExtendMembershipException {
// First of all get candidate from extSource directly
Candidate candidate = null;
try {
if (extSource instanceof ExtSourceApi) {
// get first subject, then create candidate
Map<String, String> subject = ((ExtSourceSimpleApi) extSource).getSubjectByLogin(login);
candidate = new Candidate(getPerunBl().getExtSourcesManagerBl().getCandidate(sess, subject, extSource, login));
} else if (extSource instanceof ExtSourceSimpleApi) {
// get candidates from external source by login
candidate = new Candidate(getPerunBl().getExtSourcesManagerBl().getCandidate(sess, extSource, login));
}
} catch (CandidateNotExistsException | SubjectNotExistsException ex) {
throw new InternalErrorException("Can't find candidate for login " + login + " in extSource " + extSource, ex);
} catch (ExtSourceUnsupportedOperationException ex) {
throw new InternalErrorException("Some operation is not allowed for extSource " + extSource, ex);
} finally {
if (extSource instanceof ExtSourceSimpleApi) {
try {
((ExtSourceSimpleApi) extSource).close();
} catch (ExtSourceUnsupportedOperationException e) {
// silently skip
} catch (Exception e) {
log.error("Failed to close connection to extsource", e);
}
}
}
return this.createMember(sess, vo, candidate, groups);
}
use of cz.metacentrum.perun.core.api.exceptions.SubjectNotExistsException in project perun by CESNET.
the class ExtSourcesManagerBlImpl method getCandidate.
@Override
public Candidate getCandidate(PerunSession sess, ExtSource source, String login) throws InternalErrorException, ExtSourceNotExistsException, CandidateNotExistsException, ExtSourceUnsupportedOperationException {
// 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);
// Get the subject from the extSource
Map<String, String> subject = null;
try {
subject = ((ExtSourceSimpleApi) source).getSubjectByLogin(login);
} catch (SubjectNotExistsException e) {
throw new CandidateNotExistsException(login);
}
if (subject == null) {
throw new CandidateNotExistsException("Candidate with login [" + login + "] not exists");
}
//If first name of candidate is not in format of name, set null instead
candidate.setFirstName(subject.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(subject.get("lastName"));
if (candidate.getLastName() != null) {
Matcher name = namePattern.matcher(candidate.getLastName());
if (!name.matches())
candidate.setLastName(null);
}
candidate.setMiddleName(subject.get("middleName"));
candidate.setTitleAfter(subject.get("titleAfter"));
candidate.setTitleBefore(subject.get("titleBefore"));
//Set service user
if (subject.get("isServiceUser") == null) {
candidate.setServiceUser(false);
} else {
String isServiceUser = subject.get("isServiceUser");
if (isServiceUser.equals("true")) {
candidate.setServiceUser(true);
} else {
candidate.setServiceUser(false);
}
}
//Set sponsored user
if (subject.get("isSponsoredUser") == null) {
candidate.setSponsoredUser(false);
} else {
String isSponsoredUser = subject.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 : subject.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, subject.get(attrName));
} else if (attrName.startsWith(ExtSourcesManagerImpl.USEREXTSOURCEMAPPING)) {
//skip null additional ext sources
if (subject.get(attrName) == null)
continue;
// Add additionalUserExtSources
// Entry contains extSourceName|extSourceType|extLogin[|LoA]
String[] userExtSourceRaw = subject.get(attrName).split("\\|");
log.debug("Processing additionalUserExtSource {}", subject.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(sess, additionalExtSourceName);
} catch (ExtSourceNotExistsException e) {
try {
// Create new one if not exists
additionalExtSource = new ExtSource(additionalExtSourceName, additionalExtSourceType);
additionalExtSource = getPerunBl().getExtSourcesManagerBl().createExtSource(sess, 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.exceptions.SubjectNotExistsException in project perun by CESNET.
the class ExtSourcePerun method findRichUser.
private RichUser findRichUser(String login) throws SubjectNotExistsException {
Map<String, Object> params = new HashMap<>();
List<RichUser> richUsers = this.findRichUsers(login);
List<RichUser> matchesRichUsers = new ArrayList<>();
for (RichUser richUser : richUsers) {
List<UserExtSource> userExtSources = richUser.getUserExtSources();
for (UserExtSource userExtSource : userExtSources) {
if (extSourceNameForLogin.equals(userExtSource.getExtSource().getName())) {
if (login.equals(userExtSource.getLogin()))
matchesRichUsers.add(richUser);
}
}
}
if (matchesRichUsers.isEmpty())
throw new SubjectNotExistsException("There is no subject with login " + login + " in extSource " + extSourceNameForLogin + " in System perun with RPC url: " + perunUrl);
if (matchesRichUsers.size() > 1)
throw new InternalErrorException("There are more then one subject with login " + login + " in extSource " + extSourceNameForLogin + " in System perun with RPC url: " + perunUrl);
return richUsers.get(0);
}
Aggregations