diff options
Diffstat (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/service')
3 files changed, 248 insertions, 0 deletions
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java new file mode 100644 index 0000000..5a1a492 --- /dev/null +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java @@ -0,0 +1,70 @@ +package com.ufund.api.ufundapi.service; + +import com.ufund.api.ufundapi.model.User; +import com.ufund.api.ufundapi.model.UserAuth; +import com.ufund.api.ufundapi.persistence.UserAuthDAO; +import org.springframework.stereotype.Component; + +import java.io.IOException; + +@Component +public class AuthService { + + private final UserAuthDAO userAuthDAO; + private final UserService userService; + + public AuthService(UserAuthDAO userAuthDAO, UserService userService) { + this.userAuthDAO = userAuthDAO; + this.userService = userService; + } + + /** + * Check if the provided key has access to the provided user. + * + * @param targetUsername The targetUsername of the user trying to be accessed. + * @param key The api key obtained by the client from logging in. + * @throws IllegalAccessException Thrown if access was denied to the user. + */ + public void authenticate(String targetUsername, String key) throws IllegalAccessException, IOException { + var userAuth = userAuthDAO.getUserAuth(key); + if (userAuth == null) { + throw new IllegalAccessException("Unauthenticated"); + } + + var username = userAuth.getUsername(); + var userType = userService.getUser(username).getType(); + if (!username.equals(targetUsername) && userType != User.UserType.MANAGER) { + throw new IllegalAccessException("Unauthorized"); + } + } + + /** + * Attempt to log in with the provided credentials + * + * @param username The username of the user + * @param password The password of the user + * @return An API key if the authentication was successful. + * @throws IllegalAccessException Thrown if the username or password was incorrect + * @throws IOException If there was an issue saving the authentication + */ + public String login(String username, String password) throws IllegalAccessException, IOException { + var usr = userService.getUser(username); + if (usr == null || !usr.verifyPassword(password)) { + throw new IllegalAccessException("Unauthorized"); + } + var userAuth = UserAuth.generate(username); + userAuthDAO.addUserAuth(userAuth); + return userAuth.getKey(); + } + + /** + * Logs out the current user + * + * @param key The API key to of the client + * @throws IOException Thrown if there was an error saving the authentication + */ + public void logout(String key) throws IOException { + userAuthDAO.removeUserAuth(key); + } + +} diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java new file mode 100644 index 0000000..78f8f85 --- /dev/null +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java @@ -0,0 +1,106 @@ +package com.ufund.api.ufundapi.service; + +import java.io.IOException; +import java.util.Arrays; + +import org.springframework.stereotype.Component; + +import com.ufund.api.ufundapi.DuplicateKeyException; +import com.ufund.api.ufundapi.model.Need; +import com.ufund.api.ufundapi.persistence.CupboardDAO; + +@Component +public class CupboardService { + + private final CupboardDAO cupboardDAO; + + public CupboardService(CupboardDAO cupboardDAO) { + this.cupboardDAO = cupboardDAO; + } + + /** + * Creates a new Need + * + * @param name The name of the need to create + * @param maxGoal The max goal of the new need + * @param goalType The goal type of the new need + * @return The need that was created + * @throws IOException Thrown if there was any issue saving the data + * @throws DuplicateKeyException If there already exists a need with the same name + */ + public Need createNeed(String name, double maxGoal, Need.GoalType goalType) throws IOException, DuplicateKeyException { + + if (maxGoal <= 0) { + throw new IllegalArgumentException("Max Goal must be greater than zero"); + } + + for (Need searchNeed : cupboardDAO.getNeeds()) { + if (searchNeed.getName().equalsIgnoreCase(name)) { + throw new DuplicateKeyException("Duplicate names are not allowed"); + } + } + + Need need = new Need(name, goalType, maxGoal); + return cupboardDAO.addNeed(need); + + } + + /** + * Get all the needs in the cupboard + * + * @return An array containing all needs + * @throws IOException Thrown if there was any issue saving the data + */ + public Need[] getNeeds() throws IOException { + return cupboardDAO.getNeeds(); + } + + /** + * Returns an array of needs filtered by a search + * + * @param search The search substring + * @return The requested array + * @throws IOException Thrown if there was any issue saving the data + */ + public Need[] searchNeeds(String search) throws IOException { + return Arrays.stream(cupboardDAO.getNeeds()) + .filter(i -> i.getName().toLowerCase().contains(search.toLowerCase())) + .toArray(Need[]::new); + } + + /** + * Gets a need with the specified ID + * + * @param id the ID of the need + * @return The resulting Need or null if the need was not found + */ + public Need getNeed(int id) throws IOException { + return cupboardDAO.getNeed(id); + } + + /** + * Updates a need + * + * @param id The ID of the need to update + * @param need The need object to set (note: the ID is ignored) + * @return The updated need object + * @throws IOException Thrown if there was an issue saving the changes + */ + public Need updateNeed(Need need, int id) throws IOException { + if (need.getId() != id) { + throw new IllegalArgumentException("ID in URL and body must match"); + } + return cupboardDAO.updateNeed(need); + } + + /** + * Delete a need from the cupboard + * + * @param id the ID of the need + * @return True if the need was deleted + * @throws IOException Thrown on any problem removing the need + */ + public boolean deleteNeed(int id) throws IOException { + return cupboardDAO.deleteNeed(id); + } +} diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java new file mode 100644 index 0000000..935ee72 --- /dev/null +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java @@ -0,0 +1,72 @@ +package com.ufund.api.ufundapi.service; + +import java.io.IOException; + +import com.ufund.api.ufundapi.DuplicateKeyException; +import com.ufund.api.ufundapi.model.User; +import com.ufund.api.ufundapi.persistence.UserDAO; +import org.springframework.stereotype.Component; + +@Component +public class UserService { + + private final UserDAO userDAO; + + public UserService(UserDAO userDao) { + this.userDAO = userDao; + } + + /** + * Creates a new user + * + * @param username The username of the user + * @param password The password of the user + * @return The created user object + * @throws IOException Thrown on any problem saving the file + */ + public User createUser(String username, String password) throws IOException, DuplicateKeyException { + if (userDAO.getUser(username) != null) { + throw new DuplicateKeyException("A user with this name already exists"); + } + User user = User.create(username, password); + return userDAO.addUser(user); + } + + /** + * Gets a user with the given username + * + * @param username The username of the user + * @return The user object with that username + * @throws IOException If there was any problem saving the file + */ + public User getUser(String username) throws IOException { + return userDAO.getUser(username); + } + + /** + * Updates a user + * + * @param user The ID of the user to update + * @param username The user object to set (note: the ID is ignored) + * @return The updated user object + * @throws IOException Thrown if there was any issue saving the data + */ + public User updateUser(User user, String username) throws IOException { + if (!user.getUsername().equals(username)) { + throw new IllegalArgumentException("ID in URL and body must match"); + } + return userDAO.updateUser(user); + } + + /** + * Deletes a user + * + * @param username The username of the user to delete + * @return True if the user was deleted + * @throws IOException Thrown if there was any issue saving the data + */ + public boolean deleteUser(String username) throws IOException { + return userDAO.deleteUser(username); + } + +} |