Search in sources :

Example 1 with Invitation

use of io.gravitee.repository.management.model.Invitation in project gravitee-management-rest-api by gravitee-io.

the class InvitationServiceImpl method create.

@Override
public InvitationEntity create(final NewInvitationEntity invitation) {
    final List<InvitationEntity> invitations = findByReference(invitation.getReferenceType(), invitation.getReferenceId());
    if (invitations.stream().map(InvitationEntity::getEmail).anyMatch(invitation.getEmail()::equals)) {
        throw new InvitationEmailAlreadyExistsException(invitation.getEmail());
    }
    try {
        // First check if user exists
        final Optional<UserEntity> existingUser = userService.findByEmail(invitation.getEmail());
        if (existingUser.isPresent()) {
            final UserEntity user = existingUser.get();
            Set<String> groupUsers = membershipService.getMembershipsByReference(MembershipReferenceType.GROUP, invitation.getReferenceId()).stream().map(MembershipEntity::getMemberId).collect(Collectors.toSet());
            final GroupEntity group = groupService.findById(invitation.getReferenceId());
            if (groupUsers.contains(user.getId())) {
                throw new MemberEmailAlreadyExistsException(invitation.getEmail());
            }
            // override permission if not allowed
            final boolean hasPermission = permissionService.hasPermission(RolePermission.ENVIRONMENT_GROUP, GraviteeContext.getCurrentEnvironment(), CREATE, UPDATE, DELETE);
            if (!hasPermission && group.isLockApiRole()) {
                invitation.setApiRole(null);
            }
            if (!hasPermission && group.isLockApplicationRole()) {
                invitation.setApplicationRole(null);
            }
            addMember(invitation.getReferenceType().name(), invitation.getReferenceId(), user.getId(), invitation.getApiRole(), invitation.getApplicationRole());
            return null;
        } else {
            sendGroupInvitationEmail(invitation);
            final Invitation createdInvitation = invitationRepository.create(convert(invitation));
            return convert(createdInvitation);
        }
    } catch (TechnicalException ex) {
        final String message = "An error occurs while trying to create invitation for email " + invitation.getEmail();
        LOGGER.error(message, ex);
        throw new TechnicalManagementException(message, ex);
    }
}
Also used : InvitationEmailAlreadyExistsException(io.gravitee.rest.api.service.exceptions.InvitationEmailAlreadyExistsException) MemberEmailAlreadyExistsException(io.gravitee.rest.api.service.exceptions.MemberEmailAlreadyExistsException) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Invitation(io.gravitee.repository.management.model.Invitation) UuidString(io.gravitee.rest.api.service.common.UuidString) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 2 with Invitation

use of io.gravitee.repository.management.model.Invitation in project gravitee-management-rest-api by gravitee-io.

the class InvitationServiceImpl method delete.

@Override
public void delete(final String invitationId, final String referenceId) {
    try {
        final Optional<Invitation> optionalInvitation = invitationRepository.findById(invitationId);
        if (!optionalInvitation.isPresent() || !optionalInvitation.get().getReferenceId().equals(referenceId)) {
            throw new InvitationNotFoundException(invitationId);
        }
        invitationRepository.delete(invitationId);
    } catch (TechnicalException te) {
        final String msg = "An error occurs while trying to delete the invitation " + invitationId;
        LOGGER.error(msg, te);
        throw new TechnicalManagementException(msg, te);
    }
}
Also used : InvitationNotFoundException(io.gravitee.rest.api.service.exceptions.InvitationNotFoundException) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Invitation(io.gravitee.repository.management.model.Invitation) UuidString(io.gravitee.rest.api.service.common.UuidString) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 3 with Invitation

use of io.gravitee.repository.management.model.Invitation in project gravitee-management-rest-api by gravitee-io.

the class InvitationServiceImpl method convert.

private Invitation convert(final NewInvitationEntity invitationEntity) {
    final Invitation invitation = new Invitation();
    invitation.setId(UuidString.generateRandom());
    invitation.setReferenceId(invitationEntity.getReferenceId());
    invitation.setReferenceType(invitationEntity.getReferenceType().name());
    invitation.setApiRole(invitationEntity.getApiRole());
    invitation.setApplicationRole(invitationEntity.getApplicationRole());
    invitation.setEmail(invitationEntity.getEmail());
    final Date now = new Date();
    invitation.setCreatedAt(now);
    return invitation;
}
Also used : Invitation(io.gravitee.repository.management.model.Invitation) Date(java.util.Date)

Example 4 with Invitation

use of io.gravitee.repository.management.model.Invitation in project gravitee-management-rest-api by gravitee-io.

the class InvitationServiceImpl method update.

@Override
public InvitationEntity update(final UpdateInvitationEntity invitation) {
    try {
        final Optional<Invitation> invitationOptional = invitationRepository.findById(invitation.getId());
        if (invitationOptional.isPresent()) {
            final Invitation invitationToUpdate = invitationOptional.get();
            if (!invitationToUpdate.getReferenceId().equals(invitation.getReferenceId())) {
                throw new InvitationNotFoundException(invitation.getId());
            }
            invitationToUpdate.setApiRole(invitation.getApiRole());
            invitationToUpdate.setApplicationRole(invitation.getApplicationRole());
            invitationToUpdate.setUpdatedAt(new Date());
            return convert(invitationRepository.update(invitationToUpdate));
        } else {
            throw new InvitationNotFoundException(invitation.getId());
        }
    } catch (TechnicalException ex) {
        final String message = "An error occurs while trying to update invitation with email " + invitation.getEmail();
        LOGGER.error(message, ex);
        throw new TechnicalManagementException(message, ex);
    }
}
Also used : InvitationNotFoundException(io.gravitee.rest.api.service.exceptions.InvitationNotFoundException) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Invitation(io.gravitee.repository.management.model.Invitation) UuidString(io.gravitee.rest.api.service.common.UuidString) Date(java.util.Date) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Aggregations

Invitation (io.gravitee.repository.management.model.Invitation)4 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)3 UuidString (io.gravitee.rest.api.service.common.UuidString)3 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)3 InvitationNotFoundException (io.gravitee.rest.api.service.exceptions.InvitationNotFoundException)2 Date (java.util.Date)2 InvitationEmailAlreadyExistsException (io.gravitee.rest.api.service.exceptions.InvitationEmailAlreadyExistsException)1 MemberEmailAlreadyExistsException (io.gravitee.rest.api.service.exceptions.MemberEmailAlreadyExistsException)1