| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using FAU.Logica.DTOs; |
| | | 4 | | using FAU.Logica.Services; |
| | | 5 | | |
| | | 6 | | namespace FAU.API.Controllers; |
| | | 7 | | |
| | | 8 | | [ApiController] |
| | | 9 | | [Route("api/[controller]")] |
| | | 10 | | public class TestController : ControllerBase |
| | | 11 | | { |
| | | 12 | | private readonly ILogger<TestController> _logger; |
| | | 13 | | |
| | | 14 | | private readonly IAuditoriaService _auditoriaService; |
| | | 15 | | |
| | 0 | 16 | | public TestController(ILogger<TestController> logger, IAuditoriaService auditoriaService) |
| | | 17 | | { |
| | 0 | 18 | | _logger = logger; |
| | 0 | 19 | | _auditoriaService = auditoriaService; |
| | 0 | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Endpoint público de prueba (sin autenticación) |
| | | 24 | | /// </summary> |
| | | 25 | | [HttpGet("public")] |
| | | 26 | | [AllowAnonymous] |
| | | 27 | | public IActionResult Public() |
| | | 28 | | { |
| | 0 | 29 | | return Ok(ApiResponse<string>.SuccessResult( |
| | 0 | 30 | | "Este es un endpoint público - accesible sin autenticación", |
| | 0 | 31 | | "Respuesta exitosa")); |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Endpoint protegido de prueba (requiere autenticación) |
| | | 36 | | /// </summary> |
| | | 37 | | [HttpGet("protected")] |
| | | 38 | | [Authorize] |
| | | 39 | | public IActionResult Protected() |
| | | 40 | | { |
| | 0 | 41 | | var username = User.Identity?.Name; |
| | 0 | 42 | | var roles = User.Claims |
| | 0 | 43 | | .Where(c => c.Type == System.Security.Claims.ClaimTypes.Role) |
| | 0 | 44 | | .Select(c => c.Value) |
| | 0 | 45 | | .ToList(); |
| | | 46 | | |
| | 0 | 47 | | var responseData = new |
| | 0 | 48 | | { |
| | 0 | 49 | | Message = "Este es un endpoint protegido - requiere autenticación", |
| | 0 | 50 | | Username = username, |
| | 0 | 51 | | Roles = roles, |
| | 0 | 52 | | UserId = User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value |
| | 0 | 53 | | }; |
| | | 54 | | |
| | 0 | 55 | | return Ok(ApiResponse<object>.SuccessResult(responseData, "Autenticado correctamente")); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Endpoint solo para administradores |
| | | 60 | | /// </summary> |
| | | 61 | | [HttpGet("admin-only")] |
| | | 62 | | [Authorize(Roles = "Administrador")] |
| | | 63 | | public IActionResult AdminOnly() |
| | | 64 | | { |
| | 0 | 65 | | var username = User.Identity?.Name; |
| | | 66 | | |
| | 0 | 67 | | var responseData = new |
| | 0 | 68 | | { |
| | 0 | 69 | | Message = "Este endpoint solo es accesible por administradores", |
| | 0 | 70 | | Username = username, |
| | 0 | 71 | | AccessLevel = "Administrador" |
| | 0 | 72 | | }; |
| | | 73 | | |
| | 0 | 74 | | return Ok(ApiResponse<object>.SuccessResult(responseData, "Acceso de administrador confirmado")); |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Endpoint con múltiples roles permitidos |
| | | 79 | | /// </summary> |
| | | 80 | | [HttpGet("multi-role")] |
| | | 81 | | [Authorize(Roles = "Administrador,Usuario")] |
| | | 82 | | public IActionResult MultiRole() |
| | | 83 | | { |
| | 0 | 84 | | var username = User.Identity?.Name; |
| | 0 | 85 | | var roles = User.Claims |
| | 0 | 86 | | .Where(c => c.Type == System.Security.Claims.ClaimTypes.Role) |
| | 0 | 87 | | .Select(c => c.Value) |
| | 0 | 88 | | .ToList(); |
| | | 89 | | |
| | 0 | 90 | | var responseData = new |
| | 0 | 91 | | { |
| | 0 | 92 | | Message = "Este endpoint es accesible por Administradores y Usuarios", |
| | 0 | 93 | | Username = username, |
| | 0 | 94 | | Roles = roles |
| | 0 | 95 | | }; |
| | | 96 | | |
| | 0 | 97 | | return Ok(ApiResponse<object>.SuccessResult(responseData, "Acceso autorizado")); |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Endpoint Auditoria de prueba (sin autenticación) |
| | | 102 | | /// </summary> |
| | | 103 | | [HttpGet("Auditoria")] |
| | | 104 | | [AllowAnonymous] |
| | | 105 | | public IActionResult Auditoria() |
| | | 106 | | { |
| | 0 | 107 | | _auditoriaService.LogAuditoria(1, 1, 1, "some host like 1.2.3.4"); |
| | 0 | 108 | | return Ok(ApiResponse<string>.SuccessResult( |
| | 0 | 109 | | "Este es un endpoint de auditoria - accesible sin autenticación", |
| | 0 | 110 | | "Respuesta exitosa")); |
| | | 111 | | } |
| | | 112 | | } |
| | | 113 | | |