use of io.imunity.furms.api.validation.exceptions.UserAlreadyHasRoleError in project furms by unity-idm.
the class InvitatoryService method inviteUser.
public void inviteUser(PersistentId userId, ResourceId resourceId, Role role, String resourceName) {
FURMSUser user = usersDAO.findById(userId).orElseThrow(() -> new IllegalArgumentException("Could not invite user due to wrong email address."));
if (user.fenixUserId.isEmpty())
throw new UnsupportedUserException("Only fenix users supported");
if (invitationRepository.findBy(user.email, role, resourceId).isPresent())
throw new DuplicatedInvitationError("This invitation already exists");
if (isSupportRoleCheckExistingAlsoForAdminRole(resourceId, role, user.email))
throw new DuplicatedInvitationError("This invitation already exists");
if (isSiteAdminRoleCheckExistingAlsoForSupportRole(resourceId, role, user.email))
throw new DuplicatedInvitationError("This invitation already exists");
if (resourceId.type.equals(PROJECT) && applicationRepository.existsBy(resourceId.id.toString(), user.fenixUserId.get()))
throw new UserAlreadyAppliedForMembershipException("User waiting for application approval");
if (usersDAO.getUserAttributes(user.fenixUserId.get()).attributesByResource.getOrDefault(resourceId, Set.of()).contains(new UserAttribute(role)))
throw new UserAlreadyHasRoleError("User already has this role");
Invitation invitation = Invitation.builder().resourceId(resourceId).role(role).resourceName(resourceName).userId(user.fenixUserId.get()).originator(authzService.getCurrentAuthNUser().email).email(user.email).utcExpiredAt(getExpiredAtTime()).build();
invitationRepository.create(invitation);
LOG.info("Inviting FENIX admin role to {}", userId);
userInvitationNotificationService.notifyUserAboutNewRole(user.id.get(), invitation.role);
publisher.publishEvent(new InviteUserEvent(user.fenixUserId.get(), user.email, resourceId));
}
Aggregations