Fase 2 - T2
This commit is contained in:
@@ -3,7 +3,7 @@ using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Threading.Tasks;
|
||||
using System.Linq;
|
||||
using vita_asistente.Models;
|
||||
|
||||
namespace vita_asistente.Controllers
|
||||
{
|
||||
@@ -61,13 +61,82 @@ namespace vita_asistente.Controllers
|
||||
return View();
|
||||
}
|
||||
|
||||
// GET: Roles/Edit/5
|
||||
public async Task<IActionResult> Edit(string id)
|
||||
{
|
||||
if (id == null) return NotFound();
|
||||
var role = await _roleManager.FindByIdAsync(id);
|
||||
if (role == null) return NotFound();
|
||||
ViewBag.RoleName = role.Name;
|
||||
return View(role);
|
||||
}
|
||||
|
||||
// POST: Roles/Edit/5
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Edit(string id, IdentityRole roleActualizado)
|
||||
{
|
||||
if (id != roleActualizado.Id) return NotFound();
|
||||
|
||||
var roleExistente = await _roleManager.FindByIdAsync(id);
|
||||
if (roleExistente == null) return NotFound();
|
||||
|
||||
// Actualizar solo el nombre
|
||||
roleExistente.Name = roleActualizado.Name;
|
||||
var result = await _roleManager.UpdateAsync(roleExistente);
|
||||
|
||||
if (result.Succeeded)
|
||||
{
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
foreach (var error in result.Errors)
|
||||
{
|
||||
ModelState.AddModelError("", error.Description);
|
||||
}
|
||||
return View(roleActualizado);
|
||||
}
|
||||
|
||||
// GET: Roles/Disable/5
|
||||
public async Task<IActionResult> Disable(string id)
|
||||
{
|
||||
if (id == null) return NotFound();
|
||||
var role = await _roleManager.FindByIdAsync(id);
|
||||
if (role == null) return NotFound();
|
||||
return View(role);
|
||||
}
|
||||
|
||||
// POST: Roles/Disable/5
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> DisableConfirmed(string id)
|
||||
{
|
||||
var role = await _roleManager.FindByIdAsync(id);
|
||||
if (role != null)
|
||||
{
|
||||
// En Identity, "deshabilitar" un rol implica eliminarlo
|
||||
// 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);
|
||||
|
||||
if (usersInRole.Any())
|
||||
{
|
||||
ModelState.AddModelError("", "No se puede deshabilitar un rol que tiene usuarios asignados.");
|
||||
return View("Disable", role);
|
||||
}
|
||||
|
||||
await _roleManager.DeleteAsync(role);
|
||||
}
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
// GET: Roles/Delete/5
|
||||
public async Task<IActionResult> Delete(string id)
|
||||
{
|
||||
if (id == null) return NotFound();
|
||||
var rol = await _roleManager.FindByIdAsync(id);
|
||||
if (rol == null) return NotFound();
|
||||
return View(rol);
|
||||
var role = await _roleManager.FindByIdAsync(id);
|
||||
if (role == null) return NotFound();
|
||||
return View(role);
|
||||
}
|
||||
|
||||
// POST: Roles/Delete/5
|
||||
@@ -75,10 +144,28 @@ namespace vita_asistente.Controllers
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> DeleteConfirmed(string id)
|
||||
{
|
||||
var rol = await _roleManager.FindByIdAsync(id);
|
||||
if (rol != null)
|
||||
var role = await _roleManager.FindByIdAsync(id);
|
||||
if (role != null)
|
||||
{
|
||||
await _roleManager.DeleteAsync(rol);
|
||||
// Verificar que no sea el rol "Admin" (o cualquier otro rol esencial)
|
||||
if (role.Name == "Admin" || role.Name == "Administrador")
|
||||
{
|
||||
ModelState.AddModelError("", "No se puede eliminar el rol Admin/Administrador.");
|
||||
return View(role);
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
if (usersInRole.Any())
|
||||
{
|
||||
ModelState.AddModelError("", "No se puede eliminar un rol que tiene usuarios asignados.");
|
||||
return View(role);
|
||||
}
|
||||
|
||||
await _roleManager.DeleteAsync(role);
|
||||
}
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user