Любовь, ненависть, боль, наслаждение, жизнь, смерть. Здесь есть всё... Вот что значит — быть человеком.

This commit is contained in:
Кашин Максим 2023-05-15 15:37:45 +04:00
parent 69d2ccb2b3
commit 8c8bfedcdf
4 changed files with 28 additions and 13 deletions

Binary file not shown.

View File

@ -1,12 +1,17 @@
package com.example.maxim.lab3.controller; package com.example.maxim.lab3.controller;
import com.example.maxim.lab3.model.UserRole;
import com.example.maxim.lab3.service.StoreService; import com.example.maxim.lab3.service.StoreService;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.security.Principal;
@Controller @Controller
@RequestMapping("/store") @RequestMapping("/store")
public class StoreMVCController { public class StoreMVCController {
@ -33,16 +38,21 @@ public class StoreMVCController {
return "buyer-info"; return "buyer-info";
} }
@GetMapping(value = {"/edit", "/edit/{id}"}) @GetMapping(value = {"/edit", "/edit/{id}"})
public String editStore(@PathVariable(required = false) Long id, public String editStore(@PathVariable(required = false) Long id, Model model, Principal principal) {
Model model) { String roleName = ((Authentication)principal).getAuthorities().toArray()[0].toString();
if (id == null || id <= 0) { if (UserRole.ADMIN.toString().equals(roleName)) {
model.addAttribute("StoreDTO", new StoreDTO()); if (id == null || id <= 0) {
model.addAttribute("StoreDTO", new StoreDTO());
} else {
model.addAttribute("storeId", id);
model.addAttribute("StoreDTO", new StoreDTO(storeService.findStore(id)));
}
return "/store-edit";
} else { } else {
model.addAttribute("storeId", id); return "redirect:/error";
model.addAttribute("StoreDTO", new StoreDTO(storeService.findStore(id)));
} }
return "store-edit";
} }
@PostMapping(value = {"", "/{id}"}) @PostMapping(value = {"", "/{id}"})
public String saveStore(@PathVariable(required = false) Long id, public String saveStore(@PathVariable(required = false) Long id,
@ModelAttribute @Valid StoreDTO storeDTO, @ModelAttribute @Valid StoreDTO storeDTO,
@ -60,9 +70,14 @@ public class StoreMVCController {
return "redirect:/store"; return "redirect:/store";
} }
@PostMapping("/delete/{id}") @PostMapping("/delete/{id}")
public String deleteStore(@PathVariable Long id) { public String deleteStore(@PathVariable Long id, Principal principal) {
storeService.deleteStore(id); String roleName = ((Authentication)principal).getAuthorities().toArray()[0].toString();
return "redirect:/store"; if (UserRole.ADMIN.toString().equals(roleName)) {
storeService.deleteStore(id);
return "redirect:/store";
} else {
return "redirect:/error";
}
} }
} }

View File

@ -1,5 +1,5 @@
spring.main.banner-mode=off spring.main.banner-mode=off
server.port=8081 server.port=8080
#server.tomcat.relaxed-query-chars=|,{,},[,] #server.tomcat.relaxed-query-chars=|,{,},[,]
spring.datasource.url=jdbc:h2:file:./data spring.datasource.url=jdbc:h2:file:./data
spring.datasource.driverClassName=org.h2.Driver spring.datasource.driverClassName=org.h2.Driver

View File

@ -7,7 +7,7 @@
<body> <body>
<div layout:fragment="content"> <div layout:fragment="content">
<div> <div>
<a sec:authorize="isAuthenticated() and hasRole('ROLE_ADMIN')" class="btn btn-outline-dark text-center d-flex justify-content-md-center mx-5" <a sec:authorize="isAuthenticated()" class="btn btn-outline-dark text-center d-flex justify-content-md-center mx-5"
th:href="@{/store/edit}"> th:href="@{/store/edit}">
<i class="fa-solid fa-plus"></i> Добавить <i class="fa-solid fa-plus"></i> Добавить
</a> </a>
@ -36,7 +36,7 @@
th:href="@{/store/edit/{id}(id=${store.id})}"> th:href="@{/store/edit/{id}(id=${store.id})}">
<i class="fa fa-pencil" aria-hidden="true"></i> Изменить <i class="fa fa-pencil" aria-hidden="true"></i> Изменить
</a> </a>
<button sec:authorize="isAuthenticated() and hasRole('ROLE_ADMIN')" type="button" class="btn btn-outline-dark text-center d-flex justify-content-md-center mx-5" style="min-width: 120px;" <button sec:authorize="isAuthenticated()" type="button" class="btn btn-outline-dark text-center d-flex justify-content-md-center mx-5" style="min-width: 120px;"
th:attr="onclick=|confirm('Удалить запись?') && document.getElementById('remove-${store.id}').click()|"> th:attr="onclick=|confirm('Удалить запись?') && document.getElementById('remove-${store.id}').click()|">
<i class="fa fa-trash" aria-hidden="true"></i> Удалить <i class="fa fa-trash" aria-hidden="true"></i> Удалить
</button> </button>