260 lines
12 KiB
C#
260 lines
12 KiB
C#
using CuentasCobrar.CORE.Exceptions;
|
|
using CuentasCobrar.UI.Helpers;
|
|
using CuentasCobrar.UI.Models;
|
|
using CuentasCobrar.UI.Models.Filters;
|
|
using CuentasCobrar.UI.Models.Paginacion;
|
|
using CuentasCobrar.UI.Services.Interfaces;
|
|
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using static CuentasCobrar.UI.Helpers.Enum;
|
|
|
|
namespace CuentasCobrar.UI.Controllers
|
|
{
|
|
public class UsuarioController : BaseController
|
|
{
|
|
private readonly IUsuarioAPI _usuarioAPI;
|
|
private readonly IRolDeUsuarioAPI _rolDeUsuarioAPI;
|
|
public UsuarioController ( IUsuarioAPI usuarioAPI, IRolDeUsuarioAPI rolDeUsuarioAPI )
|
|
{
|
|
_usuarioAPI = usuarioAPI;
|
|
_rolDeUsuarioAPI = rolDeUsuarioAPI;
|
|
}
|
|
public async Task<ActionResult> Index ( PaginacionFilterModel filtros )
|
|
{
|
|
var usuarios = new ListaDePaginacion<UsuarioViewModel> ();
|
|
try
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
usuarios = await _usuarioAPI.ObtenerUsuariosPaginados ( token, filtros.NumeroDePagina, filtros.RegistrosPorPagina );
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (ex.GetType () == typeof ( AuthorizeException ))
|
|
{
|
|
AuthorizeException excepcion = ex as AuthorizeException;
|
|
CustomNotification ( $"Error {excepcion.number}: {excepcion.Message}", TipoNotificacion.error, NotificationPosition.Center, "Usuario" );
|
|
return RedirectToAction ( "Index", "Home" );
|
|
}
|
|
else
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Usuario" );
|
|
return RedirectToAction ( "Index", "Home" );
|
|
}
|
|
}
|
|
return View ( usuarios );
|
|
}
|
|
public async Task<ActionResult> ActualizarUsuario ( UsuarioViewModel usuario, PaginacionFilterModel filtros )
|
|
{
|
|
try
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
bool respuesta = false;
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
respuesta = await _usuarioAPI.ActualizarUsuario ( token, usuario );
|
|
if (respuesta)
|
|
{
|
|
CustomNotification ( "Se modificó el usuario de manera exitosa", TipoNotificacion.success, NotificationPosition.Center, "Usuario" );
|
|
return RedirectToAction ( "Index", new { numeroDePagina = filtros.NumeroDePagina, registrosPorPagina = filtros.RegistrosPorPagina } );
|
|
}
|
|
else
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Usuario" );
|
|
return RedirectToAction ( "Index", new { numeroDePagina = filtros.NumeroDePagina, registrosPorPagina = filtros.RegistrosPorPagina } );
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ViewBag.Usuarios = await _usuarioAPI.ObtenerUsuariosPaginados ( token, filtros.NumeroDePagina, filtros.RegistrosPorPagina );
|
|
return View ( "Editar", usuario );
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
if (ex.GetType () == typeof ( AuthorizeException ))
|
|
{
|
|
AuthorizeException excepcion = ex as AuthorizeException;
|
|
CustomNotification ( $"Error {excepcion.number}: {excepcion.Message}", TipoNotificacion.error, NotificationPosition.Center, "Usuario" );
|
|
return RedirectToAction ( "Index", new { numeroDePagina = filtros.NumeroDePagina, registrosPorPagina = filtros.RegistrosPorPagina } );
|
|
}
|
|
else
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Usuario" );
|
|
return RedirectToAction ( "Index", new { numeroDePagina = filtros.NumeroDePagina, registrosPorPagina = filtros.RegistrosPorPagina } );
|
|
}
|
|
}
|
|
}
|
|
|
|
public async Task<ActionResult> AgregarUsuario ( UsuarioViewModel usuario, PaginacionFilterModel filtros )
|
|
{
|
|
try
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
bool respuesta = false;
|
|
if (ModelState.IsValid)
|
|
{
|
|
respuesta = await _usuarioAPI.AgregarUsuario ( token, usuario );
|
|
if (respuesta)
|
|
{
|
|
CustomNotification ( "Se agregó el usuario de manera exitosa", TipoNotificacion.success, NotificationPosition.Center, "Usuario" );
|
|
return RedirectToAction ( "Index", new { numeroDePagina = filtros.NumeroDePagina, registrosPorPagina = filtros.RegistrosPorPagina } );
|
|
|
|
}
|
|
else
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Usuario" );
|
|
return RedirectToAction ( "Index", new { numeroDePagina = filtros.NumeroDePagina, registrosPorPagina = filtros.RegistrosPorPagina } );
|
|
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ViewBag.Usuarios = await _usuarioAPI.ObtenerUsuariosPaginados ( token, filtros.NumeroDePagina, filtros.RegistrosPorPagina );
|
|
return View ( "Agregar", usuario );
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
if (ex.GetType () == typeof ( AuthorizeException ))
|
|
{
|
|
AuthorizeException excepcion = ex as AuthorizeException;
|
|
CustomNotification ( $"Error {excepcion.number}: {excepcion.Message}", TipoNotificacion.error, NotificationPosition.Center, "Usuario" );
|
|
return RedirectToAction ( "Index", new { numeroDePagina = filtros.NumeroDePagina, registrosPorPagina = filtros.RegistrosPorPagina } );
|
|
}
|
|
else
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Usuario" );
|
|
return RedirectToAction ( "Index", new { numeroDePagina = filtros.NumeroDePagina, registrosPorPagina = filtros.RegistrosPorPagina } );
|
|
}
|
|
}
|
|
}
|
|
|
|
public async Task EliminarUsuario ( string id )
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
List<RolDeUsuarioViewModel>? rolesDeUsuario = await _rolDeUsuarioAPI.ObtenerRolesDeUsuario ( token, id );
|
|
foreach (RolDeUsuarioViewModel rolDeUsuario in rolesDeUsuario)
|
|
{
|
|
await _rolDeUsuarioAPI.EliminarRolDeUsuario ( token, rolDeUsuario.IdUsuario, rolDeUsuario.IdRol );
|
|
}
|
|
await _usuarioAPI.EliminarUsuario ( token, id );
|
|
}
|
|
|
|
public async Task<ActionResult> Editar ( string id, PaginacionFilterModel filtros )
|
|
{
|
|
UsuarioViewModel? usuario = new UsuarioViewModel ();
|
|
try
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
ViewBag.Usuarios = await _usuarioAPI.ObtenerUsuariosPaginados ( token, filtros.NumeroDePagina, filtros.RegistrosPorPagina );
|
|
usuario = await _usuarioAPI.ObtenerUsuario ( token, id );
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (ex.GetType () == typeof ( AuthorizeException ))
|
|
{
|
|
AuthorizeException excepcion = ex as AuthorizeException;
|
|
CustomNotification ( $"Error {excepcion.number}: {excepcion.Message}", TipoNotificacion.error, NotificationPosition.Center, "Usuario" );
|
|
return RedirectToAction ( "Index" );
|
|
}
|
|
else
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Usuario" );
|
|
return RedirectToAction ( "Index", new { numeroDePagina = filtros.NumeroDePagina, registrosPorPagina = filtros.RegistrosPorPagina } );
|
|
}
|
|
}
|
|
return View ( usuario );
|
|
}
|
|
|
|
public async Task<ActionResult> Agregar ( PaginacionFilterModel filtros )
|
|
{
|
|
try
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
ViewBag.Usuarios = await _usuarioAPI.ObtenerUsuariosPaginados ( token, filtros.NumeroDePagina, filtros.RegistrosPorPagina );
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (ex.GetType () == typeof ( AuthorizeException ))
|
|
{
|
|
AuthorizeException excepcion = ex as AuthorizeException;
|
|
CustomNotification ( $"Error {excepcion.number}: {excepcion.Message}", TipoNotificacion.error, NotificationPosition.Center, "Usuario" );
|
|
return RedirectToAction ( "Index" );
|
|
}
|
|
else
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Usuario" );
|
|
return RedirectToAction ( "Index", new { numeroDePagina = filtros.NumeroDePagina, registrosPorPagina = filtros.RegistrosPorPagina } );
|
|
}
|
|
}
|
|
return View ();
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<IActionResult> SignOut ()
|
|
{
|
|
Response.Cookies.Delete ( "CookieToken" );
|
|
Response.Cookies.Delete ( "CookieRoles" );
|
|
|
|
var redirectUrl = Url.Content ( "~/" ); // Se configura así para asegurarse que vuelva al login una vez cierre sesion
|
|
var properties = new AuthenticationProperties { RedirectUri = redirectUrl };
|
|
string scheme = OpenIdConnectDefaults.AuthenticationScheme;
|
|
// obtener el id_token
|
|
var idToken = await HttpContext.GetTokenAsync ( "id_token" );
|
|
// enviar el valor id_token al middleware de autenticacion
|
|
properties.Items["id_token_hint"] = idToken;
|
|
return SignOut ( properties, CookieAuthenticationDefaults.AuthenticationScheme, scheme );
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult SignIn ()
|
|
{
|
|
string scheme = OpenIdConnectDefaults.AuthenticationScheme;
|
|
var redirectUrl = Url.Content ( "~/" );
|
|
var properties = new AuthenticationProperties { RedirectUri = redirectUrl };
|
|
return Challenge ( properties, scheme );
|
|
}
|
|
|
|
}
|
|
}
|