Skip to content

Commit

Permalink
Merge pull request #1 from JamHaven/dev
Browse files Browse the repository at this point in the history
[Dev] production release for project presentation
  • Loading branch information
JamHaven authored May 25, 2020
2 parents ecd7799 + f68bbaf commit a5c8d61
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 443 deletions.
21 changes: 15 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-starter</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
-->
</dependency> -->

<dependency>
<groupId>org.springframework.cloud</groupId>
Expand All @@ -82,17 +81,17 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

<!--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

</dependency> -->
<!--
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
</dependency> -->

<dependency>
<groupId>io.jsonwebtoken</groupId>
Expand Down Expand Up @@ -164,6 +163,16 @@
<groupId>com.sun.xml.messaging.saaj</groupId>
<artifactId>saaj-impl</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
</dependency>

</dependencies>

<build>
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/pacApp/ApplicationStartup.java
Original file line number Diff line number Diff line change
Expand Up @@ -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> {

Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/pacApp/pacConfig/WebSecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ protected void configure(HttpSecurity httpSecurity) throws Exception {
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.antMatchers("/users/**").permitAll()
.antMatchers(HttpMethod.POST, "/user").permitAll()
.antMatchers("/auth/**").permitAll()
.antMatchers("/registration").permitAll()
.antMatchers("/h2-console/*").permitAll()
.antMatchers("/h2-console/**").permitAll()
.antMatchers("/swagger-ui**").permitAll()
.antMatchers("/webjars/**").permitAll()
.antMatchers("/swagger-resources/**").permitAll()
Expand Down
73 changes: 50 additions & 23 deletions src/main/java/pacApp/pacController/UserController.java
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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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();

Expand All @@ -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);

Expand All @@ -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)
Expand All @@ -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);

Expand Down Expand Up @@ -175,5 +203,4 @@ protected UserInfo convertUserToUserInfo(User user) {
return userInfo;
}


}
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);
}
}
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);
}

}
94 changes: 0 additions & 94 deletions src/main/java/pacApp/pacModel/Car.java

This file was deleted.

10 changes: 0 additions & 10 deletions src/main/java/pacApp/pacModel/CarType.java

This file was deleted.

Loading

0 comments on commit a5c8d61

Please sign in to comment.