Skip to content

Commit

Permalink
Merge pull request #29 from SELab-2/feature/jpa
Browse files Browse the repository at this point in the history
JPA setup
  • Loading branch information
Matthias-VE authored Mar 3, 2024
2 parents 4801bf3 + 8af4396 commit ae80829
Show file tree
Hide file tree
Showing 50 changed files with 1,351 additions and 97 deletions.
18 changes: 14 additions & 4 deletions backend/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
FROM eclipse-temurin:17-jdk-alpine
VOLUME /tmp
COPY build/libs/*T.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
FROM gradle:jdk17 AS build
COPY --chown=gradle:gradle . /home/gradle/src
WORKDIR /home/gradle/src
RUN gradle build --no-daemon

FROM eclipse-temurin:17

EXPOSE 8080

RUN mkdir /app

COPY --from=build /home/gradle/src/build/libs/*.jar /app/spring-boot-application.jar
ENTRYPOINT ["java", "-jar","/app/spring-boot-application.jar"]

6 changes: 4 additions & 2 deletions backend/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
plugins {
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.2'
id 'io.spring.dependency-management' version '1.1.4'
Expand All @@ -19,7 +19,6 @@ repositories {

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
developmentOnly 'org.springframework.boot:spring-boot-docker-compose'
runtimeOnly 'org.postgresql:postgresql'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
implementation 'org.springframework.boot:spring-boot-starter-web'
Expand All @@ -33,7 +32,10 @@ dependencies {
implementation 'com.auth0:java-jwt:3.18.2'
implementation 'com.auth0:jwks-rsa:0.18.0'
implementation 'javax.servlet:javax.servlet-api:4.0.1'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'org.postgresql:postgresql'

implementation "org.springframework.boot:spring-boot-devtools"
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.ugent.selab2.config;
package com.ugent.pidgeon.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class AuthConfig {

@Value("${azure.activedirectory.tenant-id}")
Expand All @@ -16,14 +18,15 @@ public class AuthConfig {
@Bean
public FilterRegistrationBean<JwtAuthenticationFilter> filterRegistrationBean() {
System.out.println("tenantId: " + tenantId);

FilterRegistrationBean<JwtAuthenticationFilter> filter = new FilterRegistrationBean<>();
filter.setFilter(new JwtAuthenticationFilter(tenantId));
filter.addUrlPatterns("/api/*");
filter.addUrlPatterns("/api/ietswatiknietwiltesten");
return filter;
}

@Bean
SecurityFilterChain web(HttpSecurity http) throws Exception {
public SecurityFilterChain web(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((authorize) -> authorize
.anyRequest().permitAll()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ugent.selab2.config;
package com.ugent.pidgeon.config;

import com.auth0.jwk.Jwk;
import com.auth0.jwk.JwkException;
Expand All @@ -8,8 +8,8 @@
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.ugent.selab2.model.Auth;
import com.ugent.selab2.model.User;
import com.ugent.pidgeon.model.Auth;
import com.ugent.pidgeon.model.User;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.StringUtils;
Expand All @@ -30,6 +30,7 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
public JwtAuthenticationFilter(String tenantId)
{
try {
logger.info("tenantId: " + tenantId);
provider = new UrlJwkProvider(new URL("https://login.microsoftonline.com/"+tenantId+"/discovery/v2.0/keys"));
} catch (MalformedURLException e) {
e.printStackTrace();
Expand All @@ -55,11 +56,18 @@ protected void doFilterInternal(jakarta.servlet.http.HttpServletRequest request,
algorithm.verify(jwt);// if the token signature is invalid, the method will throw SignatureVerificationException

// get the data from the token
String name = jwt.getClaim("name").asString();
String email = jwt.getClaim("email").asString();
String displayName = jwt.getClaim("name").asString();
String firstName = jwt.getClaim("given_name").asString();
String lastName = jwt.getClaim("family_name").asString();
String email = jwt.getClaim("unique_name").asString();
List<String> groups = jwt.getClaim("groups").asList(String.class);
String oid = jwt.getClaim("oid").asString();
User user = new User(name, email, groups, oid);

// print full object
//logger.info(jwt.getClaims());


User user = new User(displayName, firstName,lastName, email, groups, oid);

Auth authUser = new Auth(user, new ArrayList<>());
SecurityContextHolder.getContext().setAuthentication(authUser);
Expand All @@ -73,8 +81,6 @@ protected void doFilterInternal(jakarta.servlet.http.HttpServletRequest request,

response.setStatus(HttpStatus.UNAUTHORIZED.value()); // Forbidden
}
logger.info("Token: " + token);

} else {
logger.warn("No token found!");
response.setStatus(HttpStatus.UNAUTHORIZED.value()); // Unauthorized
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ugent.selab2.config;
package com.ugent.pidgeon.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ugent.selab2.config;
package com.ugent.pidgeon.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.ugent.selab2.controllers;
import com.ugent.selab2.model.Auth;
import com.ugent.selab2.model.User;
package com.ugent.pidgeon.controllers;
import com.ugent.pidgeon.model.Auth;
import com.ugent.pidgeon.model.User;
import com.ugent.pidgeon.postgre.repository.UserRepository;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AuthTestController {
@Autowired
private UserRepository userRepository;

@GetMapping("/api/test")
public User testApi(HttpServletRequest request, Auth auth) {
Expand All @@ -21,7 +25,7 @@ public String ping() {

@GetMapping("/")
public String index() {
return "Running...";
return "Running!!!...";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.ugent.pidgeon.controllers;

import com.ugent.pidgeon.postgre.models.CourseEntity;
import com.ugent.pidgeon.postgre.models.GroupClusterEntity;
import com.ugent.pidgeon.postgre.models.ProjectEntity;
import com.ugent.pidgeon.postgre.models.UserEntity;
import com.ugent.pidgeon.postgre.repository.CourseRepository;
import com.ugent.pidgeon.postgre.repository.GroupClusterRepository;
import com.ugent.pidgeon.postgre.repository.ProjectRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class JpaCourseController {
@Autowired
private CourseRepository courseRepository;

@Autowired
private GroupClusterRepository groupClusterRepository;

@Autowired
private ProjectRepository projectRepository;

@GetMapping("/api/courses")
public String getCourses() {
StringBuilder res = new StringBuilder();
for (CourseEntity course : courseRepository.findAll()) {
res.append(course.getName()).append(" with users: ");
for (CourseRepository.UserWithRelation user : courseRepository.findUsersByCourseId(course.getId())) {
UserEntity userEntity = user.getUser();
String relation = user.getRelation();
res.append(userEntity.getName()).append("(").append(relation).append("), ");
}
res.append("- with group clusters:");
for (GroupClusterEntity groupcluster: groupClusterRepository.findByCourseId(course.getId())) {
res.append(groupcluster.getName()).append(" (").append(groupcluster.getGroupAmount()).append("), ");
}
res.append("- with projects:");
for (ProjectEntity project: projectRepository.findByCourseId(course.getId())) {
res.append(project.getName()).append(", ");
}
res.append("|\n");
}

return res.toString();
}

// @GetMapping("/api/course")
// public String addCourse(String name, String description) {
// CourseEntity course = new CourseEntity("test", "added to test creating with contstructing");
// course.setId(1);
// courseRepository.save(course);
// return "Course added";
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.ugent.pidgeon.controllers;

import com.ugent.pidgeon.postgre.models.GroupEntity;
import com.ugent.pidgeon.postgre.models.GroupFeedbackEntity;
import com.ugent.pidgeon.postgre.models.SubmissionEntity;
import com.ugent.pidgeon.postgre.models.UserEntity;
import com.ugent.pidgeon.postgre.repository.GroupFeedbackRepository;
import com.ugent.pidgeon.postgre.repository.GroupRepository;
import com.ugent.pidgeon.postgre.repository.SubmissionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class JpaGroupController {

@Autowired
private GroupRepository groupRepository;

@Autowired
private GroupFeedbackRepository groupFeedbackRepository;

@Autowired
SubmissionRepository submissionRepository;

@GetMapping("/api/groups")
public List<String> getGroups() {
List<String> res = new ArrayList<>();
for (GroupEntity group : groupRepository.findAll()) {
StringBuilder groupString = new StringBuilder();
groupString.append(group.getName()).append("-with users: ");
for (UserEntity user : groupRepository.findCourseUsersByGroupId(group.getId())) {
groupString.append(user.getName()).append(", ");
}
List<Long> projectIds = groupRepository.findProjectsByGroupId(group.getId());
groupString.append("-with grades: ");
for (long projectId : projectIds) {
GroupFeedbackEntity feedback = groupFeedbackRepository.findByGroupIdAndProjectId(group.getId(), projectId);
groupString.append(feedback.getGrade()).append(", ");
}
groupString.append("-with submissions: ");
for (long projectId : projectIds) {
for (SubmissionEntity submission : submissionRepository.findByGroupIdAndProjectId(group.getId(), projectId)) {
groupString.append(submission.getSubmissionTime()).append(", ");
}
}
groupString.append("|");
res.add(groupString.toString());
}
return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.ugent.pidgeon.controllers;

import com.ugent.pidgeon.postgre.models.DeadlineEntity;
import com.ugent.pidgeon.postgre.models.ProjectEntity;
import com.ugent.pidgeon.postgre.models.TestEntity;
import com.ugent.pidgeon.postgre.repository.ProjectRepository;
import com.ugent.pidgeon.postgre.repository.TestRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
public class JpaProjectController {
@Autowired
private ProjectRepository projectRepository;

@Autowired
private TestRepository testRepository;

@GetMapping("/api/projects")
public List<String> getProjects() {
List<String> res = new ArrayList<>();
for (ProjectEntity project : projectRepository.findAll()) {
StringBuilder projectString = new StringBuilder(project.getName());
Optional<TestEntity> test = testRepository.findById(project.getId());
test.ifPresent(testEntity -> projectString.append(" with test: ").append(testEntity.getId()));
projectString.append(" with deadlines: ");
for (DeadlineEntity deadline : project.getDeadlines()) {
projectString.append(deadline.getDeadline());
}
res.add(projectString.toString());
}
return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.ugent.pidgeon.controllers;

import com.ugent.pidgeon.postgre.models.FileEntity;
import com.ugent.pidgeon.postgre.models.SubmissionEntity;
import com.ugent.pidgeon.postgre.repository.FileRepository;
import com.ugent.pidgeon.postgre.repository.SubmissionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
public class JpaSubmissionController {
@Autowired
private SubmissionRepository submissionRepository;

@Autowired
private FileRepository fileRepository;

@GetMapping("/api/submissions")
public List<String> getSubmissions() {
List<String> res = new ArrayList<>();
for (SubmissionEntity submission : submissionRepository.findAll()) {
StringBuilder submissionString = new StringBuilder();
submissionString.append(submission.getSubmissionTime()).append(" with files: ");
Optional<FileEntity> file = fileRepository.findById(submission.getFileId());
file.ifPresent(fileEntity -> submissionString.append(fileEntity.getName()).append(", "));

submissionString.append("|");
res.add(submissionString.toString());
}
return res;
}

}
Loading

0 comments on commit ae80829

Please sign in to comment.