ApiUserController.java

  1. package com.privacydashboard.application.data.apiController;

  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import com.fasterxml.jackson.databind.node.ArrayNode;
  4. import com.fasterxml.jackson.databind.node.ObjectNode;
  5. import com.privacydashboard.application.data.GlobalVariables.Role;
  6. import com.privacydashboard.application.data.entity.IoTApp;
  7. import com.privacydashboard.application.data.entity.User;
  8. import com.privacydashboard.application.data.service.DataBaseService;
  9. import com.privacydashboard.application.security.UserDetailsServiceImpl;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.http.ResponseEntity;
  12. import org.springframework.web.bind.MissingServletRequestParameterException;
  13. import org.springframework.web.bind.annotation.*;

  14. import java.io.IOException;
  15. import java.util.List;

  16. @RestController
  17. public class ApiUserController {
  18.     @Autowired
  19.     private DataBaseService dataBaseService;
  20.     @Autowired
  21.     private UserDetailsServiceImpl userDetailsServiceImpl;
  22.     @Autowired
  23.     private ApiGeneralController apiGeneralController;

  24.     /**
  25.      * Get information about a user
  26.      * RESTRICTIONS: NONE
  27.      * @param userId Id of the User to get information from
  28.      * @return Json object representing the User. Bad request if User with that Id does not exist
  29.      */
  30.     @GetMapping
  31.     @RequestMapping("api/user/get")
  32.     public ResponseEntity<?> get(@RequestParam() String userId){
  33.         try {
  34.             User user= apiGeneralController.getUserFromId(userId);
  35.             ObjectNode userJson = apiGeneralController.createJsonFromUser(user);
  36.             userJson.remove("mail");
  37.             userJson.remove("hashedPassword");
  38.             return ResponseEntity.ok(userJson);
  39.         }
  40.         catch (IllegalArgumentException e){
  41.             return ResponseEntity.badRequest().body(e.getMessage());
  42.         }
  43.     }

  44.     /**
  45.      * Get information about a user, including the mail
  46.      * RESTRICTIONS: The one calling the function MUST BE the user identified by the userId, a contact of him or userId is a Controller or DPO
  47.      * @param userId Id of the User to get information from
  48.      * @return Json object representing the User. Bad request if User with that Id does not exist or the one calling the function is not allowed to access these informations
  49.      */
  50.     @GetMapping
  51.     @RequestMapping("api/user/getDetailed")
  52.     public ResponseEntity<?> getDetailed(@RequestParam() String userId){
  53.         try {
  54.             User user= apiGeneralController.getUserFromId(userId);
  55.             if(!users1CanGetDetailOfUser2(apiGeneralController.getAuthenicatedUser(), user)){
  56.                 return ResponseEntity.badRequest().body("you must be the user identified by the userId");
  57.             }
  58.             ObjectNode userJson = apiGeneralController.createJsonFromUser(user);
  59.             userJson.remove("hashedPassword");
  60.             return ResponseEntity.ok(userJson);
  61.         }
  62.         catch (IllegalArgumentException e){
  63.             return ResponseEntity.badRequest().body(e.getMessage());
  64.         }
  65.     }

  66.     /**
  67.      * Get all the contacts of a user, excluding password
  68.      * RESTRICTIONS: The one calling the function MUST BE the user identified by the userId
  69.      * @param userId Id of the User to get information from
  70.      * @return Json object representing all the contacts. Bad request if User with that Id does not exist or the one calling the function is not the user identified by the userId
  71.      */
  72.     @GetMapping
  73.     @RequestMapping("api/user/getAllContacts")
  74.     public ResponseEntity<?> getAllContacts(@RequestParam() String userId){
  75.         try {
  76.             User user= apiGeneralController.getUserFromId(userId);
  77.             if(!apiGeneralController.isAuthenticatedUserId(userId)){
  78.                 return ResponseEntity.badRequest().body("you must be the user identified by the userId");
  79.             }
  80.             List<User> contacts= dataBaseService.getAllContactsFromUser(user);
  81.             ObjectMapper mapper = new ObjectMapper();
  82.             ObjectNode contactJson;
  83.             ArrayNode contactsArray = mapper.createArrayNode();
  84.             for(User contact : contacts){
  85.                 contactJson= apiGeneralController.createJsonFromUser(contact);
  86.                 contactJson.remove("hashedPassword");
  87.                 contactsArray.add(contactJson);
  88.             }

  89.             return ResponseEntity.ok(contactsArray);
  90.         }
  91.         catch (IllegalArgumentException e){
  92.             return ResponseEntity.badRequest().body(e.getMessage());
  93.         }
  94.     }

  95.     /**
  96.      * Get all the apps of a user if user=controller/dpo. Get only the app you have if user=subject
  97.      * RESTRICTIONS: user MUST be controller/DPO OR You must be controller/dpo of some apps of the user
  98.      * @param userId ID of the user to get apps from
  99.      * @return JSON representing all the apps. Bad request if user does not exist or not authorized
  100.      */
  101.     @GetMapping
  102.     @RequestMapping("api/user/getApps")
  103.     public  ResponseEntity<?> getApps(@RequestParam() String userId){
  104.         try{
  105.             User user= apiGeneralController.getUserFromId(userId);
  106.             List<IoTApp> appList;
  107.             User authenticatedUser= apiGeneralController.getAuthenicatedUser();
  108.             if(user.getRole().equals(Role.CONTROLLER) || user.getRole().equals(Role.DPO)) {
  109.                 appList = dataBaseService.getUserApps(user);
  110.             }
  111.             else if(authenticatedUser.getRole().equals(Role.CONTROLLER) || authenticatedUser.getRole().equals(Role.DPO)){
  112.                 appList= dataBaseService.getAppsFrom2Users(user, authenticatedUser);
  113.             }
  114.             else{
  115.                 return ResponseEntity.badRequest().body("User must be Controller/DPO or you must be associated with that user");
  116.             }
  117.             ArrayNode appsArray= new ObjectMapper().createArrayNode();
  118.             for(IoTApp app : appList){
  119.                 appsArray.add(apiGeneralController.createJsonFromApp(app));
  120.             }
  121.             return ResponseEntity.ok(appsArray);
  122.         } catch (IllegalArgumentException e){
  123.             return ResponseEntity.badRequest().body(e.getMessage());
  124.         }
  125.     }

  126.     /**
  127.      * Create a new user
  128.      * RESTRICTIONS: NONE
  129.      * @param body a JSON with the information about the user. It MUST include the NAME, ROLE, PASSWORD
  130.      * @return OK If user successfully added. Bad Request if Json is invalid, ID is invalid, there is already a user with that ID
  131.      */
  132.     @PostMapping
  133.     @RequestMapping("api/user/add")
  134.     public ResponseEntity<?> add(@RequestBody String body){
  135.         try {
  136.             User user=apiGeneralController.getUserFromJsonString(true, body);
  137.             if(user.getId()!=null && dataBaseService.getApp(user.getId()).isPresent()){
  138.                 return ResponseEntity.badRequest().body("cannot create this user");
  139.             }
  140.             dataBaseService.addUser(user);
  141.             return ResponseEntity.ok("user created successfully");
  142.         }
  143.         catch (IllegalArgumentException e){
  144.             return ResponseEntity.badRequest().body(e.getMessage());
  145.         } catch (IOException e){
  146.             return ResponseEntity.badRequest().body("invalid JSON");
  147.         }
  148.     }

  149.     /**
  150.      * Delete the user identified by the userId
  151.      * RESTRICTIONS: The one calling the function MUST BE the user identified by the userId
  152.      * @param userId Id of the user to be deleted
  153.      * @return Ok if successfully deleted. BadRequest if ID not valid, or the one calling the function is not the user identified by the userId
  154.      */
  155.     @DeleteMapping
  156.     @RequestMapping("api/user/delete")
  157.     public ResponseEntity<?> delete(@RequestParam() String userId){
  158.         try {
  159.             User user= apiGeneralController.getUserFromId(userId);
  160.             if(apiGeneralController.isAuthenticatedUserId(userId)){
  161.                 dataBaseService.deleteUser(user.getId());
  162.                 return ResponseEntity.ok("app deleted successfully");
  163.             }
  164.             else{
  165.                 return ResponseEntity.badRequest().body("you must be the user to be deleted");
  166.             }
  167.         }
  168.         catch (IllegalArgumentException e){
  169.             return ResponseEntity.badRequest().body(e.getMessage());
  170.         }
  171.     }

  172.     /**
  173.      * Update some values of the user identified by the userID
  174.      * RESTRICTIONS: The one calling the function MUST BE the user identified by the userId
  175.      * @param userId Id of the user to be updated
  176.      * @param body JSON with values to be updated
  177.      * @return Ok if successfully updated, BadRequest if JSON invalid, ID does not exist, User performing the action is not the one with that ID
  178.      */
  179.     @PutMapping
  180.     @RequestMapping("api/user/update")
  181.     public ResponseEntity<?> update(@RequestParam String userId, @RequestBody String body){
  182.         try{
  183.             User user1= apiGeneralController.getUserFromId(userId);
  184.             if(!apiGeneralController.isAuthenticatedUserId(userId)){
  185.                 return ResponseEntity.badRequest().body("you must be the user to be updated");
  186.             }
  187.             User user2= apiGeneralController.getUserFromJsonString(false, body);
  188.             dataBaseService.changeValuesForUser(user1, user2.getName(), user2.getHashedPassword(), user2.getMail());
  189.             return ResponseEntity.ok("user updated successfully");
  190.         }
  191.         catch (IllegalArgumentException e){
  192.             return ResponseEntity.badRequest().body(e.getMessage());
  193.         } catch (IOException e){
  194.             return ResponseEntity.badRequest().body("invalid JSON");
  195.         }
  196.     }

  197.     private boolean users1CanGetDetailOfUser2(User user1, User user2){
  198.         if(user1.equals(user2)){
  199.             return true;
  200.         }
  201.         if(dataBaseService.getAllContactsFromUser(user1).contains(user2)){
  202.             return true;
  203.         }
  204.         return user2.getRole().equals(Role.CONTROLLER) || user2.getRole().equals(Role.DPO);
  205.     }

  206.     @ExceptionHandler(MissingServletRequestParameterException.class)
  207.     public ResponseEntity<?> handleMissingParams(MissingServletRequestParameterException ex) {
  208.         return ResponseEntity.badRequest().body(ex.getParameterName() + " parameter is missing");
  209.     }
  210. }