@ -11,8 +11,10 @@ import com.example.proyectofinal.servicios.user.UsuarioService;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.http.HttpStatus ;
import org.springframework.http.HttpStatus ;
import org.springframework.http.ResponseEntity ;
import org.springframework.http.ResponseEntity ;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken ;
import org.springframework.security.core.Authentication ;
import org.springframework.security.core.Authentication ;
import org.springframework.security.core.GrantedAuthority ;
import org.springframework.security.core.GrantedAuthority ;
import org.springframework.security.core.context.SecurityContextHolder ;
import org.springframework.security.crypto.password.PasswordEncoder ;
import org.springframework.security.crypto.password.PasswordEncoder ;
import org.springframework.stereotype.Controller ;
import org.springframework.stereotype.Controller ;
import org.springframework.ui.Model ;
import org.springframework.ui.Model ;
@ -28,18 +30,11 @@ public class AdminController {
@Autowired
@Autowired
private UsuarioService usuarioService ;
private UsuarioService usuarioService ;
@Autowired
@Autowired
private PasswordEncoder passwordEncoder ;
private PasswordEncoder passwordEncoder ;
@Autowired
@Autowired
private AlumnoService alumnoService ;
private AlumnoService alumnoService ;
@Autowired
private CicloService cicloService ;
@Autowired
private SkillService skillService ;
@GetMapping ( "/admin/alta_admin_nuevo" )
@GetMapping ( "/admin/alta_admin_nuevo" )
public String showCreateFormAdmin ( Model model , Authentication authentication ) {
public String showCreateFormAdmin ( Model model , Authentication authentication ) {
Collection < ? extends GrantedAuthority > authorities = authentication . getAuthorities ( ) ;
Collection < ? extends GrantedAuthority > authorities = authentication . getAuthorities ( ) ;
@ -114,9 +109,13 @@ public class AdminController {
} else {
} else {
// If the password field is not empty, encrypt the new password
// If the password field is not empty, encrypt the new password
String password = usuario . getPassword ( ) ;
String password = usuario . getPassword ( ) ;
existingUser . setPassword ( passwordEncoder . encode ( password ) ) ;
usuario . setPassword ( passwordEncoder . encode ( password ) ) ;
}
}
usuarioService . saveUser ( usuario ) ;
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" ;
return "redirect:/buscador?userUpdated=true" ;
}
}