Search in sources :

Example 11 with ExtendMembershipException

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

the class GroupsManagerBlImpl method addDirectMember.

/**
 * Add a record of the member with a DIRECT membership type to the group.
 *
 * @param sess perun session
 * @param group group to add member to
 * @param member member to be added as DIRECT
 * @throws InternalErrorException
 * @throws AlreadyMemberException
 * @throws WrongAttributeValueException
 * @throws WrongReferenceAttributeValueException
 * @throws GroupNotExistsException
 */
protected void addDirectMember(PerunSession sess, Group group, Member member) throws AlreadyMemberException, WrongAttributeValueException, WrongReferenceAttributeValueException, GroupNotExistsException {
    lockGroupMembership(group, Collections.singletonList(member));
    if (this.groupsManagerImpl.isDirectGroupMember(sess, group, member))
        throw new AlreadyMemberException(member);
    boolean memberWasIndirectInGroup = this.isGroupMember(sess, group, member);
    Map<Integer, Map<Integer, MemberGroupStatus>> previousStatuses = getPreviousStatuses(sess, group, List.of(member));
    member = getGroupsManagerImpl().addMember(sess, group, member, MembershipType.DIRECT, group.getId());
    getPerunBl().getAuditer().log(sess, new DirectMemberAddedToGroup(member, group));
    // If member was indirect in group before, we don't need to change anything in other groups
    if (memberWasIndirectInGroup)
        return;
    // check all relations with this group and call addRelationMembers to reflect changes of adding member to group
    List<Integer> relations = groupsManagerImpl.getResultGroupsIds(sess, group.getId());
    for (Integer groupId : relations) {
        addRelationMembers(sess, groupsManagerImpl.getGroupById(sess, groupId), Collections.singletonList(member), group.getId());
    }
    setRequiredAttributes(sess, member, group);
    // try to set init expiration
    try {
        extendMembershipInGroup(sess, member, group);
    } catch (ExtendMembershipException e) {
        throw new InternalErrorException("Failed to set initial member-group expiration date.");
    }
    if (!VosManager.MEMBERS_GROUP.equals(group.getName())) {
        // recalculate member group state
        recalculateMemberGroupStatusRecursively(sess, member, group, previousStatuses);
    }
}
Also used : DirectMemberAddedToGroup(cz.metacentrum.perun.audit.events.GroupManagerEvents.DirectMemberAddedToGroup) AlreadyMemberException(cz.metacentrum.perun.core.api.exceptions.AlreadyMemberException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) ExtendMembershipException(cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException)

Example 12 with ExtendMembershipException

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

the class MembersManagerEntryIntegrationTest method extendMembershipForMemberWithInsufficientLoa.

@Test
public void extendMembershipForMemberWithInsufficientLoa() throws Exception {
    System.out.println(CLASS_NAME + "extendGroupMembershipForMemberWithInsufficientLoa");
    // Set membershipExpirationRules attribute
    HashMap<String, String> extendMembershipRules = new LinkedHashMap<>();
    extendMembershipRules.put(AbstractMembershipExpirationRulesModule.membershipPeriodKeyName, "1.1.");
    extendMembershipRules.put(AbstractMembershipExpirationRulesModule.membershipDoNotExtendLoaKeyName, "0,1");
    Attribute extendMembershipRulesAttribute = new Attribute(attributesManagerEntry.getAttributeDefinition(sess, AttributesManager.NS_VO_ATTR_DEF + ":membershipExpirationRules"));
    extendMembershipRulesAttribute.setValue(extendMembershipRules);
    attributesManagerEntry.setAttribute(sess, createdVo, extendMembershipRulesAttribute);
    Attribute membershipExpirationAttribute = new Attribute(attributesManagerEntry.getAttributeDefinition(sess, AttributesManager.NS_MEMBER_ATTR_DEF + ":membershipExpiration"));
    LocalDate date = LocalDate.now();
    membershipExpirationAttribute.setValue(date.toString());
    attributesManagerEntry.setAttribute(sess, createdMember, membershipExpirationAttribute);
    // Set LOA 1 for member
    ExtSource es = perun.getExtSourcesManagerBl().getExtSourceByName(sess, EXT_SOURCE_NAME);
    ues = new UserExtSource(es, "abc");
    ues.setLoa(1);
    User user = usersManagerEntry.getUserByMember(sess, createdMember);
    usersManagerEntry.addUserExtSource(sess, user, ues);
    // Try to extend membership
    try {
        membersManagerEntry.extendMembership(sess, createdMember);
    } catch (ExtendMembershipException e) {
        assertTrue(e.getReason().equals(ExtendMembershipException.Reason.INSUFFICIENTLOAFOREXTENSION));
    }
    Attribute membershipAttribute = attributesManagerEntry.getAttribute(sess, createdMember, AttributesManager.NS_MEMBER_ATTR_DEF + ":membershipExpiration");
    assertNotNull("membership attribute must be set", membershipAttribute);
    assertEquals("membership attribute value must contains same value as before extension.", date.toString(), // Attribute cannot contain any value
    membershipAttribute.getValue());
}
Also used : User(cz.metacentrum.perun.core.api.User) Attribute(cz.metacentrum.perun.core.api.Attribute) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) ExtSource(cz.metacentrum.perun.core.api.ExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) LocalDate(java.time.LocalDate) ExtendMembershipException(cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException) LinkedHashMap(java.util.LinkedHashMap) AbstractPerunIntegrationTest(cz.metacentrum.perun.core.AbstractPerunIntegrationTest) Test(org.junit.Test)

Example 13 with ExtendMembershipException

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

the class Metacentrum method approveApplication.

/**
 * Add all new Metacentrum members to "storage" group.
 * Sort them in the statistics groups.
 * Set shorter expiration to users, which are not eligible for CESNET services.
 * On approval of initial application add all new members into einfra VO.
 * On approval of any application add all members into e-infra.cz VO.
 */
@Override
public Application approveApplication(PerunSession session, Application app) throws PrivilegeException, GroupNotExistsException, MemberNotExistsException, ExternallyManagedException, WrongAttributeAssignmentException, AttributeNotExistsException, WrongReferenceAttributeValueException, WrongAttributeValueException, RegistrarException {
    PerunBl perun = (PerunBl) session.getPerun();
    Vo vo = app.getVo();
    User user = app.getUser();
    Member mem = perun.getMembersManagerBl().getMemberByUser(session, vo, user);
    // Add new members to "storage" group
    if (Application.AppType.INITIAL.equals(app.getType())) {
        Group group = perun.getGroupsManagerBl().getGroupByName(session, vo, "storage");
        try {
            perun.getGroupsManager().addMember(session, group, mem);
        } catch (AlreadyMemberException ex) {
        // IGNORE
        }
    }
    // SET EXPIRATION BASED ON "isCesnetEligibleLastSeen"
    boolean eligibleUser = isCesnetEligibleLastSeen(getIsCesnetEligibleLastSeenFromUser(session, app.getUser()));
    boolean eligibleApplication = isCesnetEligibleLastSeen(getIsCesnetEligibleLastSeenFromApplication(session, app));
    if (!eligibleUser && !eligibleApplication) {
        Attribute expirationAttribute = perun.getAttributesManagerBl().getAttribute(session, mem, A_MEMBER_MEMBERSHIP_EXPIRATION);
        // only if member already has some expiration set !!
        if (expirationAttribute.getValue() != null) {
            LocalDate date = null;
            if (Application.AppType.INITIAL.equals(app.getType())) {
                // set 3 months from now (since generic logic already set wrong expiration to the member)
                date = LocalDate.now().plusMonths(3);
            } else {
                // set 3 months from current expiration
                date = LocalDate.parse(expirationAttribute.valueAsString(), DateTimeFormatter.ISO_LOCAL_DATE).plusMonths(3);
            }
            expirationAttribute.setValue(date.toString());
            perun.getAttributesManagerBl().setAttribute(session, mem, expirationAttribute);
        }
    }
    if (Application.AppType.INITIAL.equals(app.getType())) {
        // CESNET EINFRA
        try {
            Vo einfraVo = perun.getVosManagerBl().getVoByShortName(session, "einfra");
            Member einfraMember = perun.getMembersManagerBl().createMember(session, einfraVo, user);
            log.debug("Metacentrum member added to einfra {}", einfraMember);
        } catch (VoNotExistsException e) {
            log.warn("Einfra VO not exists, can't add Metacentrum member into it.");
        } catch (AlreadyMemberException ignore) {
        // user is already in einfra
        } catch (ExtendMembershipException e) {
            // can't be member of einfra, shouldn't happen
            log.error("Metacentrum member can't be added to EINFRA VO.", e);
        }
    }
    // Handle e-INFRA CZ (for both initial and extension)
    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);
    }
    // Support statistic groups
    String statisticGroupName = "";
    List<ApplicationFormItemData> formData = registrar.getApplicationDataById(session, app.getId());
    for (ApplicationFormItemData item : formData) {
        if (Objects.equals(A_USER_RESEARCH_GROUP_STATISTICS, item.getFormItem().getPerunDestinationAttribute())) {
            statisticGroupName = item.getValue();
            break;
        }
    }
    if (statisticGroupName != null && !statisticGroupName.isEmpty()) {
        Group group;
        try {
            group = perun.getGroupsManagerBl().getGroupByName(session, app.getVo(), statisticGroupName);
        } catch (GroupNotExistsException | InternalErrorException ex) {
            // user filled non existing group, just skip adding OR wrong group name
            return app;
        }
        Attribute isStatisticGroup = perun.getAttributesManagerBl().getAttribute(session, group, A_GROUP_STATISTIC_GROUP);
        Attribute isStatisticGroupAutoFill = perun.getAttributesManagerBl().getAttribute(session, group, A_GROUP_STATISTIC_GROUP_AUTOFILL);
        boolean statisticGroup = (isStatisticGroup.getValue() != null) ? (Boolean) isStatisticGroup.getValue() : false;
        boolean statisticGroupAutoFill = (isStatisticGroupAutoFill.getValue() != null) ? (Boolean) isStatisticGroupAutoFill.getValue() : false;
        if (statisticGroup && statisticGroupAutoFill) {
            try {
                perun.getGroupsManager().addMember(session, group, mem);
            } catch (AlreadyMemberException ignored) {
            }
        }
    }
    return app;
}
Also used : GroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException) PerunBl(cz.metacentrum.perun.core.bl.PerunBl) AlreadyMemberException(cz.metacentrum.perun.core.api.exceptions.AlreadyMemberException) ApplicationFormItemData(cz.metacentrum.perun.registrar.model.ApplicationFormItemData) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) LocalDate(java.time.LocalDate) ExtendMembershipException(cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException) VoNotExistsException(cz.metacentrum.perun.core.api.exceptions.VoNotExistsException)

Example 14 with ExtendMembershipException

use of cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException 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 15 with ExtendMembershipException

use of cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException 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)

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