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