diff --git a/ApiGateway/CustomLogic.cs b/ApiGateway/CustomLogic.cs index 2ef925f..3addb5e 100644 --- a/ApiGateway/CustomLogic.cs +++ b/ApiGateway/CustomLogic.cs @@ -127,7 +127,7 @@ namespace ApiGateway foreach (Claim cl in claims) // Claim (key:value) desde el token { - if (cl.Type == claim.Key && String.Equals(claim.Key, "permisos")) + if (cl.Type == claim.Key && String.Equals(claim.Key, "prm")) { string separador = "::"; diff --git a/Microservicios/MsUsuarios/Controllers/AuthController.cs b/Microservicios/MsUsuarios/Controllers/AuthController.cs index b01e6f4..8189402 100644 --- a/Microservicios/MsUsuarios/Controllers/AuthController.cs +++ b/Microservicios/MsUsuarios/Controllers/AuthController.cs @@ -1,5 +1,6 @@ using LdapLoginLib; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; using Microsoft.IdentityModel.Tokens; using MSAdminUsuarios.Context; using MSAdminUsuarios.Dto; @@ -314,6 +315,7 @@ namespace MSAdminUsuarios.Controllers } } + // **** Original [HttpPost("pruebaUsuario")] public AutorizaDTO Token(USUARIO user) { @@ -321,6 +323,8 @@ namespace MSAdminUsuarios.Controllers where perf.TX_FKDOC_USUMS == user.TX_PKDOC_USUMS && perf.NU_FK_PFL != null && perf.BL_ESTADO_PFLXUSU == 1 select perf).ToList(); + _logger.LogInformation(perfilesPorUsuario.ToString()); + List perfiles = perfilesPorUsuario.Select(p => p.NU_FK_PFL).ToList(); List permisos = (from perfXUs in perfilesPorUsuario @@ -426,6 +430,212 @@ namespace MSAdminUsuarios.Controllers //return Encripter.Cypher(token); return autorizaDTO; } + // **** Original ** + + //[HttpPost("pruebaUsuario")] + //public string Token(USUARIO user) + //{ + // List perfilesPorUsuario = (from perf in _context.PERFILESPORUSUARIOs + // where perf.TX_FKDOC_USUMS == user.TX_PKDOC_USUMS && perf.NU_FK_PFL != null && perf.BL_ESTADO_PFLXUSU == 1 + // select perf).ToList(); + + // _logger.LogInformation(perfilesPorUsuario.ToString()); + + // List perfiles = perfilesPorUsuario.Select(p => p.NU_FK_PFL).ToList(); + + // // Verificar si alguno de los perfiles es 2 + // bool perfilEsDos = perfiles.Contains(2); + + // // Solo obtenemos permisos si no existe el perfil 2 + // List permisos = perfilEsDos ? new List() : + // (from perfXUs in perfilesPorUsuario + // join perm in _context.PERMISOSMs on perfXUs.NU_FK_PFL equals perm.NU_FK_PFL + // where perm.BL_ESTADO_PMS == 1 + // select perm).ToList(); + + // Dictionary perfilesPorPermiso = new(); + // // Todos + // if (!perfilEsDos) // Solo procesar permisos si el perfil no es 2 + // { + // foreach (PERMISO p in permisos) + // { + // string key = p.NU_FK_MS.ToString(); + // string val = p.NU_FK_PFL.ToString(); + + // if (perfilesPorPermiso.ContainsKey(key)) + // { + // string antVal = perfilesPorPermiso[key]; + // perfilesPorPermiso[key] = antVal + "," + val; + // } + // else + // { + // perfilesPorPermiso.Add(key, val); + // } + // } + // } + + // perfiles.Add(-1); + + // // Security key debe ser un environment variable seguro + // var secretKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Encripter.HashKey)); + // var signingCredentials = new SigningCredentials(secretKey, SecurityAlgorithms.HmacSha256); + // string nombres = ""; + // string apellidos = ""; + + // if (user.TX_PRIMERNOM_USUMS != null) nombres += user.TX_PRIMERNOM_USUMS; + // if (user.TX_SEGUNDONOM_USUMS != null) nombres += " " + user.TX_SEGUNDONOM_USUMS; + // if (user.TX_PRIMERAPELL_USUMS != null) apellidos += user.TX_PRIMERAPELL_USUMS; + // if (user.TX_SEGUNDOAPELL_USUMS != null) apellidos += " " + user.TX_SEGUNDOAPELL_USUMS; + + // nombres = nombres.Trim(); + // apellidos = apellidos.Trim(); + + // if (nombres == "") nombres = "N"; + // if (apellidos == "") apellidos = "A"; + + // int tipo_doc = user.NU_TIPODOC_USUMS == null ? -1 : (int)user.NU_TIPODOC_USUMS; + + // ClaimsIdentity claims = new ClaimsIdentity(new List + //{ + // new Claim("user", user.TX_PKDOC_USUMS), + // new Claim("nombres", nombres), + // new Claim("apellidos", apellidos), + // new Claim("tipo_doc", tipo_doc.ToString()), + // new Claim("pk", user.NU_PK_USUMS.ToString()), + // new Claim("sedes", user.TX_SEDES_USUMS ?? ""), + // new Claim("TX_NOMBRE_PVD", user.TX_NOMBRE_PVD ?? "{}"), + // new Claim("uid", user.TX_UUID_USUMS.ToString()) + //}); + + // foreach (int p in perfiles) + // { + // claims.AddClaim(new Claim("perfiles", p.ToString(), ClaimValueTypes.String)); + // } + + // if (!perfilEsDos) // AƱadir permisos solo si perfil no es 2 + // { + // foreach (KeyValuePair p in perfilesPorPermiso) + // { + // string key = p.Key; + // string val = p.Value; + // claims.AddClaim(new Claim("permisos", _encript.EncryptHashTkn(key) + "::" + val)); + // } + // } + + // var securityTokenDescriptor = new SecurityTokenDescriptor + // { + // Subject = claims, + // Expires = DateTime.Now.AddDays(20), + // SigningCredentials = signingCredentials + // }; + + // var jwtSecurityTokenHandler = new JwtSecurityTokenHandler(); + // var securityToken = jwtSecurityTokenHandler.CreateToken(securityTokenDescriptor); + // var token = jwtSecurityTokenHandler.WriteToken(securityToken); + + // return Encripter.Cypher(token); + //} + + //[HttpPost("pruebaUsuario")] + //public string Token(USUARIO user) + //{ + // List perfilesPorUsuario = (from perf in _context.PERFILESPORUSUARIOs + // where perf.TX_FKDOC_USUMS == user.TX_PKDOC_USUMS && perf.NU_FK_PFL != null && perf.BL_ESTADO_PFLXUSU == 1 + // select perf).ToList(); + + // _logger.LogInformation(perfilesPorUsuario.ToString()); + + // List perfiles = perfilesPorUsuario.Select(p => p.NU_FK_PFL).ToList(); + + // List permisos = (from perfXUs in perfilesPorUsuario + // join perm in _context.PERMISOSMs on perfXUs.NU_FK_PFL equals perm.NU_FK_PFL + // where perm.BL_ESTADO_PMS == 1 + // select perm).ToList(); + + // Dictionary perfilesPorPermiso = new(); + // foreach (PERMISO p in permisos) + // { + // string key = p.NU_FK_MS.ToString(); + // string val = p.NU_FK_PFL.ToString(); + + // if (perfilesPorPermiso.ContainsKey(key)) + // { + // string antVal = perfilesPorPermiso[key]; + // perfilesPorPermiso.Remove(key); + // perfilesPorPermiso.Add(key, antVal + "," + val); + // } + // else + // { + // perfilesPorPermiso.Add(key, val); + // } + // } + + // perfiles.Add(-1); + + // //Security key debe ser un environment variable seguro + // var secretKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Encripter.HashKey)); + // var signingCredentials = new SigningCredentials(secretKey, SecurityAlgorithms.HmacSha256); + // string nombres = ""; + // string apellidos = ""; + + // if (user.TX_PRIMERNOM_USUMS != null) nombres += user.TX_PRIMERNOM_USUMS; + // if (user.TX_SEGUNDONOM_USUMS != null) nombres += " " + user.TX_SEGUNDONOM_USUMS; + // if (user.TX_PRIMERAPELL_USUMS != null) apellidos += user.TX_PRIMERAPELL_USUMS; + // if (user.TX_SEGUNDOAPELL_USUMS != null) apellidos += " " + user.TX_SEGUNDOAPELL_USUMS; + + // nombres = nombres.Trim(); + // apellidos = apellidos.Trim(); + + // if (nombres == "") nombres = "N"; + // if (apellidos == "") apellidos = "A"; + + // int tipo_doc = user.NU_TIPODOC_USUMS == null ? -1 : (int)user.NU_TIPODOC_USUMS; + + // ClaimsIdentity claims = new ClaimsIdentity(new List + // { + // new Claim("us", user.TX_PKDOC_USUMS), + // new Claim("nm", nombres), + // new Claim("ap", apellidos), + // new Claim("td", tipo_doc.ToString()), + // new Claim("pk", user.NU_PK_USUMS.ToString()), + // new Claim("s", user.TX_SEDES_USUMS == null ? "" : user.TX_SEDES_USUMS), + // new Claim("PVD", user.TX_NOMBRE_PVD ?? "{}"), + // new Claim("uid", user.TX_UUID_USUMS.ToString()) + // }); + + + // foreach (int p in perfiles) + // { + // claims.AddClaim(new Claim("perfiles", p.ToString(), ClaimValueTypes.String)); + // } + + + // //foreach (PERMISO p in permisos) + // //{ + // // claims.AddClaim(new Claim("permisos", Encripter.Encrypt(p.NU_FK_MS.ToString()))); + // //} + + // foreach (KeyValuePair p in perfilesPorPermiso) + // { + // string key = p.Key; + // string val = p.Value; + + // claims.AddClaim(new Claim("prm", _encript.EncryptHashTkn(key) + "|" + val)); + // } + + // var securityTokenDescriptor = new SecurityTokenDescriptor + // { + // Subject = claims, + // Expires = DateTime.Now.AddDays(20), + // SigningCredentials = signingCredentials + // }; + + // var jwtSecurityTokenHandler = new JwtSecurityTokenHandler(); + // var securityToken = jwtSecurityTokenHandler.CreateToken(securityTokenDescriptor); + // var token = jwtSecurityTokenHandler.WriteToken(securityToken); + + // return Encripter.Cypher(token); + //} [HttpPatch("BloquearUsuario")] public IActionResult BloquearUsuario([FromBody] string usuario) diff --git a/Microservicios/MsUsuarios/Controllers/PerfilesController.cs b/Microservicios/MsUsuarios/Controllers/PerfilesController.cs index 8996369..e7c9d0d 100644 --- a/Microservicios/MsUsuarios/Controllers/PerfilesController.cs +++ b/Microservicios/MsUsuarios/Controllers/PerfilesController.cs @@ -22,7 +22,9 @@ namespace MSAdminUsuarios.Controllers { try { - List consulta = await _context.PERFILESMs.OrderBy(p => p.NU_PK_PFL).Where(p=>p.BL_ESTADO_PFL > 0).ToListAsync(); + //List consulta = await _context.PERFILESMs.OrderBy(p => p.NU_PK_PFL).Where(p=>p.BL_ESTADO_PFL > 0).ToListAsync(); + + List consulta = await _context.PERFILESMs.OrderBy(p => p.NU_PK_PFL).Where(p => p.BL_ESTADO_PFL >= 0).ToListAsync(); //var consulta = from dm in _context.PERFILESMs // where dm.BL_ESTADO_PFL>=0 diff --git a/Microservicios/MsUsuarios/Properties/launchSettings.json b/Microservicios/MsUsuarios/Properties/launchSettings.json index a87c1a7..b1c161f 100644 --- a/Microservicios/MsUsuarios/Properties/launchSettings.json +++ b/Microservicios/MsUsuarios/Properties/launchSettings.json @@ -22,6 +22,7 @@ }, "IIS Express": { "commandName": "IISExpress", + //"sslPort": 7292, "launchBrowser": true, "launchUrl": "swagger", "environmentVariables": {