Compare commits
9 Commits
8890b72f19
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| f8d022d007 | |||
| 5f8ed1e7b8 | |||
| 38e573e5fc | |||
| f3e99fa830 | |||
| 6927c6b7e6 | |||
| 090d6c9a64 | |||
| 95c2e57ab1 | |||
| aa9b7d2056 | |||
| 5f1c8f93fd |
@@ -42,6 +42,7 @@ public class SecurityConfig {
|
|||||||
.authorizeHttpRequests(auth -> auth
|
.authorizeHttpRequests(auth -> auth
|
||||||
.requestMatchers("/ws/**").permitAll() // WebSocket
|
.requestMatchers("/ws/**").permitAll() // WebSocket
|
||||||
.requestMatchers("/auth/**").permitAll()
|
.requestMatchers("/auth/**").permitAll()
|
||||||
|
.requestMatchers("/api/usuarios/confirmado/**").permitAll()
|
||||||
.requestMatchers("/favicon.ico", "/error", "/static/**", "/contador/**", "/api/**").permitAll()
|
.requestMatchers("/favicon.ico", "/error", "/static/**", "/contador/**", "/api/**").permitAll()
|
||||||
.requestMatchers("/admin/**").hasRole("ADMIN")
|
.requestMatchers("/admin/**").hasRole("ADMIN")
|
||||||
.anyRequest().authenticated()
|
.anyRequest().authenticated()
|
||||||
|
|||||||
@@ -26,16 +26,16 @@ public class AppController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Registro
|
// Registro
|
||||||
@PostMapping("/auth/registrar")
|
// @PostMapping("/auth/registrar")
|
||||||
public ResponseEntity<String> registrar(@RequestParam String username,
|
// public ResponseEntity<String> registrar(@RequestParam String username,
|
||||||
@RequestParam String password) {
|
// @RequestParam String password) {
|
||||||
if (usuarioService.estaRegistrado(username)) {
|
// if (usuarioService.estaRegistrado(username)) {
|
||||||
return ResponseEntity.status(HttpStatus.CONFLICT).body("Usuario ya existe ❌");
|
// return ResponseEntity.status(HttpStatus.CONFLICT).body("Usuario ya existe ❌");
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
usuarioService.registrarUsuario(username, password, Set.of(Rol.CLIENTE), 1L);
|
// usuarioService.registrarUsuario(username, password, Set.of(Rol.CLIENTE), 1L);
|
||||||
return ResponseEntity.ok("Usuario registrado, pendiente de aprobación ✅");
|
// return ResponseEntity.ok("Usuario registrado, pendiente de aprobación ✅");
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package com.example.fercoganbackend.controller;
|
||||||
|
|
||||||
|
import com.example.fercoganbackend.entity.Roles;
|
||||||
|
import com.example.fercoganbackend.repository.RolesRepository;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/roles")
|
||||||
|
public class RolesController {
|
||||||
|
|
||||||
|
final RolesRepository rolesRepository;
|
||||||
|
|
||||||
|
RolesController(RolesRepository rolesRepository){
|
||||||
|
this.rolesRepository=rolesRepository;
|
||||||
|
}
|
||||||
|
@GetMapping
|
||||||
|
public List<Roles> getRoles(){
|
||||||
|
return rolesRepository.findAll();
|
||||||
|
}
|
||||||
|
@PostMapping
|
||||||
|
public Roles setRoles(@RequestBody Roles roles){
|
||||||
|
return rolesRepository.save(roles);
|
||||||
|
}
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public Roles updateRoles(@PathVariable Long id, @RequestBody Roles updatedRole) {
|
||||||
|
|
||||||
|
Roles role = rolesRepository.findById(id)
|
||||||
|
.orElseThrow(() -> new RuntimeException("Rol no encontrado"));
|
||||||
|
|
||||||
|
role.setName(updatedRole.getName());
|
||||||
|
|
||||||
|
return rolesRepository.save(role);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -12,7 +12,9 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@@ -32,7 +34,14 @@ public class UserController {
|
|||||||
// ✅ Registrar nuevo usuario
|
// ✅ Registrar nuevo usuario
|
||||||
@PostMapping("/registrar")
|
@PostMapping("/registrar")
|
||||||
public Usuario registrarUsuario(@RequestBody UsuarioRequest request) {
|
public Usuario registrarUsuario(@RequestBody UsuarioRequest request) {
|
||||||
return service.registrarUsuario(request.getUsername(), request.getPassword(), request.getRoles(), request.getRol() );
|
return service.registrarUsuario(
|
||||||
|
request.getUsername(),
|
||||||
|
request.getPassword(),
|
||||||
|
request.getRoles(),
|
||||||
|
request.getRol(),
|
||||||
|
request.getCi(),
|
||||||
|
request.getCelular(),
|
||||||
|
request.getNombre());
|
||||||
}
|
}
|
||||||
|
|
||||||
// ✅ Listar usuarios pendientes (no aprobados)
|
// ✅ Listar usuarios pendientes (no aprobados)
|
||||||
@@ -66,10 +75,10 @@ public class UserController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ✅ Actualizar usuario (por id)
|
// ✅ Actualizar usuario (por id)
|
||||||
@PutMapping("/{id}")
|
//@PutMapping("/{id}")
|
||||||
public Usuario actualizarUsuario(@PathVariable Long id, @RequestBody Usuario usuario) {
|
//public Usuario actualizarUsuario(@PathVariable Long id, @RequestBody Usuario usuario) {
|
||||||
return service.actualizarUsuario(id, usuario);
|
// return service.actualizarUsuario(id, usuario);
|
||||||
}
|
//}
|
||||||
|
|
||||||
|
|
||||||
// ✅ Eliminar usuario
|
// ✅ Eliminar usuario
|
||||||
@@ -85,6 +94,23 @@ public class UserController {
|
|||||||
return ResponseEntity.ok(id);
|
return ResponseEntity.ok(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/rol/{username}")
|
||||||
|
public ResponseEntity<Map<String, Long>> obtenerRolUsuario(@PathVariable String username) {
|
||||||
|
Long rolId = service.obtenerIdRolPorUsername(username);
|
||||||
|
|
||||||
|
Map<String, Long> response = new HashMap<>();
|
||||||
|
response.put("rolId", rolId);
|
||||||
|
|
||||||
|
return ResponseEntity.ok(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public Usuario actualizarUsuario(@PathVariable Long id, @RequestBody UsuarioRequest request) {
|
||||||
|
return service.actualizarParcial(id, request);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ✅ DTO interno para registro
|
// ✅ DTO interno para registro
|
||||||
@@ -93,6 +119,59 @@ public class UserController {
|
|||||||
private String password;
|
private String password;
|
||||||
private Set<Rol> roles; //ignora esto
|
private Set<Rol> roles; //ignora esto
|
||||||
private Long rolId;
|
private Long rolId;
|
||||||
|
private Long ci;
|
||||||
|
private Long celular;
|
||||||
|
private String nombre;
|
||||||
|
private boolean aprobado;
|
||||||
|
private boolean visible;
|
||||||
|
|
||||||
|
public boolean isAprobado() {
|
||||||
|
return aprobado;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAprobado(boolean aprobado) {
|
||||||
|
this.aprobado = aprobado;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isVisible() {
|
||||||
|
return visible;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setVisible(boolean visible) {
|
||||||
|
this.visible = visible;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getRolId() {
|
||||||
|
return rolId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRolId(Long rolId) {
|
||||||
|
this.rolId = rolId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getCi() {
|
||||||
|
return ci;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCi(Long ci) {
|
||||||
|
this.ci = ci;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getCelular() {
|
||||||
|
return celular;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCelular(Long celular) {
|
||||||
|
this.celular = celular;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNombre() {
|
||||||
|
return nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNombre(String nombre) {
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
|
||||||
public Long getRol() {
|
public Long getRol() {
|
||||||
return rolId;
|
return rolId;
|
||||||
|
|||||||
@@ -23,11 +23,42 @@ public class Usuario {
|
|||||||
@JoinColumn(name = "RolId")
|
@JoinColumn(name = "RolId")
|
||||||
private Roles rol;
|
private Roles rol;
|
||||||
|
|
||||||
|
private Long ci;
|
||||||
|
|
||||||
|
private Long celular;
|
||||||
|
|
||||||
|
private String nombre;
|
||||||
|
|
||||||
private Boolean visible = true;
|
private Boolean visible = true;
|
||||||
|
|
||||||
// getters y setters
|
// getters y setters
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public Long getCi() {
|
||||||
|
return ci;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCi(Long ci) {
|
||||||
|
this.ci = ci;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getCelular() {
|
||||||
|
return celular;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCelular(Long celular) {
|
||||||
|
this.celular = celular;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNombre() {
|
||||||
|
return nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNombre(String nombre) {
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
|
||||||
public Roles getRol() {
|
public Roles getRol() {
|
||||||
return rol;
|
return rol;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
package com.example.fercoganbackend.service;
|
package com.example.fercoganbackend.service;
|
||||||
|
import com.example.fercoganbackend.controller.UserController;
|
||||||
import com.example.fercoganbackend.entity.Rol;
|
import com.example.fercoganbackend.entity.Rol;
|
||||||
import com.example.fercoganbackend.entity.Roles;
|
import com.example.fercoganbackend.entity.Roles;
|
||||||
import com.example.fercoganbackend.entity.Usuario;
|
import com.example.fercoganbackend.entity.Usuario;
|
||||||
@@ -25,7 +26,7 @@ public class UsuarioService {
|
|||||||
this.rolesRepository = rolesRepository;
|
this.rolesRepository = rolesRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Usuario registrarUsuario(String username, String password, Set<Rol> roles, Long rolId) {
|
public Usuario registrarUsuario(String username, String password, Set<Rol> roles, Long rolId, Long ci, Long celular, String nombre) {
|
||||||
Roles rol = rolesRepository.findById(rolId)
|
Roles rol = rolesRepository.findById(rolId)
|
||||||
.orElseThrow(() -> new RuntimeException("Rol no encontrado"));
|
.orElseThrow(() -> new RuntimeException("Rol no encontrado"));
|
||||||
Usuario u = new Usuario();
|
Usuario u = new Usuario();
|
||||||
@@ -34,6 +35,9 @@ public class UsuarioService {
|
|||||||
u.setRoles(roles);
|
u.setRoles(roles);
|
||||||
u.setRol(rol);
|
u.setRol(rol);
|
||||||
u.setAprobado(false); // no aprobado hasta aceptación
|
u.setAprobado(false); // no aprobado hasta aceptación
|
||||||
|
u.setCi(ci);
|
||||||
|
u.setCelular(celular);
|
||||||
|
u.setNombre(nombre);
|
||||||
return repo.save(u);
|
return repo.save(u);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,6 +99,52 @@ public class UsuarioService {
|
|||||||
.orElse(null); // O podrías lanzar una excepción si el usuario no existe
|
.orElse(null); // O podrías lanzar una excepción si el usuario no existe
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Usuario actualizarParcial(Long id, UserController.UsuarioRequest request) {
|
||||||
|
// 1. Buscamos el usuario actual o lanzamos error si no existe
|
||||||
|
Usuario usuarioExistente = repo.findById(id)
|
||||||
|
.orElseThrow(() -> new RuntimeException("Usuario no encontrado"));
|
||||||
|
|
||||||
|
// 2. Actualizamos solo si el campo no es nulo
|
||||||
|
if (request.getUsername() != null) {
|
||||||
|
usuarioExistente.setUsername(request.getUsername());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(request.getCi()!= null){
|
||||||
|
usuarioExistente.setCi(request.getCi());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(request.getCelular()!= null){
|
||||||
|
usuarioExistente.setCelular(request.getCelular());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(request.getNombre()!= null){
|
||||||
|
usuarioExistente.setNombre(request.getNombre());
|
||||||
|
}
|
||||||
|
if(request.isVisible()){
|
||||||
|
usuarioExistente.setVisible(request.isVisible());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(request.isAprobado()){
|
||||||
|
usuarioExistente.setAprobado(request.isAprobado());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.getPassword() != null) {
|
||||||
|
// ¡No olvides encriptarla si cambias el password!
|
||||||
|
usuarioExistente.setPassword(encoder.encode(request.getPassword()));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.getRol() != null) {
|
||||||
|
// Buscamos el objeto Rol por su ID (necesitas el RolesRepository)
|
||||||
|
Roles nuevoRol = rolesRepository.findById(request.getRol())
|
||||||
|
.orElseThrow(() -> new RuntimeException("El Rol especificado no existe"));
|
||||||
|
|
||||||
|
usuarioExistente.setRol(nuevoRol); // Asignamos el objeto completo
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Guardamos los cambios
|
||||||
|
return repo.save(usuarioExistente);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user