Skip to content

Commit

Permalink
Fix the inaccessible problem of signup page (#6730)
Browse files Browse the repository at this point in the history
#### What type of PR is this?

/kind bug
/area core
/milestone 2.20.x

#### What this PR does / why we need it:

This PR adds confirmPassword field into SignUpData for validation. So the signup page can be rendered correctly.

See #6718 for more.

#### Which issue(s) this PR fixes:

Fixes #6718

#### Special notes for your reviewer:

#### Does this PR introduce a user-facing change?

```release-note
None
```
  • Loading branch information
JohnNiang authored Sep 29, 2024
1 parent 1947a54 commit 83109d0
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package run.halo.app.core.user.service;

import jakarta.validation.Constraint;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import jakarta.validation.Payload;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Objects;
import java.util.Optional;
import lombok.Data;
import org.springframework.util.MultiValueMap;
Expand All @@ -14,6 +23,7 @@
* @since 2.20.0
*/
@Data
@SignUpData.SignUpDataConstraint
public class SignUpData {

@NotBlank
Expand All @@ -30,6 +40,9 @@ public class SignUpData {
@NotBlank
private String password;

@NotBlank
private String confirmPassword;

public static SignUpData of(MultiValueMap<String, String> formData) {
var form = new SignUpData();
Optional.ofNullable(formData.getFirst("username"))
Expand All @@ -52,6 +65,41 @@ public static SignUpData of(MultiValueMap<String, String> formData) {
.filter(StringUtils::hasText)
.ifPresent(form::setEmailCode);

Optional.ofNullable(formData.getFirst("confirmPassword"))
.filter(StringUtils::hasText)
.ifPresent(form::setConfirmPassword);

return form;
}

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {SignUpDataConstraintValidator.class})
public @interface SignUpDataConstraint {

String message() default "";

Class<?>[] groups() default { };

Class<? extends Payload>[] payload() default { };

}

private static class SignUpDataConstraintValidator
implements ConstraintValidator<SignUpDataConstraint, SignUpData> {

@Override
public boolean isValid(SignUpData signUpData, ConstraintValidatorContext context) {
var isValid = Objects.equals(signUpData.getPassword(), signUpData.getConfirmPassword());
if (!isValid) {
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate(
"{signup.error.confirm-password-not-match}"
)
.addPropertyNode("confirmPassword")
.addConstraintViolation();
}
return isValid;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ RouterFunction<ServerResponse> preAuthSignUpEndpoints() {
"emailCode",
signUpData.getEmailCode(),
true,
// TODO Refine i18n
new String[] {"signup.error.email-captcha.invalid"},
new String[] {"signup.error.email-code.invalid"},
null,
"Invalid Email Code"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,6 @@ problemDetail.attachment.upload.fileSizeExceeded=Make sure the file size is less
problemDetail.attachment.upload.fileTypeNotSupported=Unsupported upload of {0} type files.
problemDetail.comment.waitingForApproval=Comment is awaiting approval.

title.visibility.identification.private=(Private)
title.visibility.identification.private=(Private)
signup.error.confirm-password-not-match=The confirmation password does not match the password.
signup.error.email-code.invalid=Invalid email code.
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,6 @@ problemDetail.attachment.upload.fileSizeExceeded=最大支持上传 {0} 大小
problemDetail.attachment.upload.fileTypeNotSupported=不支持上传 {0} 类型的文件。
problemDetail.comment.waitingForApproval=评论审核中。

title.visibility.identification.private=(私有)
title.visibility.identification.private=(私有)
signup.error.confirm-password-not-match=确认密码与密码不匹配。
signup.error.email-code.invalid=邮箱验证码无效。
12 changes: 6 additions & 6 deletions application/src/main/resources/templates/signup.html
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ <h1 class="form-title" th:text="#{title}"></h1>
<div class="form-item">
<label for="confirmPassword" th:text="#{form.confirmPassword.label}"></label>
<th:block
th:replace="~{gateway_modules/input_fragments :: password(id = 'confirmPassword', name = null, required = 'true', minlength = 6, enableToggle = true)}"
th:replace="~{gateway_modules/input_fragments :: password(id = 'confirmPassword', name = 'confirmPassword', required = 'true', minlength = 6, enableToggle = true)}"
></th:block>
<!-- <p -->
<!-- class="alert alert-error" -->
<!-- th:if="${#fields.hasErrors('confirmPassword')}" -->
<!-- th:errors="*{confirmPassword}" -->
<!-- ></p> -->
<p
class="alert alert-error"
th:if="${#fields.hasErrors('confirmPassword')}"
th:errors="*{confirmPassword}"
></p>
</div>

<div class="form-item">
Expand Down

0 comments on commit 83109d0

Please sign in to comment.