Enforcing Username Uniqueness (#906)

* Enforcing Username Uniqueness

Changes in UserService.java:

Added a new method findByUsername to allow searching for usernames regardless of case sensitivity.
Added a new method isUsernameValid to validate the username.
Changes in UserController.java:

Updated the changeUsername method to ensure the new username is valid before changing it.
Updated the editUser method to ensure the new username is unique and valid.
Changes in UserRepository.java:

Added a custom JPQL query to search for usernames regardless of case sensitivity.
Changes in HTML templates (account.html and addUsers.html):

Error messages are displayed if a username is invalid or already exists.

* JPAs auto
This commit is contained in:
Ludy
2024-03-13 23:09:16 +01:00
committed by GitHub
parent ae73595335
commit 9cc7a49d12
32 changed files with 96 additions and 5 deletions

View File

@@ -176,6 +176,10 @@ public class UserService implements UserServiceInterface {
return userRepository.findByUsername(username);
}
public Optional<User> findByUsernameIgnoreCase(String username) {
return userRepository.findByUsernameIgnoreCase(username);
}
public void changeUsername(User user, String newUsername) {
user.setUsername(newUsername);
userRepository.save(user);
@@ -194,4 +198,8 @@ public class UserService implements UserServiceInterface {
public boolean isPasswordCorrect(User user, String currentPassword) {
return passwordEncoder.matches(currentPassword, user.getPassword());
}
public boolean isUsernameValid(String username) {
return username.matches("[a-zA-Z0-9]+");
}
}