Class UserService
java.lang.Object
edu.ntnu.idatt2106_2023_06.backend.service.users.UserService
- All Implemented Interfaces:
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
deleteUser
(UserDeletionDTO userDeletionDTO) Deletes a user.boolean
isSuperUser
(Long fridgeId, String username) This method checks the isSuperUser status for a user and a given fridge.boolean
isUserInFridge
(Long fridgeId, String username) This method checks if a given user is in a fridge, given fridge id.loadUser()
Loads user information from the database.loadUserDTOByUsername
(String username) Loads a user's DTO information.searchUser
(String username) This method searches for users by username.updateUser
(UserUpdateDTO userUpdateDTO) This method updates a user's information.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.
-
Constructor Details
-
UserService
public UserService()
-
-
Method Details
-
updateUser
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 interfaceIUserService
- 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 interfaceIUserService
- Parameters:
userPasswordUpdateDTO
- Old and new password, given as a UserPasswordUpdateDTOusername
- Username of the user, given as a String
-
deleteUser
Deletes a user.- Specified by:
deleteUser
in interfaceIUserService
- 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
Loads a user's DTO information.- Specified by:
loadUserDTOByUsername
in interfaceIUserService
- 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
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
This method searches for users by username.- Parameters:
username
- The username to search for.- Returns:
- A JSON string containing the users' information.
-
isSuperUser
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
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.
-