Class UserServiceImpl

java.lang.Object
com.app.service.impl.UserServiceImpl
All Implemented Interfaces:
UserService

@Service public class UserServiceImpl extends Object implements 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 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

      public Long createUser(CreateUserDto createUserDto)
      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 interface UserService
      Parameters:
      createUserDto - the DTO containing user creation data
      Returns:
      the ID of the created user
      Throws:
      ResourceAlreadyExistException - if username or email already exists
      ValidationException - if validation of DTO fails
    • activateUser

      public Long activateUser(UserActivationTokenDto userActivationTokenDto)
      Activates a user based on the provided activation token. Validates the token and deletes it after successful activation.
      Specified by:
      activateUser in interface UserService
      Parameters:
      userActivationTokenDto - DTO containing the activation token
      Returns:
      the ID of the activated user
      Throws:
      ValidationException - if token is null or expired
      jakarta.persistence.EntityNotFoundException - if token is not found
    • refreshVerificationEmailToken

      public Long refreshVerificationEmailToken(EmailDto emailDto)
      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 interface UserService
      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 fails
      jakarta.persistence.EntityNotFoundException - if user is not found
      ResourceAlreadyExistException - if valid token already exists
    • changePassword

      public Long changePassword(ChangePasswordDto changePasswordDto, String token)
      Changes the password for the currently authenticated user. Validates input and verifies current password before changing.
      Specified by:
      changePassword in interface UserService
      Parameters:
      changePasswordDto - DTO containing current and new password
      token - authentication token of the user
      Returns:
      the ID of the user whose password was changed
      Throws:
      ValidationException - if validation fails
      jakarta.persistence.EntityNotFoundException - if user is not found
      IllegalArgumentException - if current password does not match
    • lostPassword

      public Long lostPassword(EmailDto emailDto)
      Initiates password reset process by publishing activation event. Validates the email and verifies user existence.
      Specified by:
      lostPassword in interface UserService
      Parameters:
      emailDto - DTO containing the user's email
      Returns:
      the ID of the user who lost the password
      Throws:
      ValidationException - if validation fails
      jakarta.persistence.EntityNotFoundException - if user is not found
    • newPassword

      public Long newPassword(NewPasswordDto newPasswordDto)
      Sets a new password for a user based on a valid token. Validates the new password DTO and the token.
      Specified by:
      newPassword in interface UserService
      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 fails
      jakarta.persistence.EntityNotFoundException - if token is not found
    • changeEmail

      public Long changeEmail(NewEmailDto newEmailDto, String token)
      Changes the email address of the authenticated user. Validates input and verifies current password before updating.
      Specified by:
      changeEmail in interface UserService
      Parameters:
      newEmailDto - DTO containing the current password and new email
      token - authentication token of the user
      Returns:
      the ID of the user whose email was changed
      Throws:
      ValidationException - if validation fails
      jakarta.persistence.EntityNotFoundException - if user is not found
      IllegalArgumentException - if current password does not match
    • updateUser

      public Long updateUser(UpdateUserDto updateUserDto)
      Updates user role and holiday hours.
      Specified by:
      updateUser in interface UserService
      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

      public List<UserDto> getUsers(UserSpecificationDto userSpecificationDto)
      Retrieves a list of users filtered by the given specification.
      Specified by:
      getUsers in interface UserService
      Parameters:
      userSpecificationDto - DTO with user filtering criteria
      Returns:
      list of users matching the specification
      Throws:
      ValidationException - if the specification DTO is null
    • getActualLoginUser

      public UserDto getActualLoginUser(String token)
      Retrieves the currently logged-in user based on the authentication token.
      Specified by:
      getActualLoginUser in interface UserService
      Parameters:
      token - authentication token
      Returns:
      the DTO of the logged-in user
      Throws:
      jakarta.persistence.EntityNotFoundException - if user is not found
    • getActualLoginRole

      public Role getActualLoginRole(String token)
      Retrieves the role of the currently logged-in user based on the authentication token.
      Specified by:
      getActualLoginRole in interface UserService
      Parameters:
      token - authentication token
      Returns:
      the role of the logged-in user
      Throws:
      jakarta.persistence.EntityNotFoundException - if user is not found
    • getUserById

      public UserDto getUserById(Long id)
      Retrieves a user by their ID.
      Specified by:
      getUserById in interface UserService
      Parameters:
      id - the user ID
      Returns:
      the user DTO
      Throws:
      jakarta.persistence.EntityNotFoundException - if user is not found
    • deleteUser

      public Long deleteUser(String token)
      Deletes the currently authenticated user. Admin users cannot be deleted.
      Specified by:
      deleteUser in interface UserService
      Parameters:
      token - authentication token of the user to delete
      Returns:
      the ID of the deleted user
      Throws:
      ValidationException - if user is admin
      jakarta.persistence.EntityNotFoundException - if user is not found
    • deleteUser

      public Long deleteUser(Long userId, String token)
      Deletes a user by ID if the logged-in user is an admin.
      Specified by:
      deleteUser in interface UserService
      Parameters:
      userId - the ID of the user to delete
      token - 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