From 685ecffcea9f1e3ae527e07bbd7234fedb661560 Mon Sep 17 00:00:00 2001 From: Paulo <pimenta@cines.fr> Date: Tue, 24 Nov 2020 10:39:03 +0100 Subject: [PATCH 1/5] fix(user-auth): Removed role field from smart harvester user authentication --- .../SmartHarvesterAuthController.java | 39 +------------------ .../model/login/request/SignupRequest.java | 8 ---- .../model/user/SmartHarvesterUser.java | 11 +----- .../security/services/UserService.java | 8 +--- 4 files changed, 4 insertions(+), 62 deletions(-) diff --git a/src/main/java/com/smartharvester/controller/SmartHarvesterAuthController.java b/src/main/java/com/smartharvester/controller/SmartHarvesterAuthController.java index 9e5a8dd..9a8d98b 100644 --- a/src/main/java/com/smartharvester/controller/SmartHarvesterAuthController.java +++ b/src/main/java/com/smartharvester/controller/SmartHarvesterAuthController.java @@ -42,7 +42,6 @@ public class SmartHarvesterAuthController { JwtUtils jwtUtils; @PostMapping("/signin") - @Tag(name = "Test") public ResponseEntity<?> authenticateUser(@Valid @RequestBody SignInRequest signInRequest) { Authentication authentication = authenticationManager.authenticate( @@ -51,9 +50,6 @@ public class SmartHarvesterAuthController { String jwt = jwtUtils.generateJwtToken(authentication); UserService userDetails = (UserService) authentication.getPrincipal(); - //List<String> role = userDetails.getAuthorities().stream() - // .map(item -> item.getAuthority()) - // .collect(Collectors.toList()); return ResponseEntity.ok(new JwtResponse(jwt, userDetails.getId(), @@ -79,40 +75,7 @@ public class SmartHarvesterAuthController { // Create new user's account SmartHarvesterUser user = new SmartHarvesterUser(UUID.randomUUID().toString(), signUpRequest.getFirstName(), signUpRequest.getLastName(),signUpRequest.getEmail(), - encoder.encode(signUpRequest.getPassword()),signUpRequest.getRole()); - /* - Set<String> strRoles = signUpRequest.getRoles(); - Set<Role> roles = new HashSet<>(); - - if (strRoles == null) { - Role userRole = roleRepository.findByName(ERole.ROLE_USER) - .orElseThrow(() -> new RuntimeException("Error: Role is not found.")); - roles.add(userRole); - } else { - strRoles.forEach(role -> { - switch (role) { - case "admin": - Role adminRole = roleRepository.findByName(ERole.ROLE_ADMIN) - .orElseThrow(() -> new RuntimeException("Error: Role is not found.")); - roles.add(adminRole); - - break; - case "mod": - Role modRole = roleRepository.findByName(ERole.ROLE_MODERATOR) - .orElseThrow(() -> new RuntimeException("Error: Role is not found.")); - roles.add(modRole); - - break; - default: - Role userRole = roleRepository.findByName(ERole.ROLE_USER) - .orElseThrow(() -> new RuntimeException("Error: Role is not found.")); - roles.add(userRole); - } - }); - } - - user.setRoles(roles); - */ + encoder.encode(signUpRequest.getPassword())); userRepository.save(user); diff --git a/src/main/java/com/smartharvester/model/login/request/SignupRequest.java b/src/main/java/com/smartharvester/model/login/request/SignupRequest.java index 604a9a1..b024229 100644 --- a/src/main/java/com/smartharvester/model/login/request/SignupRequest.java +++ b/src/main/java/com/smartharvester/model/login/request/SignupRequest.java @@ -19,7 +19,6 @@ public class SignupRequest { private String lastName; @NotBlank private String email; - private String role; private CharSequence password; public String getFirstName() { @@ -54,11 +53,4 @@ public class SignupRequest { this.password = password; } - public String getRole() { - return this.role; - } - - public void setRole(String role) { - this.role = role; - } } diff --git a/src/main/java/com/smartharvester/model/user/SmartHarvesterUser.java b/src/main/java/com/smartharvester/model/user/SmartHarvesterUser.java index 0958e32..bd75b42 100644 --- a/src/main/java/com/smartharvester/model/user/SmartHarvesterUser.java +++ b/src/main/java/com/smartharvester/model/user/SmartHarvesterUser.java @@ -21,16 +21,14 @@ public class SmartHarvesterUser { private String email; @Field("passwordHash") private String password; - private String role; public SmartHarvesterUser(String uuid, String firstName, String lastName, String email, - String password, String role) { + String password) { this.firstName = firstName; this.lastName = lastName; this.uuid = uuid; this.email = email; this.password = password; - this.role = role; } public String getId() { @@ -81,11 +79,4 @@ public class SmartHarvesterUser { this.password = password; } - public String getRole() { - return role; - } - - public void setRole(String role) { - this.role = role; - } } diff --git a/src/main/java/com/smartharvester/security/services/UserService.java b/src/main/java/com/smartharvester/security/services/UserService.java index 6db92b8..0496c98 100644 --- a/src/main/java/com/smartharvester/security/services/UserService.java +++ b/src/main/java/com/smartharvester/security/services/UserService.java @@ -28,26 +28,22 @@ public class UserService implements UserDetails { private GrantedAuthority authority; - public UserService(String id, String firstName, String lastName, String email, String password, - GrantedAuthority authority) { + public UserService(String id, String firstName, String lastName, String email, String password) { this.id = id; this.firstName = firstName; this.lastName = lastName; this.email = email; this.password = password; - this.authority = authority; } public static UserService build(SmartHarvesterUser user) { - GrantedAuthority authority = new SimpleGrantedAuthority(user.getRole()); return new UserService( user.getId(), user.getFirstName(), user.getLastName(), user.getEmail(), - user.getPassword(), - authority); + user.getPassword()); } public String getId() { -- GitLab From c93a3fda717d60a2fc02bb362df0841ba2d5f684 Mon Sep 17 00:00:00 2001 From: Paulo <pimenta@cines.fr> Date: Thu, 26 Nov 2020 16:52:02 +0100 Subject: [PATCH 2/5] refactor(api-user): Added an end point to retrieve an user by his email --- .../controller/SmartHarvesterUserController.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/smartharvester/controller/SmartHarvesterUserController.java b/src/main/java/com/smartharvester/controller/SmartHarvesterUserController.java index 311212f..4e9f319 100644 --- a/src/main/java/com/smartharvester/controller/SmartHarvesterUserController.java +++ b/src/main/java/com/smartharvester/controller/SmartHarvesterUserController.java @@ -7,14 +7,13 @@ import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoIterable; import com.smartharvester.dao.UserDaoRepository; -import com.smartharvester.exception.ResourceNotFoundException; -import com.smartharvester.model.openapi.OpenApi; import com.smartharvester.model.user.SmartHarvesterUser; import io.swagger.v3.oas.annotations.tags.Tag; import org.bson.Document; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.*; @@ -82,4 +81,16 @@ public class SmartHarvesterUserController { return response; } + /** + * Method to fetch all users. + * @return + */ + + @GetMapping("/user/{email}") + public Collection<SmartHarvesterUser> getUserByEmail(@PathVariable("email") String email) + { + Optional<SmartHarvesterUser> response = userDaoRepository.findByEmail(email); + return (Collection<SmartHarvesterUser>) ResponseEntity.ok().body(response); + } + } -- GitLab From a511f2e345eedd2692c022ba6fea5289318a28fc Mon Sep 17 00:00:00 2001 From: Paulo <pimenta@cines.fr> Date: Tue, 1 Dec 2020 19:02:58 +0100 Subject: [PATCH 3/5] refactor(api): Minor changes on getUser() method descrption --- .../controller/SmartHarvesterUserController.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/smartharvester/controller/SmartHarvesterUserController.java b/src/main/java/com/smartharvester/controller/SmartHarvesterUserController.java index 4e9f319..3268db8 100644 --- a/src/main/java/com/smartharvester/controller/SmartHarvesterUserController.java +++ b/src/main/java/com/smartharvester/controller/SmartHarvesterUserController.java @@ -82,15 +82,17 @@ public class SmartHarvesterUserController { } /** - * Method to fetch all users. + * Method to fetch a user by email. * @return */ @GetMapping("/user/{email}") - public Collection<SmartHarvesterUser> getUserByEmail(@PathVariable("email") String email) + public ResponseEntity<Optional<SmartHarvesterUser>> getUserByEmail(@PathVariable("email") String email) { - Optional<SmartHarvesterUser> response = userDaoRepository.findByEmail(email); - return (Collection<SmartHarvesterUser>) ResponseEntity.ok().body(response); + return Optional + .ofNullable( userDaoRepository.findByEmail(email) ) + .map( user -> ResponseEntity.ok().body(user) ) + .orElseGet( () -> ResponseEntity.notFound().build() ); } } -- GitLab From bf8ce77fb33cfffffeb18e1a60abfa9664dc43da Mon Sep 17 00:00:00 2001 From: Paulo <pimenta@cines.fr> Date: Wed, 2 Dec 2020 16:05:55 +0100 Subject: [PATCH 4/5] refactor(api): Deleted /allusers endpoint based on mongtemplate --- .../SmartHarvesterUserController.java | 46 ++----------------- 1 file changed, 4 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/smartharvester/controller/SmartHarvesterUserController.java b/src/main/java/com/smartharvester/controller/SmartHarvesterUserController.java index 3268db8..0829c11 100644 --- a/src/main/java/com/smartharvester/controller/SmartHarvesterUserController.java +++ b/src/main/java/com/smartharvester/controller/SmartHarvesterUserController.java @@ -33,57 +33,19 @@ public class SmartHarvesterUserController { /** * Method to fetch all users. - * @return - */ - @GetMapping("/allusers") - public String getAllUsers() { - MongoDatabase db = mongoTemplate.getDb(); - MongoIterable<String> names = db.listCollectionNames(); - names.forEach(n-> System.out.println("Name : " + n)); - Map<String, String> categotyLookUpMap = new HashMap<>(); - ObjectMapper objectMapper = new ObjectMapper(); - Map<String, String> users = new HashMap(); - names.forEach(n-> { - if (n.equals("user")) { - MongoCollection<Document> collection = db.getCollection(n); - FindIterable<Document> iterDoc = collection.find(); - Iterator it = iterDoc.iterator(); - while (it.hasNext()) { - Document theObj = (Document) it.next(); - System.out.println(theObj.getString("firstName")); - categotyLookUpMap.put(theObj.getString("firstName"), theObj.getString("lastName")); - } - } - }); - - if (mongoTemplate.collectionExists("user")){ - System.out.println(categotyLookUpMap); - try { - String json = objectMapper.writeValueAsString(categotyLookUpMap); - System.out.println(json); - return json; - } catch (JsonProcessingException e) { - e.printStackTrace(); - } - }; - return "No"; - } - - /** - * Method to fetch all users. - * @return + * @return A collection of SmarHarversterUsers */ - @GetMapping("/allusers2") + @GetMapping("/allusers") public Collection<SmartHarvesterUser> getAll2() { - //logger.debug("Getting all OpenApi from database..."); List<SmartHarvesterUser> response = userDaoRepository.findAll(); return response; } /** * Method to fetch a user by email. - * @return + * @param email The user's email + * @return An Optional SmartHarvesterUser object */ @GetMapping("/user/{email}") -- GitLab From ac207ee6662ef50b67526f56da6c1846c8a6fb4c Mon Sep 17 00:00:00 2001 From: Paulo <pimenta@cines.fr> Date: Thu, 3 Dec 2020 17:21:22 +0100 Subject: [PATCH 5/5] refactor(response-messages): Included http response status on message response objects) --- .../SmartHarvesterAuthController.java | 12 ++++-------- .../model/login/response/MessageResponse.java | 18 +++++++++++++----- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/smartharvester/controller/SmartHarvesterAuthController.java b/src/main/java/com/smartharvester/controller/SmartHarvesterAuthController.java index 9a8d98b..cc55db2 100644 --- a/src/main/java/com/smartharvester/controller/SmartHarvesterAuthController.java +++ b/src/main/java/com/smartharvester/controller/SmartHarvesterAuthController.java @@ -12,6 +12,7 @@ import com.smartharvester.security.services.UserService; import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; @@ -63,13 +64,8 @@ public class SmartHarvesterAuthController { if (userRepository.existsByEmail(signUpRequest.getEmail())) { return ResponseEntity .badRequest() - .body(new MessageResponse("Error: Username is already taken!")); - } - - if (userRepository.existsByEmail(signUpRequest.getEmail())) { - return ResponseEntity - .badRequest() - .body(new MessageResponse("Error: Email is already in use!")); + .body(new MessageResponse("Error: Username is already taken!",HttpStatus.BAD_REQUEST) + ); } // Create new user's account @@ -79,6 +75,6 @@ public class SmartHarvesterAuthController { userRepository.save(user); - return ResponseEntity.ok(new MessageResponse("User registered successfully!")); + return ResponseEntity.ok(new MessageResponse("User registered successfully!",HttpStatus.OK)); } } diff --git a/src/main/java/com/smartharvester/model/login/response/MessageResponse.java b/src/main/java/com/smartharvester/model/login/response/MessageResponse.java index 53bfac1..66b8b66 100644 --- a/src/main/java/com/smartharvester/model/login/response/MessageResponse.java +++ b/src/main/java/com/smartharvester/model/login/response/MessageResponse.java @@ -1,15 +1,15 @@ package com.smartharvester.model.login.response; -import javax.validation.constraints.Email; -import javax.validation.constraints.NotBlank; -import javax.validation.constraints.Size; -import java.util.Set; +import org.springframework.http.HttpStatus; public class MessageResponse { private String message; + private HttpStatus statusCode; - public MessageResponse(String message) { + + public MessageResponse(String message, HttpStatus statusCode) { this.message = message; + this.statusCode = statusCode; } public String getMessage() { @@ -19,4 +19,12 @@ public class MessageResponse { public void setMessage(String message) { this.message = message; } + + public HttpStatus getStatusCode() { + return statusCode; + } + + public void setStatusCode(HttpStatus statusCode) { + this.statusCode = statusCode; + } } -- GitLab