Search in sources :

Example 1 with DuplicatedInvitationError

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;
    }
}
Also used : UTCTimeUtils(io.imunity.furms.utils.UTCTimeUtils) InviteUserEvent(io.imunity.furms.domain.invitations.InviteUserEvent) InvitationRepository(io.imunity.furms.spi.invitations.InvitationRepository) ZonedDateTime(java.time.ZonedDateTime) LoggerFactory(org.slf4j.LoggerFactory) LocalDateTime(java.time.LocalDateTime) EmailValidator(org.apache.commons.validator.routines.EmailValidator) InvitationCode(io.imunity.furms.domain.invitations.InvitationCode) Value(org.springframework.beans.factory.annotation.Value) DuplicatedInvitationError(io.imunity.furms.api.validation.exceptions.DuplicatedInvitationError) UserAlreadyAppliedForMembershipException(io.imunity.furms.api.validation.exceptions.UserAlreadyAppliedForMembershipException) UsersDAO(io.imunity.furms.spi.users.UsersDAO) Service(org.springframework.stereotype.Service) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) PROJECT(io.imunity.furms.domain.authz.roles.ResourceType.PROJECT) Invitation(io.imunity.furms.domain.invitations.Invitation) ZoneOffset(java.time.ZoneOffset) PersistentId(io.imunity.furms.domain.users.PersistentId) Logger(org.slf4j.Logger) ResourceId(io.imunity.furms.domain.authz.roles.ResourceId) UserAttribute(io.imunity.furms.domain.users.UserAttribute) UpdateInvitationUserEvent(io.imunity.furms.domain.invitations.UpdateInvitationUserEvent) ApplicationRepository(io.imunity.furms.spi.applications.ApplicationRepository) Set(java.util.Set) UUID(java.util.UUID) FURMSUser(io.imunity.furms.domain.users.FURMSUser) RemoveInvitationUserEvent(io.imunity.furms.domain.invitations.RemoveInvitationUserEvent) UserAlreadyHasRoleError(io.imunity.furms.api.validation.exceptions.UserAlreadyHasRoleError) AuthzService(io.imunity.furms.api.authz.AuthzService) Role(io.imunity.furms.domain.authz.roles.Role) InvitationId(io.imunity.furms.domain.invitations.InvitationId) Clock(java.time.Clock) Optional(java.util.Optional) UnsupportedUserException(io.imunity.furms.api.validation.exceptions.UnsupportedUserException) LocalDateTime(java.time.LocalDateTime) DuplicatedInvitationError(io.imunity.furms.api.validation.exceptions.DuplicatedInvitationError) InvitationCode(io.imunity.furms.domain.invitations.InvitationCode) FURMSUser(io.imunity.furms.domain.users.FURMSUser) UserAlreadyAppliedForMembershipException(io.imunity.furms.api.validation.exceptions.UserAlreadyAppliedForMembershipException) UnsupportedUserException(io.imunity.furms.api.validation.exceptions.UnsupportedUserException)

Example 2 with DuplicatedInvitationError

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));
}
Also used : UserAlreadyAppliedForMembershipException(io.imunity.furms.api.validation.exceptions.UserAlreadyAppliedForMembershipException) UnsupportedUserException(io.imunity.furms.api.validation.exceptions.UnsupportedUserException) DuplicatedInvitationError(io.imunity.furms.api.validation.exceptions.DuplicatedInvitationError) UserAttribute(io.imunity.furms.domain.users.UserAttribute) Invitation(io.imunity.furms.domain.invitations.Invitation) UserAlreadyHasRoleError(io.imunity.furms.api.validation.exceptions.UserAlreadyHasRoleError) FURMSUser(io.imunity.furms.domain.users.FURMSUser) InviteUserEvent(io.imunity.furms.domain.invitations.InviteUserEvent)

Aggregations

DuplicatedInvitationError (io.imunity.furms.api.validation.exceptions.DuplicatedInvitationError)2 UnsupportedUserException (io.imunity.furms.api.validation.exceptions.UnsupportedUserException)2 UserAlreadyAppliedForMembershipException (io.imunity.furms.api.validation.exceptions.UserAlreadyAppliedForMembershipException)2 UserAlreadyHasRoleError (io.imunity.furms.api.validation.exceptions.UserAlreadyHasRoleError)2 Invitation (io.imunity.furms.domain.invitations.Invitation)2 InviteUserEvent (io.imunity.furms.domain.invitations.InviteUserEvent)2 FURMSUser (io.imunity.furms.domain.users.FURMSUser)2 UserAttribute (io.imunity.furms.domain.users.UserAttribute)2 AuthzService (io.imunity.furms.api.authz.AuthzService)1 ResourceId (io.imunity.furms.domain.authz.roles.ResourceId)1 PROJECT (io.imunity.furms.domain.authz.roles.ResourceType.PROJECT)1 Role (io.imunity.furms.domain.authz.roles.Role)1 InvitationCode (io.imunity.furms.domain.invitations.InvitationCode)1 InvitationId (io.imunity.furms.domain.invitations.InvitationId)1 RemoveInvitationUserEvent (io.imunity.furms.domain.invitations.RemoveInvitationUserEvent)1 UpdateInvitationUserEvent (io.imunity.furms.domain.invitations.UpdateInvitationUserEvent)1 PersistentId (io.imunity.furms.domain.users.PersistentId)1 ApplicationRepository (io.imunity.furms.spi.applications.ApplicationRepository)1 InvitationRepository (io.imunity.furms.spi.invitations.InvitationRepository)1 UsersDAO (io.imunity.furms.spi.users.UsersDAO)1