diff --git a/src/main/java/com/nighthawk/spring_portfolio/SecurityConfig.java b/src/main/java/com/nighthawk/spring_portfolio/SecurityConfig.java index 1ad2572..3abe2e7 100644 --- a/src/main/java/com/nighthawk/spring_portfolio/SecurityConfig.java +++ b/src/main/java/com/nighthawk/spring_portfolio/SecurityConfig.java @@ -69,8 +69,9 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { // list the requests/endpoints need to be authenticated .authorizeHttpRequests(auth -> auth .requestMatchers("/authenticate").permitAll() - .requestMatchers("/mvc/person/update/**", "/mvc/person/delete/**").hasAnyAuthority("ROLE_PLAYER") - .requestMatchers("/api/person/delete/**").hasAnyAuthority("ROLE_PLAYER") + .requestMatchers("/reading").hasAnyAuthority("ROLE_ADMIN") + .requestMatchers("/mvc/person/update/**", "/mvc/person/delete/**").hasAnyAuthority("ROLE_ADMIN") + .requestMatchers("/api/person/delete/**").hasAnyAuthority("ROLE_ADMIN") .requestMatchers("/**").permitAll() ) // support cors diff --git a/src/main/java/com/nighthawk/spring_portfolio/controllers/readcontroller.java b/src/main/java/com/nighthawk/spring_portfolio/controllers/readcontroller.java index 9c93334..373ce9d 100644 --- a/src/main/java/com/nighthawk/spring_portfolio/controllers/readcontroller.java +++ b/src/main/java/com/nighthawk/spring_portfolio/controllers/readcontroller.java @@ -7,21 +7,60 @@ import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import com.nighthawk.spring_portfolio.mvc.person.Person; +import com.nighthawk.spring_portfolio.mvc.person.PersonDetailsService; +import com.nighthawk.spring_portfolio.mvc.person.PersonJpaRepository; @Controller // HTTP requests are handled as a controller, using the @Controller annotation public class readcontroller { + @Autowired + private PersonJpaRepository repository; + // @GetMapping handles GET request for /greet, maps it to greeting() method @GetMapping("/reading") - // @RequestParam handles variables binding to frontend, defaults, etc - public String read(@RequestParam(name="name", required=false, defaultValue="World") String name, Model model) { + @PreAuthorize("isAuthenticated()") - // model attributes are visible to Thymeleaf when HTML is "pre-processed" - model.addAttribute("person", name); + //@PreAuthorize("hasRole('ROLE_ADMIN')") + // @RequestParam handles variables binding to frontend, defaults, etc + public String person(Model model) { + List persons = repository.findAllByOrderByNameAsc(); + model.addAttribute("persons", persons); + // System.out.println(persons.toString()); for testing purposes + return "reading"; + } - // load HTML VIEW (greet.html) - return "reading"; + @GetMapping("/delete/{id}") + public String deletePerson(@PathVariable Long id) { + if(id < 7) + { + System.out.println("can't delete admins!"); + return "redirect:/reading"; + } + else { + repository.deleteById(id); + return "redirect:/reading"; + } + } + - } } \ No newline at end of file diff --git a/src/main/java/com/nighthawk/spring_portfolio/controllers/updatecontroller.java b/src/main/java/com/nighthawk/spring_portfolio/controllers/updatecontroller.java new file mode 100644 index 0000000..c1d0360 --- /dev/null +++ b/src/main/java/com/nighthawk/spring_portfolio/controllers/updatecontroller.java @@ -0,0 +1,51 @@ +package com.nighthawk.spring_portfolio.controllers; +/* MVC code that shows defining a simple Model, calling View, and this file serving as Controller + * Web Content with Spring MVCSpring Example: https://spring.io/guides/gs/serving-web-con + */ + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import com.nighthawk.spring_portfolio.mvc.person.Person; +import com.nighthawk.spring_portfolio.mvc.person.PersonDetailsService; +import com.nighthawk.spring_portfolio.mvc.person.PersonJpaRepository; +@Controller // HTTP requests are handled as a controller, using the @Controller annotation +public class updatecontroller { + + @Autowired + private PersonJpaRepository repository; + + // @GetMapping handles GET request for /greet, maps it to greeting() method + @GetMapping("/updating") + @PreAuthorize("isAuthenticated()") + //@PreAuthorize("hasRole('ROLE_ADMIN')") + // @RequestParam handles variables binding to frontend, defaults, etc + public String person(Model model) { + List persons = repository.findAllByOrderByNameAsc(); + model.addAttribute("persons", persons); + // System.out.println(persons.toString()); for testing purposes + return "reading"; + } + + +} \ No newline at end of file diff --git a/src/main/resources/templates/layouts/nav.html b/src/main/resources/templates/layouts/nav.html index bfa456c..2015d96 100644 --- a/src/main/resources/templates/layouts/nav.html +++ b/src/main/resources/templates/layouts/nav.html @@ -34,7 +34,7 @@ diff --git a/src/main/resources/templates/person/read.html b/src/main/resources/templates/person/read.html index 455b836..70d0349 100644 --- a/src/main/resources/templates/person/read.html +++ b/src/main/resources/templates/person/read.html @@ -35,7 +35,7 @@

Person Viewer

- + Person ID Birth Date Name diff --git a/src/main/resources/templates/reading.html b/src/main/resources/templates/reading.html index 455b836..104be4d 100644 --- a/src/main/resources/templates/reading.html +++ b/src/main/resources/templates/reading.html @@ -21,30 +21,28 @@
-

Person Viewer

- Create Person +

Person Viewer + console (cannot delete admins)

+ Create Person + Update Person + Delete Person
- + - - - + - + - - @@ -57,4 +55,5 @@

Person Viewer

+ diff --git a/src/main/resources/templates/updating.html b/src/main/resources/templates/updating.html new file mode 100644 index 0000000..9ab4083 --- /dev/null +++ b/src/main/resources/templates/updating.html @@ -0,0 +1,89 @@ + + + + + + + Person Update + + + + + +
+ +
+

Welcome, Update The Persons Details

+
+ +
+
+
+ + +

+
IDUser IDEmail PersonAgeAction
Person IDBirth DateBirth Email NameAgeUnknown Age + Delete + updating - Update - Delete
+ + + + + + +
No Roles
Name
+ +
+ +
+ + + + +
+ +
+ + + Email Error +
+
+ + + Password Error +
+
+ + + Name Error +
+
+ + + Birth Date Error +
+ +
+
+
+ + + + + + + +