From 06cbf34b5476aaf6efb7c6cc55613441be328d4b Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Mon, 13 May 2019 11:40:24 +0200 Subject: [PATCH] Added web config, added student service remove method. --- .../config/WebSecurityConfig.java | 41 +++++++++++++++++++ .../students/StudentEntityController.java | 35 +--------------- .../entity/StudentEntityRemoveController.java | 34 +++++++++++++++ .../services/StudentService.java | 30 +++++++++++++- 4 files changed, 105 insertions(+), 35 deletions(-) create mode 100644 src/main/java/nl/andrewlalis/teaching_assistant_assistant/config/WebSecurityConfig.java create mode 100644 src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/students/entity/StudentEntityRemoveController.java diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/config/WebSecurityConfig.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/config/WebSecurityConfig.java new file mode 100644 index 0000000..c6f134a --- /dev/null +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/config/WebSecurityConfig.java @@ -0,0 +1,41 @@ +package nl.andrewlalis.teaching_assistant_assistant.config; + +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.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.core.userdetails.User; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.provisioning.InMemoryUserDetailsManager; + +@Configuration +@EnableWebSecurity +public class WebSecurityConfig extends WebSecurityConfigurerAdapter { + + @Override + protected void configure(HttpSecurity http) throws Exception { + http.authorizeRequests() + .antMatchers("/").permitAll() + .anyRequest().authenticated() + .and() + .authorizeRequests() + .antMatchers("/resources/**").permitAll().anyRequest().permitAll() + .and() + .formLogin() + .loginPage("/login") + .permitAll() + .and() + .logout() + .permitAll(); + } + + public UserDetailsService userDetailsService () { + UserDetails user = User.withDefaultPasswordEncoder() + .username("andrewlalis") + .password("test") + .roles("USER") + .build(); + return new InMemoryUserDetailsManager(user); + } +} diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/students/StudentEntityController.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/students/StudentEntityController.java index 0f679d7..2d2f8f5 100644 --- a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/students/StudentEntityController.java +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/students/StudentEntityController.java @@ -1,11 +1,7 @@ package nl.andrewlalis.teaching_assistant_assistant.controllers.students; -import nl.andrewlalis.teaching_assistant_assistant.model.Course; import nl.andrewlalis.teaching_assistant_assistant.model.people.Student; -import nl.andrewlalis.teaching_assistant_assistant.model.people.teams.Team; -import nl.andrewlalis.teaching_assistant_assistant.model.repositories.CourseRepository; import nl.andrewlalis.teaching_assistant_assistant.model.repositories.StudentRepository; -import nl.andrewlalis.teaching_assistant_assistant.model.repositories.TeamRepository; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @@ -20,13 +16,9 @@ import java.util.Optional; public class StudentEntityController { private StudentRepository studentRepository; - private TeamRepository teamRepository; - private CourseRepository courseRepository; - protected StudentEntityController(StudentRepository studentRepository, TeamRepository teamRepository, CourseRepository courseRepository) { + protected StudentEntityController(StudentRepository studentRepository) { this.studentRepository = studentRepository; - this.teamRepository = teamRepository; - this.courseRepository = courseRepository; } @GetMapping("/students/{id}") @@ -35,29 +27,4 @@ public class StudentEntityController { optionalStudent.ifPresent(student -> model.addAttribute("student", student)); return "students/entity"; } - - - - @GetMapping("/students/{id}/remove") - public String getRemove(@PathVariable long id) { - Optional optionalStudent = this.studentRepository.findById(id); - optionalStudent.ifPresent(student -> { - - for (Team team : student.getTeams()) { - team.removeMember(student); - student.removeFromAssignedTeam(team); - this.teamRepository.save(team); - } - - for (Course course : student.getCourses()) { - course.removeParticipant(student); - student.removeFromAssignedCourse(course); - this.courseRepository.save(course); - } - - this.studentRepository.delete(student); - }); - - return "redirect:/students"; - } } diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/students/entity/StudentEntityRemoveController.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/students/entity/StudentEntityRemoveController.java new file mode 100644 index 0000000..59bc537 --- /dev/null +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/students/entity/StudentEntityRemoveController.java @@ -0,0 +1,34 @@ +package nl.andrewlalis.teaching_assistant_assistant.controllers.students.entity; + +import nl.andrewlalis.teaching_assistant_assistant.model.people.Student; +import nl.andrewlalis.teaching_assistant_assistant.model.repositories.StudentRepository; +import nl.andrewlalis.teaching_assistant_assistant.services.StudentService; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; + +import java.util.Optional; + +/** + * Controller for removing a student from the application. + */ +@Controller +public class StudentEntityRemoveController { + + private StudentService studentService; + private StudentRepository studentRepository; + + protected StudentEntityRemoveController(StudentService studentService, StudentRepository studentRepository) { + this.studentService = studentService; + this.studentRepository = studentRepository; + } + + @GetMapping("/students/{id}/remove") + public String getRemove(@PathVariable long id) { + Optional optionalStudent = this.studentRepository.findById(id); + optionalStudent.ifPresent(student -> this.studentService.removeStudent(student)); + + return "redirect:/students"; + } + +} diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/services/StudentService.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/services/StudentService.java index 258c771..70415aa 100644 --- a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/services/StudentService.java +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/services/StudentService.java @@ -2,10 +2,14 @@ package nl.andrewlalis.teaching_assistant_assistant.services; import nl.andrewlalis.teaching_assistant_assistant.model.Course; import nl.andrewlalis.teaching_assistant_assistant.model.people.Student; +import nl.andrewlalis.teaching_assistant_assistant.model.people.teams.Team; import nl.andrewlalis.teaching_assistant_assistant.model.repositories.CourseRepository; import nl.andrewlalis.teaching_assistant_assistant.model.repositories.StudentRepository; +import nl.andrewlalis.teaching_assistant_assistant.model.repositories.TeamRepository; import org.springframework.stereotype.Service; +import java.util.List; + /** * Helps with manipulation and various operations on individual students or groups of students (not teams). */ @@ -14,10 +18,12 @@ public class StudentService { private StudentRepository studentRepository; private CourseRepository courseRepository; + private TeamRepository teamRepository; - protected StudentService (StudentRepository studentRepository, CourseRepository courseRepository) { + protected StudentService (StudentRepository studentRepository, CourseRepository courseRepository, TeamRepository teamRepository) { this.studentRepository = studentRepository; this.courseRepository = courseRepository; + this.teamRepository = teamRepository; } /** @@ -49,4 +55,26 @@ public class StudentService { this.studentRepository.save(student); } + /** + * Removes a student from the application completely. + * @param student The student to remove. + */ + public void removeStudent(Student student) { + List teams = student.getTeams(); + for (Team team : teams) { + team.removeMember(student); + student.removeFromAssignedTeam(team); + this.teamRepository.save(team); + } + + List courses = student.getCourses(); + for (Course course : courses) { + course.removeParticipant(student); + student.removeFromAssignedCourse(course); + this.courseRepository.save(course); + } + + this.studentRepository.delete(student); + } + }