Gestion-inventario/src/main/java/com/ieslamar/GestionInventario/controllers/UserController.java

132 lines
4.8 KiB
Java
Raw Normal View History

2025-03-24 18:55:09 +00:00
package com.ieslamar.GestionInventario.controllers;
import com.ieslamar.GestionInventario.dto.BotonDTO;
import com.ieslamar.GestionInventario.entities.User;
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;
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;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.List;
2025-03-24 18:55:09 +00:00
@Controller
public class UserController {
2025-03-24 18:55:09 +00:00
private final UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
private final BotonDTO gestionUsuarios = new BotonDTO("Gestión de usuarios", "/user_management", 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 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
}
@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
@GetMapping("/register")
public String registerPage() {
return "register"; // Retorna la vista register.html
}
@PostMapping("/register")
public String register(@RequestParam String username, @RequestParam String password, @RequestParam String role, @RequestParam String mail) {
userService.registerUser(username, password, role, mail, null);
2025-03-24 18:55:09 +00:00
return "redirect:/login?success"; // 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) {
User user = userService.getUserById(id);
if (user != null) {
userService.deleteUser(user);
}
return "redirect:/user_management";
}
2025-03-24 18:55:09 +00:00
}