use of cz.metacentrum.perun.core.api.exceptions.UserNotInRoleException 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;
}
use of cz.metacentrum.perun.core.api.exceptions.UserNotInRoleException 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;
}
use of cz.metacentrum.perun.core.api.exceptions.UserNotInRoleException 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);
}
}
}
use of cz.metacentrum.perun.core.api.exceptions.UserNotInRoleException in project perun by CESNET.
the class MembersManagerEntryIntegrationTest method addSponsor.
@Test
public void addSponsor() throws Exception {
System.out.println(CLASS_NAME + "addSponsor");
// create user which can sponsor
User sponsorUser = perun.getUsersManagerBl().getUserByMember(sess, setUpSponsor(createdVo));
AuthzResolverBlImpl.setRole(sess, sponsorUser, createdVo, Role.SPONSOR);
assertTrue("user must have SPONSOR role", perun.getVosManagerBl().isUserInRoleForVo(sess, sponsorUser, Role.SPONSOR, createdVo, true));
// create another user which can sponsor
User sponsorUser2 = perun.getUsersManagerBl().getUserByMember(sess, setUpSponsor2(createdVo));
AuthzResolverBlImpl.setRole(sess, sponsorUser2, createdVo, Role.SPONSOR);
assertTrue("user must have SPONSOR role", perun.getVosManagerBl().isUserInRoleForVo(sess, sponsorUser2, Role.SPONSOR, createdVo, true));
// create user that cannot sponsor
User notsponsorUser = perun.getUsersManagerBl().getUserByMember(sess, setUpNotSponsor(createdVo));
assertFalse("user must not have SPONSOR role", perun.getVosManagerBl().isUserInRoleForVo(sess, notsponsorUser, Role.SPONSOR, createdVo, true));
// create sponsored member
Map<String, String> nameOfUser1 = new HashMap<>();
nameOfUser1.put("guestName", "Ing. Jiří Novák, CSc.");
Member sponsoredMember = createSponsoredMember(sess, createdVo, "dummy", nameOfUser1, "secret", null, sponsorUser);
assertNotNull("sponsored member must not be null", sponsoredMember);
assertTrue("sponsored memer must have flag 'sponsored' set", sponsoredMember.isSponsored());
assertTrue("sponsored member should have status VALID", sponsoredMember.getStatus() == Status.VALID);
// try add user that cannot sponsor, should fail
try {
perun.getMembersManager().sponsorMember(sess, sponsoredMember, notsponsorUser, null);
fail("user cannot sponsor but was added as sponsor");
} catch (UserNotInRoleException ex) {
// expected
}
// try to add user that already is sponsor, should fail
try {
perun.getMembersManager().sponsorMember(sess, sponsoredMember, sponsorUser, null);
fail("user cannot sponsor twice a single member");
} catch (AlreadySponsorException ex) {
// expected
}
// try to add sponsor, should succeed
perun.getMembersManager().sponsorMember(sess, sponsoredMember, sponsorUser2, null);
List<User> sponsors = perun.getUsersManagerBl().getSponsors(sess, sponsoredMember);
assertTrue("sponsor 1 is not reported as sponsor", sponsors.contains(sponsorUser));
assertTrue("sponsor 2 is not reported as sponsor", sponsors.contains(sponsorUser2));
assertTrue("unexpected sponsors", sponsors.size() == 2);
// check that it is reported
List<RichMember> sponsoredMembers1 = perun.getMembersManager().getSponsoredMembers(sess, createdVo, sponsorUser);
assertTrue("member is not in list of sponsored members for sponsor 1", sponsoredMembers1.stream().map(PerunBean::getId).anyMatch(id -> id == sponsoredMember.getId()));
List<RichMember> sponsoredMembers2 = perun.getMembersManager().getSponsoredMembers(sess, createdVo, sponsorUser2);
assertTrue("member is not in list of sponsored members for sponsor 2", sponsoredMembers2.stream().map(PerunBean::getId).anyMatch(id -> id == sponsoredMember.getId()));
// remove sponsor and add him again, sponsorship should reactivate
Sponsorship sponsorship = perun.getMembersManagerBl().getSponsorship(sess, sponsoredMember, sponsorUser);
assertTrue(sponsorship.isActive());
perun.getMembersManager().removeSponsor(sess, sponsoredMember, sponsorUser);
sponsorship = perun.getMembersManagerBl().getSponsorship(sess, sponsoredMember, sponsorUser);
assertFalse(sponsorship.isActive());
perun.getMembersManager().sponsorMember(sess, sponsoredMember, sponsorUser, null);
sponsorship = perun.getMembersManagerBl().getSponsorship(sess, sponsoredMember, sponsorUser);
assertTrue(sponsorship.isActive());
}
Aggregations