2025-01-24 19:17:26 -03:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using phronCare.API.Models.Account;
|
2025-04-29 19:40:25 -03:00
|
|
|
|
using phronCare.API.Models.Security; // Importá donde tengas ApplicationUser
|
2025-01-24 19:17:26 -03:00
|
|
|
|
|
|
|
|
|
|
namespace phronCare.API.Controllers
|
|
|
|
|
|
{
|
|
|
|
|
|
[Authorize(Roles = "Admin")]
|
|
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
|
[ApiController]
|
|
|
|
|
|
public class AccountController : ControllerBase
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly RoleManager<IdentityRole> _roleManager;
|
2025-04-29 19:40:25 -03:00
|
|
|
|
private readonly UserManager<ApplicationUser> _userManager;
|
2025-01-24 19:17:26 -03:00
|
|
|
|
|
2025-04-29 19:40:25 -03:00
|
|
|
|
public AccountController(RoleManager<IdentityRole> roleManager, UserManager<ApplicationUser> userManager)
|
2025-01-24 19:17:26 -03:00
|
|
|
|
{
|
|
|
|
|
|
_roleManager = roleManager;
|
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("GetAllRoles")]
|
|
|
|
|
|
public IActionResult GetAllRoles()
|
|
|
|
|
|
{
|
|
|
|
|
|
var roles = _roleManager.Roles.ToList();
|
|
|
|
|
|
return Ok(roles);
|
|
|
|
|
|
}
|
2025-04-29 19:40:25 -03:00
|
|
|
|
|
2025-01-24 19:17:26 -03:00
|
|
|
|
[HttpGet("GetRoleById/{id}")]
|
|
|
|
|
|
public IActionResult GetRoleById(string id)
|
|
|
|
|
|
{
|
2025-04-29 19:40:25 -03:00
|
|
|
|
var role = _roleManager.Roles.FirstOrDefault(r => r.Id == id.TrimStart('{').TrimEnd('}'));
|
2025-01-24 19:17:26 -03:00
|
|
|
|
return Ok(role);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("CreateRole")]
|
|
|
|
|
|
public async Task<IActionResult> CreateRole(Role model)
|
|
|
|
|
|
{
|
|
|
|
|
|
var existingRole = await _roleManager.FindByNameAsync(model.Name);
|
|
|
|
|
|
if (existingRole != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return BadRequest("El rol ya existe.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var newRole = new IdentityRole
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = model.Name,
|
2025-04-29 19:40:25 -03:00
|
|
|
|
NormalizedName = model.Name.ToUpper(),
|
2025-01-24 19:17:26 -03:00
|
|
|
|
ConcurrencyStamp = Guid.NewGuid().ToString()
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var result = await _roleManager.CreateAsync(newRole);
|
|
|
|
|
|
|
|
|
|
|
|
if (result.Succeeded)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Ok("Rol creado exitosamente.");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var errors = result.Errors.Select(e => e.Description);
|
2025-04-29 19:40:25 -03:00
|
|
|
|
return BadRequest($"Error al crear el rol: {string.Join(", ", errors)}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPut("UpdateRole/{id}")]
|
|
|
|
|
|
public async Task<IActionResult> UpdateRole(string id, Role model)
|
|
|
|
|
|
{
|
|
|
|
|
|
var role = await _roleManager.FindByIdAsync(id);
|
|
|
|
|
|
if (role == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return NotFound("Rol no encontrado");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
role.Name = model.Name;
|
|
|
|
|
|
role.NormalizedName = model.NormalizedName;
|
|
|
|
|
|
|
|
|
|
|
|
var result = await _roleManager.UpdateAsync(role);
|
|
|
|
|
|
|
|
|
|
|
|
if (result.Succeeded)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Ok("Rol actualizado exitosamente");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return BadRequest("Error al actualizar el rol");
|
2025-01-24 19:17:26 -03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-04-29 19:40:25 -03:00
|
|
|
|
|
2025-01-24 19:17:26 -03:00
|
|
|
|
[HttpDelete("DeleteRole/{roleId}")]
|
2025-04-29 19:40:25 -03:00
|
|
|
|
public async Task<IActionResult> DeleteRole(string roleId)
|
2025-01-24 19:17:26 -03:00
|
|
|
|
{
|
2025-04-29 19:40:25 -03:00
|
|
|
|
var existingRole = await _roleManager.FindByIdAsync(roleId);
|
2025-01-24 19:17:26 -03:00
|
|
|
|
if (existingRole == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return NotFound("El rol no se encontró.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (existingRole.Name.ToLower() == "admin")
|
|
|
|
|
|
{
|
|
|
|
|
|
return BadRequest("No se puede eliminar el rol 'Admin'.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-29 19:40:25 -03:00
|
|
|
|
var result = await _roleManager.DeleteAsync(existingRole);
|
|
|
|
|
|
|
2025-01-24 19:17:26 -03:00
|
|
|
|
if (result.Succeeded)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Ok("El rol se eliminó exitosamente.");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return BadRequest("Error al eliminar el rol.");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-04-29 19:40:25 -03:00
|
|
|
|
|
2025-01-24 19:17:26 -03:00
|
|
|
|
[HttpGet("GetAllUsers")]
|
|
|
|
|
|
public IActionResult GetAllUsers()
|
|
|
|
|
|
{
|
|
|
|
|
|
var users = _userManager.Users.ToList();
|
|
|
|
|
|
return Ok(users);
|
|
|
|
|
|
}
|
2025-04-29 19:40:25 -03:00
|
|
|
|
|
2025-01-24 19:17:26 -03:00
|
|
|
|
[HttpGet("GetUserById/{id}")]
|
|
|
|
|
|
public IActionResult GetUserById(string id)
|
|
|
|
|
|
{
|
2025-04-29 19:40:25 -03:00
|
|
|
|
var user = _userManager.Users.FirstOrDefault(u => u.Id == id.TrimStart('{').TrimEnd('}'));
|
2025-01-24 19:17:26 -03:00
|
|
|
|
return Ok(user);
|
|
|
|
|
|
}
|
2025-04-29 19:40:25 -03:00
|
|
|
|
|
2025-01-24 19:17:26 -03:00
|
|
|
|
[HttpPut("UpdateUser/{id}")]
|
2025-04-29 19:40:25 -03:00
|
|
|
|
public async Task<IActionResult> UpdateUser(string id, UserUpdate model)
|
2025-01-24 19:17:26 -03:00
|
|
|
|
{
|
|
|
|
|
|
var user = await _userManager.FindByIdAsync(id);
|
|
|
|
|
|
if (user == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return NotFound("Usuario no encontrado");
|
|
|
|
|
|
}
|
2025-04-29 19:40:25 -03:00
|
|
|
|
|
2025-01-24 19:17:26 -03:00
|
|
|
|
user.UserName = model.UserName;
|
2025-04-29 19:40:25 -03:00
|
|
|
|
user.NormalizedUserName = model.UserName.ToUpper();
|
2025-01-24 19:17:26 -03:00
|
|
|
|
user.Email = model.Email;
|
2025-04-29 19:40:25 -03:00
|
|
|
|
user.NormalizedEmail = model.Email.ToUpper();
|
2025-01-24 19:17:26 -03:00
|
|
|
|
user.TwoFactorEnabled = model.TwoFactorEnabled;
|
|
|
|
|
|
user.LockoutEnabled = model.LockoutEnabled;
|
|
|
|
|
|
|
|
|
|
|
|
var result = await _userManager.UpdateAsync(user);
|
|
|
|
|
|
|
|
|
|
|
|
if (result.Succeeded)
|
|
|
|
|
|
{
|
2025-04-29 19:40:25 -03:00
|
|
|
|
return Ok("Usuario actualizado exitosamente");
|
2025-01-24 19:17:26 -03:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return BadRequest("Error al actualizar el usuario");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpDelete("DeleteUser/{userId}")]
|
2025-04-29 19:40:25 -03:00
|
|
|
|
public async Task<IActionResult> DeleteUser(string userId)
|
2025-01-24 19:17:26 -03:00
|
|
|
|
{
|
2025-04-29 19:40:25 -03:00
|
|
|
|
var existingUser = await _userManager.FindByIdAsync(userId);
|
2025-01-24 19:17:26 -03:00
|
|
|
|
if (existingUser == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return NotFound("El usuario no se encontró.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-29 19:40:25 -03:00
|
|
|
|
if (existingUser.UserName.ToLower() == "superadmin")
|
2025-01-24 19:17:26 -03:00
|
|
|
|
{
|
|
|
|
|
|
return BadRequest("No se puede eliminar el usuario 'SuperAdmin'.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-29 19:40:25 -03:00
|
|
|
|
var result = await _userManager.DeleteAsync(existingUser);
|
|
|
|
|
|
|
2025-01-24 19:17:26 -03:00
|
|
|
|
if (result.Succeeded)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Ok("El usuario se eliminó exitosamente.");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return BadRequest("Error al eliminar el usuario.");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-04-29 19:40:25 -03:00
|
|
|
|
}
|