use of cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException in project perun by CESNET.
the class GroupsManagerBlImpl method reactivateMember.
@Override
public void reactivateMember(PerunSession sess, Member member, Group group) throws MemberNotExistsException {
// only direct membership has own status and expiration, indirect membership get it's status and expiration from origin group
if (!isDirectGroupMember(sess, group, member))
throw new MemberNotExistsException("Member does not belong to this group");
validateMemberInGroup(sess, member, group);
// Get current membershipExpiration date attribute
Attribute membershipExpirationAttribute = getMemberExpiration(sess, member, group);
// Set new value of the membershipExpiration for the member
membershipExpirationAttribute.setValue(null);
try {
getPerunBl().getAttributesManagerBl().setAttribute(sess, member, group, membershipExpirationAttribute);
} catch (WrongAttributeValueException e) {
throw new InternalErrorException("Wrong value: " + membershipExpirationAttribute.getValue(), e);
} catch (WrongReferenceAttributeValueException | WrongAttributeAssignmentException e) {
throw new InternalErrorException(e);
} catch (MemberGroupMismatchException e) {
throw new ConsistencyErrorException(e);
}
try {
extendMembershipInGroup(sess, member, group);
} catch (ExtendMembershipException ex) {
// This exception should not be thrown for null membershipExpiration attribute
throw new InternalErrorException(ex);
}
}
use of cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException in project perun by CESNET.
the class MembersManagerBlImpl method manageMembershipExpiration.
/**
* More info on https://wiki.metacentrum.cz/wiki/VO_managers%27s_manual
*
* If setAttributeValue is true, then store the membership expiration date into the attribute, otherwise
* return object pair containing true/false if the member can be extended and date specifying exact date of the new expiration
*
* @param sess sess
* @param member member to check / set membership expiration
* @param setAttributeValue TRUE = set new membership expiration date / FALSE = do NOT set new expiration date (just calculate it)
* @param throwExceptions TRUE = throw exception / FALSE = return false when member can't extend membership
* @return Pair with result in left side (can / can't extend membership) and Date in right side telling new membership expiration date
*
* @throws InternalErrorException
* @throws ExtendMembershipException When member can't extend membership and throwException is set to true.
*/
protected Pair<Boolean, Date> manageMembershipExpiration(PerunSession sess, Member member, boolean setAttributeValue, boolean throwExceptions) throws ExtendMembershipException {
// Check if the VO has set membershipExpirationRules attribute
LinkedHashMap<String, String> membershipExpirationRules;
Vo vo;
Attribute membershipExpirationRulesAttribute;
try {
vo = getPerunBl().getVosManagerBl().getVoById(sess, member.getVoId());
membershipExpirationRulesAttribute = getPerunBl().getAttributesManagerBl().getAttribute(sess, vo, MembersManager.membershipExpirationRulesAttributeName);
membershipExpirationRules = membershipExpirationRulesAttribute.valueAsMap();
// If attribute was not filled, then silently exit
if (membershipExpirationRules == null)
return new Pair<>(true, null);
} catch (VoNotExistsException e) {
throw new ConsistencyErrorException("Member " + member + " of non-existing VO id=" + member.getVoId());
} catch (AttributeNotExistsException e) {
// There is no attribute definition for membership expiration rules.
return new Pair<>(true, null);
} catch (WrongAttributeAssignmentException e) {
throw new InternalErrorException("Shouldn't happen.");
}
// Get user LOA
String memberLoa = null;
try {
User user = getPerunBl().getUsersManagerBl().getUserByMember(sess, member);
Attribute loa = getPerunBl().getAttributesManagerBl().getAttribute(sess, user, AttributesManager.NS_USER_ATTR_VIRT + ":loa");
memberLoa = Integer.toString((Integer) loa.getValue());
} catch (AttributeNotExistsException e) {
// Ignore, will be probably set further
} catch (WrongAttributeAssignmentException e) {
throw new InternalErrorException(e);
}
// Get current membershipExpiration date
Attribute membershipExpirationAttribute;
try {
membershipExpirationAttribute = getPerunBl().getAttributesManagerBl().getAttribute(sess, member, EXPIRATION);
} catch (AttributeNotExistsException e) {
throw new ConsistencyErrorException("Attribute: " + AttributesManager.NS_MEMBER_ATTR_DEF + ":membershipExpiration" + " must be defined in order to use membershipExpirationRules");
} catch (WrongAttributeAssignmentException e) {
throw new InternalErrorException(e);
}
boolean isServiceUser;
try {
User user = getPerunBl().getUsersManagerBl().getUserById(sess, member.getUserId());
isServiceUser = user.isServiceUser();
} catch (UserNotExistsException ex) {
throw new ConsistencyErrorException("User must exists for " + member + " when checking expiration rules.");
}
// and are not service users
if (membershipExpirationRules.get(AbstractMembershipExpirationRulesModule.membershipDoNotExtendLoaKeyName) != null && membershipExpirationAttribute.getValue() != null && !isServiceUser) {
if (memberLoa == null) {
// Member doesn't have LOA defined and LOA is required for extension, so do not extend membership.
log.warn("Member {} doesn't have LOA defined, but 'doNotExtendLoa' option is set for VO id {}.", member, member.getVoId());
if (throwExceptions) {
throw new ExtendMembershipException(ExtendMembershipException.Reason.NOUSERLOA, "Member " + member + " doesn't have LOA defined, but 'doNotExtendLoa' option is set for VO id " + member.getVoId() + ".");
} else {
return new Pair<>(false, null);
}
}
String[] doNotExtendLoas = membershipExpirationRules.get(AbstractMembershipExpirationRulesModule.membershipDoNotExtendLoaKeyName).split(",");
for (String doNotExtendLoa : doNotExtendLoas) {
if (doNotExtendLoa.equals(memberLoa)) {
// Member has LOA which is not allowed for extension
if (throwExceptions) {
throw new ExtendMembershipException(ExtendMembershipException.Reason.INSUFFICIENTLOAFOREXTENSION, "Member " + member + " doesn't have required LOA for VO id " + member.getVoId() + ".");
} else {
return new Pair<>(false, null);
}
}
}
}
LocalDate localDate = LocalDate.now();
// Does the user have expired membership, if yes, then for canExtendMembership return true
if (!setAttributeValue && membershipExpirationAttribute.getValue() != null) {
LocalDate currentMemberExpiration = LocalDate.parse((String) membershipExpirationAttribute.getValue(), DateTimeFormatter.ISO_LOCAL_DATE);
if (localDate.isAfter(currentMemberExpiration)) {
return new Pair<>(true, null);
}
}
String period = null;
// Default extension
if (membershipExpirationRules.get(AbstractMembershipExpirationRulesModule.membershipPeriodKeyName) != null) {
period = membershipExpirationRules.get(AbstractMembershipExpirationRulesModule.membershipPeriodKeyName);
}
// Do we extend particular LoA? Attribute syntax LoA|[period][.]
if (membershipExpirationRules.get(AbstractMembershipExpirationRulesModule.membershipPeriodLoaKeyName) != null) {
// Which period
String[] membershipPeriodLoa = membershipExpirationRules.get(AbstractMembershipExpirationRulesModule.membershipPeriodLoaKeyName).split("\\|");
String loa = membershipPeriodLoa[0];
String periodLoa = membershipPeriodLoa[1];
// Does the user have this LoA?
if (loa.equals(memberLoa)) {
if (periodLoa.endsWith(".")) {
// If period ends with ., then we do not allow extension for users with particular LoA if they are already members
if (membershipExpirationAttribute.getValue() != null) {
if (throwExceptions) {
throw new ExtendMembershipException(ExtendMembershipException.Reason.INSUFFICIENTLOAFOREXTENSION, "Member " + member + " doesn't have required LOA for VO id " + member.getVoId() + ".");
} else {
return new Pair<>(false, null);
}
}
// remove dot from the end of the string
period = periodLoa.substring(0, periodLoa.length() - 1);
} else {
period = periodLoa;
}
}
}
// Do we extend for x months or for static date?
if (period != null) {
if (period.startsWith("+")) {
if (!isMemberInGracePeriod(membershipExpirationRules, (String) membershipExpirationAttribute.getValue())) {
if (throwExceptions) {
throw new ExtendMembershipException(ExtendMembershipException.Reason.OUTSIDEEXTENSIONPERIOD, (String) membershipExpirationAttribute.getValue(), "Member " + member + " cannot extend because we are outside grace period for VO id " + member.getVoId() + ".");
} else {
return new Pair<>(false, null);
}
}
// extend calendar by given period
try {
localDate = Utils.extendDateByPeriod(localDate, period);
} catch (InternalErrorException e) {
throw new InternalErrorException("Wrong format of period in VO membershipExpirationRules attribute.", e);
}
} else {
// We will extend to particular date
// 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 VO 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> fieldAmount;
fieldAmount = Utils.prepareGracePeriodDate(m);
LocalDate gracePeriodDate = localDate.minus(fieldAmount.getLeft(), fieldAmount.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 (!setAttributeValue && membershipExpirationAttribute.getValue() != null) {
LocalDate currentMemberExpiration = LocalDate.parse((String) membershipExpirationAttribute.getValue(), DateTimeFormatter.ISO_LOCAL_DATE);
currentMemberExpiration = currentMemberExpiration.minus(fieldAmount.getLeft(), fieldAmount.getRight());
// if today is before that time, user can extend his period
if (currentMemberExpiration.isAfter(LocalDate.now())) {
if (throwExceptions) {
throw new ExtendMembershipException(ExtendMembershipException.Reason.OUTSIDEEXTENSIONPERIOD, (String) membershipExpirationAttribute.getValue(), "Member " + member + " cannot extend because we are outside grace period for VO id " + member.getVoId() + ".");
} else {
return new Pair<>(false, null);
}
}
}
}
}
}
// Set new value of the membershipExpiration for the member
if (setAttributeValue) {
membershipExpirationAttribute.setValue(localDate.toString());
try {
getPerunBl().getAttributesManagerBl().setAttribute(sess, member, membershipExpirationAttribute);
} catch (WrongAttributeValueException e) {
throw new InternalErrorException("Wrong value: " + membershipExpirationAttribute.getValue(), e);
} catch (WrongReferenceAttributeValueException | WrongAttributeAssignmentException e) {
throw new InternalErrorException(e);
}
}
}
return new Pair<>(true, Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()));
}
use of cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException in project perun by CESNET.
the class MembersManagerBlImpl method getNewExtendMembership.
@Override
public Date getNewExtendMembership(PerunSession sess, Vo vo, String loa) throws ExtendMembershipException {
// Check if the VO has set membershipExpirationRules attribute
LinkedHashMap<String, String> membershipExpirationRules;
Attribute membershipExpirationRulesAttribute;
try {
membershipExpirationRulesAttribute = getPerunBl().getAttributesManagerBl().getAttribute(sess, vo, MembersManager.membershipExpirationRulesAttributeName);
membershipExpirationRules = membershipExpirationRulesAttribute.valueAsMap();
// If attribute was not filled, then silently exit with null
if (membershipExpirationRules == null)
return null;
} catch (AttributeNotExistsException e) {
// No rules set, so leave it as it is
return null;
} catch (WrongAttributeAssignmentException e) {
throw new InternalErrorException("Shouldn't happen.");
}
// Which LOA we won't extend? This is applicable only for members who have already set expiration from the previous period
if (membershipExpirationRules.get(AbstractMembershipExpirationRulesModule.membershipDoNotExtendLoaKeyName) != null) {
String[] doNotExtendLoas = membershipExpirationRules.get(AbstractMembershipExpirationRulesModule.membershipDoNotExtendLoaKeyName).split(",");
for (String doNotExtendLoa : doNotExtendLoas) {
if (doNotExtendLoa.equals(loa)) {
// LOA provided is not allowed for extension
throw new ExtendMembershipException(ExtendMembershipException.Reason.INSUFFICIENTLOA, "Provided LoA " + loa + " doesn't have required level for VO id " + vo.getId() + ".");
}
}
}
LocalDate localDate = LocalDate.now();
String period = null;
// Default extension
if (membershipExpirationRules.get(AbstractMembershipExpirationRulesModule.membershipPeriodKeyName) != null) {
period = membershipExpirationRules.get(AbstractMembershipExpirationRulesModule.membershipPeriodKeyName);
}
// Do we extend particular LoA? Attribute syntax LoA|[period][.]
if (membershipExpirationRules.get(AbstractMembershipExpirationRulesModule.membershipPeriodLoaKeyName) != null) {
// Which period
String[] membershipPeriodLoa = membershipExpirationRules.get(AbstractMembershipExpirationRulesModule.membershipPeriodLoaKeyName).split("\\|");
String membershipLoa = membershipPeriodLoa[0];
String periodLoa = membershipPeriodLoa[1];
// Does the user have this LoA?
if (membershipLoa.equals(loa)) {
period = periodLoa;
}
}
// Do we extend for x months or for static date?
if (period != null) {
if (period.startsWith("+")) {
try {
localDate = Utils.extendDateByPeriod(localDate, period);
} catch (InternalErrorException e) {
throw new InternalErrorException("Wrong format of period in VO membershipExpirationRules attribute.", e);
}
} else {
// We will extend to particular date
// 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 VO 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()) {
LocalDate gracePeriodDate;
try {
Pair<Integer, TemporalUnit> fieldAmount = Utils.prepareGracePeriodDate(m);
gracePeriodDate = localDate.minus(fieldAmount.getLeft(), fieldAmount.getRight());
} catch (InternalErrorException e) {
throw new InternalErrorException("Wrong format of gracePeriod in VO membershipExpirationRules attribute. gracePeriod: " + gracePeriod);
}
// 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);
}
}
}
}
}
return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
}
Aggregations