Skip to content

Commit

Permalink
[FR-#4.2] Add User, Group and Script controllers
Browse files Browse the repository at this point in the history
    User, group and script controllers created and documented
with swagger. Requsted processed in Services. Hibernate repositories
used to interract with postgresDB.
    Robot table added to createDB file. Gradle build files have
minor fixes. Root project removed.
  • Loading branch information
IThror10 committed Feb 13, 2024
1 parent 0ebba07 commit da2c436
Show file tree
Hide file tree
Showing 55 changed files with 1,861 additions and 97 deletions.
21 changes: 4 additions & 17 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
plugins {
id 'java'
id 'application'

id 'org.springframework.boot' version '3.0.2'
id 'io.spring.dependency-management' version '1.1.4'
}
Expand All @@ -10,7 +7,6 @@ repositories {
mavenCentral()
}


def harmonistModule = project(':modules:harmonist')

configure([harmonistModule]) {
Expand All @@ -35,19 +31,10 @@ configure([harmonistModule]) {
}
}

dependencies {
implementation harmonistModule

// implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
// implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0'
}

application {
mainClass = 'com.RPA.RPAMain'
}

tasks.create("unitTests").configure {
dependsOn(harmonistModule.test)
}

tasks.create("run").configure {
harmonistModule.run
}
4 changes: 1 addition & 3 deletions modules/harmonist/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ buildscript {

dependencies {
compileOnly 'org.projectlombok:lombok:1.18.24'
compileOnly 'javax.servlet:javax.servlet-api:4.0.1'
annotationProcessor 'org.projectlombok:lombok:1.18.24'

implementation 'org.jetbrains:annotations:23.0.0'
implementation 'javax.validation:validation-api:2.0.1.Final'
implementation 'javax.persistence:javax.persistence-api:2.2'

implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-websocket'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-security'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.RPA.config;


import com.RPA.entity.User;
import com.RPA.service.JwtService;
import com.RPA.service.UserService;
import jakarta.servlet.FilterChain;
Expand Down Expand Up @@ -45,9 +46,7 @@ protected void doFilterInternal(
String username = jwtService.extractUserName(jwt);

if (StringUtils.isNotEmpty(username) && SecurityContextHolder.getContext().getAuthentication() == null) {
UserDetails userDetails = userService
.userDetailsService()
.loadUserByUsername(username);
User userDetails = userService.getByUsername(username);

if (jwtService.isTokenValid(jwt, userDetails)) {
SecurityContext context = SecurityContextHolder.createEmptyContext();
Expand All @@ -61,6 +60,8 @@ protected void doFilterInternal(
authToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
context.setAuthentication(authToken);
SecurityContextHolder.setContext(context);

request.setAttribute("uid", userDetails.getId());
}
}
filterChain.doFilter(request, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
return corsConfiguration;
}))
.authorizeHttpRequests(request -> request
.requestMatchers("/api/user/**").permitAll()
.requestMatchers(HttpMethod.GET, "/api/robot/admin").hasRole("admin")
.requestMatchers(HttpMethod.GET, "/api/robot/**").authenticated()
.requestMatchers(HttpMethod.POST, "/api/user/login", "/api/user", "/api/robot").permitAll()
.requestMatchers(HttpMethod.DELETE, "/api/robot").permitAll()
.requestMatchers("/swagger/**").permitAll()
.anyRequest().authenticated())
.sessionManagement(manager -> manager.sessionCreationPolicy(STATELESS))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.RPA.controller;

import com.RPA.exception.ConflictException;
import com.RPA.exception.ForbiddenException;
import com.RPA.exception.NotFoundException;
import io.swagger.v3.oas.annotations.Hidden;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Hidden
@RestControllerAdvice
public class ExceptionHandleController {
@ExceptionHandler(ConflictException.class)
@ResponseStatus(HttpStatus.CONFLICT)
public ResponseEntity handleConflictException(ConflictException ex) {
return createBody(ex.getMessage(), HttpStatus.CONFLICT);
}

@ExceptionHandler(NotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseEntity handleNotFoundException(NotFoundException ex) {
return createBody(ex.getMessage(), HttpStatus.NOT_FOUND);
}

@ExceptionHandler(ForbiddenException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ResponseEntity handleRuntimeException(ForbiddenException ex) {
return createBody(ex.getMessage(), HttpStatus.FORBIDDEN);
}

@ExceptionHandler(RuntimeException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ResponseEntity handleRuntimeException(RuntimeException ex) {
return createBody("Internal Server Error", HttpStatus.INTERNAL_SERVER_ERROR);
}

private ResponseEntity createBody(String error, HttpStatus status) {
Map<String, String> map = new HashMap<>();
map.put("timestamp", new Date().toString());
map.put("status", status.toString());
map.put("error", error);
return new ResponseEntity(map, status);
}
}
Loading

0 comments on commit da2c436

Please sign in to comment.