Merge branch '48-create-login-rest-controller' into 'master'
Resolve "Create login rest controller" Closes #48 See merge request marcel.schwarz/2020ss-qbc-geofence-timetracking!30
This commit is contained in:
commit
798ca8b168
@ -18,6 +18,7 @@ dependencies {
|
|||||||
implementation 'org.springframework.boot:spring-boot-starter-actuator'
|
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-jpa'
|
||||||
|
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
|
||||||
compileOnly 'org.projectlombok:lombok'
|
compileOnly 'org.projectlombok:lombok'
|
||||||
annotationProcessor 'org.projectlombok:lombok'
|
annotationProcessor 'org.projectlombok:lombok'
|
||||||
implementation 'org.mariadb.jdbc:mariadb-java-client'
|
implementation 'org.mariadb.jdbc:mariadb-java-client'
|
||||||
|
@ -5,9 +5,7 @@ import lombok.AllArgsConstructor;
|
|||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.*;
|
||||||
import javax.persistence.Id;
|
|
||||||
import javax.persistence.OneToOne;
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
@ -18,6 +16,7 @@ import java.util.Date;
|
|||||||
public class TimeRecord {
|
public class TimeRecord {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private long id;
|
private long id;
|
||||||
@OneToOne
|
@OneToOne
|
||||||
private TimetrackAccount account; // TimetrackAccount ID (Lazy)
|
private TimetrackAccount account; // TimetrackAccount ID (Lazy)
|
||||||
|
@ -16,7 +16,7 @@ import javax.persistence.Id;
|
|||||||
public class Role {
|
public class Role {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private long id;
|
private long id;
|
||||||
private String name;
|
private String name;
|
||||||
// TODO: Permission List
|
// TODO: Permission List
|
||||||
|
@ -5,5 +5,5 @@ public class SecurityConstants {
|
|||||||
public static final long EXPIRATION_TIME = 864_000_000; // 10 days
|
public static final long EXPIRATION_TIME = 864_000_000; // 10 days
|
||||||
public static final String TOKEN_PREFIX = "Bearer ";
|
public static final String TOKEN_PREFIX = "Bearer ";
|
||||||
public static final String HEADER_STRING = "Authorization";
|
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";
|
||||||
}
|
}
|
@ -14,7 +14,7 @@ import javax.persistence.*;
|
|||||||
public class TimetrackAccount {
|
public class TimetrackAccount {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private long id;
|
private long id;
|
||||||
@OneToOne
|
@OneToOne
|
||||||
private TimetrackUser timetrackUser; // TimetrackUser Id (Lazy) [REMOVE]
|
private TimetrackUser timetrackUser; // TimetrackUser Id (Lazy) [REMOVE]
|
||||||
|
@ -5,7 +5,6 @@ import de.hft.geotime.timetrackaccount.TimetrackAccount;
|
|||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import org.hibernate.validator.constraints.UniqueElements;
|
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -17,9 +16,9 @@ import java.util.List;
|
|||||||
public class TimetrackUser {
|
public class TimetrackUser {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private long id;
|
private long id;
|
||||||
@UniqueElements
|
@Column(unique = true)
|
||||||
private String username;
|
private String username;
|
||||||
private String password; // strip
|
private String password; // strip
|
||||||
private String firstname;
|
private String firstname;
|
||||||
|
@ -1,35 +1,38 @@
|
|||||||
package de.hft.geotime.user;
|
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.core.Authentication;
|
||||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/user")
|
|
||||||
public class UserController {
|
public class UserController {
|
||||||
|
|
||||||
private TimetrackUserRepository userRepository;
|
private final TimetrackUserRepository userRepository;
|
||||||
private BCryptPasswordEncoder bCryptPasswordEncoder;
|
private final BCryptPasswordEncoder bCryptPasswordEncoder;
|
||||||
|
|
||||||
public UserController(TimetrackUserRepository userRepository, BCryptPasswordEncoder bCryptPasswordEncoder) {
|
public UserController(TimetrackUserRepository userRepository, BCryptPasswordEncoder bCryptPasswordEncoder) {
|
||||||
this.userRepository = userRepository;
|
this.userRepository = userRepository;
|
||||||
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
|
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping("/whoami")
|
||||||
public String getUsername(Authentication authentication) {
|
public String getUsername(Authentication authentication) {
|
||||||
TimetrackUser timetrackUser = userRepository.findFirstByUsername(authentication.getName());
|
TimetrackUser timetrackUser = userRepository.findFirstByUsername(authentication.getName());
|
||||||
return "Welcome back " + timetrackUser.getFirstname() + " " + timetrackUser.getLastname();
|
return "Welcome back " + timetrackUser.getFirstname() + " " + timetrackUser.getLastname();
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: implement register, maybe move to another class
|
|
||||||
@PostMapping("/sign-up")
|
@PostMapping("/sign-up")
|
||||||
public HashMap<String, Object> signUp(@RequestBody HashMap<String, Object> payload) {
|
public ResponseEntity<String> signUp(@RequestBody TimetrackUser user) {
|
||||||
return payload;
|
user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
|
||||||
// user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
|
TimetrackUser byUsername = userRepository.findFirstByUsername(user.getUsername());
|
||||||
// userRepository.save(user);
|
if (byUsername == null) {
|
||||||
|
userRepository.save(user);
|
||||||
|
return new ResponseEntity<>("Created", HttpStatus.CREATED);
|
||||||
|
} else {
|
||||||
|
return new ResponseEntity<>("Username already exists!", HttpStatus.CONFLICT);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user