use of cz.metacentrum.perun.core.implApi.ExtSourceSimpleApi in project perun by CESNET.
the class VosManagerBlImpl method findCandidates.
public List<Candidate> findCandidates(PerunSession sess, Vo vo, String searchString, int maxNumOfResults) throws InternalErrorException {
List<Candidate> candidates = new ArrayList<Candidate>();
int numOfResults = 0;
try {
// Iterate through all registered extSources
for (ExtSource source : getPerunBl().getExtSourcesManagerBl().getVoExtSources(sess, vo)) {
// Info if this is only simple ext source, change behavior if not
boolean simpleExtSource = true;
// Get potential subjects from the extSource
List<Map<String, String>> subjects;
try {
if (source instanceof ExtSourceApi) {
// find subjects with all their properties
subjects = ((ExtSourceApi) source).findSubjects(searchString, maxNumOfResults);
simpleExtSource = false;
} else {
// find subjects only with logins - they then must be retrieved by login
subjects = ((ExtSourceSimpleApi) source).findSubjectsLogins(searchString, maxNumOfResults);
}
} catch (ExtSourceUnsupportedOperationException e1) {
log.warn("ExtSource {} doesn't support findSubjects", source.getName());
continue;
} catch (InternalErrorException e) {
log.error("Error occurred on ExtSource {}, Exception {}.", source.getName(), e);
continue;
} finally {
try {
((ExtSourceSimpleApi) source).close();
} catch (ExtSourceUnsupportedOperationException e) {
// ExtSource doesn't support that functionality, so silently skip it.
} catch (InternalErrorException e) {
log.error("Can't close extSource connection. Cause: {}", e);
}
}
Set<String> uniqueLogins = new HashSet<>();
for (Map<String, String> s : subjects) {
// Check if the user has unique identifier within extSource
if ((s.get("login") == null) || (s.get("login") != null && ((String) s.get("login")).isEmpty())) {
log.error("User '{}' cannot be added, because he/she doesn't have a unique identifier (login)", s);
// Skip to another user
continue;
}
String extLogin = (String) s.get("login");
// check uniqueness of every login in extSource
if (uniqueLogins.contains(extLogin)) {
throw new InternalErrorException("There are more than 1 login '" + extLogin + "' getting from extSource '" + source + "'");
} else {
uniqueLogins.add(extLogin);
}
// Get Candidate
Candidate candidate;
try {
if (simpleExtSource) {
// retrieve data about subjects from ext source based on ext. login
candidate = getPerunBl().getExtSourcesManagerBl().getCandidate(sess, source, extLogin);
} else {
// retrieve data about subjects from subjects we already have locally
candidate = getPerunBl().getExtSourcesManagerBl().getCandidate(sess, s, source, extLogin);
}
} catch (ExtSourceNotExistsException e) {
throw new ConsistencyErrorException("Getting candidate from non-existing extSource " + source, e);
} catch (CandidateNotExistsException e) {
throw new ConsistencyErrorException("findSubjects returned that candidate, but getCandidate cannot find him using login " + extLogin, e);
} catch (ExtSourceUnsupportedOperationException e) {
throw new InternalErrorException("extSource supports findSubjects but not getCandidate???", e);
}
try {
getPerunBl().getMembersManagerBl().getMemberByUserExtSources(sess, vo, candidate.getUserExtSources());
// Candidate is already a member of the VO, so do not add him to the list of candidates
continue;
} catch (MemberNotExistsException e) {
// This is OK
}
// Add candidate to the list of candidates
log.debug("findCandidates: returning candidate: {}", candidate);
candidates.add(candidate);
numOfResults++;
// Stop getting new members if the number of already retrieved members exceeded the maxNumOfResults
if (maxNumOfResults > 0 && numOfResults >= maxNumOfResults) {
break;
}
}
// Stop walking through next sources if the number of already retrieved members exceeded the maxNumOfResults
if (maxNumOfResults > 0 && numOfResults >= maxNumOfResults) {
break;
}
}
log.debug("Returning {} potential members for vo {}", candidates.size(), vo);
return candidates;
} catch (RuntimeException e) {
throw new InternalErrorException(e);
}
}
use of cz.metacentrum.perun.core.implApi.ExtSourceSimpleApi 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.implApi.ExtSourceSimpleApi 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.implApi.ExtSourceSimpleApi in project perun by CESNET.
the class GroupsManagerBlImpl method synchronizeGroupStructure.
@Override
public List<String> synchronizeGroupStructure(PerunSession sess, Group baseGroup) throws AttributeNotExistsException, WrongAttributeAssignmentException, ExtSourceNotExistsException, WrongAttributeValueException, WrongReferenceAttributeValueException {
List<String> skippedGroups = new ArrayList<>();
log.info("Group structure synchronization {}: started.", baseGroup);
// get extSource for group structure
ExtSource source = getGroupExtSourceForSynchronization(sess, baseGroup);
try {
// get login attribute for structure
AttributeDefinition loginAttributeDefinition = getLoginAttributeForGroupStructure(sess, baseGroup);
// get login prefix if exists
String loginPrefix = getLoginPrefixForGroupStructure(sess, baseGroup);
List<CandidateGroup> candidateGroupsToAdd = new ArrayList<>();
Map<CandidateGroup, Group> groupsToUpdate = new HashMap<>();
List<Group> groupsToRemove = new ArrayList<>();
Map<String, Group> actualGroups = getAllSubGroupsWithLogins(sess, baseGroup, loginAttributeDefinition);
List<Map<String, String>> subjectGroups = getSubjectGroupsFromExtSource(sess, source, baseGroup);
if (isThisFlatSynchronization(sess, baseGroup)) {
for (Map<String, String> subjectGroup : subjectGroups) {
subjectGroup.put(PARENT_GROUP_LOGIN, null);
}
}
List<String> mergeAttributes = getAttributesListFromExtSource(source, MERGE_GROUP_ATTRIBUTES);
List<CandidateGroup> candidateGroups = getPerunBl().getExtSourcesManagerBl().generateCandidateGroups(sess, subjectGroups, source, loginPrefix);
categorizeGroupsForSynchronization(actualGroups, candidateGroups, candidateGroupsToAdd, groupsToUpdate, groupsToRemove);
// order of operations is important here
// removing need to go first to be able to replace groups with same name but different login
// updating need to be last to set right order of groups again
List<Integer> removedGroupsIds = removeFormerGroupsWhileSynchronization(sess, baseGroup, groupsToRemove, skippedGroups);
addMissingGroupsWhileSynchronization(sess, baseGroup, candidateGroupsToAdd, loginAttributeDefinition, skippedGroups, mergeAttributes);
updateExistingGroupsWhileSynchronization(sess, baseGroup, groupsToUpdate, removedGroupsIds, loginAttributeDefinition, skippedGroups, mergeAttributes);
setUpSynchronizationAttributesForAllSubGroups(sess, baseGroup, source, loginAttributeDefinition, loginPrefix);
syncResourcesForSynchronization(sess, baseGroup, loginAttributeDefinition, skippedGroups);
log.info("Group structure synchronization {}: ended.", baseGroup);
return skippedGroups;
} finally {
if (source instanceof ExtSourceSimpleApi) {
try {
((ExtSourceSimpleApi) source).close();
} catch (ExtSourceUnsupportedOperationException e) {
// silently skip
} catch (Exception e) {
log.error("Failed to close extsource after structure synchronization.", e);
}
}
}
}
use of cz.metacentrum.perun.core.implApi.ExtSourceSimpleApi in project perun by CESNET.
the class GroupsManagerBlImpl method convertSubjectsToCandidates.
/**
* Convert List of subjects to list of Candidates.
*
* To getting Candidate can use 1 of 3 possible options:
* 1] membersSource and source are not equals => we have just login, other attributes neet to get from membersSource
* 2] membersSource==source and membersSource is instance of ExtSourceApi => we already have all attributes in subject
* 3] membersSource==source and membersSource is instance of SimplExtSourceApi => we have just login, need to read other attributes again
*
* If candidate cannot be get for some reason, add this reason to skippedMembers list and skip him.
*
* @param sess
* @param subjects list of subjects from ExtSource (at least login should be here)
* @param membersSource optional member ExtSource (if members attributes are from other source then their logins)
* @param source default group ExtSource
* @param actualGroupMembers actual members of synchronized group
* @param skippedMembers not successfully synchronized members are skipped and information about it should be added here
*
* @return list of successfully created candidates from subjects
*
* @throws InternalErrorException if some internal error occurs
*/
private List<Candidate> convertSubjectsToCandidates(PerunSession sess, List<Map<String, String>> subjects, ExtSource membersSource, ExtSource source, List<RichMember> actualGroupMembers, List<String> skippedMembers) {
List<Candidate> candidates = new ArrayList<>();
// mapping structure for more efficient searching of actual group members
Map<UserExtSource, RichMember> mappingStructure = this.createMappingStructure(actualGroupMembers);
for (Map<String, String> subject : subjects) {
String login = subject.get("login");
// Skip subjects, which doesn't have login
if (login == null || login.isEmpty()) {
log.debug("Subject {} doesn't contain attribute login, skipping.", subject);
skippedMembers.add("MemberEntry:[" + subject + "] was skipped because login is missing");
continue;
}
try {
// 1] sources of login and other attributes are not same
if (!membersSource.equals(source)) {
// need to read attributes from the new memberSource, we can't use locally data there (there are from other extSource)
candidates.add(new Candidate(getPerunBl().getExtSourcesManagerBl().getCandidate(sess, membersSource, login)));
// 2] sources are same and we work with source which is instance of ExtSourceApi
} else if (membersSource instanceof ExtSourceApi) {
// we can use the data from this source without reading them again (all exists in the map of subject attributes)
candidates.add(new Candidate(getPerunBl().getExtSourcesManagerBl().getCandidate(sess, subject, membersSource, login)));
// 3] sources are same and we work with source which is instace of ExtSourceSimpleApi
} else if (membersSource instanceof ExtSourceSimpleApi) {
// we can't use the data from this source, we need to read them again (they are not in the map of subject attributes)
candidates.add(new Candidate(getPerunBl().getExtSourcesManagerBl().getCandidate(sess, membersSource, login)));
} else {
// this could not happen without change in extSource API code
throw new InternalErrorException("ExtSource is other instance than SimpleApi or Api and this is not supported!");
}
} catch (CandidateNotExistsException e) {
log.warn("getGroupSubjects subjects returned login {}, but it cannot be obtained using getCandidate()", login);
// If member can't be find in the member's extSource (we are missing other attributes) we can try find him in the group
UserExtSource subjectUserExtSource = new UserExtSource(membersSource, login);
// If member is in the group, we can create a simple object from him to preserve his existence in the group
if (mappingStructure.containsKey(subjectUserExtSource)) {
RichMember richMember = mappingStructure.get(subjectUserExtSource);
// convert richMember to simple candidate object (to prevent wrong attribute updating)
candidates.add(BeansUtils.convertRichMemberToCandidate(richMember, subjectUserExtSource));
skippedMembers.add("MemberEntry:[" + richMember + "] was skipped from updating in the group, because he can't be found by login:'" + login + "' in extSource " + membersSource);
} else {
skippedMembers.add("MemberEntry:[" + subject + "] was skipped from adding to the group because he can't be found by login:'" + login + "' in extSource " + membersSource);
}
} catch (ExtSourceUnsupportedOperationException e) {
log.warn("ExtSource {} doesn't support getCandidate operation.", membersSource);
skippedMembers.add("MemberEntry:[" + subject + "] was skipped because extSource " + membersSource + " not support method getCandidate");
} catch (ParserException e) {
log.warn("Can't parse value {} from candidate with login {}", e.getParsedValue(), login);
skippedMembers.add("MemberEntry:[" + subject + "] was skipped because of problem with parsing value '" + e.getParsedValue() + "'");
}
}
return candidates;
}
Aggregations