183 lines
6.6 KiB
Java
183 lines
6.6 KiB
Java
package com.ieslamar.GestionInventario.controllers;
|
|
|
|
|
|
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;
|
|
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;
|
|
|
|
@Controller
|
|
public class UserController {
|
|
|
|
|
|
private final UserService userService;
|
|
private final DepartamentoService departamentoService;
|
|
|
|
public UserController(UserService userService, DepartamentoService departamentoService) {
|
|
this.userService = userService;
|
|
this.departamentoService = departamentoService;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
|
|
@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";
|
|
}
|
|
|
|
|
|
@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);
|
|
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";
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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";
|
|
}
|
|
|
|
|
|
}
|