use of cz.metacentrum.perun.core.api.ExtSource in project perun by CESNET.
the class MembersManagerBlImpl method createMember.
//MAIN METHOD
public Member createMember(PerunSession sess, Vo vo, SpecificUserType specificUserType, Candidate candidate, List<Group> groups, List<String> overwriteUserAttributes) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, AlreadyMemberException, ExtendMembershipException, GroupOperationsException {
log.debug("Creating member for VO {} from candidate {}", vo, candidate);
// Get the user
User user = null;
if (candidate.getUserExtSources() != null) {
for (UserExtSource ues : candidate.getUserExtSources()) {
// Check if the extSource exists
ExtSource tmpExtSource = getPerunBl().getExtSourcesManagerBl().checkOrCreateExtSource(sess, ues.getExtSource().getName(), ues.getExtSource().getType());
// Set the extSource ID
ues.getExtSource().setId(tmpExtSource.getId());
try {
// Try to find the user by userExtSource
user = getPerunBl().getUsersManagerBl().getUserByExtSourceNameAndExtLogin(sess, ues.getExtSource().getName(), ues.getLogin());
} catch (UserExtSourceNotExistsException e) {
// This is OK, non-existent userExtSource will be assigned later
} catch (UserNotExistsException e) {
// Ignore, we are only checking if the user exists
} catch (ExtSourceNotExistsException e) {
// Ignore, we are only checking if the user exists
}
}
}
// If user hasn't been found, then create him
if (user == null) {
user = new User();
user.setFirstName(candidate.getFirstName());
user.setLastName(candidate.getLastName());
user.setMiddleName(candidate.getMiddleName());
user.setTitleAfter(candidate.getTitleAfter());
user.setTitleBefore(candidate.getTitleBefore());
if (specificUserType.equals(specificUserType.SERVICE))
user.setServiceUser(true);
if (specificUserType.equals(specificUserType.SPONSORED))
user.setSponsoredUser(true);
// Store the user, this must be done in separate transaction
user = getPerunBl().getUsersManagerBl().createUser(sess, user);
log.debug("createMember: new user: {}", user);
}
// Assign missing userExtSource and update LoA
if (candidate.getUserExtSources() != null) {
for (UserExtSource userExtSource : candidate.getUserExtSources()) {
try {
UserExtSource currentUserExtSource = getPerunBl().getUsersManagerBl().getUserExtSourceByExtLogin(sess, userExtSource.getExtSource(), userExtSource.getLogin());
// Update LoA
currentUserExtSource.setLoa(userExtSource.getLoa());
getPerunBl().getUsersManagerBl().updateUserExtSource(sess, currentUserExtSource);
} catch (UserExtSourceNotExistsException e) {
// Create userExtSource
try {
getPerunBl().getUsersManagerBl().addUserExtSource(sess, user, userExtSource);
} catch (UserExtSourceExistsException e1) {
throw new ConsistencyErrorException("Adding userExtSource which already exists: " + userExtSource);
}
}
}
}
try {
Member member = getMemberByUser(sess, vo, user);
throw new AlreadyMemberException(member);
} catch (MemberNotExistsException IGNORE) {
}
// Create the member
Member member = getMembersManagerImpl().createMember(sess, vo, user);
getPerunBl().getAuditer().log(sess, "{} created.", member);
// Create the member's attributes
List<Attribute> membersAttributes = new ArrayList<Attribute>();
List<Attribute> usersAttributesToMerge = new ArrayList<>();
List<Attribute> usersAttributesToModify = new ArrayList<>();
if (candidate.getAttributes() != null) {
for (String attributeName : candidate.getAttributes().keySet()) {
AttributeDefinition attributeDefinition;
try {
attributeDefinition = getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, attributeName);
} catch (AttributeNotExistsException ex) {
throw new InternalErrorException(ex);
}
Attribute attribute = new Attribute(attributeDefinition);
attribute.setValue(getPerunBl().getAttributesManagerBl().stringToAttributeValue(candidate.getAttributes().get(attributeName), attribute.getType()));
if (getPerunBl().getAttributesManagerBl().isFromNamespace(sess, attribute, AttributesManager.NS_MEMBER_ATTR_DEF) || getPerunBl().getAttributesManagerBl().isFromNamespace(sess, attribute, AttributesManager.NS_MEMBER_ATTR_OPT)) {
// This is member's attribute
membersAttributes.add(attribute);
} else if (getPerunBl().getAttributesManagerBl().isFromNamespace(sess, attribute, AttributesManager.NS_USER_ATTR_DEF) || getPerunBl().getAttributesManagerBl().isFromNamespace(sess, attribute, AttributesManager.NS_USER_ATTR_OPT)) {
if (overwriteUserAttributes != null && !overwriteUserAttributes.isEmpty() && overwriteUserAttributes.contains(attribute.getName())) {
usersAttributesToModify.add(attribute);
} else {
usersAttributesToMerge.add(attribute);
}
}
}
}
// Store the attributes
try {
//if empty, skip setting or merging empty arrays of attributes at all
if (!membersAttributes.isEmpty())
getPerunBl().getAttributesManagerBl().setAttributes(sess, member, membersAttributes);
if (!usersAttributesToMerge.isEmpty())
getPerunBl().getAttributesManagerBl().mergeAttributesValues(sess, user, usersAttributesToMerge);
if (!usersAttributesToModify.isEmpty())
getPerunBl().getAttributesManagerBl().setAttributes(sess, user, usersAttributesToModify);
} catch (WrongAttributeAssignmentException e) {
throw new InternalErrorException(e);
}
// Set the initial membershipExpiration
// Get user LOA
String memberLoa = null;
try {
Attribute loa = getPerunBl().getAttributesManagerBl().getAttribute(sess, member, AttributesManager.NS_MEMBER_ATTR_VIRT + ":loa");
memberLoa = (String) loa.getValue();
} catch (AttributeNotExistsException e) {
// user has no loa defined - if required by VO, it will be stopped in checking method later
} catch (WrongAttributeAssignmentException e) {
throw new InternalErrorException(e);
}
// check if user can be member
this.canBeMemberInternal(sess, vo, user, memberLoa, true);
// set initial membership expiration
this.extendMembership(sess, member);
insertToMemberGroup(sess, member, vo);
// add member also to all groups in list
if (groups != null && !groups.isEmpty()) {
for (Group group : groups) {
try {
perunBl.getGroupsManagerBl().addMember(sess, group, member);
} catch (NotMemberOfParentGroupException ex) {
throw new InternalErrorException("Member " + member + " can't be add to the group " + group + " because he is not member of it's parent group.", ex);
} catch (GroupNotExistsException e) {
throw new ConsistencyErrorException(e);
}
}
}
return member;
}
use of cz.metacentrum.perun.core.api.ExtSource in project perun by CESNET.
the class ExtSourcesManagerBlImpl method checkExtSourceAssignedToVo.
@Override
public void checkExtSourceAssignedToVo(PerunSession sess, ExtSource extSource, int voId) throws InternalErrorException, ExtSourceNotAssignedException, VoNotExistsException {
Vo vo = getPerunBl().getVosManagerBl().getVoById(sess, voId);
List<ExtSource> voExtSources = getPerunBl().getExtSourcesManagerBl().getVoExtSources(sess, vo);
if (!voExtSources.contains(extSource))
throw new ExtSourceNotAssignedException("ExtSource " + extSource + " is not assigned to vo " + vo);
}
use of cz.metacentrum.perun.core.api.ExtSource 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.ExtSource in project perun by CESNET.
the class VosManagerBlImpl method deleteVo.
public void deleteVo(PerunSession sess, Vo vo, boolean forceDelete) throws InternalErrorException, RelationExistsException {
log.debug("Deleting vo {}", vo);
try {
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 atributes", vo);
// Delete resources
List<Resource> resources = getPerunBl().getResourcesManagerBl().getResources(sess, vo);
if ((resources.size() == 0) || ((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 ((groups.size() > 1) && 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
getVosManagerImpl().deleteVo(sess, vo);
getPerunBl().getAuditer().log(sess, "{} deleted.", vo);
}
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) 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);
}
}
Aggregations