Search in sources :

Example 1 with AlreadySponsoredMemberException

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

the class MembersManagerEntryIntegrationTest method setAndUnsetSponsorshipForMember.

@Test
public void setAndUnsetSponsorshipForMember() throws Exception {
    System.out.println(CLASS_NAME + "setAndUnsetSponsorshipForMember");
    Member sponsorMember = setUpSponsor(createdVo);
    User sponsorUser = perun.getUsersManagerBl().getUserByMember(sess, sponsorMember);
    Group sponsors = new Group("sponsors", "users able to sponsor");
    sponsors = perun.getGroupsManagerBl().createGroup(sess, createdVo, sponsors);
    AuthzResolverBlImpl.setRole(sess, sponsors, createdVo, Role.SPONSOR);
    perun.getGroupsManagerBl().addMember(sess, sponsors, sponsorMember);
    Member memberToBeSponsored = setUpMember(createdVo);
    assertTrue("member shouldn't be created as sponsored one", !memberToBeSponsored.isSponsored());
    perun.getMembersManager().setSponsorshipForMember(sess, memberToBeSponsored, sponsorUser, null);
    try {
        perun.getMembersManager().setSponsorshipForMember(sess, memberToBeSponsored, sponsorUser, null);
        fail("member should be already sponsored");
    } catch (AlreadySponsoredMemberException ex) {
    // expected
    }
    Member newSponsoredMember = perun.getMembersManagerBl().getMemberById(sess, memberToBeSponsored.getId());
    assertTrue("member should be sponsored now", newSponsoredMember.isSponsored());
    assertEquals("there should be exactly 1 sponsor at this moment", 1, perun.getUsersManagerBl().getSponsors(sess, newSponsoredMember).size());
    perun.getMembersManager().unsetSponsorshipForMember(sess, memberToBeSponsored);
    newSponsoredMember = perun.getMembersManagerBl().getMemberById(sess, memberToBeSponsored.getId());
    assertTrue("member shouldn't be sponsored any more", !newSponsoredMember.isSponsored());
    perun.getMembersManager().setSponsorshipForMember(sess, memberToBeSponsored, sponsorUser, null);
    newSponsoredMember = perun.getMembersManagerBl().getMemberById(sess, memberToBeSponsored.getId());
    assertTrue("member should be sponsored again", newSponsoredMember.isSponsored());
}
Also used : Group(cz.metacentrum.perun.core.api.Group) User(cz.metacentrum.perun.core.api.User) AlreadySponsoredMemberException(cz.metacentrum.perun.core.api.exceptions.AlreadySponsoredMemberException) RichMember(cz.metacentrum.perun.core.api.RichMember) Member(cz.metacentrum.perun.core.api.Member) AbstractPerunIntegrationTest(cz.metacentrum.perun.core.AbstractPerunIntegrationTest) Test(org.junit.Test)

Example 2 with AlreadySponsoredMemberException

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

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

AlreadySponsoredMemberException (cz.metacentrum.perun.core.api.exceptions.AlreadySponsoredMemberException)3 Member (cz.metacentrum.perun.core.api.Member)2 RichMember (cz.metacentrum.perun.core.api.RichMember)2 User (cz.metacentrum.perun.core.api.User)2 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)2 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)2 UserNotInRoleException (cz.metacentrum.perun.core.api.exceptions.UserNotInRoleException)2 SponsoredMemberSet (cz.metacentrum.perun.audit.events.MembersManagerEvents.SponsoredMemberSet)1 SponsorshipEstablished (cz.metacentrum.perun.audit.events.MembersManagerEvents.SponsorshipEstablished)1 AbstractPerunIntegrationTest (cz.metacentrum.perun.core.AbstractPerunIntegrationTest)1 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)1 BanOnVo (cz.metacentrum.perun.core.api.BanOnVo)1 Group (cz.metacentrum.perun.core.api.Group)1 RichUser (cz.metacentrum.perun.core.api.RichUser)1 Sponsorship (cz.metacentrum.perun.core.api.Sponsorship)1 Vo (cz.metacentrum.perun.core.api.Vo)1 AlreadyAdminException (cz.metacentrum.perun.core.api.exceptions.AlreadyAdminException)1 AlreadySponsorException (cz.metacentrum.perun.core.api.exceptions.AlreadySponsorException)1 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)1 MemberNotSponsoredException (cz.metacentrum.perun.core.api.exceptions.MemberNotSponsoredException)1