@ -11,7 +11,9 @@ import com.example.proyectofinal.servicios.user.UsuarioService;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.http.HttpStatus ;
import org.springframework.http.ResponseEntity ;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken ;
import org.springframework.security.core.Authentication ;
import org.springframework.security.core.context.SecurityContextHolder ;
import org.springframework.security.crypto.password.PasswordEncoder ;
import org.springframework.stereotype.Controller ;
import org.springframework.ui.Model ;
@ -70,6 +72,9 @@ public class UserController {
if ( usuario ! = null ) {
usuario . setEmail ( alumno . getCorreo ( ) ) ;
usuarioService . saveUser ( usuario ) ;
Authentication auth = SecurityContextHolder . getContext ( ) . getAuthentication ( ) ;
Authentication newAuth = new UsernamePasswordAuthenticationToken ( usuario . getNombreLogIn ( ) , auth . getCredentials ( ) , auth . getAuthorities ( ) ) ;
SecurityContextHolder . getContext ( ) . setAuthentication ( newAuth ) ;
}
}
alumnoService . save ( alumno ) ;
@ -97,7 +102,7 @@ public class UserController {
usuario . setPassword ( existingUser . getPassword ( ) ) ;
} else {
String password = usuario . getPassword ( ) ;
existingUser . setPassword ( passwordEncoder . encode ( password ) ) ;
usuario . setPassword ( passwordEncoder . encode ( password ) ) ;
}
usuario . setRol ( usuarioService . getRolById ( 2 ) ) ;
Alumno alumno = alumnoService . findByEmail ( existingUser . getEmail ( ) ) ;
@ -106,6 +111,10 @@ public class UserController {
alumnoService . save ( alumno ) ;
}
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 ) ;
return "redirect:/buscador?userUpdated=true" ;
}