diff --git a/backend/src/main/java/com/park/utmstack/service/UtmConfigurationParameterService.java b/backend/src/main/java/com/park/utmstack/service/UtmConfigurationParameterService.java index 21a3ba7fb..3780eca76 100644 --- a/backend/src/main/java/com/park/utmstack/service/UtmConfigurationParameterService.java +++ b/backend/src/main/java/com/park/utmstack/service/UtmConfigurationParameterService.java @@ -1,15 +1,18 @@ package com.park.utmstack.service; import com.park.utmstack.config.Constants; +import com.park.utmstack.domain.User; import com.park.utmstack.domain.UtmConfigurationParameter; import com.park.utmstack.repository.UtmConfigurationParameterRepository; import com.park.utmstack.util.CipherUtil; +import com.park.utmstack.util.exceptions.UtmMailException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.event.EventListener; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; +import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; @@ -33,17 +36,23 @@ public class UtmConfigurationParameterService { private static final String CLASSNAME = "UtmConfigurationParameterService"; private final Logger log = LoggerFactory.getLogger(UtmConfigurationParameterService.class); - private final UtmConfigurationParameterRepository utmConfigurationParameterRepository; + private final UtmConfigurationParameterRepository configParamRepository; + private final UserService userService; + private final MailService mailService; - public UtmConfigurationParameterService(UtmConfigurationParameterRepository utmConfigurationParameterRepository) { - this.utmConfigurationParameterRepository = utmConfigurationParameterRepository; + public UtmConfigurationParameterService(UtmConfigurationParameterRepository configParamRepository, + UserService userService, + MailService mailService) { + this.configParamRepository = configParamRepository; + this.userService = userService; + this.mailService = mailService; } @EventListener(ApplicationReadyEvent.class) public void init() { final String ctx = CLASSNAME + ".init"; try { - List params = utmConfigurationParameterRepository.findAll(); + List params = configParamRepository.findAll(); if (CollectionUtils.isEmpty(params)) return; params.forEach(p -> { @@ -57,17 +66,25 @@ public void init() { } } - public void saveAll(List parameters) throws Exception { + public void saveAll(List params) throws UtmMailException { final String ctx = CLASSNAME + ".saveAll"; try { + // If the configuration to save is: Enable Two-Factor Authentication then we need to check + // if the email configuration is OK + params.stream().filter(p -> p.getConfParamShort().equals(Constants.PROP_TFA_ENABLE) + && Boolean.parseBoolean(p.getConfParamValue())) + .findFirst().ifPresent(tfa -> validateMailConfOnMFAActivation()); + Map cfg = new HashMap<>(); - parameters.forEach(p -> { + for (UtmConfigurationParameter p : params) { cfg.put(p.getConfParamShort(), p.getConfParamValue()); if (StringUtils.hasText(p.getConfParamValue()) && p.getConfParamDatatype().equalsIgnoreCase("password")) p.setConfParamValue(CipherUtil.encrypt(p.getConfParamValue(), System.getenv(Constants.ENV_ENCRYPTION_KEY))); - }); - utmConfigurationParameterRepository.saveAll(parameters); + } + configParamRepository.saveAll(params); Constants.CFG.putAll(cfg); + } catch (UtmMailException e) { + throw new UtmMailException(ctx + ": " + e.getMessage()); } catch (Exception e) { throw new RuntimeException(ctx + ": " + e.getMessage()); } @@ -82,7 +99,7 @@ public void saveAll(List parameters) throws Exception @Transactional(readOnly = true) public Page findAll(Pageable pageable) { log.debug("Request to get all UtmConfigurationParameters"); - return utmConfigurationParameterRepository.findAll(pageable); + return configParamRepository.findAll(pageable); } @@ -95,7 +112,7 @@ public Page findAll(Pageable pageable) { @Transactional(readOnly = true) public Optional findOne(Long id) { log.debug("Request to get UtmConfigurationParameter : {}", id); - return utmConfigurationParameterRepository.findById(id); + return configParamRepository.findById(id); } /** @@ -105,18 +122,27 @@ public Optional findOne(Long id) { */ public void delete(Long id) { log.debug("Request to delete UtmConfigurationParameter : {}", id); - utmConfigurationParameterRepository.deleteById(id); + configParamRepository.deleteById(id); } public Map getValueMapForDateSetting() throws Exception { final String ctx = CLASSNAME + ".getValueMapForDateSetting"; try { - return utmConfigurationParameterRepository - .findAllBySectionId(DATE_FORMAT_SETTING_ID).stream() - .collect(Collectors.toMap(UtmConfigurationParameter::getConfParamShort, - UtmConfigurationParameter::getConfParamValue)); + return configParamRepository + .findAllBySectionId(DATE_FORMAT_SETTING_ID).stream() + .collect(Collectors.toMap(UtmConfigurationParameter::getConfParamShort, + UtmConfigurationParameter::getConfParamValue)); } catch (Exception e) { throw new Exception(ctx + ": " + e.getMessage()); } } + + private void validateMailConfOnMFAActivation() throws UtmMailException { + final String ctx = CLASSNAME + ".validateMailConfOnMFAActivation"; + try { + mailService.sendCheckEmail(List.of(userService.getCurrentUserLogin().getEmail())); + } catch (Exception e) { + throw new UtmMailException(ctx + ": " + e.getLocalizedMessage()); + } + } } diff --git a/backend/src/main/java/com/park/utmstack/util/UtilResponse.java b/backend/src/main/java/com/park/utmstack/util/UtilResponse.java index 2c040df80..eaff4d45e 100644 --- a/backend/src/main/java/com/park/utmstack/util/UtilResponse.java +++ b/backend/src/main/java/com/park/utmstack/util/UtilResponse.java @@ -27,5 +27,9 @@ public static ResponseEntity buildLockedResponse(String msg) { public static ResponseEntity buildUnauthorizedResponse(String msg) { return buildErrorResponse(HttpStatus.UNAUTHORIZED, msg); } + + public static ResponseEntity buildPreconditionFailedResponse(String msg) { + return buildErrorResponse(HttpStatus.PRECONDITION_FAILED, msg); + } } diff --git a/backend/src/main/java/com/park/utmstack/util/exceptions/UtmMailException.java b/backend/src/main/java/com/park/utmstack/util/exceptions/UtmMailException.java new file mode 100644 index 000000000..288577b9e --- /dev/null +++ b/backend/src/main/java/com/park/utmstack/util/exceptions/UtmMailException.java @@ -0,0 +1,7 @@ +package com.park.utmstack.util.exceptions; + +public class UtmMailException extends RuntimeException { + public UtmMailException(String message) { + super(message); + } +} diff --git a/backend/src/main/java/com/park/utmstack/web/rest/UtmConfigurationParameterResource.java b/backend/src/main/java/com/park/utmstack/web/rest/UtmConfigurationParameterResource.java index 0e75042b5..6174341a8 100644 --- a/backend/src/main/java/com/park/utmstack/web/rest/UtmConfigurationParameterResource.java +++ b/backend/src/main/java/com/park/utmstack/web/rest/UtmConfigurationParameterResource.java @@ -6,7 +6,8 @@ import com.park.utmstack.service.UtmConfigurationParameterService; import com.park.utmstack.service.application_events.ApplicationEventService; import com.park.utmstack.service.dto.UtmConfigurationParameterCriteria; -import com.park.utmstack.web.rest.util.HeaderUtil; +import com.park.utmstack.util.UtilResponse; +import com.park.utmstack.util.exceptions.UtmMailException; import com.park.utmstack.web.rest.util.PaginationUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -14,7 +15,6 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.util.Assert; import org.springframework.web.bind.annotation.*; @@ -33,13 +33,10 @@ public class UtmConfigurationParameterResource { private final Logger log = LoggerFactory.getLogger(UtmConfigurationParameterResource.class); - private static final String ENTITY_NAME = "utmConfigurationParameter"; private static final String CLASSNAME = "UtmConfigurationParameterResource"; private final UtmConfigurationParameterService utmConfigurationParameterService; - private final UtmConfigurationParameterQueryService utmConfigurationParameterQueryService; - private final ApplicationEventService applicationEventService; public UtmConfigurationParameterResource(UtmConfigurationParameterService utmConfigurationParameterService, @@ -64,12 +61,21 @@ public ResponseEntity updateConfigurationParameters(@Valid @RequestBody Li Assert.notEmpty(parameters, "There isn't any parameter to update"); utmConfigurationParameterService.saveAll(parameters); return ResponseEntity.ok().build(); + } catch (UtmMailException e) { + String msg = ctx + ": " + e.getMessage(); + log.error(msg); + applicationEventService.createEvent(msg, ApplicationEventType.ERROR); + return UtilResponse.buildPreconditionFailedResponse(msg); + } catch (IllegalArgumentException e) { + String msg = ctx + ": " + e.getMessage(); + log.error(msg); + applicationEventService.createEvent(msg, ApplicationEventType.ERROR); + return UtilResponse.buildBadRequestResponse(msg); } catch (Exception e) { String msg = ctx + ": " + e.getMessage(); log.error(msg); applicationEventService.createEvent(msg, ApplicationEventType.ERROR); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).headers( - HeaderUtil.createFailureAlert("", "", msg)).body(null); + return UtilResponse.buildInternalServerErrorResponse(msg); } } diff --git a/frontend/src/app/assets-discover/shared/components/asset-group-add/asset-group-add.component.html b/frontend/src/app/assets-discover/shared/components/asset-group-add/asset-group-add.component.html index fda3395bf..24af3e7fe 100644 --- a/frontend/src/app/assets-discover/shared/components/asset-group-add/asset-group-add.component.html +++ b/frontend/src/app/assets-discover/shared/components/asset-group-add/asset-group-add.component.html @@ -1,4 +1,4 @@ -