Search in sources :

Example 1 with MemberNotSponsoredException

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

the class MembersManagerBlImpl method sponsorMember.

@Override
public Member sponsorMember(PerunSession session, Member sponsoredMember, User sponsor, LocalDate validityTo) throws MemberNotSponsoredException, AlreadySponsorException, UserNotInRoleException {
    // check that sponsoring user has role SPONSOR for the VO
    Vo vo = getMemberVo(session, sponsoredMember);
    if (!getPerunBl().getVosManagerBl().isUserInRoleForVo(session, sponsor, Role.SPONSOR, vo, true)) {
        throw new UserNotInRoleException("user " + sponsor.getId() + " is not in role SPONSOR for VO " + vo.getId());
    }
    if (!sponsoredMember.isSponsored()) {
        throw new MemberNotSponsoredException("member " + sponsoredMember.getId() + " is not marked as sponsored");
    }
    // check whether the user is already sponsor
    List<User> sponsors = getPerunBl().getUsersManagerBl().getSponsors(session, sponsoredMember);
    if (sponsors.stream().map(PerunBean::getId).anyMatch(id -> id == sponsor.getId())) {
        throw new AlreadySponsorException("member " + sponsoredMember.getId() + " is already sponsored by user " + sponsor.getId());
    }
    // add the sponsor
    getMembersManagerImpl().addSponsor(session, 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);
    }
    getPerunBl().getAuditer().log(session, new SponsorshipEstablished(sponsoredMember, sponsor, validityTo));
    return sponsoredMember;
}
Also used : User(cz.metacentrum.perun.core.api.User) RichUser(cz.metacentrum.perun.core.api.RichUser) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) MemberNotSponsoredException(cz.metacentrum.perun.core.api.exceptions.MemberNotSponsoredException) UserNotInRoleException(cz.metacentrum.perun.core.api.exceptions.UserNotInRoleException) AlreadySponsorException(cz.metacentrum.perun.core.api.exceptions.AlreadySponsorException) 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 2 with MemberNotSponsoredException

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

the class MembersManagerBlImpl method unsetSponsorshipForMember.

@Override
public Member unsetSponsorshipForMember(PerunSession session, Member sponsoredMember) throws MemberNotSponsoredException {
    if (!sponsoredMember.isSponsored()) {
        throw new MemberNotSponsoredException(sponsoredMember + " is not sponsored member!");
    }
    // set member to be sponsored
    List<User> sponsors = getPerunBl().getUsersManagerBl().getSponsors(session, sponsoredMember);
    sponsoredMember = getMembersManagerImpl().unsetSponsorshipForMember(session, sponsoredMember);
    getPerunBl().getAuditer().log(session, new SponsoredMemberUnset(sponsoredMember));
    for (User sponsor : sponsors) {
        getPerunBl().getAuditer().log(session, new SponsorshipRemoved(sponsoredMember, sponsor));
    }
    return sponsoredMember;
}
Also used : User(cz.metacentrum.perun.core.api.User) RichUser(cz.metacentrum.perun.core.api.RichUser) SponsoredMemberUnset(cz.metacentrum.perun.audit.events.MembersManagerEvents.SponsoredMemberUnset) MemberNotSponsoredException(cz.metacentrum.perun.core.api.exceptions.MemberNotSponsoredException) SponsorshipRemoved(cz.metacentrum.perun.audit.events.MembersManagerEvents.SponsorshipRemoved)

Example 3 with MemberNotSponsoredException

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

the class MembersManagerBlImpl method processMemberAfterRemovingLastSponsor.

/**
 * Execute process which expires member and removes his sponsorship.
 *
 * @param sess perun sess
 * @param sponsoredMember who will be processed
 * @param vo of sponsored member
 */
private void processMemberAfterRemovingLastSponsor(PerunSession sess, Member sponsoredMember, Vo vo) {
    // Check whether the member should be expired according to vo's expiration rules.
    boolean shouldBeExpired = true;
    try {
        Attribute expirationRules = getPerunBl().getAttributesManagerBl().getAttribute(sess, vo, VO_EXPIRATION_RULES_ATTR);
        Map<String, String> rulesMap = expirationRules.valueAsMap();
        if (rulesMap != null && rulesMap.get(expireSponsoredMembers) != null)
            shouldBeExpired = Boolean.parseBoolean(rulesMap.get(expireSponsoredMembers));
    } catch (AttributeNotExistsException | WrongAttributeAssignmentException e) {
    // When some error occurs we use default value, which means that the member should be expired.
    }
    if (shouldBeExpired) {
        // Set member's expiration to today and set status to expired.
        try {
            Attribute expiration = getPerunBl().getAttributesManagerBl().getAttribute(sess, sponsoredMember, EXPIRATION);
            expiration.setValue(BeansUtils.getDateFormatterWithoutTime().format(new Date()));
            getPerunBl().getAttributesManagerBl().setAttribute(sess, sponsoredMember, expiration);
        } catch (WrongAttributeAssignmentException | AttributeNotExistsException | WrongAttributeValueException | WrongReferenceAttributeValueException e) {
            throw new InternalErrorException("cannot set expiration date to today for sponsored member " + sponsoredMember.getId(), e);
        }
        try {
            expireMember(sess, sponsoredMember);
        } catch (WrongReferenceAttributeValueException | WrongAttributeValueException ex) {
            throw new InternalErrorException("cannot expire member " + sponsoredMember.getId(), ex);
        }
    }
    // Remove member's sponsorship.
    try {
        unsetSponsorshipForMember(sess, sponsoredMember);
    } catch (MemberNotSponsoredException e) {
        // Should not happen
        throw new InternalErrorException(e);
    }
}
Also used : Attribute(cz.metacentrum.perun.core.api.Attribute) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Date(java.util.Date) LocalDate(java.time.LocalDate) MemberNotSponsoredException(cz.metacentrum.perun.core.api.exceptions.MemberNotSponsoredException) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Example 4 with MemberNotSponsoredException

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

the class MembersManagerBlImpl method moveMembersSponsorships.

/**
 * Moves sponsorships in VO from source user to target user - moves sponsorships
 * where the source user is sponsor or where the source member is sponsored.
 *
 * @param sess
 * @param vo VO to move sponsorships in
 * @param sourceUser user to move sponsorships from
 * @param targetUser user to move sponsorships to
 * @param sourceMember member of the source user in the VO
 * @param targetMember member of the target user in the VO
 */
private void moveMembersSponsorships(PerunSession sess, Vo vo, User sourceUser, User targetUser, Member sourceMember, Member targetMember) {
    // move sponsorships where sourceUser is sponsor
    List<Member> sponsoredMembers = this.getSponsoredMembers(sess, vo, sourceUser);
    for (Member sponsoredMember : sponsoredMembers) {
        try {
            Sponsorship sponsorship = this.getSponsorship(sess, sponsoredMember, sourceUser);
            // if target user isn't in role SPONSOR for the VO, assign the role to him
            if (!getPerunBl().getVosManagerBl().isUserInRoleForVo(sess, targetUser, Role.SPONSOR, vo, true)) {
                AuthzResolverBlImpl.setRole(sess, targetUser, vo, Role.SPONSOR);
            }
            // first add sponsorship to targetUser
            this.sponsorMember(sess, sponsoredMember, targetUser, sponsorship.getValidityTo());
            // then remove sponsorship from sourceUser
            this.removeSponsor(sess, sponsoredMember, sourceUser);
        } catch (SponsorshipDoesNotExistException | MemberNotSponsoredException e) {
            throw new ConsistencyErrorException(e);
        } catch (AlreadySponsorException e) {
            log.warn("When moving sponsorships from sponsor {} to sponsor {}, the sponsor already sponsored " + "member {}. Exception: {}", sourceUser, targetUser, sponsoredMember, e);
        } catch (UserNotInRoleException | AlreadyAdminException | RoleCannotBeManagedException e) {
            throw new InternalErrorException(e);
        }
    }
    // move sponsorships where sourceMember is sponsored
    if (sourceMember.isSponsored()) {
        List<User> sponsors = getPerunBl().getUsersManagerBl().getSponsors(sess, sourceMember);
        try {
            for (int i = 0; i < sponsors.size(); i++) {
                User sponsor = sponsors.get(i);
                Sponsorship sponsorship = this.getSponsorship(sess, sourceMember, sponsor);
                // because the member needs to be marked as sponsored first
                if (i == 0) {
                    this.setSponsorshipForMember(sess, targetMember, sponsor, sponsorship.getValidityTo());
                } else {
                    this.sponsorMember(sess, targetMember, sponsor, sponsorship.getValidityTo());
                }
            }
        } catch (SponsorshipDoesNotExistException e) {
            throw new ConsistencyErrorException(e);
        } catch (AlreadySponsoredMemberException | MemberNotSponsoredException | AlreadySponsorException | UserNotInRoleException e) {
            throw new InternalErrorException(e);
        }
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) User(cz.metacentrum.perun.core.api.User) RichUser(cz.metacentrum.perun.core.api.RichUser) AlreadySponsoredMemberException(cz.metacentrum.perun.core.api.exceptions.AlreadySponsoredMemberException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) MemberNotSponsoredException(cz.metacentrum.perun.core.api.exceptions.MemberNotSponsoredException) AlreadyAdminException(cz.metacentrum.perun.core.api.exceptions.AlreadyAdminException) UserNotInRoleException(cz.metacentrum.perun.core.api.exceptions.UserNotInRoleException) Sponsorship(cz.metacentrum.perun.core.api.Sponsorship) AlreadySponsorException(cz.metacentrum.perun.core.api.exceptions.AlreadySponsorException) RoleCannotBeManagedException(cz.metacentrum.perun.core.api.exceptions.RoleCannotBeManagedException) SponsorshipDoesNotExistException(cz.metacentrum.perun.core.api.exceptions.SponsorshipDoesNotExistException) RichMember(cz.metacentrum.perun.core.api.RichMember) Member(cz.metacentrum.perun.core.api.Member)

Aggregations

MemberNotSponsoredException (cz.metacentrum.perun.core.api.exceptions.MemberNotSponsoredException)4 RichUser (cz.metacentrum.perun.core.api.RichUser)3 User (cz.metacentrum.perun.core.api.User)3 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)3 AlreadySponsorException (cz.metacentrum.perun.core.api.exceptions.AlreadySponsorException)2 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)2 UserNotInRoleException (cz.metacentrum.perun.core.api.exceptions.UserNotInRoleException)2 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)2 WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)2 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)2 SponsoredMemberUnset (cz.metacentrum.perun.audit.events.MembersManagerEvents.SponsoredMemberUnset)1 SponsorshipEstablished (cz.metacentrum.perun.audit.events.MembersManagerEvents.SponsorshipEstablished)1 SponsorshipRemoved (cz.metacentrum.perun.audit.events.MembersManagerEvents.SponsorshipRemoved)1 Attribute (cz.metacentrum.perun.core.api.Attribute)1 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)1 BanOnVo (cz.metacentrum.perun.core.api.BanOnVo)1 Member (cz.metacentrum.perun.core.api.Member)1 RichMember (cz.metacentrum.perun.core.api.RichMember)1 Sponsorship (cz.metacentrum.perun.core.api.Sponsorship)1 Vo (cz.metacentrum.perun.core.api.Vo)1