From 9f01acef95b2e60eb8f42e39bbaf4717135db28f Mon Sep 17 00:00:00 2001 From: Stoyan Zlatev Date: Tue, 20 Feb 2024 14:56:42 +0100 Subject: [PATCH] WEB: GNS passwords --- .../LaaProduction.Http/HttpClient.cs | 25 +-- .../Interfaces/IHttpClient.cs | 10 +- .../LaaProductionWeb/API/SendController.cs | 5 +- .../Controllers/SearchController.cs | 157 +++++++++++++++++- 4 files changed, 178 insertions(+), 19 deletions(-) diff --git a/LaaProductionWeb/LaaProduction.Http/HttpClient.cs b/LaaProductionWeb/LaaProduction.Http/HttpClient.cs index beafc7fc..076ad80f 100644 --- a/LaaProductionWeb/LaaProduction.Http/HttpClient.cs +++ b/LaaProductionWeb/LaaProduction.Http/HttpClient.cs @@ -32,20 +32,20 @@ public static IHttpClient CreateHttpClient(string baseURI, string routePrefix) => new HttpClient(baseURI, routePrefix); - public IHttpMessage Delete(string route) - => new HttpMessage(this, HttpMethod.Delete, $"{routePrefix}/{route.TrimStart('/')}"); + public IHttpMessage Delete(string route, bool full = false) + => new HttpMessage(this, HttpMethod.Delete, this.Route(route, full)); - public IHttpMessage Get(string route) - => new HttpMessage(this, HttpMethod.Get, $"{routePrefix}/{route.TrimStart('/')}"); + public IHttpMessage Get(string route, bool full = false) + => new HttpMessage(this, HttpMethod.Get, this.Route(route, full)); - public IHttpMessage Options(string route) - => new HttpMessage(this, HttpMethod.Options, $"{routePrefix}/{route.TrimStart('/')}"); + public IHttpMessage Options(string route, bool full = false) + => new HttpMessage(this, HttpMethod.Options, this.Route(route, full)); - public IHttpMessage Post(string route) - => new HttpMessage(this, HttpMethod.Post, $"{routePrefix}/{route.TrimStart('/')}"); + public IHttpMessage Post(string route, bool full = false) + => new HttpMessage(this, HttpMethod.Post, this.Route(route, full)); - public IHttpMessage Put(string route) - => new HttpMessage(this, HttpMethod.Put, $"{routePrefix}/{route.TrimStart('/')}"); + public IHttpMessage Put(string route, bool full = false) + => new HttpMessage(this, HttpMethod.Put, this.Route(route, full)); internal async Task> SendAsync(HttpMessage fluentHttpRequestMessage) { @@ -87,5 +87,10 @@ return httpResult; } + + private string Route(string route, bool full) + { + return full ? route : $"{routePrefix}/{route.TrimStart('/')}"; + } } } diff --git a/LaaProductionWeb/LaaProduction.Http/Interfaces/IHttpClient.cs b/LaaProductionWeb/LaaProduction.Http/Interfaces/IHttpClient.cs index 7c817993..3f16c63e 100644 --- a/LaaProductionWeb/LaaProduction.Http/Interfaces/IHttpClient.cs +++ b/LaaProductionWeb/LaaProduction.Http/Interfaces/IHttpClient.cs @@ -2,14 +2,14 @@ { public interface IHttpClient { - IHttpMessage Delete(string route); + IHttpMessage Delete(string route, bool full = false); - IHttpMessage Get(string route); + IHttpMessage Get(string route, bool full = false); - IHttpMessage Options(string route); + IHttpMessage Options(string route, bool full = false); - IHttpMessage Post(string url); + IHttpMessage Post(string route, bool full = false); - IHttpMessage Put(string url); + IHttpMessage Put(string route, bool full = false); } } diff --git a/LaaProductionWeb/LaaProductionWeb/API/SendController.cs b/LaaProductionWeb/LaaProductionWeb/API/SendController.cs index bd1bed60..049b195f 100644 --- a/LaaProductionWeb/LaaProductionWeb/API/SendController.cs +++ b/LaaProductionWeb/LaaProductionWeb/API/SendController.cs @@ -9,7 +9,7 @@ using System.Web.Http; [RoutePrefix(ROUTE_PREFIX)] - public class SendController : ApiController + public class SendController : ControllerBase { const string ROUTE_PREFIX = "api/Send"; @@ -21,7 +21,7 @@ if (!this.ModelState.IsValid) { - response = this.BadRequest(this.ModelState.ErrorsJson()); + response = this.BadRequest(); } else if (!this.TrySendEmail(model, out string state)) { @@ -146,6 +146,7 @@ return recordId; } + } public class SendModel diff --git a/LaaProductionWeb/LaaProductionWeb/Controllers/SearchController.cs b/LaaProductionWeb/LaaProductionWeb/Controllers/SearchController.cs index 41fe4c42..df059c5e 100644 --- a/LaaProductionWeb/LaaProductionWeb/Controllers/SearchController.cs +++ b/LaaProductionWeb/LaaProductionWeb/Controllers/SearchController.cs @@ -1,11 +1,12 @@ namespace LaaProductionWeb.Controllers { + using LaaProductionDI; using LaaProduction.Personalization; using LaaProduction.Http.Interfaces; - using LaaProductionWeb.App_Infrastructure; using LaaProduction.Services.Interfaces; using LaaProduction.Services.Models.Search; - using LaaProductionDI; + using LaaProductionWeb.API; + using LaaProductionWeb.App_Infrastructure; using System; using System.Collections.Generic; @@ -103,5 +104,157 @@ return this.View(model); } + + [HttpGet] + [AllowedRoles(EmployeesRoles.WEB_Passwords)] + public async Task Passwords() + { + var fieldsResult = await this.httpClient + .Get("/Search/Wildcard") + .AuthorizeBearer(this.User.GetNameIdentifier()) + .SendAsync>(); + + var model = new SearchPasswordsModel(); + + if (fieldsResult.Succeeded) + { + if (fieldsResult.Value != null) + { + model.Fields = fieldsResult.Value.ToDictionary(x => x, x => true); + } + } + else + { + this.ModelState.AddModelErrors(fieldsResult); + } + + return this.View(model); + } + + [HttpPost] + [AllowedRoles(EmployeesRoles.WEB_Passwords)] + public async Task Passwords(SearchPasswordsModel model) + { + var fields = string.Empty; + + if (model.Fields.Any(x => x.Value)) + { + fields = WebUtility.UrlEncode(string.Join(",", model.Fields.Where(x => x.Value).Select(x => x.Key))); + } + + // http://10.49.40.25/MeterProcessState/api/FinalCheck/GetEncryptionKey?PcbID=45451 + + var httpResult = await this.httpClient + .Post($"/Search/Wildcard") + .WhitJsonBody(new { model.Token, fields }) + .AuthorizeBearer(this.User.GetNameIdentifier()) + .SendAsync>>(); + + if (httpResult.Succeeded) + { + model.Results = httpResult.Value ?? Array.Empty>(); + } + else + { + this.ModelState.AddModelErrors(httpResult); + } + + return this.View(model); + } + + public async Task FS(string pcbid) + { + var userId = this.User?.Identity?.Name; + var email = this.ordersService.GetUserEmail(userId); + + if (string.IsNullOrWhiteSpace(email) || email.Length < 11) + { + email = "Roland.Drabesch@xylem.com"; + + // return "Leider kanst Du keine Funkschlüssel anfordern!"; + } + + var result = string.Empty; + + try + { + var httpResult = await this.httpClient + .Get($"http://10.49.40.25/MeterProcessState/api/FinalCheck/GetEncryptionKey?PcbID={pcbid}", full: true) + .SendAsync(); + + if (httpResult.Succeeded) + { + result = httpResult.Value?.ToUpper(); + } + + // TODO: remove in the feature + //using (var httpClient = new HttpClient()) + //{ + // using (var httpResponse = await httpClient.GetAsync($"http://10.49.40.25/MeterProcessState/api/FinalCheck/GetEncryptionKey?PcbID={pcbid}")) + // { + // if (httpResponse.Content != null) + // { + // result = await httpResponse.Content.ReadAsStringAsync(); + // result = result?.ToUpper(); + // } + // } + //} + } + catch (Exception e) + { + result = e.ToString(); + } + + try + { + await this.httpClient + .Post("api/Send/Email") + .WhitJsonBody(new SendModel + { + Body = $"MitarbeiterNr: {userId} => PcbID: {pcbid}", + CC = new string [] { "Roland.Drabesch@xylem.com", "Stoyan.Zlatev@xylem.com" }, + IPAddress = HttpContext.Request.UserHostAddress, + IsHtml = false, + PCName = HttpContext.Request.UserHostName, + Recipients = new string[] { "Roland.Drabesch@xylem.com", "Stoyan.Zlatev@xylem.com" }, + Sender = email, + StationNr = "0000", + Subject = "Funkschlüssel wurde angefragt.", + Trace = $"{nameof(SearchController)}.{nameof(FS)}" + }) + .SendAsync(); + + // TODO: remove in the feature + //this.smtpClient + // .CreateMessage() + // .Subject("Funkschlüssel wurde angefragt.") + // .Body($"MitarbeiterNr: {userId} => PcbID: {pcbid}") + // .From(email) + // .To("Roland.Drabesch@xylem.com") + // .CC("Stoyan.Zlatev@xylem.com") + // .Send(); + } + catch (Exception e) + { + await this.httpClient + .Post("api/Send/Email") + .WhitJsonBody(new SendModel + { + Body = $"{e}", + CC = new string[] { "Roland.Drabesch@xylem.com", "Stoyan.Zlatev@xylem.com" }, + IPAddress = HttpContext.Request.UserHostAddress, + IsHtml = false, + PCName = HttpContext.Request.UserHostName, + Recipients = new string[] { "Roland.Drabesch@xylem.com", "Stoyan.Zlatev@xylem.com" }, + Sender = email, + StationNr = "0000", + Subject = "Funkschlüssel wurde angefragt.", + Trace = $"{nameof(SearchController)}.{nameof(FS)}" + }) + .SendAsync(); + } + + return result.Trim('"'); + } } } \ No newline at end of file