From 8e7f9c06d38638734aac0946ee70f1df77416565 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Sat, 9 May 2020 19:12:43 +0200 Subject: [PATCH 1/3] Implement Sign-up, Add id generation type identity to Entities --- backend/build.gradle | 1 + .../java/de/hft/geotime/record/TimeRecord.java | 5 ++--- .../main/java/de/hft/geotime/role/Role.java | 2 +- .../timetrackaccount/TimetrackAccount.java | 2 +- .../de/hft/geotime/user/TimetrackUser.java | 4 ++-- .../de/hft/geotime/user/UserController.java | 18 +++++++++++------- 6 files changed, 18 insertions(+), 14 deletions(-) diff --git a/backend/build.gradle b/backend/build.gradle index 499d544..2d135e8 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -18,6 +18,7 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + implementation 'org.springframework.boot:spring-boot-starter-data-rest' compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' implementation 'org.mariadb.jdbc:mariadb-java-client' diff --git a/backend/src/main/java/de/hft/geotime/record/TimeRecord.java b/backend/src/main/java/de/hft/geotime/record/TimeRecord.java index 6e7293a..c349c58 100644 --- a/backend/src/main/java/de/hft/geotime/record/TimeRecord.java +++ b/backend/src/main/java/de/hft/geotime/record/TimeRecord.java @@ -5,9 +5,7 @@ import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.OneToOne; +import javax.persistence.*; import java.time.Duration; import java.util.Date; @@ -18,6 +16,7 @@ import java.util.Date; public class TimeRecord { @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; @OneToOne private TimetrackAccount account; // TimetrackAccount ID (Lazy) diff --git a/backend/src/main/java/de/hft/geotime/role/Role.java b/backend/src/main/java/de/hft/geotime/role/Role.java index 7b319b7..f887577 100644 --- a/backend/src/main/java/de/hft/geotime/role/Role.java +++ b/backend/src/main/java/de/hft/geotime/role/Role.java @@ -16,7 +16,7 @@ import javax.persistence.Id; public class Role { @Id - @GeneratedValue(strategy = GenerationType.AUTO) + @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; private String name; // TODO: Permission List diff --git a/backend/src/main/java/de/hft/geotime/timetrackaccount/TimetrackAccount.java b/backend/src/main/java/de/hft/geotime/timetrackaccount/TimetrackAccount.java index 4d0c8e1..7a220f1 100644 --- a/backend/src/main/java/de/hft/geotime/timetrackaccount/TimetrackAccount.java +++ b/backend/src/main/java/de/hft/geotime/timetrackaccount/TimetrackAccount.java @@ -14,7 +14,7 @@ import javax.persistence.*; public class TimetrackAccount { @Id - @GeneratedValue(strategy = GenerationType.AUTO) + @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; @OneToOne private TimetrackUser timetrackUser; // TimetrackUser Id (Lazy) [REMOVE] diff --git a/backend/src/main/java/de/hft/geotime/user/TimetrackUser.java b/backend/src/main/java/de/hft/geotime/user/TimetrackUser.java index 47f85df..e02de32 100644 --- a/backend/src/main/java/de/hft/geotime/user/TimetrackUser.java +++ b/backend/src/main/java/de/hft/geotime/user/TimetrackUser.java @@ -17,9 +17,9 @@ import java.util.List; public class TimetrackUser { @Id - @GeneratedValue(strategy = GenerationType.AUTO) + @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; - @UniqueElements + @Column(unique = true) private String username; private String password; // strip private String firstname; diff --git a/backend/src/main/java/de/hft/geotime/user/UserController.java b/backend/src/main/java/de/hft/geotime/user/UserController.java index 5d73f85..40f5df8 100644 --- a/backend/src/main/java/de/hft/geotime/user/UserController.java +++ b/backend/src/main/java/de/hft/geotime/user/UserController.java @@ -1,11 +1,11 @@ package de.hft.geotime.user; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.web.bind.annotation.*; -import java.util.HashMap; - @RestController @RequestMapping("/user") public class UserController { @@ -24,12 +24,16 @@ public class UserController { return "Welcome back " + timetrackUser.getFirstname() + " " + timetrackUser.getLastname(); } - // TODO: implement register, maybe move to another class @PostMapping("/sign-up") - public HashMap signUp(@RequestBody HashMap payload) { - return payload; - // user.setPassword(bCryptPasswordEncoder.encode(user.getPassword())); - // userRepository.save(user); + public ResponseEntity signUp(@RequestBody TimetrackUser user) { + user.setPassword(bCryptPasswordEncoder.encode(user.getPassword())); + TimetrackUser byUsername = userRepository.findFirstByUsername(user.getUsername()); + if (byUsername == null) { + userRepository.save(user); + return new ResponseEntity<>("Created", HttpStatus.CREATED); + } else { + return new ResponseEntity<>("Username already exists!", HttpStatus.CONFLICT); + } } } From 6abdab2cf62459cabad57ca03e2d0ddefa0b72b7 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Sat, 9 May 2020 19:13:50 +0200 Subject: [PATCH 2/3] Formatting and little optimization --- backend/src/main/java/de/hft/geotime/user/TimetrackUser.java | 1 - backend/src/main/java/de/hft/geotime/user/UserController.java | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/backend/src/main/java/de/hft/geotime/user/TimetrackUser.java b/backend/src/main/java/de/hft/geotime/user/TimetrackUser.java index e02de32..8dcc7c7 100644 --- a/backend/src/main/java/de/hft/geotime/user/TimetrackUser.java +++ b/backend/src/main/java/de/hft/geotime/user/TimetrackUser.java @@ -5,7 +5,6 @@ import de.hft.geotime.timetrackaccount.TimetrackAccount; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; -import org.hibernate.validator.constraints.UniqueElements; import javax.persistence.*; import java.util.List; diff --git a/backend/src/main/java/de/hft/geotime/user/UserController.java b/backend/src/main/java/de/hft/geotime/user/UserController.java index 40f5df8..d467ea8 100644 --- a/backend/src/main/java/de/hft/geotime/user/UserController.java +++ b/backend/src/main/java/de/hft/geotime/user/UserController.java @@ -10,8 +10,8 @@ import org.springframework.web.bind.annotation.*; @RequestMapping("/user") public class UserController { - private TimetrackUserRepository userRepository; - private BCryptPasswordEncoder bCryptPasswordEncoder; + private final TimetrackUserRepository userRepository; + private final BCryptPasswordEncoder bCryptPasswordEncoder; public UserController(TimetrackUserRepository userRepository, BCryptPasswordEncoder bCryptPasswordEncoder) { this.userRepository = userRepository; From f186a89bb8870bd89cfa2506b17e23d31831c724 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Sat, 9 May 2020 19:20:35 +0200 Subject: [PATCH 3/3] Change sign-up url, Add whoami endpoint --- .../main/java/de/hft/geotime/security/SecurityConstants.java | 2 +- backend/src/main/java/de/hft/geotime/user/UserController.java | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/backend/src/main/java/de/hft/geotime/security/SecurityConstants.java b/backend/src/main/java/de/hft/geotime/security/SecurityConstants.java index d568748..f977cf3 100644 --- a/backend/src/main/java/de/hft/geotime/security/SecurityConstants.java +++ b/backend/src/main/java/de/hft/geotime/security/SecurityConstants.java @@ -5,5 +5,5 @@ public class SecurityConstants { public static final long EXPIRATION_TIME = 864_000_000; // 10 days public static final String TOKEN_PREFIX = "Bearer "; public static final String HEADER_STRING = "Authorization"; - public static final String SIGN_UP_URL = "/user/sign-up"; + public static final String SIGN_UP_URL = "/sign-up"; } \ No newline at end of file diff --git a/backend/src/main/java/de/hft/geotime/user/UserController.java b/backend/src/main/java/de/hft/geotime/user/UserController.java index d467ea8..723dde1 100644 --- a/backend/src/main/java/de/hft/geotime/user/UserController.java +++ b/backend/src/main/java/de/hft/geotime/user/UserController.java @@ -7,7 +7,6 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.web.bind.annotation.*; @RestController -@RequestMapping("/user") public class UserController { private final TimetrackUserRepository userRepository; @@ -18,7 +17,7 @@ public class UserController { this.bCryptPasswordEncoder = bCryptPasswordEncoder; } - @GetMapping + @GetMapping("/whoami") public String getUsername(Authentication authentication) { TimetrackUser timetrackUser = userRepository.findFirstByUsername(authentication.getName()); return "Welcome back " + timetrackUser.getFirstname() + " " + timetrackUser.getLastname();