Skip to content

Commit

Permalink
Api registrazione utente da admin
Browse files Browse the repository at this point in the history
  • Loading branch information
villaflaminio committed May 14, 2023
1 parent eeb6871 commit 4cf87ac
Show file tree
Hide file tree
Showing 16 changed files with 218 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ build/

### VS Code ###
.vscode/
/src/main/resources/application-localdev.properties
5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM openjdk:17-oracle
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]
67 changes: 67 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
version: "2.2"
services:
# App backend service

db:
image: mysql:5.7
container_name: facility_management_db
volumes:
- facility_management_db:/var/lib/mysql
restart: always
ports:
- "6001:3306"
environment:
MYSQL_ROOT_PASSWORD: 4P2bauIt7WIdpXnd
MYSQL_DATABASE: facility_management
MYSQL_PASSWORD: 4P2bauIt7WIdpXnd
networks:
network:
ipv4_address: 172.95.0.2

app-server:
# Configuration for building the docker image for the backend service
container_name: facility_management_be
build:
context: facilityManager
dockerfile: Dockerfile
ports:
- 6003:8080 # Forward the exposed port 8080 on the container to port 5002 on the host machine
restart: always
depends_on:
- db # This service depends on mysql. Start that first.
environment: # Pass environment variables to the service
- spring.profiles.active=prod
- spring.datasource.url=jdbc:mysql://db:3306/facility_management?useSSL=false&serverTimezone=UTC
- spring.datasource.username=root
- spring.datasource.password=4P2bauIt7WIdpXnd
networks:
network:
ipv4_address: 172.95.0.3

# for php myadmin goto http://localhost:5001
phpmyadmino:
container_name: facility_management_db_phpmyadmin
depends_on:
- db
image: phpmyadmin
restart: always
ports:
- "6002:80"
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: 4P2bauIt7WIdpXnd
networks:
network:
ipv4_address: 172.95.0.4

volumes:
facility_management_db:

networks:
network:
driver: bridge
ipam:
config:
-
subnet: 172.95.0.0/16
gateway: 172.95.0.1
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@

import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.annotations.servers.Server;
import io.swagger.v3.oas.models.OpenAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.task.TaskExecutor;

@SpringBootApplication
@OpenAPIDefinition(info = @Info(title = "FacilityManagerApplication", version = "2.0", description = "FacilityManager Application"))
import java.util.List;

@SpringBootApplication
//@OpenAPIDefinition(info = @Info(title = "FacilityManagerApplication", version = "2.0", description = "FacilityManager Application"),servers = {
// @Server(url = "https://facilitymanager.be.flaminiovilla.it/", description = "Default Server URL")
//})
@OpenAPIDefinition(info = @Info(title = "FacilityManagerApplication", version = "2.0", description = "FacilityManager Application")
)
public class FacilityManagerApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import it.bruffa.facilitymanager.model.dto.AuthResponseDTO;
import it.bruffa.facilitymanager.model.dto.LoginDto;
import it.bruffa.facilitymanager.model.dto.TokenRefreshResponseDto;
import it.bruffa.facilitymanager.model.dto.request.RegisterUserRequestDto;
import it.bruffa.facilitymanager.model.dto.request.SignUpRequestDto;
import it.bruffa.facilitymanager.model.entity.User;
import it.bruffa.facilitymanager.model.exception.ApiError;
Expand Down Expand Up @@ -35,8 +36,11 @@ public interface AuthController{
//sign up
@Operation(summary = "Sign up", description = "Sign up", tags = {"auth"})
@PostMapping("/signup")
ResponseEntity<User> registerUser(@Valid @RequestBody SignUpRequestDto signUpRequestDto) throws Exception;
ResponseEntity<User> signup(@Valid @RequestBody SignUpRequestDto signUpRequestDto) throws Exception;

@Operation(summary = "Sign up from admin", description = "Sign up", tags = {"auth"})
@PostMapping("/registerUser")
ResponseEntity<User> registerUser(@Valid @RequestBody RegisterUserRequestDto registerUser) throws Exception;

@Operation(summary = "Logout", description = "Logout", tags = {"auth"})
@PostMapping("/logout")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import it.bruffa.facilitymanager.model.dto.AuthResponseDTO;
import it.bruffa.facilitymanager.model.dto.LoginDto;
import it.bruffa.facilitymanager.model.dto.TokenRefreshResponseDto;
import it.bruffa.facilitymanager.model.dto.request.RegisterUserRequestDto;
import it.bruffa.facilitymanager.model.dto.request.SignUpRequestDto;
import it.bruffa.facilitymanager.model.entity.User;
import it.bruffa.facilitymanager.model.entity.UserPrincipal;
Expand Down Expand Up @@ -50,8 +51,13 @@ public ResponseEntity<UserMeInfo> getCurrentUser(@CurrentUser UserPrincipal user
return authService.getCurrentUser(userPrincipal);
}
@Override
public ResponseEntity<User> registerUser(SignUpRequestDto signUpRequestDto) throws Exception {
return authService.registerUser(signUpRequestDto);
public ResponseEntity<User> signup(SignUpRequestDto signUpRequestDto) throws Exception {
return authService.signup(signUpRequestDto);
}

@Override
public ResponseEntity<User> registerUser(RegisterUserRequestDto registerUser) throws Exception {
return authService.registerUser(registerUser);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package it.bruffa.facilitymanager.model.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;

@Data
public class RegisterUserRequestDto {
@NotBlank
@Schema(description = "First name of the user", example = "Mario")
private String firstName;

@NotBlank
@Schema(description = "Last name of the user", example = "Rossi")
private String lastName;

@NotBlank
@Email
@Schema(description = "Email of the user", example = "mariorossi@gmail.com")
private String email;

@NotNull
@Schema(description = "Latitude of the user", example = "45.123456")
private Double latitude;

@NotNull
@Schema(description = "Longitude of the user", example = "9.123456")
private Double longitude;

@NotBlank
@Schema(description = "Role of the user", example = "ROLE_USER / ROLE_ADMIN / ROLE_CLEANER / ROLE_MAINTAINER")
private String role;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ public interface UserDetailInfo {

String getPassword();

Boolean isEnable();
Boolean getEnable();

Boolean isCredentialExpired();
Boolean getCredentialExpired();

Boolean isAccountNonExpired();
Boolean getAccountNonExpired();

Boolean isAccountNonLocked();
Boolean getAccountNonLocked();

Collection<RoleInfo> getRoles();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import it.bruffa.facilitymanager.model.dto.AuthResponseDTO;
import it.bruffa.facilitymanager.model.dto.LoginDto;
import it.bruffa.facilitymanager.model.dto.TokenRefreshResponseDto;
import it.bruffa.facilitymanager.model.dto.request.RegisterUserRequestDto;
import it.bruffa.facilitymanager.model.dto.request.SignUpRequestDto;
import it.bruffa.facilitymanager.model.entity.User;
import it.bruffa.facilitymanager.model.entity.UserPrincipal;
Expand All @@ -14,7 +15,7 @@
public interface AuthService {
ResponseEntity<AuthResponseDTO> login(LoginDto loginDto);

ResponseEntity<User> registerUser(SignUpRequestDto signUpRequestDto) throws Exception;
ResponseEntity<User> signup(SignUpRequestDto signUpRequestDto) throws Exception;


ResponseEntity<TokenRefreshResponseDto> refreshtoken(String refreshToken) throws Exception;
Expand All @@ -26,4 +27,6 @@ public interface AuthService {
ResponseEntity<?> getAuthenticationToChangePassword(String token);

ResponseEntity<Boolean> logout(HttpServletRequest request, HttpServletResponse response);

ResponseEntity<User> registerUser(RegisterUserRequestDto registerUser) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package it.bruffa.facilitymanager.service;

import it.bruffa.facilitymanager.model.dto.MailResponse;
import it.bruffa.facilitymanager.model.dto.request.ChangePassworRequest;
import it.bruffa.facilitymanager.model.entity.User;
import it.bruffa.facilitymanager.model.entity.UserPrincipal;
Expand All @@ -18,4 +19,6 @@ public interface UserService {

UserDetailInfo getUserById(Long userId);

MailResponse requestResetPassword(User user);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import it.bruffa.facilitymanager.model.dto.AuthResponseDTO;
import it.bruffa.facilitymanager.model.dto.LoginDto;
import it.bruffa.facilitymanager.model.dto.TokenRefreshResponseDto;
import it.bruffa.facilitymanager.model.dto.request.RegisterUserRequestDto;
import it.bruffa.facilitymanager.model.dto.request.SignUpRequestDto;
import it.bruffa.facilitymanager.model.entity.*;
import it.bruffa.facilitymanager.model.exception.ExpiredJwtException;
Expand All @@ -16,6 +17,7 @@
import it.bruffa.facilitymanager.security.CustomAuthenticationManager;
import it.bruffa.facilitymanager.security.JwtUtility;
import it.bruffa.facilitymanager.service.AuthService;
import it.bruffa.facilitymanager.service.UserService;
import it.bruffa.facilitymanager.utilities.PropertiesHelper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -46,6 +48,8 @@ public class AuthServiceImpl implements AuthService {
private PasswordResetTokenRepository passwordResetTokenRepository;
@Autowired
private JwtUtility tokenProvider;
@Autowired
private UserService userService;

private final CustomAuthenticationManager authenticationManager;
private final JwtUtility jwtUtility;
Expand Down Expand Up @@ -98,7 +102,7 @@ public ResponseEntity<AuthResponseDTO> login(LoginDto loginDto) {
}

@Override
public ResponseEntity<User> registerUser(SignUpRequestDto signUpRequestDto) throws Exception {
public ResponseEntity<User> signup(SignUpRequestDto signUpRequestDto) throws Exception {
// Check if the email is already in use.
if (userRepository.existsByEmail(signUpRequestDto.getEmail()))
throw new Exception("Email is already in use!");
Expand Down Expand Up @@ -206,15 +210,14 @@ public ResponseEntity<?> createFirstUser(HttpServletRequest req) throws Exceptio
user.setAccountNonLocked(true);
user.setAccountNonExpired(true);
user.setCredentialExpired(false);
user.setLatitude(0.0);
user.setLongitude(0.0);
user.setEnable(true);



// Save the user in the database.
try {
userRepository.save(user);
} catch (Exception e) {
throw new InvalidCredentialsException("User already present!");
throw e;

}
return ResponseEntity.ok(user);
Expand Down Expand Up @@ -265,6 +268,41 @@ public ResponseEntity<Boolean> logout(HttpServletRequest request, HttpServletRes
return ResponseEntity.ok(true);
}

@Override
public ResponseEntity<User> registerUser(RegisterUserRequestDto registerUser) throws Exception {
// Check if the email is already in use.
if (userRepository.existsByEmail(registerUser.getEmail()))
throw new Exception("Email is already in use!");

// Trying to find the role with the given name, if not found a bad request exception will be thrown.
Role role = roleRepository.findByName(registerUser.getRole()).orElseThrow(() -> new ItemNotFoundException("Role not found"));
// Create the collection to store all the roles.
ArrayList<Role> roles = new ArrayList<>();
roles.add(role);

// Creating user's account
User user = new User();

user.setFirstName(registerUser.getFirstName());
user.setLastName(registerUser.getLastName());
user.setEmail(registerUser.getEmail());
user.setLatitude(registerUser.getLatitude());
user.setLongitude(registerUser.getLongitude());
user.setRoles(roles);
user.setEnable(true);
user.setAccountNonLocked(true);
user.setAccountNonExpired(true);
user.setCredentialExpired(false);

// Save the user in the database.
User result = userRepository.save(user);

userService.requestResetPassword(result);
// Return the created user.
return ResponseEntity.ok(result);

}

/**
* Check if the token is found.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public User changePassword(UserPrincipal userPrincipal, ChangePassworRequest bod

// Save the user.
User updatedUser = userRepository.save(user);
updatedUser.setPassword(null);
return updatedUser;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application-dev.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
spring.datasource.url=jdbc:mysql://localhost:3306/facility_manager?useUnicode=true&useSSL=false&serverTimezone=UTC
spring.datasource.url=jdbc:mysql://localhost:3306/facility_manager2?useUnicode=true&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=flaminio
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
Expand Down
11 changes: 11 additions & 0 deletions src/main/resources/application-prod.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
spring.datasource.url=jdbc:mysql://db:3306/facility_management?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=4P2bauIt7WIdpXnd
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update



mail.username=software@gmail.com
mail.password=ekssdfsdfob
9 changes: 6 additions & 3 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
spring.profiles.active=dev
spring.profiles.active=localdev
spring.jpa.properties.hibernate.hbm2dll.create_namespaces=true
server.port=8080

springdoc.enable-native-support=true
springdoc.swagger-ui.path=/swagger-ui.html
#MQTT dashboard to test
#https://mqtthq.com/client

#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
#Swagger UI
# http://localhost:8080/swagger-ui/index.html

#H2 console
#SCRIPT TO 'data_init.sql'

#cd facilityManager/
#git pull
#mvn -U clean package install -DskipTests
#docker-compose up --no-deps --build
Loading

0 comments on commit 4cf87ac

Please sign in to comment.