use of cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException in project perun by CESNET.
the class ExpirationNotifScheduler method performAutoExtension.
/**
* For given vos, perform auto extension of soon expiring members (in a month or less).
*
* @param vos vos where the auto extension will be performed
*/
private void performAutoExtension(Collection<Vo> vos) {
LocalDate nextMonth = getCurrentLocalDate().plusMonths(1);
List<Member> soonExpiringMembers = perun.getSearcherBl().getMembersByExpiration(sess, "<=", nextMonth);
Map<Integer, Attribute> expAttrsByVos = vos.stream().collect(toMap(Vo::getId, this::getVoExpirationAttribute));
for (Member member : soonExpiringMembers) {
Attribute voExpAttribute = expAttrsByVos.get(member.getVoId());
if (canBeAutoExtended(member, voExpAttribute)) {
try {
perun.getMembersManagerBl().extendMembership(sess, member);
} catch (ExtendMembershipException e) {
log.error("Failed to auto-extend member: {}, exception: {}", member, e);
}
}
}
}
use of cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException in project perun by CESNET.
the class IT4Innovations method approveApplication.
/**
* Add approved VO members into e-INFRA CZ VO.
*/
@Override
public Application approveApplication(PerunSession session, Application app) throws WrongReferenceAttributeValueException, WrongAttributeValueException {
PerunBl perun = (PerunBl) session.getPerun();
User user = app.getUser();
Vo vo = app.getVo();
// For INITIAL VO APPLICATIONS
if (Application.AppType.INITIAL.equals(app.getType()) && app.getGroup() == null) {
try {
Vo einfraVo = perun.getVosManagerBl().getVoByShortName(session, "e-infra.cz");
Member einfraMember = perun.getMembersManagerBl().createMember(session, einfraVo, user);
log.debug("{} member added to \"e-INFRA CZ\": {}", vo.getName(), einfraMember);
perun.getMembersManagerBl().validateMemberAsync(session, einfraMember);
} catch (VoNotExistsException e) {
log.warn("e-INFRA CZ VO doesn't exists, {} member can't be added into it.", vo.getName());
} catch (AlreadyMemberException ignore) {
// user is already in e-INFRA CZ
} catch (ExtendMembershipException e) {
// can't be member of e-INFRA CZ, shouldn't happen
log.error("{} member can't be added to \"e-INFRA CZ\": {}", vo.getName(), e);
}
}
return app;
}
use of cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException in project perun by CESNET.
the class LifeScienceHostelRIAcc method approveApplication.
/**
* Create proper UserExtSource
*/
@Override
public Application approveApplication(PerunSession session, Application app) throws PrivilegeException, GroupNotExistsException, MemberNotExistsException, ExternallyManagedException, WrongReferenceAttributeValueException, WrongAttributeValueException, RegistrarException, ExtSourceNotExistsException, AttributeNotExistsException, WrongAttributeAssignmentException, VoNotExistsException, ExtendMembershipException, AlreadyMemberException {
PerunBl perun = (PerunBl) session.getPerun();
User user = app.getUser();
if (user != null) {
// Create UES for user
Attribute userLogin = perun.getAttributesManagerBl().getAttribute(session, user, AttributesManager.NS_USER_ATTR_DEF + ":" + LOGIN_NAMESPACE);
if (userLogin != null && userLogin.getValue() != null) {
ExtSource extSource = perun.getExtSourcesManagerBl().getExtSourceByName(session, LS_HOSTEL_EXT_SOURCE_NAME);
String login = userLogin.valueAsString();
UserExtSource ues = new UserExtSource(extSource, login + LS_HOSTEL_SCOPE);
ues.setLoa(0);
try {
perun.getUsersManagerBl().addUserExtSource(session, user, ues);
} catch (UserExtSourceExistsException ex) {
// this is OK
}
}
if (Application.AppType.INITIAL.equals(app.getType())) {
try {
Vo vo = perun.getVosManagerBl().getVoByShortName(session, VO_SHORTNAME);
Member member = perun.getMembersManagerBl().createMember(session, vo, user);
log.debug("LS Hostel member added to the main VO Lifescience {}", member);
} catch (VoNotExistsException e) {
log.warn("VO: " + VO_SHORTNAME + " not exists, can't add member into it.");
} catch (AlreadyMemberException ignore) {
// user is already in lifescience
} catch (ExtendMembershipException e) {
// can't be member of lifescience, shouldn't happen
log.error("LS Hostel member can't be added to VO: " + VO_SHORTNAME, e);
}
}
// User doesn't have login - don't set UES
}
return app;
}
use of cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException in project perun by CESNET.
the class GroupsManagerBlImpl method addMissingMemberWhileSynchronization.
/**
* Get new candidate and add him to the Group.
*
* If Candidate can't be added to Group, skip him and add this information to skippedMembers list.
*
* When creating new member from Candidate, if user already exists, merge his attributes,
* if attribute exists in list of overwriteUserAttributesList, update it instead of merging.
*
* This method runs in separate transaction.
*
* @param sess perun session
* @param group to be synchronized
* @param candidate new member (candidate)
* @param overwriteUserAttributesList list of attributes to be updated for user if found
* @param mergeMemberAttributesList list of attributes to be merged for member if found
* @param skippedMembers list of not successfully synchronized members
*/
public void addMissingMemberWhileSynchronization(PerunSession sess, Group group, Candidate candidate, List<String> overwriteUserAttributesList, List<String> mergeMemberAttributesList, List<String> skippedMembers) {
Member member;
try {
// Check if the member is already in the VO (just not in the group)
member = getPerunBl().getMembersManagerBl().getMemberByUserExtSources(sess, getPerunBl().getGroupsManagerBl().getVo(sess, group), candidate.getUserExtSources());
// member exists - update attributes
RichMember memberToUpdate = getPerunBl().getMembersManagerBl().getRichMember(sess, member);
updateExistingMemberWhileSynchronization(sess, group, candidate, memberToUpdate, overwriteUserAttributesList, mergeMemberAttributesList, new ArrayList<>());
} catch (MemberNotExistsException e) {
try {
// We have new member (candidate), so create him using synchronous createMember (and overwrite chosen user attributes)
member = getPerunBl().getMembersManagerBl().createMemberSync(sess, getPerunBl().getGroupsManagerBl().getVo(sess, group), candidate, null, overwriteUserAttributesList);
log.info("Group synchronization {}: New member id {} created during synchronization.", group, member.getId());
} catch (AlreadyMemberException e1) {
// Check if the member is already in the VO (just not in the group)
try {
member = getPerunBl().getMembersManagerBl().getMemberByUserExtSources(sess, getPerunBl().getGroupsManagerBl().getVo(sess, group), candidate.getUserExtSources());
// member exists - update attribute
RichMember memberToUpdate = getPerunBl().getMembersManagerBl().getRichMember(sess, member);
updateExistingMemberWhileSynchronization(sess, group, candidate, memberToUpdate, overwriteUserAttributesList, mergeMemberAttributesList, new ArrayList<>());
} catch (Exception e2) {
// Something is still wrong, thrown consistency exception
throw new ConsistencyErrorException("Trying to add existing member (it is not possible to get him by userExtSource even if is also not possible to create him in DB)!");
}
} catch (AttributeValueException e1) {
log.warn("Can't create member from candidate {} due to attribute value exception {}.", candidate, e1);
skippedMembers.add("MemberEntry:[" + candidate + "] was skipped because there was problem when createing member from candidate: Exception: " + e1.getName() + " => '" + e1.getMessage() + "'");
return;
} catch (ExtendMembershipException e1) {
log.warn("Can't create member from candidate {} due to membership expiration exception {}.", candidate, e1);
skippedMembers.add("MemberEntry:[" + candidate + "] was skipped because membership expiration: Exception: " + e1.getName() + " => " + e1.getMessage() + "]");
return;
}
}
try {
// Add the member to the group
if (!group.getName().equals(VosManager.MEMBERS_GROUP)) {
// Do not add members to the generic members group
try {
getPerunBl().getGroupsManagerBl().addMember(sess, group, member);
} catch (GroupNotExistsException ex) {
// Shouldn't happen, group should always exist
throw new ConsistencyErrorException(ex);
}
}
log.info("Group synchronization {}: New member id {} added.", group, member.getId());
} catch (AlreadyMemberException e) {
// This part is ok, it means someone add member before synchronization ends, log it and skip this member
log.debug("Member {} was added to group {} before adding process. Skip this member.", member, group);
return;
} catch (AttributeValueException e) {
// There is a problem with attribute value, so set INVALID status of the member
getPerunBl().getMembersManagerBl().invalidateMember(sess, member);
}
// Try to validate member
updateMemberStatus(sess, member);
}
use of cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException in project perun by CESNET.
the class GroupsManagerBlImpl method extendForStaticDate.
/**
* Returns localDate extended to given date
*
* @param localDate localDate
* @param period period
* @param membershipExpirationRules rules used in check
* @param membershipExpirationAttribute attributes used to check
* @param member member that is checked
* @param group group that is checked
* @return localDate localDate extended to given date
* @throws InternalErrorException internal error
* @throws ExtendMembershipException when cannot extend
*/
private LocalDate extendForStaticDate(LocalDate localDate, String period, LinkedHashMap<String, String> membershipExpirationRules, Attribute membershipExpirationAttribute, Member member, Group group) throws ExtendMembershipException {
// Parse date
Pattern p = Pattern.compile("([0-9]+).([0-9]+).");
Matcher m = p.matcher(period);
if (!m.matches()) {
throw new InternalErrorException("Wrong format of period in Group membershipExpirationRules attribute. Period: " + period);
}
localDate = Utils.getClosestExpirationFromStaticDate(m);
// Is there a grace period?
if (membershipExpirationRules.get(AbstractMembershipExpirationRulesModule.membershipGracePeriodKeyName) != null) {
String gracePeriod = membershipExpirationRules.get(AbstractMembershipExpirationRulesModule.membershipGracePeriodKeyName);
// If the extension is requested in period-gracePeriod then extend to next period
// Get the value of the grace period
p = Pattern.compile("([0-9]+)([dmy]?)");
m = p.matcher(gracePeriod);
if (m.matches()) {
Pair<Integer, TemporalUnit> amountField = Utils.prepareGracePeriodDate(m);
LocalDate gracePeriodDate = localDate.minus(amountField.getLeft(), amountField.getRight());
// Check if we are in grace period
if (gracePeriodDate.isBefore(LocalDate.now())) {
// We are in grace period, so extend to the next period
localDate = localDate.plusYears(1);
}
// If we do not need to set the attribute value, only check if the current member's expiration time is not in grace period
if (membershipExpirationAttribute.getValue() != null) {
LocalDate currentMemberExpirationDate = LocalDate.parse((String) membershipExpirationAttribute.getValue()).minus(amountField.getLeft(), amountField.getRight());
// if today is before that time, user can extend his period
if (currentMemberExpirationDate.isAfter(LocalDate.now())) {
throw new ExtendMembershipException(ExtendMembershipException.Reason.OUTSIDEEXTENSIONPERIOD, (String) membershipExpirationAttribute.getValue(), "Member " + member + " cannot extend because we are outside grace period for GROUP id " + group.getId() + ".");
}
}
}
}
return localDate;
}
Aggregations