use of cz.metacentrum.perun.core.api.ExtSource in project perun by CESNET.
the class VosManagerBlImpl method deleteVo.
@Override
public void deleteVo(PerunSession sess, Vo vo, boolean forceDelete) {
log.debug("Deleting vo {}", vo);
try {
// remove admins of this vo
List<Group> adminGroups = getVosManagerImpl().getAdminGroups(sess, vo);
for (Group adminGroup : adminGroups) {
try {
AuthzResolverBlImpl.unsetRole(sess, adminGroup, vo, Role.VOADMIN);
} catch (GroupNotAdminException e) {
log.warn("When trying to unsetRole VoAdmin for group {} in the vo {} the exception was thrown {}", adminGroup, vo, e);
// skip and log as warning
}
}
List<User> adminUsers = getVosManagerImpl().getAdmins(sess, vo);
for (User adminUser : adminUsers) {
try {
AuthzResolverBlImpl.unsetRole(sess, adminUser, vo, Role.VOADMIN);
} catch (UserNotAdminException e) {
log.warn("When trying to unsetRole VoAdmin for user {} in the vo {} the exception was thrown {}", adminUser, vo, e);
// skip and log as warning
}
}
List<Member> members = getPerunBl().getMembersManagerBl().getMembers(sess, vo);
log.debug("Deleting vo {} members", vo);
// Check if there are some members left
if (members != null && members.size() > 0) {
if (forceDelete) {
getPerunBl().getMembersManagerBl().deleteAllMembers(sess, vo);
} else
throw new RelationExistsException("Vo vo=" + vo + " contains members");
}
log.debug("Removing vo {} resources and theirs attributes", vo);
// Delete resources
List<Resource> resources = getPerunBl().getResourcesManagerBl().getResources(sess, vo);
if ((resources.size() == 0) || forceDelete) {
for (Resource resource : resources) {
getPerunBl().getAttributesManagerBl().removeAllAttributes(sess, resource);
// Remove binding between service and resource
List<Service> services = getPerunBl().getResourcesManagerBl().getAssignedServices(sess, resource);
for (Service service : services) {
getPerunBl().getResourcesManagerBl().removeService(sess, resource, service);
}
getPerunBl().getResourcesManagerBl().deleteResource(sess, resource);
}
} else {
throw new RelationExistsException("Vo vo=" + vo + " contains resources");
}
log.debug("Removing vo {} groups", vo);
// Delete all groups
List<Group> groups = getPerunBl().getGroupsManagerBl().getGroups(sess, vo);
if (groups.size() != 1) {
if (groups.size() < 1)
throw new ConsistencyErrorException("'members' group is missing");
if (forceDelete) {
getPerunBl().getGroupsManagerBl().deleteAllGroups(sess, vo);
} else {
throw new RelationExistsException("Vo vo=" + vo + " contains groups");
}
}
// Finally delete binding between Vo and external source
List<ExtSource> ess = getPerunBl().getExtSourcesManagerBl().getVoExtSources(sess, vo);
log.debug("Deleting {} external sources binded to the vo {}", ess.size(), vo);
for (ExtSource es : ess) {
getPerunBl().getExtSourcesManagerBl().removeExtSource(sess, vo, es);
}
// Delete members group
log.debug("Removing an administrators' group from the vo {}", vo);
getPerunBl().getGroupsManagerBl().deleteMembersGroup(sess, vo);
// delete all VO reserved logins from KDC
List<Integer> list = getVosManagerImpl().getVoApplicationIds(sess, vo);
for (Integer appId : list) {
// for each application
for (Pair<String, String> login : getVosManagerImpl().getApplicationReservedLogins(appId)) {
// for all reserved logins - delete them in ext. system (e.g. KDC)
try {
// !!! left = namespace / right = login !!!
getPerunBl().getUsersManagerBl().deletePassword(sess, login.getRight(), login.getLeft());
} catch (LoginNotExistsException ex) {
log.error("Login: {} not exists in namespace {} while deleting passwords", login.getRight(), login.getLeft());
}
}
}
// delete all VO reserved logins from DB
getVosManagerImpl().deleteVoReservedLogins(sess, vo);
// VO applications, submitted data and app_form are deleted on cascade with "deleteVo()"
// Delete VO attributes
getPerunBl().getAttributesManagerBl().removeAllAttributes(sess, vo);
// Delete all Vo tags (for resources in Vo)
getPerunBl().getResourcesManagerBl().deleteAllResourcesTagsForVo(sess, vo);
} catch (Exception ex) {
throw new InternalErrorException(ex);
}
// Finally delete the VO
Vo deletedVo = getVosManagerImpl().deleteVo(sess, vo);
getPerunBl().getAuditer().log(sess, new VoDeleted(deletedVo));
}
use of cz.metacentrum.perun.core.api.ExtSource in project perun by CESNET.
the class VosManagerBlImpl method findCandidates.
public List<Candidate> findCandidates(PerunSession sess, Vo vo, String searchString, int maxNumOfResults, List<ExtSource> extSources, boolean filterExistingMembers) {
List<Candidate> candidates = new ArrayList<>();
int numOfResults = 0;
try {
// Iterate through given extSources
for (ExtSource source : extSources) {
try {
// 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.", 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 && 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 = 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 = new Candidate(getPerunBl().getExtSourcesManagerBl().getCandidate(sess, source, extLogin));
} else {
// retrieve data about subjects from subjects we already have locally
candidate = new Candidate(getPerunBl().getExtSourcesManagerBl().getCandidate(sess, s, source, extLogin));
}
} 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);
}
if (filterExistingMembers) {
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;
}
}
} catch (InternalErrorException e) {
log.error("Failed to get candidates from ExtSource: {}", source);
} 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);
}
}
}
// 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.api.ExtSource in project perun by CESNET.
the class UsersManagerBlImpl method addUserExtSource.
@Override
public UserExtSource addUserExtSource(PerunSession sess, User user, UserExtSource userExtSource) throws UserExtSourceExistsException {
// Check if the userExtSource already exists
if (usersManagerImpl.userExtSourceExists(sess, userExtSource)) {
throw new UserExtSourceExistsException("UserExtSource " + userExtSource + " already exists.");
}
// Check if userExtsource is type of IDP (special testing behavior)
if (userExtSource.getExtSource().getType().equals(ExtSourcesManager.EXTSOURCE_IDP)) {
// If extSource of this userExtSource is type of IDP, test uniqueness of login in this extSource type for all users
String login = userExtSource.getLogin();
List<UserExtSource> userExtSources = getAllUserExtSourcesByTypeAndLogin(sess, ExtSourcesManager.EXTSOURCE_IDP, login);
if (!userExtSources.stream().allMatch(ues -> ues.getUserId() == user.getId())) {
if (userExtSources.stream().allMatch(ues -> ues.getUserId() == userExtSources.get(0).getUserId())) {
// Duplicate identity belongs to different user - block it!!
throw new InternalErrorException("ExtLogin: " + login + " is already used for extSourceType: " + ExtSourcesManager.EXTSOURCE_IDP);
} else {
// more users cannot have the same login
throw new ConsistencyErrorException("There are " + userExtSources.size() + " extLogins: " + login + " for extSourceType: " + ExtSourcesManager.EXTSOURCE_IDP);
}
}
}
userExtSource = getUsersManagerImpl().addUserExtSource(sess, user, userExtSource);
getPerunBl().getAuditer().log(sess, new UserExtSourceAddedToUser(userExtSource, user));
return userExtSource;
}
use of cz.metacentrum.perun.core.api.ExtSource in project perun by CESNET.
the class UsersManagerBlImpl method getUserByExtSourceNameAndExtLogin.
@Override
public User getUserByExtSourceNameAndExtLogin(PerunSession sess, String extSourceName, String extLogin) throws ExtSourceNotExistsException, UserExtSourceNotExistsException, UserNotExistsException {
ExtSource extSource = perunBl.getExtSourcesManagerBl().getExtSourceByName(sess, extSourceName);
UserExtSource userExtSource = this.getUserExtSourceByExtLogin(sess, extSource, extLogin);
return this.getUserByUserExtSource(sess, userExtSource);
}
use of cz.metacentrum.perun.core.api.ExtSource in project perun by CESNET.
the class VosManagerBlImpl method findCandidates.
public List<Candidate> findCandidates(PerunSession sess, Group group, String searchString) throws InternalErrorException {
List<Candidate> candidates = new ArrayList<>();
try {
// Iterate through all registered extSources in the group
for (ExtSource source : getPerunBl().getExtSourcesManagerBl().getGroupExtSources(sess, group)) {
// 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);
simpleExtSource = false;
} else {
// find subjects only with logins - they then must be retrieved by login
subjects = ((ExtSourceSimpleApi) source).findSubjectsLogins(searchString);
}
} 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 {
Vo vo = getPerunBl().getVosManagerBl().getVoById(sess, group.getVoId());
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 (VoNotExistsException e) {
throw new InternalErrorException(e);
} catch (MemberNotExistsException e) {
// This is OK
}
// Add candidate to the list of candidates
log.debug("findCandidates: returning candidate: {}", candidate);
candidates.add(candidate);
}
}
log.debug("Returning {} potential members for group {}", candidates.size(), group);
return candidates;
} catch (RuntimeException e) {
throw new InternalErrorException(e);
}
}
Aggregations