Class UserServiceImpl
java.lang.Object
com.app.service.impl.UserServiceImpl
- All Implemented Interfaces:
UserService
Implementation of
UserService
providing user management functionality.
This service handles user creation, activation, password management,
email changes, user updates, retrieval and deletion operations.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionactivateUser
(UserActivationTokenDto userActivationTokenDto) Activates a user based on the provided activation token.changeEmail
(NewEmailDto newEmailDto, String token) Changes the email address of the authenticated user.changePassword
(ChangePasswordDto changePasswordDto, String token) Changes the password for the currently authenticated user.createUser
(CreateUserDto createUserDto) Creates a new user with given data.deleteUser
(Long userId, String token) Deletes a user by ID if the logged-in user is an admin.deleteUser
(String token) Deletes the currently authenticated user.getActualLoginRole
(String token) Retrieves the role of the currently logged-in user based on the authentication token.getActualLoginUser
(String token) Retrieves the currently logged-in user based on the authentication token.getUserById
(Long id) Retrieves a user by their ID.getUsers
(UserSpecificationDto userSpecificationDto) Retrieves a list of users filtered by the given specification.void
init()
Initializes the service by saving an admin user on startup.lostPassword
(EmailDto emailDto) Initiates password reset process by publishing activation event.newPassword
(NewPasswordDto newPasswordDto) Sets a new password for a user based on a valid token.refreshVerificationEmailToken
(EmailDto emailDto) Refreshes the verification email token for a user.updateUser
(UpdateUserDto updateUserDto) Updates user role and holiday hours.
-
Constructor Details
-
UserServiceImpl
public UserServiceImpl()
-
-
Method Details
-
init
@PostConstruct public void init()Initializes the service by saving an admin user on startup. The password is encoded before saving. -
createUser
Creates a new user with given data. Validates the input and checks for existing username and email. Encodes the password before saving. Publishes a user activation event.- Specified by:
createUser
in interfaceUserService
- Parameters:
createUserDto
- the DTO containing user creation data- Returns:
- the ID of the created user
- Throws:
ResourceAlreadyExistException
- if username or email already existsValidationException
- if validation of DTO fails
-
activateUser
Activates a user based on the provided activation token. Validates the token and deletes it after successful activation.- Specified by:
activateUser
in interfaceUserService
- Parameters:
userActivationTokenDto
- DTO containing the activation token- Returns:
- the ID of the activated user
- Throws:
ValidationException
- if token is null or expiredjakarta.persistence.EntityNotFoundException
- if token is not found
-
refreshVerificationEmailToken
Refreshes the verification email token for a user. Validates the input email and checks if the user is already activated. If an existing valid token exists, throws an exception. Otherwise, deletes expired token and publishes a new activation event.- Specified by:
refreshVerificationEmailToken
in interfaceUserService
- Parameters:
emailDto
- DTO containing the user's email- Returns:
- the ID of the user for whom the token was refreshed
- Throws:
ValidationException
- if user is already activated or validation failsjakarta.persistence.EntityNotFoundException
- if user is not foundResourceAlreadyExistException
- if valid token already exists
-
changePassword
Changes the password for the currently authenticated user. Validates input and verifies current password before changing.- Specified by:
changePassword
in interfaceUserService
- Parameters:
changePasswordDto
- DTO containing current and new passwordtoken
- authentication token of the user- Returns:
- the ID of the user whose password was changed
- Throws:
ValidationException
- if validation failsjakarta.persistence.EntityNotFoundException
- if user is not foundIllegalArgumentException
- if current password does not match
-
lostPassword
Initiates password reset process by publishing activation event. Validates the email and verifies user existence.- Specified by:
lostPassword
in interfaceUserService
- Parameters:
emailDto
- DTO containing the user's email- Returns:
- the ID of the user who lost the password
- Throws:
ValidationException
- if validation failsjakarta.persistence.EntityNotFoundException
- if user is not found
-
newPassword
Sets a new password for a user based on a valid token. Validates the new password DTO and the token.- Specified by:
newPassword
in interfaceUserService
- Parameters:
newPasswordDto
- DTO containing the token and new password- Returns:
- the ID of the user whose password was updated, or null if invalid
- Throws:
ValidationException
- if validation failsjakarta.persistence.EntityNotFoundException
- if token is not found
-
changeEmail
Changes the email address of the authenticated user. Validates input and verifies current password before updating.- Specified by:
changeEmail
in interfaceUserService
- Parameters:
newEmailDto
- DTO containing the current password and new emailtoken
- authentication token of the user- Returns:
- the ID of the user whose email was changed
- Throws:
ValidationException
- if validation failsjakarta.persistence.EntityNotFoundException
- if user is not foundIllegalArgumentException
- if current password does not match
-
updateUser
Updates user role and holiday hours.- Specified by:
updateUser
in interfaceUserService
- Parameters:
updateUserDto
- DTO containing user ID, new role, and holiday hours- Returns:
- the ID of the updated user
- Throws:
jakarta.persistence.EntityNotFoundException
- if user is not found
-
getUsers
Retrieves a list of users filtered by the given specification.- Specified by:
getUsers
in interfaceUserService
- Parameters:
userSpecificationDto
- DTO with user filtering criteria- Returns:
- list of users matching the specification
- Throws:
ValidationException
- if the specification DTO is null
-
getActualLoginUser
Retrieves the currently logged-in user based on the authentication token.- Specified by:
getActualLoginUser
in interfaceUserService
- Parameters:
token
- authentication token- Returns:
- the DTO of the logged-in user
- Throws:
jakarta.persistence.EntityNotFoundException
- if user is not found
-
getActualLoginRole
Retrieves the role of the currently logged-in user based on the authentication token.- Specified by:
getActualLoginRole
in interfaceUserService
- Parameters:
token
- authentication token- Returns:
- the role of the logged-in user
- Throws:
jakarta.persistence.EntityNotFoundException
- if user is not found
-
getUserById
Retrieves a user by their ID.- Specified by:
getUserById
in interfaceUserService
- Parameters:
id
- the user ID- Returns:
- the user DTO
- Throws:
jakarta.persistence.EntityNotFoundException
- if user is not found
-
deleteUser
Deletes the currently authenticated user. Admin users cannot be deleted.- Specified by:
deleteUser
in interfaceUserService
- Parameters:
token
- authentication token of the user to delete- Returns:
- the ID of the deleted user
- Throws:
ValidationException
- if user is adminjakarta.persistence.EntityNotFoundException
- if user is not found
-
deleteUser
Deletes a user by ID if the logged-in user is an admin.- Specified by:
deleteUser
in interfaceUserService
- Parameters:
userId
- the ID of the user to deletetoken
- authentication token of the logged-in user- Returns:
- the ID of the deleted user
- Throws:
jakarta.persistence.EntityNotFoundException
- if logged-in user or user to delete is not found, or if logged-in user is not admin
-