using EgwCoreLib.Razor; using GPW.CORE.Data.DbModels; using GPW.CORE.Smart.Data; using Microsoft.AspNetCore.Components; using System.Text; namespace GPW.CORE.Smart.Components.Compo { public partial class InvioLinkEmail { #region Public Properties [Parameter] public int IdxDipAdmin { get; set; } = 0; #endregion Public Properties #region Protected Methods protected override async Task OnInitializedAsync() { initToggler(); await initConf(); await ReloadData(); } protected override async Task OnParametersSetAsync() { // verifico se user sia admin... var listaRuoli = await CDService.Dip2RuoliGetAll(); var rigaDip = listaRuoli.FirstOrDefault(x => x.IdxDipendente == IdxDipAdmin && x.CodRuolo == "ResetAuthKey"); userIsAdmin = rigaDip != null; } #endregion Protected Methods #region Private Fields private bool isSendingData = false; private List ListaDip = new List(); private bool userIsAdmin = false; #endregion Private Fields #region Private Properties [Inject] private CoreSmartDataService CDService { get; set; } = null!; [Inject] private IConfiguration Configuration { get; set; } = null!; private bool forceResetAuthKey { get; set; } = false; private int idxDipendente { get; set; } = 0; [Inject] private MessageService MService { get; set; } = null!; private Toggler.SelectGlobalToggle ToggleData { get; set; } = new Toggler.SelectGlobalToggle(); private string UrlLinkExt { get; set; } = ""; private string UrlLinkInt { get; set; } = ""; #endregion Private Properties #region Private Methods /// /// Effettua vero reset authKey dipendente... /// /// private async Task doResetAuthKey() { var rigaDip = ListaDip.FirstOrDefault(x => x.IdxDipendente == idxDipendente); if (rigaDip != null) { Guid newGuid = Guid.NewGuid(); string newAuthKey = $"{newGuid}"; rigaDip.AuthKey = newAuthKey.ToUpper(); // update! await CDService.DipendentiUpdate(rigaDip); } await Task.Delay(1); } /// /// Invia email con link x accesso /// /// private async Task doSendEmailAccessLink() { isSendingData = true; await Task.Delay(1); // recupero record dip da selezioanta... if (idxDipendente > 0) { string authKey = ""; string destinatario = ""; // se riechiesto reset --> ricalcolo authKey x dip if (forceResetAuthKey) { await doResetAuthKey(); await ReloadData(); } var rigaDip = ListaDip.FirstOrDefault(x => x.IdxDipendente == idxDipendente); if (rigaDip != null) { authKey = System.Web.HttpUtility.UrlEncode(rigaDip.AuthKey); destinatario = rigaDip.Email; // compongo testo messaggio string oggetto = "Link autorizzazione device per GPW Core.Smart"; string userUrl = $"{UrlLinkInt}jumper?idxDipendente={idxDipendente}&authKey={authKey}"; string userWebUrl = $"{UrlLinkExt}jumper?idxDipendente={idxDipendente}&authKey={authKey}"; // corpo StringBuilder sbBody = new StringBuilder(); sbBody.AppendLine("Hai ricevuto questa email su richiesta tua o dell'Admin per poter procedere a registrare un (nuovo) devices con GPW"); sbBody.AppendLine(""); sbBody.AppendLine("
"); sbBody.AppendLine("Rete Interna"); sbBody.AppendLine($"Per proseguire clicca sul link seguente (rete interna)"); //sbBody.AppendLine(userUrl); sbBody.AppendLine("
"); sbBody.AppendLine("Internet"); sbBody.AppendLine($"Oppure sul link seguente (internet)"); //sbBody.AppendLine(userWebUrl); sbBody.AppendLine("
"); sbBody.AppendLine(""); sbBody.AppendLine(""); sbBody.AppendLine("Team GPW EgalWare"); string msgBody = sbBody.ToString().Replace($"{Environment.NewLine}", "
"); // invio bool fatto = await CDService.sendEmailAsync(destinatario, oggetto, msgBody); } } isSendingData = false; await Task.Delay(1); } private async Task evToggled(Toggler.SelectGlobalToggle newTogData) { ToggleData = newTogData; forceResetAuthKey = !ToggleData.isActive; await Task.Delay(1); } /// /// init valori da config /// /// private async Task initConf() { UrlLinkInt = Configuration.GetValue("OptPar:UrlLinkInt") ?? "http://missing.internal.link"; UrlLinkExt = Configuration.GetValue("OptPar:UrlLinkExt") ?? "http://missing.external.link"; await Task.Delay(1); } private void initToggler() { ToggleData = new Toggler.SelectGlobalToggle() { leftString = "Reset + Invio", rightString = "Solo Invio", placardCss = "bg-light border-light text-dark", }; } private async Task ReloadData() { var rawList = await CDService.DipendentiGetAll(); ListaDip = rawList .Where(x => x.Attivo == true) .OrderBy(x => x.Cognome) .ThenBy(x => x.Nome) .ToList(); } #endregion Private Methods } }