Search in sources :

Example 1 with SponsorshipDoesNotExistException

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

the class MembersManagerBlImpl method convertUserToSponsor.

@Override
public Sponsor convertUserToSponsor(PerunSession sess, User user, Member sponsoredMember) {
    Sponsor sponsor = new Sponsor(user);
    Sponsorship sponsorship;
    try {
        sponsorship = perunBl.getMembersManagerBl().getSponsorship(sess, sponsoredMember, user);
    } catch (SponsorshipDoesNotExistException e) {
        throw new InternalErrorException(e);
    }
    sponsor.setActive(sponsorship.isActive());
    sponsor.setValidityTo(sponsorship.getValidityTo());
    if (user instanceof RichUser) {
        sponsor.setUserAttributes(((RichUser) user).getUserAttributes());
        sponsor.setUserExtSources(((RichUser) user).getUserExtSources());
    }
    return sponsor;
}
Also used : Sponsorship(cz.metacentrum.perun.core.api.Sponsorship) RichUser(cz.metacentrum.perun.core.api.RichUser) Sponsor(cz.metacentrum.perun.core.api.Sponsor) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) SponsorshipDoesNotExistException(cz.metacentrum.perun.core.api.exceptions.SponsorshipDoesNotExistException)

Example 2 with SponsorshipDoesNotExistException

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

RichUser (cz.metacentrum.perun.core.api.RichUser)2 Sponsorship (cz.metacentrum.perun.core.api.Sponsorship)2 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)2 SponsorshipDoesNotExistException (cz.metacentrum.perun.core.api.exceptions.SponsorshipDoesNotExistException)2 Member (cz.metacentrum.perun.core.api.Member)1 RichMember (cz.metacentrum.perun.core.api.RichMember)1 Sponsor (cz.metacentrum.perun.core.api.Sponsor)1 User (cz.metacentrum.perun.core.api.User)1 AlreadyAdminException (cz.metacentrum.perun.core.api.exceptions.AlreadyAdminException)1 AlreadySponsorException (cz.metacentrum.perun.core.api.exceptions.AlreadySponsorException)1 AlreadySponsoredMemberException (cz.metacentrum.perun.core.api.exceptions.AlreadySponsoredMemberException)1 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)1 MemberNotSponsoredException (cz.metacentrum.perun.core.api.exceptions.MemberNotSponsoredException)1 RoleCannotBeManagedException (cz.metacentrum.perun.core.api.exceptions.RoleCannotBeManagedException)1 UserNotInRoleException (cz.metacentrum.perun.core.api.exceptions.UserNotInRoleException)1