Add generic list functionality for users and departments; implement new endpoints and update security configuration

This commit is contained in:
jon ander 2025-04-14 19:30:42 +02:00
parent 458d79d401
commit 8146848e8f
8 changed files with 114 additions and 22 deletions

View File

@ -2,6 +2,7 @@
package com.ieslamar.GestionInventario; package com.ieslamar.GestionInventario;
import com.ieslamar.GestionInventario.dto.BotonDTO; import com.ieslamar.GestionInventario.dto.BotonDTO;
import com.ieslamar.GestionInventario.entities.Role; import com.ieslamar.GestionInventario.entities.Role;
import com.ieslamar.GestionInventario.services.DepartamentoService;
import com.ieslamar.GestionInventario.services.UserService; import com.ieslamar.GestionInventario.services.UserService;
import org.springframework.boot.CommandLineRunner; import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -17,10 +18,13 @@ public class DataInitializer {
// userService.registerUser("user", "user123", Role.USER.name(), "user123@ejemplo.com",null); // userService.registerUser("user", "user123", Role.USER.name(), "user123@ejemplo.com",null);
// }; // };
// } // }
// public static void test(){
// BotonDTO boton1 = new BotonDTO("Inventario", "/Inventario/management"); // @Bean
// boton1.isRequireAdmin(); // public CommandLineRunner initData(DepartamentoService departamentoService) {
// // return args -> {
// departamentoService.registerDepartamento("Informática");
// departamentoService.registerDepartamento("Ciencias");
// };
// } // }
} }

View File

@ -25,7 +25,7 @@ public class SecurityConfig {
.requestMatchers("/css/**", "/js/**", "/images/**").permitAll() // Permitir acceso a CSS, JS e imágenes .requestMatchers("/css/**", "/js/**", "/images/**").permitAll() // Permitir acceso a CSS, JS e imágenes
.requestMatchers("/admin/**").hasRole("ADMIN") .requestMatchers("/admin/**").hasRole("ADMIN")
.requestMatchers("/user/**").hasAnyRole("USER", "ADMIN") .requestMatchers("/user/**").hasAnyRole("USER", "ADMIN")
.requestMatchers("/management", "/user_management", "/register").hasRole("ADMIN") .requestMatchers("/management", "/user_management", "/register","/generic_list","generic_list_departamento").hasRole("ADMIN")
.requestMatchers("/", "/login").permitAll() .requestMatchers("/", "/login").permitAll()
.anyRequest().authenticated() .anyRequest().authenticated()
) )

View File

@ -3,6 +3,7 @@ package com.ieslamar.GestionInventario.controllers;
import com.ieslamar.GestionInventario.dto.BotonDTO; import com.ieslamar.GestionInventario.dto.BotonDTO;
import com.ieslamar.GestionInventario.entities.User; import com.ieslamar.GestionInventario.entities.User;
import com.ieslamar.GestionInventario.services.DepartamentoService;
import com.ieslamar.GestionInventario.services.UserService; import com.ieslamar.GestionInventario.services.UserService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
@ -20,15 +21,18 @@ import java.util.ArrayList;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
@Controller @Controller
public class UserController { public class UserController {
private final UserService userService; private final UserService userService;
private final DepartamentoService departamentoService;
public UserController(UserService userService) { public UserController(UserService userService, DepartamentoService departamentoService) {
this.userService = userService; this.userService = userService;
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_management", true, null);
@ -128,4 +132,51 @@ public class UserController {
return "redirect:/user_management"; return "redirect:/user_management";
} }
@GetMapping("/generic_list")
public String genericPage(Model model) {
model.addAttribute("title", "Gestión Genérica");
model.addAttribute("headers", List.of("ID", "Nombre", "Rol", "Email", "Acciones"));
model.addAttribute("items", userService.getAllUsers().stream()
.map(user -> Map.of(
"id", user.getId(),
"values", List.of(user.getId(), user.getNombre(), user.getRole(), user.getMail())
))
.toList());
model.addAttribute("actionsUrl", Map.of(
"edit", "/edit_user",
"delete", "/delete_user"
));
model.addAttribute("backUrl", "/Inventario/home");
model.addAttribute("addUrl", "/Inventario/register");
return "generic_list";
}
@GetMapping("/generic_list_departamento")
public String genericPageDepartamento(Model model) {
model.addAttribute("title", "Gestión Genérica");
model.addAttribute("headers", List.of("ID", "Nombre","Acciones"));
model.addAttribute("items", departamentoService.getAllDepartamentos().stream()
.map(departamento -> Map.of(
"id", departamento.getId(),
"values", List.of(departamento.getId(), departamento.getNombre())
))
.toList());
model.addAttribute("actionsUrl", Map.of(
"edit", "/edit_user",
"delete", "/delete_user"
));
model.addAttribute("backUrl", "/Inventario/home");
model.addAttribute("addUrl", "/Inventario/register");
return "generic_list";
}
} }

View File

@ -16,4 +16,10 @@ public class DepartamentoService {
public List<Departamento> getAllDepartamentos() { public List<Departamento> getAllDepartamentos() {
return departamentoRepository.findAll(); return departamentoRepository.findAll();
} }
public void registerDepartamento(String nombre) {
Departamento departamento = new Departamento();
departamento.setNombre(nombre);
departamentoRepository.save(departamento);
}
} }

View File

@ -23,7 +23,7 @@ USE `inventario` ;
DROP TABLE IF EXISTS `inventario`.`departamentos` ; DROP TABLE IF EXISTS `inventario`.`departamentos` ;
CREATE TABLE IF NOT EXISTS `inventario`.`departamentos` ( CREATE TABLE IF NOT EXISTS `inventario`.`departamentos` (
`iddepartamentos` INT NOT NULL, `iddepartamentos` INT NOT NULL AUTO_INCREMENT,
`Nombre` VARCHAR(45) NULL, `Nombre` VARCHAR(45) NULL,
PRIMARY KEY (`iddepartamentos`)) PRIMARY KEY (`iddepartamentos`))
ENGINE = InnoDB; ENGINE = InnoDB;
@ -147,7 +147,7 @@ ENGINE = InnoDB;
DROP TABLE IF EXISTS `inventario`.`proveedores` ; DROP TABLE IF EXISTS `inventario`.`proveedores` ;
CREATE TABLE IF NOT EXISTS `inventario`.`proveedores` ( CREATE TABLE IF NOT EXISTS `inventario`.`proveedores` (
`idproveedores` INT NOT NULL, `idproveedores` INT NOT NULL AUTO_INCREMENT,
`nombre` VARCHAR(45) NOT NULL, `nombre` VARCHAR(45) NOT NULL,
PRIMARY KEY (`idproveedores`)) PRIMARY KEY (`idproveedores`))
ENGINE = InnoDB; ENGINE = InnoDB;
@ -159,7 +159,7 @@ ENGINE = InnoDB;
DROP TABLE IF EXISTS `inventario`.`factura` ; DROP TABLE IF EXISTS `inventario`.`factura` ;
CREATE TABLE IF NOT EXISTS `inventario`.`factura` ( CREATE TABLE IF NOT EXISTS `inventario`.`factura` (
`idfactura` INT NOT NULL, `idfactura` INT NOT NULL AUTO_INCREMENT,
`foto` LONGBLOB NULL, `foto` LONGBLOB NULL,
`fecha` DATE NOT NULL, `fecha` DATE NOT NULL,
`fin_garantia` DATE NOT NULL, `fin_garantia` DATE NOT NULL,

View File

@ -1,13 +0,0 @@
<!-- fragments/buttons.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.w3.org/1999/xhtml">
<body>
<div th:fragment="List()" class="container1">
</div>
</body>
</html>

View File

@ -0,0 +1,44 @@
<!DOCTYPE html>
<html lang="es" xmlns:th="http://www.thymeleaf.org">
<head>
<title th:text="${title}">Página Genérica</title>
<link rel="stylesheet" th:href="@{/css/table_style.css}">
</head>
<body>
<div class="contenido">
<h2 th:text="${title}"></h2>
<div class="table-container">
<table class="table1">
<thead>
<tr>
<th th:each="header : ${headers}" th:text="${header}"></th>
</tr>
</thead>
<tbody>
<tr th:each="item : ${items}">
<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; }">
🗑️
</button>
</form>
</td>
</tr>
</tbody>
</table>
</div>
<div class="botones">
<a th:if="${backUrl}" th:href="${backUrl}">
<button class="button_salir">Volver</button>
</a>
<a th:if="${addUrl}" th:href="${addUrl}">
<button>Añadir</button>
</a>
</div>
</div>
</body>
</html>