mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-21 22:41:05 +08:00
feat: enforce username length
This commit is contained in:
@@ -3,6 +3,7 @@ package com.openisle.service;
|
||||
import com.openisle.model.User;
|
||||
import com.openisle.model.Role;
|
||||
import com.openisle.service.PasswordValidator;
|
||||
import com.openisle.service.UsernameValidator;
|
||||
import com.openisle.exception.FieldException;
|
||||
import com.openisle.repository.UserRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -18,9 +19,11 @@ import java.util.Random;
|
||||
public class UserService {
|
||||
private final UserRepository userRepository;
|
||||
private final PasswordValidator passwordValidator;
|
||||
private final UsernameValidator usernameValidator;
|
||||
private final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
||||
|
||||
public User register(String username, String email, String password) {
|
||||
usernameValidator.validate(username);
|
||||
passwordValidator.validate(password);
|
||||
// ── 先按用户名查 ──────────────────────────────────────────
|
||||
Optional<User> byUsername = userRepository.findByUsername(username);
|
||||
@@ -99,6 +102,7 @@ public class UserService {
|
||||
User user = userRepository.findByUsername(currentUsername)
|
||||
.orElseThrow(() -> new IllegalArgumentException("User not found"));
|
||||
if (newUsername != null && !newUsername.equals(currentUsername)) {
|
||||
usernameValidator.validate(newUsername);
|
||||
userRepository.findByUsername(newUsername).ifPresent(u -> {
|
||||
throw new FieldException("username", "User name already exists");
|
||||
});
|
||||
|
||||
22
src/main/java/com/openisle/service/UsernameValidator.java
Normal file
22
src/main/java/com/openisle/service/UsernameValidator.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package com.openisle.service;
|
||||
|
||||
import com.openisle.exception.FieldException;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* Simple validator for usernames.
|
||||
*/
|
||||
@Service
|
||||
public class UsernameValidator {
|
||||
/**
|
||||
* Validate the username string.
|
||||
*
|
||||
* @param username the username to validate
|
||||
*/
|
||||
public void validate(String username) {
|
||||
if (username == null || username.length() < 6) {
|
||||
throw new FieldException("username", "Username must be at least 6 characters long");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user