use of io.imunity.furms.api.validation.exceptions.DuplicatedInvitationError in project furms by unity-idm.
the class InvitatoryService method inviteUser.
public void inviteUser(String email, ResourceId resourceId, Role role, String resourceName) {
Optional<FURMSUser> furmsUser = usersDAO.getAllUsers().stream().filter(user -> user.email.equals(email)).findAny();
if (furmsUser.isPresent()) {
inviteUser(furmsUser.get().id.get(), resourceId, role, resourceName);
return;
}
if (!isEmailValid(email))
throw new IllegalArgumentException("Email is not valid");
if (invitationRepository.findBy(email, role, resourceId).isPresent())
throw new DuplicatedInvitationError("This invitation already exists");
if (isSupportRoleCheckExistingAlsoForAdminRole(resourceId, role, email))
throw new DuplicatedInvitationError("This invitation already exists");
if (isSiteAdminRoleCheckExistingAlsoForSupportRole(resourceId, role, email))
throw new DuplicatedInvitationError("This invitation already exists");
LocalDateTime expiredAt = getExpiredAtTime();
InvitationCode invitationCode = usersDAO.inviteUser(resourceId, role, email, expiredAt.toInstant(ZoneOffset.UTC));
try {
invitationRepository.create(Invitation.builder().resourceId(resourceId).resourceName(resourceName).role(role).email(email).originator(authzService.getCurrentAuthNUser().email).code(invitationCode).utcExpiredAt(expiredAt).build());
} catch (Exception e) {
usersDAO.removeInvitation(invitationCode);
throw e;
}
}
use of io.imunity.furms.api.validation.exceptions.DuplicatedInvitationError 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