java.lang.Object
edu.ntnu.idatt2106_2023_06.backend.service.users.UserService
All Implemented Interfaces:
IUserService

@Service public class UserService extends Object implements IUserService
This service class handles the business logic for user-related operations. It implements the IUserService interface. It provides methods for updating and deleting users, as well as loading a user by username.
  • Constructor Details

    • UserService

      public UserService()
  • Method Details

    • updateUser

      @Transactional public String updateUser(UserUpdateDTO userUpdateDTO) throws UserNotFoundException
      This method updates a user's information. It first checks if the given user exists in the database. It then checks if the new username already exists, and throws a UserExistsException if it does. It then creates a new User object with the updated information and saves it to the database.
      Specified by:
      updateUser in interface IUserService
      Parameters:
      userUpdateDTO - A UserUpdateDTO object containing the user's current username, the new username (if any), and other updated information.
      Throws:
      UserNotFoundException - If the user is not found in the database.
      UserExistsException - If the new username already exists in the database.
    • updateUserPassword

      @Transactional public void updateUserPassword(UserPasswordUpdateDTO userPasswordUpdateDTO, String username)
      This method allows a user to update their password; however, only if the user provided the correct old password. Additionally, the new password is hashed and salted using BCrypt.
      Specified by:
      updateUserPassword in interface IUserService
      Parameters:
      userPasswordUpdateDTO - Old and new password, given as a UserPasswordUpdateDTO
      username - Username of the user, given as a String
    • deleteUser

      @Transactional public void deleteUser(UserDeletionDTO userDeletionDTO)
      Deletes a user.
      Specified by:
      deleteUser in interface IUserService
      Parameters:
      userDeletionDTO - The DTO containing the username of the user performing the action and the username of the user to delete.
      Throws:
      UserNotFoundException - If either of the users is not found in the database.
      UnauthorizedException - If the user performing the action is not an admin or is trying to delete themselves.
    • loadUserDTOByUsername

      public UserLoadDTO loadUserDTOByUsername(String username)
      Loads a user's DTO information.
      Specified by:
      loadUserDTOByUsername in interface IUserService
      Parameters:
      username - The username of the user to load.
      Returns:
      The DTO containing the user's information.
      Throws:
      org.springframework.security.core.userdetails.UsernameNotFoundException - If the user is not found in the database.
    • loadUser

      public UserLoadDTO loadUser()
      Loads user information from the database. The user is identified by the ID in the JWT token.
      Returns:
      The user object.
      Throws:
      org.springframework.security.core.userdetails.UsernameNotFoundException - If the user is not found in the database.
    • searchUser

      public String searchUser(String username)
      This method searches for users by username.
      Parameters:
      username - The username to search for.
      Returns:
      A JSON string containing the users' information.
    • isSuperUser

      public boolean isSuperUser(Long fridgeId, String username)
      This method checks the isSuperUser status for a user and a given fridge.
      Parameters:
      fridgeId - The id of the fridge the user is, given as a Long object.
      username - The username to search for.
      Returns:
      Boolean representing the superuser status of the user.
    • isUserInFridge

      public boolean isUserInFridge(Long fridgeId, String username)
      This method checks if a given user is in a fridge, given fridge id.
      Parameters:
      fridgeId - The id of the fridge the user is, given as a Long object.
      username - The username to search for.
      Returns:
      Boolean representing whether user is in fridge.