Search in sources :

Example 86 with TechnicalException

use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.

the class GroupServiceImpl method delete.

@Override
public void delete(String groupId) {
    try {
        logger.debug("delete {}", groupId);
        Optional<Group> group = groupRepository.findById(groupId);
        if (!group.isPresent()) {
            throw new GroupNotFoundException(groupId);
        }
        // remove all members
        membershipRepository.findByReferenceAndRole(MembershipReferenceType.GROUP, groupId, null, null).forEach(member -> {
            try {
                membershipRepository.delete(member);
            } catch (TechnicalException ex) {
                logger.error("An error occurs while trying to delete a group", ex);
                throw new TechnicalManagementException("An error occurs while trying to delete a group", ex);
            }
        });
        // remove all applications or apis
        Date updatedDate = new Date();
        apiRepository.findByGroups(Collections.singletonList(groupId)).forEach(api -> {
            api.getGroups().remove(groupId);
            api.setUpdatedAt(updatedDate);
            try {
                apiRepository.update(api);
            } catch (TechnicalException ex) {
                logger.error("An error occurs while trying to delete a group", ex);
                throw new TechnicalManagementException("An error occurs while trying to delete a group", ex);
            }
        });
        applicationRepository.findByGroups(Collections.singletonList(groupId)).forEach(application -> {
            application.getGroups().remove(groupId);
            application.setUpdatedAt(updatedDate);
            try {
                applicationRepository.update(application);
            } catch (TechnicalException ex) {
                logger.error("An error occurs while trying to delete a group", ex);
                throw new TechnicalManagementException("An error occurs while trying to delete a group", ex);
            }
        });
        // remove group
        groupRepository.delete(groupId);
        // Audit
        auditService.createPortalAuditLog(Collections.singletonMap(GROUP, groupId), GROUP_DELETED, new Date(), group.get(), null);
        logger.debug("delete {} - DONE", groupId);
    } catch (TechnicalException ex) {
        logger.error("An error occurs while trying to delete a group", ex);
        throw new TechnicalManagementException("An error occurs while trying to delete a group", ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) GroupNotFoundException(io.gravitee.management.service.exceptions.GroupNotFoundException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 87 with TechnicalException

use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.

the class GroupServiceImpl method isUserAuthorizedToAccessPortalData.

@Override
public boolean isUserAuthorizedToAccessPortalData(List<String> excludedGroups, String username) {
    // in anonymous mode, only pages without restrictions are authorized
    if (username == null) {
        return (excludedGroups == null || excludedGroups.isEmpty());
    }
    if (excludedGroups != null && !excludedGroups.isEmpty()) {
        // for public apis, default authorized groups are all groups,
        Set<String> authorizedGroups;
        try {
            authorizedGroups = groupRepository.findAll().stream().map(Group::getId).collect(Collectors.toSet());
        } catch (TechnicalException ex) {
            logger.error("An error occurs while trying to find all groups", ex);
            throw new TechnicalManagementException("An error occurs while trying to find all groups", ex);
        }
        authorizedGroups.removeAll(excludedGroups);
        for (String authorizedGroupId : authorizedGroups) {
            if (membershipService.getMember(MembershipReferenceType.GROUP, authorizedGroupId, username, RoleScope.API) != null) {
                return true;
            }
        }
        return false;
    }
    return true;
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 88 with TechnicalException

use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.

the class MembershipServiceImpl method removeRole.

@Override
public boolean removeRole(MembershipReferenceType referenceType, String referenceId, String userId, RoleScope roleScope) {
    try {
        Optional<Membership> optionalMembership = membershipRepository.findById(userId, referenceType, referenceId);
        if (optionalMembership.isPresent()) {
            Membership membership = optionalMembership.get();
            Membership previousMembership = new Membership(membership);
            membership.getRoles().remove(roleScope.getId());
            membership.setUpdatedAt(new Date());
            if (membership.getRoles().isEmpty()) {
                throw new MemberWithoutRoleException(membership.getUserId());
            } else {
                membershipRepository.update(membership);
                createAuditLog(MEMBERSHIP_UPDATED, membership.getUpdatedAt(), previousMembership, membership);
                return true;
            }
        }
        return false;
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to get membership for {} {} and user", referenceType, referenceId, userId, ex);
        throw new TechnicalManagementException("An error occurs while trying to get members for " + referenceType + " " + referenceId + " and user " + userId, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Membership(io.gravitee.repository.management.model.Membership)

Example 89 with TechnicalException

use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.

the class MetadataServiceImpl method findDefaultByKey.

@Override
public MetadataEntity findDefaultByKey(final String key) {
    try {
        LOGGER.debug("Find default metadata by key");
        final Optional<Metadata> optMetadata = metadataRepository.findById(key, DEFAUT_REFERENCE_ID, MetadataReferenceType.DEFAULT);
        if (optMetadata.isPresent()) {
            return convert(optMetadata.get());
        } else {
            return null;
        }
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to find default metadata by key", ex);
        throw new TechnicalManagementException("An error occurred while trying to find default metadata by key", ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Metadata(io.gravitee.repository.management.model.Metadata) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 90 with TechnicalException

use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.

the class PageServiceImpl method findMaxPortalPageOrder.

@Override
public int findMaxPortalPageOrder() {
    try {
        logger.debug("Find Max Order Portal Page");
        final Integer maxPageOrder = pageRepository.findMaxPortalPageOrder();
        return maxPageOrder == null ? 0 : maxPageOrder;
    } catch (TechnicalException ex) {
        logger.error("An error occured when searching max order portal page", ex);
        throw new TechnicalManagementException("An error occured when searching max order portal ", ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Aggregations

TechnicalException (io.gravitee.repository.exceptions.TechnicalException)102 TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)80 Logger (org.slf4j.Logger)22 LoggerFactory (org.slf4j.LoggerFactory)22 Autowired (org.springframework.beans.factory.annotation.Autowired)22 Component (org.springframework.stereotype.Component)20 java.util (java.util)18 Collectors (java.util.stream.Collectors)18 io.gravitee.management.model (io.gravitee.management.model)16 AuditService (io.gravitee.management.service.AuditService)12 UUID (io.gravitee.common.utils.UUID)11 Date (java.util.Date)11 IdGenerator (io.gravitee.common.utils.IdGenerator)9 IOException (java.io.IOException)9 io.gravitee.management.service (io.gravitee.management.service)8 ApiRatingUnavailableException (io.gravitee.management.service.exceptions.ApiRatingUnavailableException)8 Metadata (io.gravitee.repository.management.model.Metadata)8 Rating (io.gravitee.repository.management.model.Rating)8 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7