Search in sources :

Example 41 with TechnicalManagementException

use of io.gravitee.management.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.

the class UserServiceImpl method create.

/**
 * Allows to complete the creation of a user which is pre-created.
 * @param registerUserEntity a valid token and a password
 * @return the user
 */
@Override
public UserEntity create(final RegisterUserEntity registerUserEntity) {
    checkUserRegistrationEnabled();
    try {
        final String jwtSecret = environment.getProperty("jwt.secret");
        if (jwtSecret == null || jwtSecret.isEmpty()) {
            throw new IllegalStateException("JWT secret is mandatory");
        }
        final Map<String, Object> claims = new JWTVerifier(jwtSecret).verify(registerUserEntity.getToken());
        final NewUserEntity newUserEntity = new NewUserEntity();
        newUserEntity.setUsername(claims.get(Claims.SUBJECT).toString());
        newUserEntity.setEmail(claims.get(Claims.EMAIL).toString());
        newUserEntity.setFirstname(claims.get(Claims.FIRSTNAME).toString());
        newUserEntity.setLastname(claims.get(Claims.LASTNAME).toString());
        newUserEntity.setPassword(registerUserEntity.getPassword());
        LOGGER.debug("Create an internal user {}", newUserEntity);
        Optional<User> checkUser = userRepository.findByUsername(newUserEntity.getUsername());
        if (checkUser.isPresent() && StringUtils.isNotBlank(checkUser.get().getPassword())) {
            throw new UsernameAlreadyExistsException(newUserEntity.getUsername());
        }
        User user = convert(newUserEntity);
        user.setId(UUID.toString(UUID.random()));
        // Encrypt password if internal user
        if (user.getPassword() != null) {
            user.setPassword(passwordEncoder.encode(user.getPassword()));
        }
        // Set date fields
        user.setUpdatedAt(new Date());
        user = userRepository.update(user);
        auditService.createPortalAuditLog(Collections.singletonMap(USER, user.getUsername()), User.AuditEvent.USER_CREATED, user.getUpdatedAt(), null, user);
        return convert(user, true);
    } catch (Exception ex) {
        LOGGER.error("An error occurs while trying to create an internal user with the token {}", registerUserEntity.getToken(), ex);
        throw new TechnicalManagementException(ex.getMessage(), ex);
    }
}
Also used : UsernameAlreadyExistsException(io.gravitee.management.service.exceptions.UsernameAlreadyExistsException) User(io.gravitee.repository.management.model.User) JWTVerifier(com.auth0.jwt.JWTVerifier) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) UserNotFoundException(io.gravitee.management.service.exceptions.UserNotFoundException) UsernameAlreadyExistsException(io.gravitee.management.service.exceptions.UsernameAlreadyExistsException) DefaultRoleNotFoundException(io.gravitee.management.service.exceptions.DefaultRoleNotFoundException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException) IOException(java.io.IOException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 42 with TechnicalManagementException

use of io.gravitee.management.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.

the class UserServiceImpl method connect.

@Override
public UserEntity connect(String userId) {
    try {
        LOGGER.debug("Connection of {}", userId);
        Optional<User> checkUser = userRepository.findById(userId);
        if (!checkUser.isPresent()) {
            throw new UserNotFoundException(userId);
        }
        User user = checkUser.get();
        User previousUser = new User(user);
        // First connection: create default application for user
        if (defaultApplicationForFirstConnection && user.getLastConnectionAt() == null) {
            LOGGER.debug("Create a default application for {}", userId);
            NewApplicationEntity defaultApp = new NewApplicationEntity();
            defaultApp.setName("Default application");
            defaultApp.setDescription("My default application");
            applicationService.create(defaultApp, userId);
        }
        // Set date fields
        user.setLastConnectionAt(new Date());
        user.setUpdatedAt(user.getLastConnectionAt());
        User updatedUser = userRepository.update(user);
        auditService.createPortalAuditLog(Collections.singletonMap(USER, userId), User.AuditEvent.USER_CONNECTED, user.getUpdatedAt(), previousUser, user);
        return convert(updatedUser, true);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to connect {}", userId, ex);
        throw new TechnicalManagementException("An error occurs while trying to connect " + userId, ex);
    }
}
Also used : UserNotFoundException(io.gravitee.management.service.exceptions.UserNotFoundException) User(io.gravitee.repository.management.model.User) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 43 with TechnicalManagementException

use of io.gravitee.management.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.

the class ViewServiceImpl method delete.

@Override
public void delete(final String viewId) {
    if (View.ALL_ID.equals(viewId)) {
        LOGGER.error("Delete the default view is forbidden");
        throw new TechnicalManagementException("Delete the default view is forbidden");
    }
    try {
        Optional<View> viewOptional = viewRepository.findById(viewId);
        if (viewOptional.isPresent()) {
            viewRepository.delete(viewId);
            auditService.createPortalAuditLog(Collections.singletonMap(VIEW, viewId), VIEW_DELETED, new Date(), null, viewOptional.get());
            // delete all reference on APIs
            apiService.deleteViewFromAPIs(viewId);
        }
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to delete view {}", viewId, ex);
        throw new TechnicalManagementException("An error occurs while trying to delete view " + viewId, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) View(io.gravitee.repository.management.model.View) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 44 with TechnicalManagementException

use of io.gravitee.management.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.

the class ViewServiceImpl method create.

@Override
public List<ViewEntity> create(final List<NewViewEntity> viewEntities) {
    // First we prevent the duplicate view name
    final List<String> viewNames = viewEntities.stream().map(NewViewEntity::getName).collect(Collectors.toList());
    final Optional<ViewEntity> optionalView = findAll().stream().filter(view -> viewNames.contains(view.getName())).findAny();
    if (optionalView.isPresent()) {
        throw new DuplicateViewNameException(optionalView.get().getName());
    }
    final List<ViewEntity> savedViews = new ArrayList<>(viewEntities.size());
    viewEntities.forEach(viewEntity -> {
        try {
            View view = convert(viewEntity);
            savedViews.add(convert(viewRepository.create(view)));
            auditService.createPortalAuditLog(Collections.singletonMap(VIEW, view.getId()), VIEW_CREATED, new Date(), null, view);
        } catch (TechnicalException ex) {
            LOGGER.error("An error occurs while trying to create view {}", viewEntity.getName(), ex);
            throw new TechnicalManagementException("An error occurs while trying to create view " + viewEntity.getName(), ex);
        }
    });
    return savedViews;
}
Also used : ViewEntity(io.gravitee.management.model.ViewEntity) java.util(java.util) UpdateViewEntity(io.gravitee.management.model.UpdateViewEntity) Logger(org.slf4j.Logger) ApiService(io.gravitee.management.service.ApiService) ViewService(io.gravitee.management.service.ViewService) VIEW_DELETED(io.gravitee.repository.management.model.View.AuditEvent.VIEW_DELETED) DuplicateViewNameException(io.gravitee.management.service.exceptions.DuplicateViewNameException) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) VIEW(io.gravitee.repository.management.model.Audit.AuditProperties.VIEW) VIEW_UPDATED(io.gravitee.repository.management.model.View.AuditEvent.VIEW_UPDATED) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException) View(io.gravitee.repository.management.model.View) Autowired(org.springframework.beans.factory.annotation.Autowired) NewViewEntity(io.gravitee.management.model.NewViewEntity) AuditService(io.gravitee.management.service.AuditService) Collectors(java.util.stream.Collectors) IdGenerator(io.gravitee.common.utils.IdGenerator) Component(org.springframework.stereotype.Component) VIEW_CREATED(io.gravitee.repository.management.model.View.AuditEvent.VIEW_CREATED) ViewRepository(io.gravitee.repository.management.api.ViewRepository) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) ViewEntity(io.gravitee.management.model.ViewEntity) UpdateViewEntity(io.gravitee.management.model.UpdateViewEntity) NewViewEntity(io.gravitee.management.model.NewViewEntity) DuplicateViewNameException(io.gravitee.management.service.exceptions.DuplicateViewNameException) View(io.gravitee.repository.management.model.View) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 45 with TechnicalManagementException

use of io.gravitee.management.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.

the class ViewServiceImpl method createDefaultView.

@Override
public void createDefaultView() {
    View view = new View();
    view.setId(View.ALL_ID);
    view.setName("All");
    view.setDefaultView(true);
    view.setOrder(0);
    view.setCreatedAt(new Date());
    view.setUpdatedAt(view.getCreatedAt());
    try {
        viewRepository.create(view);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to create view {}", view.getName(), ex);
        throw new TechnicalManagementException("An error occurs while trying to create view " + view.getName(), ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) View(io.gravitee.repository.management.model.View) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Aggregations

TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)90 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)83 Logger (org.slf4j.Logger)17 LoggerFactory (org.slf4j.LoggerFactory)17 Autowired (org.springframework.beans.factory.annotation.Autowired)17 Component (org.springframework.stereotype.Component)17 Collectors (java.util.stream.Collectors)13 java.util (java.util)11 Date (java.util.Date)11 AuditService (io.gravitee.management.service.AuditService)10 IdGenerator (io.gravitee.common.utils.IdGenerator)9 io.gravitee.management.model (io.gravitee.management.model)9 User (io.gravitee.repository.management.model.User)9 ApiRatingUnavailableException (io.gravitee.management.service.exceptions.ApiRatingUnavailableException)8 UserNotFoundException (io.gravitee.management.service.exceptions.UserNotFoundException)8 Metadata (io.gravitee.repository.management.model.Metadata)8 Rating (io.gravitee.repository.management.model.Rating)8 DuplicateMetadataNameException (io.gravitee.management.service.exceptions.DuplicateMetadataNameException)7 ApiService (io.gravitee.management.service.ApiService)6 MetadataService (io.gravitee.management.service.MetadataService)6