Rename generic_list.html to list.html; update UserController and SecurityConfig for new user and department list endpoints
This commit is contained in:
parent
8146848e8f
commit
1df99d3ea0
|
|
@ -25,7 +25,7 @@ public class SecurityConfig {
|
|||
.requestMatchers("/css/**", "/js/**", "/images/**").permitAll() // Permitir acceso a CSS, JS e imágenes
|
||||
.requestMatchers("/admin/**").hasRole("ADMIN")
|
||||
.requestMatchers("/user/**").hasAnyRole("USER", "ADMIN")
|
||||
.requestMatchers("/management", "/user_management", "/register","/generic_list","generic_list_departamento").hasRole("ADMIN")
|
||||
.requestMatchers("/management", "/user_management", "/register","/user_list","/departamento_list").hasRole("ADMIN")
|
||||
.requestMatchers("/", "/login").permitAll()
|
||||
.anyRequest().authenticated()
|
||||
)
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import com.ieslamar.GestionInventario.dto.BotonDTO;
|
|||
import com.ieslamar.GestionInventario.entities.User;
|
||||
import com.ieslamar.GestionInventario.services.DepartamentoService;
|
||||
import com.ieslamar.GestionInventario.services.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
|
@ -13,13 +12,11 @@ import org.springframework.ui.Model;
|
|||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
|
@ -35,9 +32,9 @@ public class UserController {
|
|||
this.departamentoService = departamentoService;
|
||||
}
|
||||
|
||||
private final BotonDTO gestionUsuarios = new BotonDTO("Gestión de usuarios", "/user_management", true, null);
|
||||
private final BotonDTO gestionUsuarios = new BotonDTO("Gestión de usuarios", "/user_list", true, null);
|
||||
private final BotonDTO gestionProductos = new BotonDTO("Gestión de Productos", "/user_management", true, null);
|
||||
private final BotonDTO gestionDepartamentos = new BotonDTO("Gestión de Departamentos", "/user_management", true, null);
|
||||
private final BotonDTO gestionDepartamentos = new BotonDTO("Gestión de Departamentos", "/departamento_list", true, null);
|
||||
private final BotonDTO gestionCategorias = new BotonDTO("Gestión de Categorías", "/user_management", true, null);
|
||||
private final BotonDTO gestionUbicaciones = new BotonDTO("Gestión de Ubicaciones", "/user_management", true, null);
|
||||
private final BotonDTO gestionInventario = new BotonDTO("Gestión de Inventario", "/user_management", true, null);
|
||||
|
|
@ -113,36 +110,21 @@ public class UserController {
|
|||
@PostMapping("/register")
|
||||
public String register(@RequestParam String username, @RequestParam String password, @RequestParam String role, @RequestParam String mail) {
|
||||
userService.registerUser(username, password, role, mail, null);
|
||||
return "redirect:/login?success"; // Redirige al login tras registrarse
|
||||
return "redirect:/user_list"; // Redirige al login tras registrarse
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/user_management")
|
||||
public String listUsers(Model model) {
|
||||
List<User> users = userService.getAllUsers();
|
||||
model.addAttribute("users", users);
|
||||
return "user_management";
|
||||
}
|
||||
@GetMapping("/delete_user")
|
||||
public String deleteUser(@RequestParam Long id) {
|
||||
@PostMapping("/delete_user")
|
||||
public String deleteUser(@RequestParam("id") Long id) {
|
||||
User user = userService.getUserById(id);
|
||||
if (user != null) {
|
||||
userService.deleteUser(user);
|
||||
}
|
||||
return "redirect:/user_management";
|
||||
userService.deleteUser(user); // Llama al servicio para eliminar el usuario
|
||||
return "redirect:/user_list"; // Redirige a la página de gestión de usuarios
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@GetMapping("/generic_list")
|
||||
@GetMapping("/user_list")
|
||||
public String genericPage(Model model) {
|
||||
model.addAttribute("title", "Gestión Genérica");
|
||||
model.addAttribute("title", "Gestión de usuarios");
|
||||
model.addAttribute("headers", List.of("ID", "Nombre", "Rol", "Email", "Acciones"));
|
||||
model.addAttribute("items", userService.getAllUsers().stream()
|
||||
.map(user -> Map.of(
|
||||
|
|
@ -151,15 +133,15 @@ public class UserController {
|
|||
))
|
||||
.toList());
|
||||
model.addAttribute("actionsUrl", Map.of(
|
||||
"edit", "/edit_user",
|
||||
"edit", "",
|
||||
"delete", "/delete_user"
|
||||
));
|
||||
model.addAttribute("backUrl", "/Inventario/home");
|
||||
model.addAttribute("addUrl", "/Inventario/register");
|
||||
return "generic_list";
|
||||
return "list";
|
||||
}
|
||||
|
||||
@GetMapping("/generic_list_departamento")
|
||||
@GetMapping("/departamento_list")
|
||||
public String genericPageDepartamento(Model model) {
|
||||
model.addAttribute("title", "Gestión Genérica");
|
||||
model.addAttribute("headers", List.of("ID", "Nombre","Acciones"));
|
||||
|
|
@ -170,12 +152,12 @@ public class UserController {
|
|||
))
|
||||
.toList());
|
||||
model.addAttribute("actionsUrl", Map.of(
|
||||
"edit", "/edit_user",
|
||||
"delete", "/delete_user"
|
||||
"edit", "",
|
||||
"delete", ""
|
||||
));
|
||||
model.addAttribute("backUrl", "/Inventario/home");
|
||||
model.addAttribute("addUrl", "/Inventario/register");
|
||||
return "generic_list";
|
||||
return "list";
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -19,10 +19,9 @@
|
|||
<td th:each="value : ${item.values}" th:text="${value}"></td>
|
||||
<td class="celda-acciones">
|
||||
<button th:if="${actionsUrl.edit}" th:href="${actionsUrl.edit} + '/' + ${item.id}" th:title="Editar">✏️</button>
|
||||
<form th:if="${actionsUrl.delete}" th:action="${actionsUrl.delete}" method="post" style="display:inline;">
|
||||
<input type="hidden" th:name="id" th:value="${item.id}"/>
|
||||
<button type="submit" th:title="Borrar" formaction="#"
|
||||
onclick="if(confirm('¿Estás seguro de que deseas borrar este elemento?')) { this.form.action = this.form.getAttribute('th:action'); } else { return false; }">
|
||||
<form th:if="${actionsUrl.delete}" th:action="@{/delete_user}" method="post" style="display:inline;">
|
||||
<input type="hidden" name="id" th:value="${item.id}" />
|
||||
<button type="submit" th:title="Borrar" onclick="return confirm('¿Estás seguro de que deseas borrar este elemento?');">
|
||||
🗑️
|
||||
</button>
|
||||
</form>
|
||||
|
|
@ -1,54 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="es" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<title>Gestión de Usuarios</title>
|
||||
<link rel="stylesheet" th:href="@{/css/table_style.css}">
|
||||
</head>
|
||||
<body>
|
||||
<div class="contenido">
|
||||
<h2>Gestión de Usuarios</h2>
|
||||
<div class="table-container">
|
||||
<table class="table1">
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="celda-id">ID</th>
|
||||
<th>Nombre</th>
|
||||
<th>Rol</th>
|
||||
<th>Email</th>
|
||||
<th>Acciones</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr th:each="user : ${users}">
|
||||
<td class="celda-id" th:text="${user.id}"></td>
|
||||
<td th:text="${user.nombre}"></td>
|
||||
<td th:text="${user.role}"></td>
|
||||
<td th:title="${user.mail}" th:text="${user.mail}"></td>
|
||||
<td class="celda-acciones">
|
||||
<button th:title="Editar">✏️</button>
|
||||
<form th:action="@{/delete_user}" method="get" style="display:inline;">
|
||||
<input type="hidden" th:name="id" th:value="${user.id}"/>
|
||||
<button type="submit" th:title="Borrar" formaction="#"
|
||||
onclick="if(confirm('¿Estás seguro de que deseas borrar este usuario?')) { this.form.action = this.form.getAttribute('th:action'); } else { return false; }">
|
||||
🗑️
|
||||
</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div class="botones">
|
||||
<a href="/Inventario/home">
|
||||
<button class="button_salir">Volver al inicio</button>
|
||||
</a>
|
||||
<a href="/Inventario/register">
|
||||
<button>Añadir usuario</button>
|
||||
</a>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
||||
Loading…
Reference in New Issue