diff --git a/src/main/java/com/ieslamar/GestionInventario/controllers/UserController.java b/src/main/java/com/ieslamar/GestionInventario/controllers/UserController.java index 0d3f5f1..1f31f90 100644 --- a/src/main/java/com/ieslamar/GestionInventario/controllers/UserController.java +++ b/src/main/java/com/ieslamar/GestionInventario/controllers/UserController.java @@ -7,6 +7,9 @@ import com.ieslamar.GestionInventario.services.UserService; import org.springframework.beans.factory.annotation.Autowired; 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; @@ -27,15 +30,23 @@ public class UserController { return "login"; // Retorna la vista login.html } - @GetMapping("/home") - public String homePage(Model model) { - List botones = List.of( - new BotonDTO("Inventario", "/management", false, null), - new BotonDTO("Gestión", "/management", true, null) - ); - model.addAttribute("buttons", botones); - return "home"; // Retorna la vista home.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) + ); + + // 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(); + + model.addAttribute("buttons", filteredButtons); + return "home"; // Retorna la vista home.html +} @GetMapping("/register") public String registerPage() { diff --git a/src/main/resources/templates/fragments/buttons.html b/src/main/resources/templates/fragments/buttons.html index ed02e5d..24c501b 100644 --- a/src/main/resources/templates/fragments/buttons.html +++ b/src/main/resources/templates/fragments/buttons.html @@ -3,25 +3,17 @@
-
-
-
- - - -
-
-
- - - -
-
-
+
+
+
+ diff --git a/src/main/resources/templates/management_list.html b/src/main/resources/templates/management_list.html deleted file mode 100644 index bba7277..0000000 --- a/src/main/resources/templates/management_list.html +++ /dev/null @@ -1,25 +0,0 @@ - - - -
-

-
- - - - - - - - - - - - -
- -
-
-
- - \ No newline at end of file