From b982cda152121bb8462acfa2cd8734702406f31c Mon Sep 17 00:00:00 2001 From: vicsash Date: Tue, 4 Jun 2024 20:01:23 +0200 Subject: [PATCH] Ajustes al update del usuario, ahora toma en cuanta si el usuario esta renovando datos de si mismo y si es un alumno o no. --- .../controllers/AdminController.java | 36 ++++++++++++++++--- .../controllers/UserController.java | 2 +- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/example/proyectofinal/controllers/AdminController.java b/src/main/java/com/example/proyectofinal/controllers/AdminController.java index d6665f0..a55bd1e 100644 --- a/src/main/java/com/example/proyectofinal/controllers/AdminController.java +++ b/src/main/java/com/example/proyectofinal/controllers/AdminController.java @@ -15,6 +15,7 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio import org.springframework.security.core.Authentication; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -112,11 +113,38 @@ public class AdminController { String password = usuario.getPassword(); usuario.setPassword(passwordEncoder.encode(password)); } + if(usuario.getRol().getId() == 2 && alumnoService.findByEmail(usuario.getEmail()) != null){ + // Get the currently authenticated user's login name + String currentUserName = authentication.getName(); + // Retrieve the Usuario object associated with the authenticated login name + Usuario currentUser = usuarioService.findByLogInName(currentUserName); + // Get the email of the currently authenticated user + String currentUserEmail = currentUser.getEmail(); + // Retrieve the Alumno object associated with the email + Alumno alumno = alumnoService.findByEmail(currentUserEmail); + if(!usuario.getEmail().equalsIgnoreCase(currentUserEmail)) { + alumno.setCorreo(usuario.getEmail()); + alumnoService.save(alumno); + } + } +// Get the currently authenticated user's login name + String currentUserName = authentication.getName(); + // Retrieve the Usuario object associated with the authenticated login name + Usuario currentUser = usuarioService.findByLogInName(currentUserName); + usuarioService.saveUser(usuario); - Authentication auth = SecurityContextHolder.getContext().getAuthentication(); - String updatedLoginName = usuario.getNombreLogIn(); - Authentication newAuth = new UsernamePasswordAuthenticationToken(updatedLoginName, auth.getCredentials(), auth.getAuthorities()); - SecurityContextHolder.getContext().setAuthentication(newAuth); + + // Get the ID of the currently authenticated user + Long currentUserId = currentUser.getId(); + // Get the ID of the user being updated + Long updatedUserId = usuario.getId(); + // Check if the currently authenticated user is the user being updated + if (currentUserId.equals(updatedUserId)) { + Authentication auth = SecurityContextHolder.getContext().getAuthentication(); + // Update the authentication in the security context + Authentication newAuth = new UsernamePasswordAuthenticationToken(usuario.getNombreLogIn(), auth.getCredentials(), auth.getAuthorities()); + SecurityContextHolder.getContext().setAuthentication(newAuth); + } return "redirect:/buscador?userUpdated=true"; } diff --git a/src/main/java/com/example/proyectofinal/controllers/UserController.java b/src/main/java/com/example/proyectofinal/controllers/UserController.java index 66584e8..dbe88f5 100644 --- a/src/main/java/com/example/proyectofinal/controllers/UserController.java +++ b/src/main/java/com/example/proyectofinal/controllers/UserController.java @@ -89,7 +89,7 @@ public class UserController { } @PostMapping("/usuario/update_usuario_normal") - public String updateUserNormal(@ModelAttribute Usuario usuario, Authentication authentication) { + public String updateUserNormal(@ModelAttribute Usuario usuario) { Usuario existingUser = usuarioService.findUserById(usuario.getId()); if (existingUser == null) { throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Este usuario no existe");