Class UserService
java.lang.Object
tech.lastbox.lastshield.basicauth.service.UserService
@Service
@ConditionalOnProperty(name="lastshield.basicauth",
havingValue="true")
public class UserService
extends Object
Service class for managing user-related operations, such as creating new users, logging in, and updating user details.
This service is conditionally enabled based on the 'lastshield.basicauth' property, meaning it is only active
when basic authentication is enabled in the application.
-
Constructor Summary
ConstructorsConstructorDescriptionUserService(UserRepository userRepository, org.springframework.security.crypto.password.PasswordEncoder passwordEncoder) -
Method Summary
Modifier and TypeMethodDescriptioncreateUser(String name, String username, String rawPassword) Creates a new user.findUserByUsername(String username) Find a user by their username.Retrieves all users as UserDTO objects.Attempts to log in with the provided username and password.updateUser(User user) Updates the user details.
-
Constructor Details
-
UserService
public UserService(UserRepository userRepository, org.springframework.security.crypto.password.PasswordEncoder passwordEncoder)
-
-
Method Details
-
findUserByUsername
Find a user by their username.- Parameters:
username- the username of the user to find.- Returns:
- the User object if found.
- Throws:
UsernameNotFoundException- if no user with the given username is found.
-
createUser
public User createUser(String name, String username, String rawPassword) throws DuplicatedUserException Creates a new user. If a user with the given username already exists, throws DuplicatedUserException.- Parameters:
name- the name of the new user.username- the username of the new user.rawPassword- the raw password for the new user.- Returns:
- the created User object.
- Throws:
DuplicatedUserException- if a user with the given username already exists.
-
login
Attempts to log in with the provided username and password. Returns an Optional containing the user if the login is successful (username matches and password is correct).- Parameters:
username- the username to log in with.rawPassword- the raw password to check.- Returns:
- an Optional containing the User if login is successful, empty otherwise.
-
getUsersAsDTO
Retrieves all users as UserDTO objects.- Returns:
- a List of UserDTOs representing all users.
-
updateUser
Updates the user details. If the user is not registered (i.e., ID is null or 0), throws UnregisteredUserException.- Parameters:
user- the user object containing the updated information.- Returns:
- the updated User object.
- Throws:
UnregisteredUserException- if the user is not registered in the database.
-