-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from JamHaven/dev
[Dev] production release for project presentation
- Loading branch information
Showing
14 changed files
with
148 additions
and
443 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,9 @@ | |
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Component | ||
public class ApplicationStartup implements ApplicationListener<ApplicationReadyEvent> { | ||
|
||
|
@@ -31,13 +34,29 @@ public void onApplicationEvent(ApplicationReadyEvent event){ | |
log.info(event.toString()); | ||
//this.repository.deleteAll(); | ||
|
||
List<User> users = this.repository.findAll(); | ||
|
||
if(users != null) { | ||
log.info("users.count:" + users.size()); | ||
for(User user : users) { | ||
log.info(user.toString()); | ||
} | ||
} | ||
|
||
User superuser = this.repository.findById(1L); | ||
|
||
if (superuser != null) { | ||
log.info("superuser: " + superuser.toString()); | ||
return; | ||
} | ||
|
||
Optional<User> optSuperUser = this.repository.findOneByEmail("[email protected]"); | ||
|
||
if (optSuperUser.isPresent()) { | ||
log.info("superuser: " + optSuperUser.get().toString()); | ||
return; | ||
} | ||
|
||
superuser = new User(1L,"[email protected]"); | ||
String password = this.passwordEncoder.encode("admin"); | ||
superuser.setPassword(password); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package pacApp.pacController; | ||
|
||
import org.apache.commons.validator.routines.EmailValidator; | ||
import org.apache.commons.validator.routines.RegexValidator; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
@@ -12,9 +14,10 @@ | |
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.web.bind.annotation.*; | ||
import pacApp.pacData.UserRepository; | ||
import pacApp.pacException.RegistrationBadRequestException; | ||
import pacApp.pacLogic.Constants; | ||
import pacApp.pacModel.Currency; | ||
import pacApp.pacModel.User; | ||
import pacApp.pacModel.request.Booking; | ||
import pacApp.pacModel.request.UserInfo; | ||
import pacApp.pacModel.response.GenericResponse; | ||
import pacApp.pacSecurity.JwtAuthenticatedProfile; | ||
|
@@ -43,7 +46,6 @@ public List<ServiceInstance> serviceInstancesByApplicationName(@PathVariable Str | |
@CrossOrigin | ||
@GetMapping("/users") | ||
public ResponseEntity getAllUsers(){ | ||
/* | ||
String userEmail = super.getAuthentication().getName(); | ||
|
||
Optional<User> optUser = this.repository.findOneByEmail(userEmail); | ||
|
@@ -58,12 +60,12 @@ public ResponseEntity getAllUsers(){ | |
//TODO: implement user roles | ||
|
||
long userId = user.getId(); | ||
String email = user.getEmail(); | ||
|
||
if (userId != 1L) { | ||
if (userId != 1L && !email.equals("[email protected]")) { | ||
GenericResponse response = new GenericResponse(HttpStatus.FORBIDDEN.value(),"Request forbidden"); | ||
return new ResponseEntity<>(response, HttpStatus.FORBIDDEN); | ||
} | ||
*/ | ||
|
||
List<User> users = this.repository.findAll(); | ||
|
||
|
@@ -73,15 +75,7 @@ public ResponseEntity getAllUsers(){ | |
@RequestMapping(value = "/user", method = RequestMethod.GET, | ||
produces = MediaType.APPLICATION_JSON_VALUE) | ||
public ResponseEntity getUserInfo() { | ||
Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | ||
|
||
if (!(auth instanceof JwtAuthenticatedProfile)) { | ||
GenericResponse response = new GenericResponse(HttpStatus.FORBIDDEN.value(),"Authentication failure"); | ||
return new ResponseEntity<>(response, HttpStatus.FORBIDDEN); | ||
} | ||
|
||
JwtAuthenticatedProfile authenticatedProfile = (JwtAuthenticatedProfile) auth; | ||
String userEmail = authenticatedProfile.getName(); | ||
String userEmail = super.getAuthentication().getName(); | ||
|
||
Optional<User> optUser = this.repository.findOneByEmail(userEmail); | ||
|
||
|
@@ -97,6 +91,48 @@ public ResponseEntity getUserInfo() { | |
return new ResponseEntity<>(userInfo, HttpStatus.OK); | ||
} | ||
|
||
@RequestMapping(value = "/user", method = RequestMethod.POST, | ||
consumes = MediaType.APPLICATION_JSON_VALUE, | ||
produces = MediaType.APPLICATION_JSON_VALUE) | ||
public ResponseEntity<GenericResponse> registerUser(@RequestBody User user){ | ||
log.info("registerUser: " + user.toString()); | ||
|
||
if (user.getEmail() == null || user.getPassword() == null) { | ||
throw new RegistrationBadRequestException(); | ||
} | ||
|
||
EmailValidator emailValidator = EmailValidator.getInstance(); | ||
|
||
if (!emailValidator.isValid(user.getEmail())) { | ||
throw new RegistrationBadRequestException(); | ||
} | ||
|
||
Optional<User> optUser = this.repository.findOneByEmail(user.getEmail()); | ||
|
||
if (optUser.isPresent()){ | ||
GenericResponse response = new GenericResponse(HttpStatus.CONFLICT.value(),"User already registered"); | ||
return new ResponseEntity<>(response,HttpStatus.CONFLICT); | ||
} | ||
|
||
RegexValidator validator = new RegexValidator("((?=.*[a-z])(?=.*\\d)(?=.*[@#$%])(?=.*[A-Z]).{6,16})"); | ||
|
||
if (!validator.isValid(user.getPassword())) { | ||
throw new RegistrationBadRequestException(); | ||
} | ||
|
||
if (user.getDefaultCurrency() == null) { | ||
user.setDefaultCurrency(Constants.SERVICE_CURRENCY); | ||
} | ||
|
||
user.setId(0L); | ||
|
||
this.repository.saveUser(user); | ||
|
||
GenericResponse response = new GenericResponse(HttpStatus.OK.value(), "User registration successful"); | ||
|
||
return new ResponseEntity<>(response, HttpStatus.OK); | ||
} | ||
|
||
@RequestMapping(value = "/user", method = RequestMethod.PUT, | ||
consumes = MediaType.APPLICATION_JSON_VALUE, | ||
produces = MediaType.APPLICATION_JSON_VALUE) | ||
|
@@ -106,15 +142,7 @@ public ResponseEntity updateUser(@RequestBody UserInfo userInfo) { | |
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | ||
|
||
if (!(auth instanceof JwtAuthenticatedProfile)) { | ||
GenericResponse response = new GenericResponse(403,"Authentication failure"); | ||
return new ResponseEntity<>(response, HttpStatus.FORBIDDEN); | ||
} | ||
|
||
JwtAuthenticatedProfile authenticatedProfile = (JwtAuthenticatedProfile) auth; | ||
String userEmail = authenticatedProfile.getName(); | ||
String userEmail = super.getAuthentication().getName(); | ||
|
||
Optional<User> optUser = this.repository.findOneByEmail(userEmail); | ||
|
||
|
@@ -175,5 +203,4 @@ protected UserInfo convertUserToUserInfo(User user) { | |
return userInfo; | ||
} | ||
|
||
|
||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/pacApp/pacException/RegistrationBadRequestAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package pacApp.pacException; | ||
|
||
import pacApp.pacModel.response.GenericResponse; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@ControllerAdvice | ||
public class RegistrationBadRequestAdvice { | ||
@ResponseBody | ||
@ExceptionHandler(RegistrationBadRequestException.class) | ||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
public ResponseEntity badRegistrationHandler(RegistrationBadRequestException ex){ | ||
GenericResponse response = new GenericResponse(400, ex.getMessage()); | ||
|
||
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/pacApp/pacException/RegistrationBadRequestException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package pacApp.pacException; | ||
|
||
public class RegistrationBadRequestException extends RuntimeException { | ||
|
||
public RegistrationBadRequestException(){ | ||
super("Incorrect registration data"); | ||
} | ||
public RegistrationBadRequestException (String message) { | ||
super(message); | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.