Search in sources :

Example 16 with VoNotExistsException

use of cz.metacentrum.perun.core.api.exceptions.VoNotExistsException in project perun by CESNET.

the class Einfracz 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;
}
Also used : User(cz.metacentrum.perun.core.api.User) PerunBl(cz.metacentrum.perun.core.bl.PerunBl) Vo(cz.metacentrum.perun.core.api.Vo) AlreadyMemberException(cz.metacentrum.perun.core.api.exceptions.AlreadyMemberException) Member(cz.metacentrum.perun.core.api.Member) ExtendMembershipException(cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException) VoNotExistsException(cz.metacentrum.perun.core.api.exceptions.VoNotExistsException)

Example 17 with VoNotExistsException

use of cz.metacentrum.perun.core.api.exceptions.VoNotExistsException in project perun by CESNET.

the class LifeScienceHostelRI 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;
}
Also used : UserExtSourceExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceExistsException) User(cz.metacentrum.perun.core.api.User) Attribute(cz.metacentrum.perun.core.api.Attribute) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) PerunBl(cz.metacentrum.perun.core.bl.PerunBl) Vo(cz.metacentrum.perun.core.api.Vo) AlreadyMemberException(cz.metacentrum.perun.core.api.exceptions.AlreadyMemberException) ExtSource(cz.metacentrum.perun.core.api.ExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) Member(cz.metacentrum.perun.core.api.Member) ExtendMembershipException(cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException) VoNotExistsException(cz.metacentrum.perun.core.api.exceptions.VoNotExistsException)

Example 18 with VoNotExistsException

use of cz.metacentrum.perun.core.api.exceptions.VoNotExistsException in project perun by CESNET.

the class MembersManagerBlImpl method setSponsorshipForMember.

@Override
public Member setSponsorshipForMember(PerunSession session, Member sponsoredMember, User sponsor, LocalDate validityTo) throws AlreadySponsoredMemberException, UserNotInRoleException, AlreadySponsorException {
    if (sponsoredMember.isSponsored()) {
        throw new AlreadySponsoredMemberException(sponsoredMember + " is already sponsored member!");
    }
    // Test if Vo exists and sponsor has the right role in it
    Vo membersVo;
    try {
        membersVo = getPerunBl().getVosManagerBl().getVoById(session, sponsoredMember.getVoId());
    } catch (VoNotExistsException ex) {
        throw new ConsistencyErrorException("Vo for " + sponsoredMember + " not exists!");
    }
    if (!getPerunBl().getVosManagerBl().isUserInRoleForVo(session, sponsor, Role.SPONSOR, membersVo, true)) {
        throw new UserNotInRoleException("User " + sponsor.getId() + " is not in role SPONSOR for VO " + membersVo.getId());
    }
    // set member to be sponsored
    sponsoredMember = getMembersManagerImpl().setSponsorshipForMember(session, sponsoredMember, sponsor, validityTo);
    getPerunBl().getAuditer().log(session, new SponsoredMemberSet(sponsoredMember));
    getPerunBl().getAuditer().log(session, new SponsorshipEstablished(sponsoredMember, sponsor, validityTo));
    // remove expiration and validate member
    try {
        AttributeDefinition expiration = getPerunBl().getAttributesManagerBl().getAttributeDefinition(session, EXPIRATION);
        getPerunBl().getAttributesManagerBl().removeAttribute(session, sponsoredMember, expiration);
    } catch (WrongAttributeAssignmentException | AttributeNotExistsException | WrongAttributeValueException | WrongReferenceAttributeValueException ex) {
        throw new InternalErrorException("cannot remove expiration date for sponsored member " + sponsoredMember.getId(), ex);
    }
    try {
        validateMember(session, sponsoredMember);
    } catch (WrongReferenceAttributeValueException | WrongAttributeValueException ex) {
        throw new InternalErrorException("cannot validate sponsored member " + sponsoredMember.getId(), ex);
    }
    return sponsoredMember;
}
Also used : SponsoredMemberSet(cz.metacentrum.perun.audit.events.MembersManagerEvents.SponsoredMemberSet) ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) AlreadySponsoredMemberException(cz.metacentrum.perun.core.api.exceptions.AlreadySponsoredMemberException) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) VoNotExistsException(cz.metacentrum.perun.core.api.exceptions.VoNotExistsException) UserNotInRoleException(cz.metacentrum.perun.core.api.exceptions.UserNotInRoleException) SponsorshipEstablished(cz.metacentrum.perun.audit.events.MembersManagerEvents.SponsorshipEstablished) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) Vo(cz.metacentrum.perun.core.api.Vo) BanOnVo(cz.metacentrum.perun.core.api.BanOnVo) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Example 19 with VoNotExistsException

use of cz.metacentrum.perun.core.api.exceptions.VoNotExistsException in project perun by CESNET.

the class MembersManagerBlImpl method insertToMemberGroup.

public void insertToMemberGroup(PerunSession sess, Member member, Vo vo) throws AlreadyMemberException {
    // Insert member into the members group
    try {
        getPerunBl().getVosManagerBl().checkVoExists(sess, vo);
        Group g = getPerunBl().getGroupsManagerBl().getGroupByName(sess, vo, VosManager.MEMBERS_GROUP);
        getPerunBl().getGroupsManagerBl().addMemberToMembersGroup(sess, g, member);
    } catch (GroupNotExistsException | VoNotExistsException e) {
        throw new InternalErrorException(e);
    } catch (WrongAttributeValueException | WrongReferenceAttributeValueException e) {
        // Member is not valid, so he couldn't have truly required atributes, neither he couldn't have influence on user attributes
        throw new ConsistencyErrorException(e);
    }
}
Also used : Group(cz.metacentrum.perun.core.api.Group) ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) GroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException) ParentGroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.ParentGroupNotExistsException) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) VoNotExistsException(cz.metacentrum.perun.core.api.exceptions.VoNotExistsException)

Example 20 with VoNotExistsException

use of cz.metacentrum.perun.core.api.exceptions.VoNotExistsException 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()));
}
Also used : User(cz.metacentrum.perun.core.api.User) RichUser(cz.metacentrum.perun.core.api.RichUser) Attribute(cz.metacentrum.perun.core.api.Attribute) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) Matcher(java.util.regex.Matcher) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) LocalDate(java.time.LocalDate) ExtendMembershipException(cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException) VoNotExistsException(cz.metacentrum.perun.core.api.exceptions.VoNotExistsException) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) Vo(cz.metacentrum.perun.core.api.Vo) BanOnVo(cz.metacentrum.perun.core.api.BanOnVo) Pair(cz.metacentrum.perun.core.api.Pair) Pattern(java.util.regex.Pattern) ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) TemporalUnit(java.time.temporal.TemporalUnit) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Aggregations

VoNotExistsException (cz.metacentrum.perun.core.api.exceptions.VoNotExistsException)26 Vo (cz.metacentrum.perun.core.api.Vo)24 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)18 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)14 Member (cz.metacentrum.perun.core.api.Member)10 ArrayList (java.util.ArrayList)9 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)8 Attribute (cz.metacentrum.perun.core.api.Attribute)7 ExtSource (cz.metacentrum.perun.core.api.ExtSource)7 User (cz.metacentrum.perun.core.api.User)7 ExtendMembershipException (cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException)7 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)7 WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)7 Group (cz.metacentrum.perun.core.api.Group)6 AlreadyMemberException (cz.metacentrum.perun.core.api.exceptions.AlreadyMemberException)6 PerunBl (cz.metacentrum.perun.core.bl.PerunBl)6 MemberNotExistsException (cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException)5 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)5 Map (java.util.Map)5 BanOnVo (cz.metacentrum.perun.core.api.BanOnVo)4