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;
}
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;
}
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);
}
}
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);
}
}
}
Aggregations