Package vaultWeb.services
Class UserService
java.lang.Object
vaultWeb.services.UserService
Service class for managing users.
Provides functionality for user registration, checking for existing usernames, and retrieving all users. Passwords are securely encoded before storing.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionRetrieves a list of all registered users.voidregisterUser(User user) Registers a new user by encoding their password and assigning the default role.booleanusernameExists(String username) Checks if a username already exists in the database.
-
Constructor Details
-
UserService
public UserService()
-
-
Method Details
-
registerUser
Registers a new user by encoding their password and assigning the default role.Steps performed by this method:
- Check if the username already exists in the database. If so, throw
DuplicateUsernameException. - Encode the plaintext password using the injected
PasswordEncoder. - Save the user entity with the hashed password to the database via
UserRepository.
Important: - The PasswordEncoder bean must match the encoder used during authentication to correctly verify passwords.
- Parameters:
user- TheUserentity containing username and plaintext password.- Throws:
DuplicateUsernameException- if a user with the same username already exists.
- Check if the username already exists in the database. If so, throw
-
usernameExists
Checks if a username already exists in the database.- Parameters:
username- The username to check.- Returns:
trueif the username exists,falseotherwise.
-
getAllUsers
Retrieves a list of all registered users.
-