2025-03-24 18:55:09 +00:00
|
|
|
package com.ieslamar.GestionInventario.controllers;
|
|
|
|
|
|
|
|
|
|
|
2025-04-11 15:54:32 +00:00
|
|
|
import com.ieslamar.GestionInventario.dto.BotonDTO;
|
2025-04-15 15:32:13 +00:00
|
|
|
import com.ieslamar.GestionInventario.entities.Departamento;
|
2025-04-02 18:32:57 +00:00
|
|
|
import com.ieslamar.GestionInventario.entities.User;
|
2025-04-14 17:30:42 +00:00
|
|
|
import com.ieslamar.GestionInventario.services.DepartamentoService;
|
2025-03-26 14:54:51 +00:00
|
|
|
import com.ieslamar.GestionInventario.services.UserService;
|
2025-04-14 17:00:10 +00:00
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
2025-03-24 18:55:09 +00:00
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
|
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;
|
2025-04-14 17:00:10 +00:00
|
|
|
import org.springframework.security.core.GrantedAuthority;
|
2025-04-15 15:32:13 +00:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2025-04-14 17:00:10 +00:00
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
2025-04-02 18:32:57 +00:00
|
|
|
import java.util.List;
|
2025-04-14 17:30:42 +00:00
|
|
|
import java.util.Map;
|
2025-03-24 18:55:09 +00:00
|
|
|
|
|
|
|
|
@Controller
|
|
|
|
|
public class UserController {
|
|
|
|
|
|
|
|
|
|
private final UserService userService;
|
2025-04-14 17:30:42 +00:00
|
|
|
private final DepartamentoService departamentoService;
|
2025-04-15 15:32:13 +00:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
|
2025-03-24 18:55:09 +00:00
|
|
|
|
2025-04-14 17:30:42 +00:00
|
|
|
public UserController(UserService userService, DepartamentoService departamentoService) {
|
2025-03-24 18:55:09 +00:00
|
|
|
this.userService = userService;
|
2025-04-14 17:30:42 +00:00
|
|
|
this.departamentoService = departamentoService;
|
2025-03-24 18:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
2025-04-14 18:18:58 +00:00
|
|
|
private final BotonDTO gestionUsuarios = new BotonDTO("Gestión de usuarios", "/user_list", true, null);
|
2025-04-14 17:00:10 +00:00
|
|
|
private final BotonDTO gestionProductos = new BotonDTO("Gestión de Productos", "/user_management", true, null);
|
2025-04-14 18:18:58 +00:00
|
|
|
private final BotonDTO gestionDepartamentos = new BotonDTO("Gestión de Departamentos", "/departamento_list", true, null);
|
2025-04-14 17:00:10 +00:00
|
|
|
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);
|
|
|
|
|
private final BotonDTO gestionTiposDatos = new BotonDTO("Gestión de Tipos de dato", "/user_management", true, null);
|
|
|
|
|
|
|
|
|
|
private final BotonDTO inventario = new BotonDTO("Inventario", "/management", false, null);
|
|
|
|
|
private final BotonDTO gestion = new BotonDTO("Gestión", "/management", true, null);
|
|
|
|
|
|
|
|
|
|
private final BotonDTO home = new BotonDTO("Salir", "/home", false, "button_salir");
|
|
|
|
|
|
|
|
|
|
private List<BotonDTO> filtrarBotones(Authentication auth,List<BotonDTO> botones){
|
|
|
|
|
List<BotonDTO> botones_response = new ArrayList<>();
|
|
|
|
|
List<String> roles = auth.getAuthorities()
|
|
|
|
|
.stream()
|
|
|
|
|
.map(GrantedAuthority::getAuthority)
|
|
|
|
|
.toList();
|
|
|
|
|
if (roles.contains("ROLE_ADMIN")) {
|
|
|
|
|
return botones;
|
|
|
|
|
}
|
|
|
|
|
for (BotonDTO botone : botones) {
|
|
|
|
|
if (!botone.isRequireAdmin()) {
|
|
|
|
|
botones_response.add(botone);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return botones_response;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-24 18:55:09 +00:00
|
|
|
@GetMapping("/login")
|
|
|
|
|
public String loginPage() {
|
|
|
|
|
return "login"; // Retorna la vista login.html
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-14 17:00:10 +00:00
|
|
|
@GetMapping("/home")
|
|
|
|
|
public String homePage(Model model) {
|
|
|
|
|
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
|
|
|
|
List<BotonDTO> botones = filtrarBotones(auth,new ArrayList<>(List.of(
|
|
|
|
|
inventario,
|
|
|
|
|
gestion
|
|
|
|
|
)));
|
|
|
|
|
model.addAttribute("buttons", botones);
|
|
|
|
|
return "home";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping("/management")
|
|
|
|
|
public String managementPage(Model model){
|
|
|
|
|
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
|
|
|
|
List<BotonDTO>botones = filtrarBotones(auth,new ArrayList<>(List.of(
|
|
|
|
|
gestionUsuarios,
|
|
|
|
|
gestionProductos,
|
|
|
|
|
gestionDepartamentos,
|
|
|
|
|
gestionCategorias,
|
|
|
|
|
gestionUbicaciones,
|
|
|
|
|
gestionInventario,
|
|
|
|
|
gestionCategorias,
|
|
|
|
|
gestionTiposDatos,
|
|
|
|
|
home
|
|
|
|
|
)));
|
|
|
|
|
model.addAttribute("buttons", botones);
|
|
|
|
|
return "management";
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-24 18:55:09 +00:00
|
|
|
|
2025-04-15 15:32:13 +00:00
|
|
|
@GetMapping("/userPage")
|
|
|
|
|
public String userPage(Model model) {
|
|
|
|
|
model.addAttribute("departamentos",departamentoService.getAllDepartamentos());
|
|
|
|
|
return "userPage"; // Retorna la vista register.html
|
2025-03-24 18:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
2025-04-15 15:32:13 +00:00
|
|
|
@PostMapping("/userPage")
|
|
|
|
|
public String userPage(@RequestParam String username,
|
|
|
|
|
@RequestParam String password,
|
|
|
|
|
@RequestParam String role,
|
|
|
|
|
@RequestParam String mail,
|
|
|
|
|
@RequestParam(required = false) Integer iddepartamento
|
|
|
|
|
) {
|
|
|
|
|
Departamento departamento = null;
|
|
|
|
|
if(iddepartamento != null)
|
|
|
|
|
departamento = departamentoService.getDepartamentoById(iddepartamento);
|
|
|
|
|
userService.registerUser(username, password, role, mail, departamento);
|
2025-04-14 18:18:58 +00:00
|
|
|
return "redirect:/user_list"; // Redirige al login tras registrarse
|
2025-03-24 18:55:09 +00:00
|
|
|
}
|
2025-04-07 18:32:56 +00:00
|
|
|
|
2025-04-14 18:18:58 +00:00
|
|
|
@PostMapping("/delete_user")
|
|
|
|
|
public String deleteUser(@RequestParam("id") Long id) {
|
2025-04-07 18:32:56 +00:00
|
|
|
User user = userService.getUserById(id);
|
2025-04-14 18:18:58 +00:00
|
|
|
userService.deleteUser(user); // Llama al servicio para eliminar el usuario
|
|
|
|
|
return "redirect:/user_list"; // Redirige a la página de gestión de usuarios
|
2025-04-07 18:32:56 +00:00
|
|
|
}
|
2025-03-28 15:16:00 +00:00
|
|
|
|
2025-04-14 17:30:42 +00:00
|
|
|
|
2025-04-14 18:18:58 +00:00
|
|
|
@GetMapping("/user_list")
|
2025-04-14 17:30:42 +00:00
|
|
|
public String genericPage(Model model) {
|
2025-04-14 18:18:58 +00:00
|
|
|
model.addAttribute("title", "Gestión de usuarios");
|
2025-04-15 15:32:13 +00:00
|
|
|
model.addAttribute("headers", List.of("ID", "Nombre", "Rol", "Email","Departamento", "Acciones"));
|
2025-04-14 17:30:42 +00:00
|
|
|
model.addAttribute("items", userService.getAllUsers().stream()
|
|
|
|
|
.map(user -> Map.of(
|
|
|
|
|
"id", user.getId(),
|
2025-04-15 15:32:13 +00:00
|
|
|
"values", List.of(user.getId(), user.getNombre(), user.getRole(), user.getMail(), user.getDepartamento() != null ? user.getDepartamento().getNombre() : " ")
|
2025-04-14 17:30:42 +00:00
|
|
|
))
|
|
|
|
|
.toList());
|
|
|
|
|
model.addAttribute("actionsUrl", Map.of(
|
2025-04-14 18:18:58 +00:00
|
|
|
"edit", "",
|
2025-04-14 17:30:42 +00:00
|
|
|
"delete", "/delete_user"
|
|
|
|
|
));
|
|
|
|
|
model.addAttribute("backUrl", "/Inventario/home");
|
2025-04-15 15:32:13 +00:00
|
|
|
model.addAttribute("addUrl", "/Inventario/userPage");
|
2025-04-14 18:18:58 +00:00
|
|
|
return "list";
|
2025-04-14 17:30:42 +00:00
|
|
|
}
|
|
|
|
|
|
2025-04-15 15:32:13 +00:00
|
|
|
|
2025-04-14 18:18:58 +00:00
|
|
|
@GetMapping("/departamento_list")
|
2025-04-14 17:30:42 +00:00
|
|
|
public String genericPageDepartamento(Model model) {
|
2025-04-15 15:32:13 +00:00
|
|
|
model.addAttribute("title", "Gestión de Departamentos");
|
2025-04-14 17:30:42 +00:00
|
|
|
model.addAttribute("headers", List.of("ID", "Nombre","Acciones"));
|
2025-04-15 15:32:13 +00:00
|
|
|
|
|
|
|
|
model.addAttribute(
|
|
|
|
|
"items",
|
|
|
|
|
departamentoService.getAllDepartamentos().stream().map(departamento -> Map.of("id", departamento.getId(),
|
|
|
|
|
"values", List.of(departamento.getId(), departamento.getNombre()))).toList()
|
|
|
|
|
);
|
|
|
|
|
|
2025-04-14 17:30:42 +00:00
|
|
|
model.addAttribute("actionsUrl", Map.of(
|
2025-04-14 18:18:58 +00:00
|
|
|
"edit", "",
|
|
|
|
|
"delete", ""
|
2025-04-14 17:30:42 +00:00
|
|
|
));
|
|
|
|
|
model.addAttribute("backUrl", "/Inventario/home");
|
2025-04-15 15:32:13 +00:00
|
|
|
model.addAttribute("addUrl", "/Inventario/");
|
2025-04-14 18:18:58 +00:00
|
|
|
return "list";
|
2025-04-14 17:30:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-03-24 18:55:09 +00:00
|
|
|
}
|