|
|
@ -37,30 +37,25 @@ public class AdminController {
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
private AlumnoService alumnoService;
|
|
|
|
private AlumnoService alumnoService;
|
|
|
|
|
|
|
|
|
|
|
|
@GetMapping("/admin/alta_admin_nuevo")
|
|
|
|
private void checkUserRole(Authentication authentication) {
|
|
|
|
public String showCreateFormAdmin(Model model, Authentication authentication) {
|
|
|
|
|
|
|
|
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
|
|
|
|
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
|
|
|
|
boolean isAdmin = authorities.stream()
|
|
|
|
boolean isAdmin = authorities.stream()
|
|
|
|
.anyMatch(grantedAuthority -> grantedAuthority.getAuthority().equals("ADMIN"));
|
|
|
|
.anyMatch(grantedAuthority -> grantedAuthority.getAuthority().equals("ADMIN"));
|
|
|
|
if (!isAdmin) {
|
|
|
|
if (!isAdmin) {
|
|
|
|
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "No tienes permisos para acceder a esta página");
|
|
|
|
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "No tienes permisos para acceder a esta página");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("/admin/alta_admin_nuevo")
|
|
|
|
|
|
|
|
public String showCreateFormAdmin(Model model, Authentication authentication) {
|
|
|
|
|
|
|
|
checkUserRole(authentication);
|
|
|
|
model.addAttribute("usuario", new Usuario()); // Add this line
|
|
|
|
model.addAttribute("usuario", new Usuario()); // Add this line
|
|
|
|
|
|
|
|
|
|
|
|
return "admin/usuario/create";
|
|
|
|
return "admin/usuario/create";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@PostMapping("/admin/alta_admin_nuevo")
|
|
|
|
@PostMapping("/admin/alta_admin_nuevo")
|
|
|
|
public String saveNewAdmin(@ModelAttribute Usuario usuario, Authentication authentication) {
|
|
|
|
public String saveNewAdmin(@ModelAttribute Usuario usuario, Authentication authentication) {
|
|
|
|
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
|
|
|
|
checkUserRole(authentication);
|
|
|
|
boolean isAdmin = authorities.stream()
|
|
|
|
|
|
|
|
.anyMatch(grantedAuthority -> grantedAuthority.getAuthority().equals("ADMIN"));
|
|
|
|
|
|
|
|
if (!isAdmin) {
|
|
|
|
|
|
|
|
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "No tienes permisos para acceder a esta página");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
usuario.setRol(usuarioService.getRolById(1));
|
|
|
|
usuario.setRol(usuarioService.getRolById(1));
|
|
|
|
|
|
|
|
|
|
|
|
if(usuarioService.getUserByLogInName(usuario.getNombreLogIn()) == null){
|
|
|
|
if(usuarioService.getUserByLogInName(usuario.getNombreLogIn()) == null){
|
|
|
|
usuarioService.createUsuario(usuario.getNombreUsuario(), usuario.getNombreLogIn(), usuario.getEmail(), usuario.getPassword(), usuario.getRol().getId());
|
|
|
|
usuarioService.createUsuario(usuario.getNombreUsuario(), usuario.getNombreLogIn(), usuario.getEmail(), usuario.getPassword(), usuario.getRol().getId());
|
|
|
|
return "redirect:/buscador?userCreated=true";
|
|
|
|
return "redirect:/buscador?userCreated=true";
|
|
|
@ -80,12 +75,7 @@ public class AdminController {
|
|
|
|
|
|
|
|
|
|
|
|
@GetMapping("/admin/editar_usuario")
|
|
|
|
@GetMapping("/admin/editar_usuario")
|
|
|
|
public String showUpdateFormAdmin(Model model, Authentication authentication) {
|
|
|
|
public String showUpdateFormAdmin(Model model, Authentication authentication) {
|
|
|
|
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
|
|
|
|
checkUserRole(authentication);
|
|
|
|
boolean isAdmin = authorities.stream()
|
|
|
|
|
|
|
|
.anyMatch(grantedAuthority -> grantedAuthority.getAuthority().equals("ADMIN"));
|
|
|
|
|
|
|
|
if (!isAdmin) {
|
|
|
|
|
|
|
|
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "No tienes permisos para acceder a esta página");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
List<Usuario> users = usuarioService.findAll();
|
|
|
|
List<Usuario> users = usuarioService.findAll();
|
|
|
|
model.addAttribute("users", users);
|
|
|
|
model.addAttribute("users", users);
|
|
|
|
model.addAttribute("usuario", new Usuario());
|
|
|
|
model.addAttribute("usuario", new Usuario());
|
|
|
@ -94,12 +84,7 @@ public class AdminController {
|
|
|
|
//TODO add confirmation of if the user is a studen and if it is then cahnge the first email in alumno table
|
|
|
|
//TODO add confirmation of if the user is a studen and if it is then cahnge the first email in alumno table
|
|
|
|
@PostMapping("/admin/update_usuario")
|
|
|
|
@PostMapping("/admin/update_usuario")
|
|
|
|
public String updateUser(@ModelAttribute Usuario usuario, Authentication authentication) {
|
|
|
|
public String updateUser(@ModelAttribute Usuario usuario, Authentication authentication) {
|
|
|
|
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
|
|
|
|
checkUserRole(authentication);
|
|
|
|
boolean isAdmin = authorities.stream()
|
|
|
|
|
|
|
|
.anyMatch(grantedAuthority -> grantedAuthority.getAuthority().equals("ADMIN"));
|
|
|
|
|
|
|
|
if (!isAdmin) {
|
|
|
|
|
|
|
|
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "No tienes permisos para acceder a esta página");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Usuario existingUser = usuarioService.findUserById(usuario.getId());
|
|
|
|
Usuario existingUser = usuarioService.findUserById(usuario.getId());
|
|
|
|
if (existingUser == null) {
|
|
|
|
if (existingUser == null) {
|
|
|
|
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Este usuario no existe");
|
|
|
|
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Este usuario no existe");
|
|
|
@ -150,19 +135,15 @@ public class AdminController {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@GetMapping("/admin/user/{id}")
|
|
|
|
@GetMapping("/admin/user/{id}")
|
|
|
|
public ResponseEntity<Usuario> getUser(@PathVariable Long id) {
|
|
|
|
public ResponseEntity<Usuario> getUser(@PathVariable Long id,Authentication authentication) {
|
|
|
|
|
|
|
|
checkUserRole(authentication);
|
|
|
|
Usuario usuario = usuarioService.findUserById(id);
|
|
|
|
Usuario usuario = usuarioService.findUserById(id);
|
|
|
|
return ResponseEntity.ok(usuario);
|
|
|
|
return ResponseEntity.ok(usuario);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@GetMapping("/admin/delete_usuario")
|
|
|
|
@GetMapping("/admin/delete_usuario")
|
|
|
|
public String showDeleteFormAdmin(Model model, Authentication authentication) {
|
|
|
|
public String showDeleteFormAdmin(Model model, Authentication authentication) {
|
|
|
|
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
|
|
|
|
checkUserRole(authentication);
|
|
|
|
boolean isAdmin = authorities.stream()
|
|
|
|
|
|
|
|
.anyMatch(grantedAuthority -> grantedAuthority.getAuthority().equals("ADMIN"));
|
|
|
|
|
|
|
|
if (!isAdmin) {
|
|
|
|
|
|
|
|
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "No tienes permisos para acceder a esta página");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
List<Usuario> users = usuarioService.findAll();
|
|
|
|
List<Usuario> users = usuarioService.findAll();
|
|
|
|
model.addAttribute("users", users);
|
|
|
|
model.addAttribute("users", users);
|
|
|
|
model.addAttribute("usuario", new Usuario());
|
|
|
|
model.addAttribute("usuario", new Usuario());
|
|
|
@ -171,12 +152,7 @@ public class AdminController {
|
|
|
|
|
|
|
|
|
|
|
|
@GetMapping("/admin/delete_usuario/{id}")
|
|
|
|
@GetMapping("/admin/delete_usuario/{id}")
|
|
|
|
public ResponseEntity<String> deleteUser(@PathVariable Long id, Authentication authentication) {
|
|
|
|
public ResponseEntity<String> deleteUser(@PathVariable Long id, Authentication authentication) {
|
|
|
|
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
|
|
|
|
checkUserRole(authentication);
|
|
|
|
boolean isAdmin = authorities.stream()
|
|
|
|
|
|
|
|
.anyMatch(grantedAuthority -> grantedAuthority.getAuthority().equals("ADMIN"));
|
|
|
|
|
|
|
|
if (!isAdmin) {
|
|
|
|
|
|
|
|
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "No tienes permisos para acceder a esta página");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Usuario existingUser = usuarioService.findUserById(id);
|
|
|
|
Usuario existingUser = usuarioService.findUserById(id);
|
|
|
|
if (existingUser == null) {
|
|
|
|
if (existingUser == null) {
|
|
|
|
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Este usuario no existe");
|
|
|
|
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Este usuario no existe");
|
|
|
@ -201,19 +177,11 @@ public class AdminController {
|
|
|
|
|
|
|
|
|
|
|
|
@GetMapping("/admin/firstUser")
|
|
|
|
@GetMapping("/admin/firstUser")
|
|
|
|
public String showAlterUserForm(Model model, Authentication authentication) {
|
|
|
|
public String showAlterUserForm(Model model, Authentication authentication) {
|
|
|
|
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
|
|
|
|
checkUserRole(authentication);
|
|
|
|
boolean isAdmin = authorities.stream()
|
|
|
|
|
|
|
|
.anyMatch(grantedAuthority -> grantedAuthority.getAuthority().equals("ADMIN"));
|
|
|
|
|
|
|
|
if (!isAdmin) {
|
|
|
|
|
|
|
|
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "No tienes permisos para acceder a esta página");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
String username = authentication.getName();
|
|
|
|
String username = authentication.getName();
|
|
|
|
Usuario usuario = usuarioService.findByLogInName(username);
|
|
|
|
Usuario usuario = usuarioService.findByLogInName(username);
|
|
|
|
model.addAttribute("usuario", usuario);
|
|
|
|
model.addAttribute("usuario", usuario);
|
|
|
|
model.addAttribute("newPassword", "");
|
|
|
|
model.addAttribute("newPassword", "");
|
|
|
|
return "admin/usuario/first_login_admin";
|
|
|
|
return "admin/usuario/first_login_admin";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|