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);
}
}
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);
}
}
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);
}
}
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;
}
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);
}
}
Aggregations