use of cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException in project perun by CESNET.
the class urn_perun_user_attribute_def_def_login_namespace_vsup method changedAttributeHook.
/**
* When login changes: first set / changed always change eduroam-vsup login too !!
* When login is set add UserExtSource, since logins are generated in Perun.
* When login is set, set also school mail u:d:vsupMail
*
* @param session
* @param user
* @param attribute
* @throws InternalErrorException
* @throws WrongReferenceAttributeValueException
*/
@Override
public void changedAttributeHook(PerunSessionImpl session, User user, Attribute attribute) throws WrongReferenceAttributeValueException {
if (attribute.getValue() != null) {
// add UES
ExtSource es;
try {
es = session.getPerunBl().getExtSourcesManagerBl().getExtSourceByName(session, "AD");
} catch (ExtSourceNotExistsException ex) {
throw new InternalErrorException("AD ext source on VŠUP doesn't exists.", ex);
}
try {
session.getPerunBl().getUsersManagerBl().getUserExtSourceByExtLogin(session, es, (String) attribute.getValue());
} catch (UserExtSourceNotExistsException ex) {
// add UES
UserExtSource ues = new UserExtSource(es, 2, (String) attribute.getValue());
try {
session.getPerunBl().getUsersManagerBl().addUserExtSource(session, user, ues);
} catch (UserExtSourceExistsException ex2) {
throw new ConsistencyErrorException(ex2);
}
}
// set eduroam-login
Attribute eduroamLogin = null;
try {
eduroamLogin = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, EDUROAM_VSUP_NAMESPACE);
if (!Objects.equals(attribute.getValue(), eduroamLogin.getValue())) {
eduroamLogin.setValue(attribute.getValue());
session.getPerunBl().getAttributesManagerBl().setAttribute(session, user, eduroamLogin);
}
} catch (WrongAttributeAssignmentException ex) {
throw new InternalErrorException(ex);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException(ex);
} catch (WrongAttributeValueException ex) {
throw new WrongReferenceAttributeValueException(attribute, eduroamLogin, "Mismatch in checking of users VŠUP login and eduroam login.", ex);
}
// set všup school mail
Attribute schoolMail = null;
try {
schoolMail = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, VSUP_MAIL_NAMESPACE);
if (!Objects.equals(attribute.getValue(), schoolMail.getValue())) {
schoolMail.setValue(attribute.getValue() + "@vsup.cz");
session.getPerunBl().getAttributesManagerBl().setAttribute(session, user, schoolMail);
}
} catch (WrongAttributeAssignmentException ex) {
throw new InternalErrorException(ex);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException(ex);
} catch (WrongAttributeValueException ex) {
throw new WrongReferenceAttributeValueException(attribute, schoolMail, "Mismatch in checking of users VŠUP login and schoolMail.", ex);
}
}
}
use of cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException in project perun by CESNET.
the class urn_perun_user_attribute_def_virt_studentIdentifiers method processAddUserExtSource.
/**
* Set userExtSource with attributes for member's user if not exists.
*
* @param sess Perun session
* @param group from which appropriate attributes will be obtained
* @param member for which the xtSource with attributes will be processed
*/
private void processAddUserExtSource(PerunSessionImpl sess, Group group, Member member) {
User user = sess.getPerunBl().getUsersManagerBl().getUserByMember(sess, member);
Attribute organizationScope = tryGetAttribute(sess, group, A_G_D_organizationScopeFriendlyName);
if (organizationScope == null || organizationScope.getValue() == null) {
return;
}
Attribute organizationNamespace = this.tryGetAttribute(sess, group, A_G_D_organizationNamespaceFriendlyName);
if (organizationNamespace == null || organizationNamespace.getValue() == null) {
return;
}
Attribute userLoginID = tryGetAttribute(sess, user, A_U_D_loginNamespaceFriendlyNamePrefix + organizationNamespace.valueAsString());
if (userLoginID == null || userLoginID.getValue() == null) {
return;
}
ExtSource extSource = tryGetExtSource(sess, organizationScope.valueAsString());
// Create and set userExtSource if not exists
try {
sess.getPerunBl().getUsersManagerBl().getUserExtSourceByExtLogin(sess, extSource, userLoginID.valueAsString());
} catch (UserExtSourceNotExistsException e) {
UserExtSource ues = new UserExtSource(extSource, userLoginID.valueAsString());
try {
ues = sess.getPerunBl().getUsersManagerBl().addUserExtSource(sess, user, ues);
} catch (UserExtSourceExistsException userExtSourceExistsException) {
// Should not happened
throw new InternalErrorException(e);
}
Attribute schacHomeOrganization = tryGetAttribute(sess, ues, A_UES_D_schacHomeOrganizationFriendlyName);
Attribute eduPersonScopedAffiliation = tryGetAttribute(sess, ues, A_UES_D_eduPersonScopedAffiliationFriendlyName);
Attribute schacPersonalUniqueCode = tryGetAttribute(sess, ues, A_UES_D_schacPersonalUniqueCodeFriendlyName);
schacHomeOrganization.setValue(organizationScope.valueAsString());
eduPersonScopedAffiliation.setValue(affiliationPrefix + organizationScope.valueAsString());
List<String> spucValue = new ArrayList<>();
spucValue.add(studentIdentifiersValuePrefix + organizationScope.valueAsString() + ":" + userLoginID.valueAsString());
schacPersonalUniqueCode.setValue(spucValue);
try {
sess.getPerunBl().getAttributesManagerBl().setAttributes(sess, ues, Arrays.asList(schacHomeOrganization, eduPersonScopedAffiliation, schacPersonalUniqueCode));
} catch (WrongAttributeValueException | WrongAttributeAssignmentException | WrongReferenceAttributeValueException ex) {
// Should not happened
throw new InternalErrorException(ex);
}
}
}
use of cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException in project perun by CESNET.
the class GroupsManagerBlImpl method categorizeMembersForLightweightSynchronization.
/**
* For lightweight synchronization prepare candidate to add and members to remove.
*
* Get all subjects from loginSource and try to find users in Perun by their login and this ExtSource.
* If found, look if this user is already in synchronized Group. If yes skip him, if not add him to candidateToAdd
* If not found in vo of the group, skip him.
*
* Rest of former members need to be add to membersToRemove to remove them from group.
*
* This method fill 2 member structures which get as parameters:
* 1. candidateToAdd - New members of the group
* 2. membersToRemove - Former members who are not in synchronized ExtSource now
*
* @param sess
* @param group
* @param loginSource
* @param memberSource
* @param groupMembers
* @param candidatesToAdd
* @param membersToRemove
* @param skippedMembers
*/
private void categorizeMembersForLightweightSynchronization(PerunSession sess, Group group, ExtSource loginSource, ExtSource memberSource, List<RichMember> groupMembers, List<Candidate> candidatesToAdd, List<RichMember> membersToRemove, List<String> skippedMembers) {
// Get subjects from loginSource
List<Map<String, String>> subjects = getSubjectsFromExtSource(sess, loginSource, group);
// Prepare structure of userIds with richMembers to better work with actual members
Map<Integer, RichMember> idsOfUsersInGroup = new HashMap<>();
for (RichMember richMember : groupMembers) {
idsOfUsersInGroup.put(richMember.getUserId(), richMember);
}
// try to find users by login and loginSource
for (Map<String, String> subjectFromLoginSource : subjects) {
if (subjectFromLoginSource == null) {
log.error("Null value in the subjects list. Skipping.");
continue;
}
String login = subjectFromLoginSource.get("login");
// Skip subjects, which doesn't have login
if (login == null || login.isEmpty()) {
log.debug("Subject {} doesn't contain attribute login, skipping.", subjectFromLoginSource);
skippedMembers.add("MemberEntry:[" + subjectFromLoginSource + "] was skipped because login is missing");
continue;
}
// try to find user from perun by login and member extSource (need to use memberSource because loginSource is not saved by synchronization)
User user = null;
List<UserExtSource> userExtSources = new ArrayList<>();
try {
UserExtSource userExtSource = getPerunBl().getUsersManagerBl().getUserExtSourceByExtLogin(sess, memberSource, login);
userExtSources.add(userExtSource);
} catch (UserExtSourceNotExistsException e) {
// skipping, this extSource does not exist and thus won't be in the list
}
Vo groupVo = getVo(sess, group);
List<UserExtSource> additionalUserExtSources = Utils.extractAdditionalUserExtSources(sess, subjectFromLoginSource).stream().map(RichUserExtSource::asUserExtSource).collect(toList());
userExtSources.addAll(additionalUserExtSources);
for (UserExtSource source : userExtSources) {
try {
user = getPerunBl().getUsersManagerBl().getUserByUserExtSource(sess, source);
// check if user is already member of group's vo
if (getPerunBl().getUsersManagerBl().getVosWhereUserIsMember(sess, user).contains(groupVo)) {
if (idsOfUsersInGroup.containsKey(user.getId())) {
// we can skip this one, because he is already in group, and remove him from the map
// but first we need to also validate him if he was disabled before (invalidate and then validate)
RichMember richMember = idsOfUsersInGroup.get(user.getId());
if (richMember != null && Status.DISABLED.equals(richMember.getStatus())) {
getPerunBl().getMembersManagerBl().invalidateMember(sess, richMember);
try {
getPerunBl().getMembersManagerBl().validateMember(sess, richMember);
} catch (WrongAttributeValueException | WrongReferenceAttributeValueException e) {
log.info("Switching member id {} into INVALID state from DISABLED, because there was problem with attributes {}.", richMember.getId(), e);
}
}
idsOfUsersInGroup.remove(user.getId());
} else {
// he is not yet in group, so we need to create a candidate
Candidate candidate = new Candidate(user, source);
// for lightweight synchronization we want to skip all update of attributes
candidate.setAttributes(new HashMap<>());
candidatesToAdd.add(candidate);
}
break;
}
} catch (UserNotExistsException e) {
// skip because the user from this ExtSource does not exist so we can continue
}
}
// If user not found in group's vo, skip him and log it
if (user == null) {
log.debug("Subject {} with login {} was skipped during lightweight synchronization of group {} because he is not in vo of the group yet.", subjectFromLoginSource, login, group);
}
}
// Rest of them need to be removed
membersToRemove.addAll(idsOfUsersInGroup.values());
}
use of cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException in project perun by CESNET.
the class UsersManagerBlImpl method checkThatCandidateUesesDontExist.
/**
* Check that none of the given userExtSources exist. If so, the UserExtSourceExistsException
* is thrown.
*
* @param sess session
* @param candidate candidate
* @throws UserExtSourceExistsException if some of the given userExtSources already exist.
*/
private void checkThatCandidateUesesDontExist(PerunSession sess, Candidate candidate) throws UserExtSourceExistsException {
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 user = getPerunBl().getUsersManagerBl().getUserByExtSourceNameAndExtLogin(sess, ues.getExtSource().getName(), ues.getLogin());
if (user != null) {
throw new UserExtSourceExistsException(ues);
}
} catch (UserExtSourceNotExistsException | UserNotExistsException | ExtSourceNotExistsException e) {
// This is OK, we don't want it to exist
}
}
}
}
use of cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException in project perun by CESNET.
the class UsersManagerBlImpl method addMissingCandidatesUes.
/**
* For given user, set user extsources from candiate, which have not been set before.
*
* @param sess session
* @param user user
* @param candidate candidate to take userExtSources
*/
private void addMissingCandidatesUes(PerunSession sess, User user, Candidate candidate) {
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, e1);
}
} catch (UserExtSourceExistsException e1) {
throw new ConsistencyErrorException("Updating login of userExtSource to value which already" + " exists: " + userExtSource, e1);
}
}
}
}
Aggregations