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.

master
vicsash 4 months ago
parent 8609b1df15
commit b982cda152

@ -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);
// 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();
String updatedLoginName = usuario.getNombreLogIn();
Authentication newAuth = new UsernamePasswordAuthenticationToken(updatedLoginName, auth.getCredentials(), auth.getAuthorities());
// 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";
}

@ -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");

Loading…
Cancel
Save

Powered by INFORMATICA.FP.EDU.ES.