diff --git a/src/main/java/com/example/proyectofinal/controllers/AdminController.java b/src/main/java/com/example/proyectofinal/controllers/AdminController.java index 3116d70..7fc7d17 100644 --- a/src/main/java/com/example/proyectofinal/controllers/AdminController.java +++ b/src/main/java/com/example/proyectofinal/controllers/AdminController.java @@ -11,8 +11,10 @@ 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.GrantedAuthority; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -28,18 +30,11 @@ public class AdminController { @Autowired private UsuarioService usuarioService; - @Autowired private PasswordEncoder passwordEncoder; - @Autowired private AlumnoService alumnoService; - @Autowired - private CicloService cicloService; - @Autowired - private SkillService skillService; - @GetMapping("/admin/alta_admin_nuevo") public String showCreateFormAdmin(Model model, Authentication authentication) { Collection authorities = authentication.getAuthorities(); @@ -114,9 +109,13 @@ public class AdminController { } else { // If the password field is not empty, encrypt the new password String password = usuario.getPassword(); - existingUser.setPassword(passwordEncoder.encode(password)); + usuario.setPassword(passwordEncoder.encode(password)); } 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"; } diff --git a/src/main/java/com/example/proyectofinal/controllers/UserController.java b/src/main/java/com/example/proyectofinal/controllers/UserController.java index e853a4b..f334a62 100644 --- a/src/main/java/com/example/proyectofinal/controllers/UserController.java +++ b/src/main/java/com/example/proyectofinal/controllers/UserController.java @@ -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"; } diff --git a/src/main/java/com/example/proyectofinal/controllers/modelControllers/AlumnoController.java b/src/main/java/com/example/proyectofinal/controllers/modelControllers/AlumnoController.java index 594e8f0..8ad1a3a 100644 --- a/src/main/java/com/example/proyectofinal/controllers/modelControllers/AlumnoController.java +++ b/src/main/java/com/example/proyectofinal/controllers/modelControllers/AlumnoController.java @@ -126,10 +126,8 @@ public class AlumnoController { alumno.setSkills(skillEntities); assert keywords != null; alumno.setKeywords(keywords); - // Fetch the existing Alumno from the database Alumno existingAlumno = alumnoService.findById(alumno.getId()); - if (!existingAlumno.equals(alumno)) { Usuario usuario = usuarioService.findByEmail(existingAlumno.getCorreo()); if (usuario != null) { diff --git a/src/main/java/com/example/proyectofinal/temp b/src/main/java/com/example/proyectofinal/temp index a23f55f..e69de29 100644 --- a/src/main/java/com/example/proyectofinal/temp +++ b/src/main/java/com/example/proyectofinal/temp @@ -1 +0,0 @@ -currently for the login I'm using the default and not recomened way of creating user, and log in witht the default ligin provided by spring. Can I rework my login to a more complex level with the clases Autoridad, wich has the authorities \ No newline at end of file diff --git a/src/main/resources/templates/admin/usuario/update.html b/src/main/resources/templates/admin/usuario/update.html index fc954c7..a261adf 100644 --- a/src/main/resources/templates/admin/usuario/update.html +++ b/src/main/resources/templates/admin/usuario/update.html @@ -130,7 +130,6 @@ $('#nombreUsuario').val(user.nombreUsuario); $('#nombreLogIn').val(user.nombreLogIn); $('#email').val(user.email); - $('#password').val(user.password); $('#rol').val(user.rol.id); }, error: function(jqXHR, textStatus, errorThrown) { @@ -139,14 +138,25 @@ }); }); $(document).ready(function() { - $('#password, #confirmPassword').on('input', function() { - var password = $('#password').val(); - var confirmPassword = $('#confirmPassword').val(); + var $password = $('#password'); + var $confirmPassword = $('#confirmPassword'); - if (password && confirmPassword && password !== confirmPassword) { - $('#confirmPassword').prop('disabled', false); + $password.on('input', function() { + var password = $password.val(); + + if (password) { + $confirmPassword.prop('disabled', false); } else { - $('#confirmPassword').prop('disabled', true); + $confirmPassword.prop('disabled', true); + } + }); + $('form').on('submit', function(e) { + var password = $password.val(); + var confirmPassword = $confirmPassword.val(); + + if (password !== confirmPassword) { + alert("Las contraseñas no coinciden!"); + e.preventDefault(); // Prevent form from submitting } }); }); diff --git a/src/main/resources/templates/buscador_admin.html b/src/main/resources/templates/buscador_admin.html index 9a2ffc2..0c54167 100644 --- a/src/main/resources/templates/buscador_admin.html +++ b/src/main/resources/templates/buscador_admin.html @@ -199,6 +199,7 @@ if (userUpdated === 'true') { alert('Usuario actualizado correctamente'); + userUpdated === 'false'; } }); @@ -231,6 +232,7 @@ if (userDeleted === 'true') { alert('Usuario Borrado'); + userDeleted === 'false'; } }); // Get reference to the date picker diff --git a/src/main/resources/templates/buscador_alumno.html b/src/main/resources/templates/buscador_alumno.html index ac94e47..03b2b2d 100644 --- a/src/main/resources/templates/buscador_alumno.html +++ b/src/main/resources/templates/buscador_alumno.html @@ -207,6 +207,7 @@ if (userUpdated === 'true') { alert('Usuario actualizado correctamente'); + userUpdated === 'false'; } }); // Get reference to the date picker diff --git a/src/main/resources/templates/list/empresas.html b/src/main/resources/templates/list/empresas.html index 14870e0..c4f974c 100644 --- a/src/main/resources/templates/list/empresas.html +++ b/src/main/resources/templates/list/empresas.html @@ -63,7 +63,7 @@ - [[${empresa.nombre}]] + [[${empresa.nombre}]] [[${empresa.cif}]] [[${empresa.correo}]] diff --git a/src/main/resources/templates/user/update_usuario.html b/src/main/resources/templates/user/update_usuario.html index 90f1711..5410a87 100644 --- a/src/main/resources/templates/user/update_usuario.html +++ b/src/main/resources/templates/user/update_usuario.html @@ -85,7 +85,7 @@
- +
@@ -101,14 +101,25 @@ window.location.href = "/buscador"; } $(document).ready(function() { - $('#password, #confirmPassword').on('input', function() { - var password = $('#password').val(); - var confirmPassword = $('#confirmPassword').val(); + var $password = $('#password'); + var $confirmPassword = $('#confirmPassword'); - if (password && confirmPassword && password !== confirmPassword) { - $('#confirmPassword').prop('disabled', false); + $password.on('input', function() { + var password = $password.val(); + + if (password) { + $confirmPassword.prop('disabled', false); } else { - $('#confirmPassword').prop('disabled', true); + $confirmPassword.prop('disabled', true); + } + }); + $('form').on('submit', function(e) { + var password = $password.val(); + var confirmPassword = $confirmPassword.val(); + + if (password !== confirmPassword) { + alert("Las contraseñas no coinciden!"); + e.preventDefault(); // Prevent form from submitting } }); });