diff --git a/src/main/java/com/ieslamar/GestionInventario/controllers/UserController.java b/src/main/java/com/ieslamar/GestionInventario/controllers/UserController.java index 1f31f90..042277e 100644 --- a/src/main/java/com/ieslamar/GestionInventario/controllers/UserController.java +++ b/src/main/java/com/ieslamar/GestionInventario/controllers/UserController.java @@ -5,14 +5,20 @@ 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; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.GrantedAuthority; - 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; @Controller @@ -25,28 +31,75 @@ public class UserController { 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 filtrarBotones(Authentication auth,List botones){ + List botones_response = new ArrayList<>(); + List 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("/filtered-buttons") -public String getFilteredButtons(Model model, Authentication authentication) { - List botones = List.of( - new BotonDTO("Inventario", "/management", false, null), - new BotonDTO("Gestión", "/management", true, null) - ); + @GetMapping("/home") + public String homePage(Model model) { + Authentication auth = SecurityContextHolder.getContext().getAuthentication(); + List botones = filtrarBotones(auth,new ArrayList<>(List.of( + inventario, + gestion + ))); + model.addAttribute("buttons", botones); + return "home"; + } - // Filtrar botones según el rol del usuario - List filteredButtons = botones.stream() - .filter(button -> !button.isRequireAdmin() || - (authentication != null && authentication.getAuthorities().stream() - .anyMatch(auth -> auth.getAuthority().equals("ROLE_ADMIN")))) - .toList(); + @GetMapping("/management") + public String managementPage(Model model){ + Authentication auth = SecurityContextHolder.getContext().getAuthentication(); + Listbotones = filtrarBotones(auth,new ArrayList<>(List.of( + gestionUsuarios, + gestionProductos, + gestionDepartamentos, + gestionCategorias, + gestionUbicaciones, + gestionInventario, + gestionCategorias, + gestionTiposDatos, + home + ))); + model.addAttribute("buttons", botones); + return "management"; + } - model.addAttribute("buttons", filteredButtons); - return "home"; // Retorna la vista home.html -} @GetMapping("/register") public String registerPage() { @@ -58,10 +111,6 @@ public String getFilteredButtons(Model model, Authentication authentication) { userService.registerUser(username, password, role, mail, null); return "redirect:/login?success"; // Redirige al login tras registrarse } - @GetMapping("/management") - public String managementPage(){ - return "management"; - } @GetMapping("/user_management") diff --git a/src/main/resources/static/css/style.css b/src/main/resources/static/css/style.css index 03bd210..629a6a5 100644 --- a/src/main/resources/static/css/style.css +++ b/src/main/resources/static/css/style.css @@ -20,6 +20,7 @@ p { .container1{ + margin: 10px; background-color: rgba(255,255,255,0.4); display: inline-flex; flex-direction: column; diff --git a/src/main/resources/templates/fragments/buttons.html b/src/main/resources/templates/fragments/buttons.html index 24c501b..5b4993a 100644 --- a/src/main/resources/templates/fragments/buttons.html +++ b/src/main/resources/templates/fragments/buttons.html @@ -2,15 +2,13 @@ -
-
+ -
diff --git a/src/main/resources/templates/fragments/list.html b/src/main/resources/templates/fragments/list.html new file mode 100644 index 0000000..9708975 --- /dev/null +++ b/src/main/resources/templates/fragments/list.html @@ -0,0 +1,13 @@ + + + + +
+ +
+ + + + + + diff --git a/src/main/resources/templates/home.html b/src/main/resources/templates/home.html index 502a0f6..3fb72d4 100644 --- a/src/main/resources/templates/home.html +++ b/src/main/resources/templates/home.html @@ -11,7 +11,7 @@
-
+
diff --git a/src/main/resources/templates/management.html b/src/main/resources/templates/management.html index 73f4755..2b98d92 100644 --- a/src/main/resources/templates/management.html +++ b/src/main/resources/templates/management.html @@ -6,37 +6,7 @@

- \ No newline at end of file