aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/main/java/com/ufund/api/ufundapi
diff options
context:
space:
mode:
Diffstat (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi')
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java65
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java5
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java38
3 files changed, 73 insertions, 35 deletions
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java
index aa9598d..02526af 100644
--- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java
+++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java
@@ -5,29 +5,30 @@ import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
-import com.ufund.api.ufundapi.persistence.UserAuthDAO;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.ufund.api.ufundapi.model.User;
-import com.ufund.api.ufundapi.persistence.UserDAO;
+import com.ufund.api.ufundapi.service.AuthService;
+import com.ufund.api.ufundapi.service.UserService;
@RestController
@RequestMapping("users")
public class UserController {
private static final Logger LOG = Logger.getLogger(UserController.class.getName());
- private final UserDAO UserDAO;
- private final UserAuthDAO userAuthDAO;
+ private final UserService userService;
+ private final AuthService authService;
/**
- * Create a user controller to receive REST signals
- *
- * @param userDAO The Data Access Object
+ * Creates a UserController
+ *
+ * @param userService
+ * @param authService
*/
- public UserController(UserDAO userDAO, UserAuthDAO userAuthDAO) {
- this.UserDAO = userDAO;
- this.userAuthDAO = userAuthDAO;
+ public UserController(UserService userService, AuthService authService) {
+ this.userService = userService;
+ this.authService = authService;
}
/**
@@ -37,13 +38,14 @@ public class UserController {
* otherwise
*/
@PostMapping("")
- public ResponseEntity<Boolean> createUser(@RequestBody Map<String, String> params) {
+ public ResponseEntity<User> createUser(@RequestBody Map<String, String> params) {
String username = params.get("username");
String password = params.get("password");
try {
- if (UserDAO.addUser(User.create(username, password)) != null) {
- return new ResponseEntity<>(true, HttpStatus.CREATED);
+ User user = userService.createUser(username, password);
+ if (user == null) {
+ return new ResponseEntity<>(user, HttpStatus.CREATED);
} else {
return new ResponseEntity<>(HttpStatus.CONFLICT);
}
@@ -65,19 +67,16 @@ public class UserController {
public ResponseEntity<User> getUser(@PathVariable String username, @RequestHeader("jelly-api-key") String key) {
LOG.log(Level.INFO, "GET /user/{0}", username);
- var userAuth = userAuthDAO.getUserAuth(key);
- if (userAuth == null || !userAuth.getUsername().equals(username)) {
- return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
- }
-
try {
- User user = UserDAO.getUser(username);
+ authService.authenticate(username, key);
+ User user = userService.getUser(username);
if (user != null) {
return new ResponseEntity<>(user.withoutPasswordHash(), HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
-
+ } catch (IllegalAccessException ex) {
+ return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
} catch (IOException e) {
LOG.log(Level.SEVERE, e.getLocalizedMessage());
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
@@ -92,16 +91,12 @@ public class UserController {
* @return OK response and the user if it was successful, or
* INTERNAL_SERVER_ERROR if there was an issue
*/
- @PutMapping("/{name}")
- public ResponseEntity<User> updateUser(@RequestBody User user, @PathVariable String name, @RequestHeader("jelly-api-key") String key) {
-
- var userAuth = userAuthDAO.getUserAuth(key);
- if (userAuth == null || !userAuth.getUsername().equals(user.getUsername())) {
- return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
- }
+ @PutMapping("/{username}")
+ public ResponseEntity<User> updateUser(@RequestBody User user, @PathVariable String username, @RequestHeader("jelly-api-key") String key) {
try {
- user = UserDAO.updateUser(user, name);
+ authService.authenticate(username, key);
+ user = userService.updateUser(user, username);
if (user != null) {
return new ResponseEntity<>(user, HttpStatus.OK);
} else {
@@ -110,6 +105,8 @@ public class UserController {
} catch (IOException e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
+ } catch (IllegalAccessException e) {
+ return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
}
@@ -121,21 +118,19 @@ public class UserController {
* INTERNAL_SERVER_ERROR if an error occurred
*/
@DeleteMapping("/{username}")
- public ResponseEntity<User> deleteUser(@PathVariable String username, @RequestHeader("jelly-api-key") String key) {
-
- var userAuth = userAuthDAO.getUserAuth(key);
- if (userAuth == null || !userAuth.getUsername().equals(username)) {
- return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
- }
+ public ResponseEntity<Boolean> deleteUser(@PathVariable String username, @RequestHeader("jelly-api-key") String key) {
try {
- if (UserDAO.deleteUser(username)) {
+ authService.authenticate(username, key);
+ if (userService.deleteUser(username)) {
return new ResponseEntity<>(HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
} catch (IOException e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
+ } catch (IllegalAccessException e) {
+ return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
}
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java
index 54ce74a..4f43f8c 100644
--- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java
+++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java
@@ -97,6 +97,11 @@ public class UserFileDAO implements UserDAO {
synchronized (users) {
var res = users.putIfAbsent(user.getUsername(), user);
save();
+ if (res == null) {
+ return user;
+ } else {
+
+ }
return res;
}
}
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
index 994512d..c23bf89 100644
--- 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
@@ -1,5 +1,43 @@
package com.ufund.api.ufundapi.service;
+import java.io.IOException;
+
+import com.ufund.api.ufundapi.model.User;
+import com.ufund.api.ufundapi.persistence.UserAuthDAO;
+import com.ufund.api.ufundapi.persistence.UserDAO;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Component;
+
+@Component
public class UserService {
+
+ private final UserDAO userDAO;
+
+ /**
+ * Create a user controller to receive REST signals
+ *
+ * @param userDao The Data Access Object
+ */
+ public UserService(UserDAO userDao, AuthService authService) {
+ this.userDAO = userDao;
+ }
+
+ public User createUser(String username, String password) throws IOException {
+ User user = User.create(username, password);
+ return userDAO.addUser(user);
+ }
+
+ public User getUser(String username) throws IOException, IllegalAccessException {
+ return userDAO.getUser(username);
+ }
+
+ public User updateUser(User user, String name) throws IllegalAccessException, IOException {
+ return userDAO.updateUser(user, name);
+ }
+
+ public Boolean deleteUser(String username) throws IllegalAccessException, IOException {
+ return userDAO.deleteUser(username);
+ }
}