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-02 18:32:57 +00:00
|
|
|
import com.ieslamar.GestionInventario.entities.User;
|
2025-03-26 14:54:51 +00:00
|
|
|
import com.ieslamar.GestionInventario.services.UserService;
|
2025-04-02 18:32:57 +00:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2025-03-24 18:55:09 +00:00
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
|
import org.springframework.ui.Model;
|
2025-04-11 16:23:26 +00:00
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
|
|
|
|
2025-03-24 18:55:09 +00:00
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
2025-04-02 18:32:57 +00:00
|
|
|
import java.util.List;
|
2025-03-24 18:55:09 +00:00
|
|
|
|
|
|
|
|
@Controller
|
|
|
|
|
public class UserController {
|
|
|
|
|
|
2025-04-02 18:32:57 +00:00
|
|
|
|
2025-03-24 18:55:09 +00:00
|
|
|
private final UserService userService;
|
|
|
|
|
|
|
|
|
|
public UserController(UserService userService) {
|
|
|
|
|
this.userService = userService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping("/login")
|
|
|
|
|
public String loginPage() {
|
|
|
|
|
return "login"; // Retorna la vista login.html
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-11 16:23:26 +00:00
|
|
|
@GetMapping("/filtered-buttons")
|
|
|
|
|
public String getFilteredButtons(Model model, Authentication authentication) {
|
|
|
|
|
List<BotonDTO> 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<BotonDTO> 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
|
|
|
|
|
}
|
2025-03-24 18:55:09 +00:00
|
|
|
|
|
|
|
|
@GetMapping("/register")
|
|
|
|
|
public String registerPage() {
|
|
|
|
|
return "register"; // Retorna la vista register.html
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping("/register")
|
2025-03-27 18:51:14 +00:00
|
|
|
public String register(@RequestParam String username, @RequestParam String password, @RequestParam String role, @RequestParam String mail) {
|
2025-04-08 18:24:38 +00:00
|
|
|
userService.registerUser(username, password, role, mail, null);
|
2025-03-24 18:55:09 +00:00
|
|
|
return "redirect:/login?success"; // Redirige al login tras registrarse
|
|
|
|
|
}
|
2025-03-28 15:16:00 +00:00
|
|
|
@GetMapping("/management")
|
|
|
|
|
public String managementPage(){
|
|
|
|
|
return "management";
|
|
|
|
|
}
|
2025-04-07 18:32:56 +00:00
|
|
|
|
2025-04-02 18:32:57 +00:00
|
|
|
|
|
|
|
|
@GetMapping("/user_management")
|
|
|
|
|
public String listUsers(Model model) {
|
|
|
|
|
List<User> users = userService.getAllUsers();
|
|
|
|
|
model.addAttribute("users", users);
|
2025-03-28 15:16:00 +00:00
|
|
|
return "user_management";
|
|
|
|
|
}
|
2025-04-07 18:32:56 +00:00
|
|
|
@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-28 15:16:00 +00:00
|
|
|
|
2025-03-24 18:55:09 +00:00
|
|
|
}
|