|
| 1 | +package life.qbic.identityaccess.application.user; |
| 2 | + |
| 3 | +import life.qbic.identityaccess.domain.user.EncryptedPassword.PasswordValidationException; |
| 4 | +import life.qbic.shared.application.ApplicationResponse; |
| 5 | + |
| 6 | +/** |
| 7 | + * <b>New password use case</b> |
| 8 | + * <p> |
| 9 | + * Set's a new password for a user. |
| 10 | + * |
| 11 | + * @since 1.0.0 |
| 12 | + */ |
| 13 | +public class NewPassword implements NewPasswordInput { |
| 14 | + |
| 15 | + private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(NewPassword.class); |
| 16 | + private final UserRegistrationService userRegistrationService; |
| 17 | + private NewPasswordOutput useCaseOutput; |
| 18 | + |
| 19 | + public NewPassword(UserRegistrationService userRegistrationService) { |
| 20 | + super(); |
| 21 | + this.userRegistrationService = userRegistrationService; |
| 22 | + } |
| 23 | + |
| 24 | + public void setUseCaseOutput(NewPasswordOutput useCaseOutput) { |
| 25 | + this.useCaseOutput = useCaseOutput; |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public void setNewUserPassword(String userId, char[] newRawPassword) { |
| 30 | + if (useCaseOutput == null) { |
| 31 | + log.error("No use case output was set"); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + ApplicationResponse response = userRegistrationService.newUserPassword(userId, newRawPassword); |
| 36 | + |
| 37 | + response.ifSuccessOrElse(ignored -> { |
| 38 | + log.info(String.format("Successful password reset for user %s", userId)); |
| 39 | + useCaseOutput.onSuccessfulNewPassword(); |
| 40 | + }, |
| 41 | + it -> it.failures().stream().filter(e -> e instanceof PasswordValidationException).findAny() |
| 42 | + .ifPresentOrElse(ignored -> { |
| 43 | + log.error(String.format("Could not set new password for user: %s", userId)); |
| 44 | + useCaseOutput.onPasswordValidationFailure(); |
| 45 | + }, |
| 46 | + () -> { |
| 47 | + log.error(String.format("Unexpected failure on password reset for user: %s", userId)); |
| 48 | + useCaseOutput.onUnexpectedFailure(); |
| 49 | + })); |
| 50 | + |
| 51 | + } |
| 52 | +} |
0 commit comments