F2 avanzada
This commit is contained in:
@@ -67,7 +67,7 @@ namespace vita_asistente.Controllers
|
||||
|
||||
// GET: /Account/Login
|
||||
[HttpGet]
|
||||
public IActionResult Login(string returnUrl = null)
|
||||
public IActionResult Login(string? returnUrl = null)
|
||||
{
|
||||
ViewData["ReturnUrl"] = returnUrl;
|
||||
return View();
|
||||
@@ -76,7 +76,7 @@ namespace vita_asistente.Controllers
|
||||
// POST: /Account/Login
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
|
||||
public async Task<IActionResult> Login(LoginViewModel model, string? returnUrl = null)
|
||||
{
|
||||
ViewData["ReturnUrl"] = returnUrl;
|
||||
|
||||
@@ -90,7 +90,7 @@ namespace vita_asistente.Controllers
|
||||
|
||||
if (result.Succeeded)
|
||||
{
|
||||
return RedirectToLocal(returnUrl);
|
||||
return RedirectToLocal(!string.IsNullOrEmpty(returnUrl) ? returnUrl : string.Empty);
|
||||
}
|
||||
if (result.RequiresTwoFactor)
|
||||
{
|
||||
@@ -143,7 +143,7 @@ namespace vita_asistente.Controllers
|
||||
|
||||
// GET: /Account/LoginWith2fa (opcional)
|
||||
[HttpGet]
|
||||
public IActionResult LoginWith2fa(bool rememberMe, string returnUrl = null)
|
||||
public IActionResult LoginWith2fa(bool rememberMe, string? returnUrl = null)
|
||||
{
|
||||
// Implementar si se usa 2FA
|
||||
return View();
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using vita_asistente.Data;
|
||||
using vita_asistente.Data.Models;
|
||||
|
||||
namespace vita_asistente.Controllers
|
||||
{
|
||||
[Authorize]
|
||||
public class PacientesController : Controller
|
||||
{
|
||||
private readonly ApplicationDbContext _context;
|
||||
|
||||
public PacientesController(ApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
// GET: Pacientes
|
||||
public async Task<IActionResult> Index(string nombreBusqueda)
|
||||
{
|
||||
var pacientes = _context.Pacientes.AsNoTracking();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(nombreBusqueda))
|
||||
{
|
||||
pacientes = pacientes.Where(p =>
|
||||
(p.Nombre + " " + p.Apellido).Contains(nombreBusqueda.Trim()) ||
|
||||
p.Documento.Contains(nombreBusqueda.Trim()));
|
||||
}
|
||||
|
||||
ViewBag.NombreBusqueda = nombreBusqueda;
|
||||
return View(await pacientes.OrderByDescending(p => p.FechaRegistro).ToListAsync());
|
||||
}
|
||||
|
||||
// GET: Pacientes/Details/5
|
||||
public async Task<IActionResult> Details(string id)
|
||||
{
|
||||
if (id == null) return NotFound();
|
||||
var paciente = await _context.Pacientes.FindAsync(id);
|
||||
if (paciente == null) return NotFound();
|
||||
return View(paciente);
|
||||
}
|
||||
|
||||
// POST: Pacientes/Activate/5
|
||||
[HttpPost]
|
||||
[Authorize(Roles = "Admin")]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Activate(string id)
|
||||
{
|
||||
var paciente = await _context.Pacientes.FindAsync(id);
|
||||
if (paciente == null) return NotFound();
|
||||
|
||||
paciente.IsActive = true;
|
||||
_context.Pacientes.Update(paciente);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -117,7 +117,7 @@ namespace vita_asistente.Controllers
|
||||
// Pero primero verificamos si tiene usuarios asignados
|
||||
using var scope = HttpContext.RequestServices.CreateScope();
|
||||
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
|
||||
var usersInRole = await userManager.GetUsersInRoleAsync(role.Name);
|
||||
var usersInRole = await userManager.GetUsersInRoleAsync(role.Name ?? string.Empty);
|
||||
|
||||
if (usersInRole.Any())
|
||||
{
|
||||
@@ -157,7 +157,7 @@ namespace vita_asistente.Controllers
|
||||
// Verificar que no tenga usuarios asignados
|
||||
using var scope = HttpContext.RequestServices.CreateScope();
|
||||
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
|
||||
var usersInRole = await userManager.GetUsersInRoleAsync(role.Name);
|
||||
var usersInRole = await userManager.GetUsersInRoleAsync(role.Name ?? string.Empty);
|
||||
|
||||
if (usersInRole.Any())
|
||||
{
|
||||
|
||||
@@ -167,8 +167,8 @@ namespace vita_asistente.Controllers
|
||||
RolesDisponibles = todosLosRoles.Select(r => new RoleCheckbox
|
||||
{
|
||||
RoleId = r.Id,
|
||||
RoleName = r.Name,
|
||||
IsSelected = rolesDelUsuario.Contains(r.Name)
|
||||
RoleName = r.Name ?? string.Empty,
|
||||
IsSelected = rolesDelUsuario.Contains(r.Name ?? string.Empty)
|
||||
}).ToList()
|
||||
};
|
||||
return View(model);
|
||||
@@ -183,7 +183,7 @@ namespace vita_asistente.Controllers
|
||||
if (usuario == null) return NotFound();
|
||||
|
||||
var rolesDelUsuario = await _userManager.GetRolesAsync(usuario);
|
||||
var rolesSeleccionados = model.RolesDisponibles.Where(r => r.IsSelected).Select(r => r.RoleName).ToList();
|
||||
var rolesSeleccionados = model.RolesDisponibles.Where(r => r.IsSelected).Where(r => !string.IsNullOrEmpty(r.RoleName)).Select(r => r.RoleName).ToList();
|
||||
|
||||
// Remover roles que ya no están seleccionados
|
||||
var rolesAEliminar = rolesDelUsuario.Except(rolesSeleccionados).ToList();
|
||||
@@ -206,15 +206,15 @@ namespace vita_asistente.Controllers
|
||||
// ViewModels para la gestión de roles (dentro del mismo archivo por simplicidad)
|
||||
public class ManageRolesViewModel
|
||||
{
|
||||
public string UsuarioId { get; set; }
|
||||
public string UsuarioNombre { get; set; }
|
||||
public List<RoleCheckbox> RolesDisponibles { get; set; }
|
||||
public required string UsuarioId { get; set; }
|
||||
public required string UsuarioNombre { get; set; }
|
||||
public required List<RoleCheckbox> RolesDisponibles { get; set; }
|
||||
}
|
||||
|
||||
public class RoleCheckbox
|
||||
public class RoleCheckbox
|
||||
{
|
||||
public string RoleId { get; set; }
|
||||
public string RoleName { get; set; }
|
||||
public required string RoleId { get; set; }
|
||||
public required string RoleName { get; set; }
|
||||
public bool IsSelected { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user