Search in sources :

Example 6 with ExtendMembershipException

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);
            }
        }
    }
}
Also used : Attribute(cz.metacentrum.perun.core.api.Attribute) LocalDate(java.time.LocalDate) Member(cz.metacentrum.perun.core.api.Member) ExtendMembershipException(cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException)

Example 7 with ExtendMembershipException

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;
}
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 8 with ExtendMembershipException

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;
}
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 9 with ExtendMembershipException

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);
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) MemberNotExistsException(cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException) GroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException) ParentGroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.ParentGroupNotExistsException) AlreadyMemberException(cz.metacentrum.perun.core.api.exceptions.AlreadyMemberException) AttributeValueException(cz.metacentrum.perun.core.api.exceptions.AttributeValueException) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) RichMember(cz.metacentrum.perun.core.api.RichMember) Member(cz.metacentrum.perun.core.api.Member) RichMember(cz.metacentrum.perun.core.api.RichMember) GroupSynchronizationAlreadyRunningException(cz.metacentrum.perun.core.api.exceptions.GroupSynchronizationAlreadyRunningException) GroupExistsException(cz.metacentrum.perun.core.api.exceptions.GroupExistsException) RelationExistsException(cz.metacentrum.perun.core.api.exceptions.RelationExistsException) MemberAlreadyRemovedException(cz.metacentrum.perun.core.api.exceptions.MemberAlreadyRemovedException) ParserException(cz.metacentrum.perun.core.api.exceptions.ParserException) GroupMoveNotAllowedException(cz.metacentrum.perun.core.api.exceptions.GroupMoveNotAllowedException) ExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) RoleCannotBeManagedException(cz.metacentrum.perun.core.api.exceptions.RoleCannotBeManagedException) GroupResourceMismatchException(cz.metacentrum.perun.core.api.exceptions.GroupResourceMismatchException) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) MemberResourceMismatchException(cz.metacentrum.perun.core.api.exceptions.MemberResourceMismatchException) ExtSourceNotAssignedException(cz.metacentrum.perun.core.api.exceptions.ExtSourceNotAssignedException) ExtSourceUnsupportedOperationException(cz.metacentrum.perun.core.api.exceptions.ExtSourceUnsupportedOperationException) GroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException) MemberGroupMismatchException(cz.metacentrum.perun.core.api.exceptions.MemberGroupMismatchException) GroupNotAllowedToAutoRegistrationException(cz.metacentrum.perun.core.api.exceptions.GroupNotAllowedToAutoRegistrationException) ExtSourceAlreadyAssignedException(cz.metacentrum.perun.core.api.exceptions.ExtSourceAlreadyAssignedException) UserExtSourceExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceExistsException) GroupAlreadyAssignedException(cz.metacentrum.perun.core.api.exceptions.GroupAlreadyAssignedException) AlreadyAdminException(cz.metacentrum.perun.core.api.exceptions.AlreadyAdminException) GroupStructureSynchronizationAlreadyRunningException(cz.metacentrum.perun.core.api.exceptions.GroupStructureSynchronizationAlreadyRunningException) ResourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ResourceNotExistsException) AlreadyMemberException(cz.metacentrum.perun.core.api.exceptions.AlreadyMemberException) ExtendMembershipException(cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException) InvalidLoginException(cz.metacentrum.perun.core.api.exceptions.InvalidLoginException) UserExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) MemberNotExistsException(cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException) UserNotAdminException(cz.metacentrum.perun.core.api.exceptions.UserNotAdminException) LoginNotExistsException(cz.metacentrum.perun.core.api.exceptions.LoginNotExistsException) GroupAlreadyRemovedFromResourceException(cz.metacentrum.perun.core.api.exceptions.GroupAlreadyRemovedFromResourceException) ParseException(java.text.ParseException) MemberNotValidYetException(cz.metacentrum.perun.core.api.exceptions.MemberNotValidYetException) CandidateNotExistsException(cz.metacentrum.perun.core.api.exceptions.CandidateNotExistsException) ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) GroupNotAdminException(cz.metacentrum.perun.core.api.exceptions.GroupNotAdminException) GroupSynchronizationNotEnabledException(cz.metacentrum.perun.core.api.exceptions.GroupSynchronizationNotEnabledException) PasswordDeletionFailedException(cz.metacentrum.perun.core.api.exceptions.PasswordDeletionFailedException) ParentGroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.ParentGroupNotExistsException) GroupAlreadyRemovedException(cz.metacentrum.perun.core.api.exceptions.GroupAlreadyRemovedException) NotGroupMemberException(cz.metacentrum.perun.core.api.exceptions.NotGroupMemberException) PasswordOperationTimeoutException(cz.metacentrum.perun.core.api.exceptions.PasswordOperationTimeoutException) AttributeValueException(cz.metacentrum.perun.core.api.exceptions.AttributeValueException) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) VoNotExistsException(cz.metacentrum.perun.core.api.exceptions.VoNotExistsException) ExtSourceAlreadyRemovedException(cz.metacentrum.perun.core.api.exceptions.ExtSourceAlreadyRemovedException) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) GroupNotDefinedOnResourceException(cz.metacentrum.perun.core.api.exceptions.GroupNotDefinedOnResourceException) ExtendMembershipException(cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException)

Example 10 with ExtendMembershipException

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;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) TemporalUnit(java.time.temporal.TemporalUnit) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) LocalDate(java.time.LocalDate) ExtendMembershipException(cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException)

Aggregations

ExtendMembershipException (cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException)18 Attribute (cz.metacentrum.perun.core.api.Attribute)10 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)10 AlreadyMemberException (cz.metacentrum.perun.core.api.exceptions.AlreadyMemberException)9 VoNotExistsException (cz.metacentrum.perun.core.api.exceptions.VoNotExistsException)9 Member (cz.metacentrum.perun.core.api.Member)8 User (cz.metacentrum.perun.core.api.User)8 LocalDate (java.time.LocalDate)8 Vo (cz.metacentrum.perun.core.api.Vo)7 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)7 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)6 PerunBl (cz.metacentrum.perun.core.bl.PerunBl)6 ExtSource (cz.metacentrum.perun.core.api.ExtSource)5 UserExtSource (cz.metacentrum.perun.core.api.UserExtSource)5 WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)5 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)5 GroupNotExistsException (cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException)4 MemberNotExistsException (cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException)4 UserExtSourceExistsException (cz.metacentrum.perun.core.api.exceptions.UserExtSourceExistsException)4 UserNotExistsException (cz.metacentrum.perun.core.api.exceptions.UserNotExistsException)4