using EgwCoreLib.Razor.Data; using GPW.CORE.Data.DbModels; using GPW.CORE.Smart8.Components.Pages; using GPW.CORE.Smart8.Data; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Http; using NLog; namespace GPW.CORE.Smart8.Components.Layout { public partial class MainLayout { #region Protected Properties [Inject] protected CoreSmartDataService CDService { get; set; } = null!; [Inject] protected IConfiguration configuration { get; set; } = null!; [Inject] protected LicenseService LicServ { get; set; } = null!; [Inject] protected MessageService MService { get; set; } = null!; [Inject] protected NavigationManager NavMan { get; set; } = null!; #endregion Protected Properties #region Protected Methods /// /// Verifica informazioni condivise applicazione /// /// protected async Task checkActivations() { bool allOk = false; if (LicServ.ValidData) { // se non fosse tutto ok if (!LicServ.HasActivData) { // chiamo refresh licenze da remoto allOk = await LicServ.RefreshLicense(); } // se non avesse info applicativo... if (!LicServ.HasAppData) { await LicServ.RefreshApplic(); } } await Task.Delay(1); return allOk; } /// /// Verifica informazioni condivise applicazione /// /// protected async Task checkSharedInfo() { bool allOk = false; if (!LicServ.ValidData) { // salvo cod app da Conf... LicServ.Applicazione = CDService.CodApp; // effettuo lettura dati preliminari da AKV (eventualmente in cache...) List? rawAkvList = await CDService.AKVList(); if (rawAkvList != null) { LicServ.AKVList = rawAkvList; allOk = LicServ.InitAkv(); } } await Task.Delay(1); return allOk; } /// /// Verifica info specifiche utente /// /// protected async Task checkUserLicense() { bool answ = false; // verifico utente attivo answ = MService.IsActive; if (answ) { var rawActList = LicServ.ActivList; // recupero hash utente string hashImpiego = MService.HashDip; // confronto con elenco attivazioni dello shared service... var foundRec = rawActList.Where(x => x.CodImpiego == hashImpiego); answ = foundRec.Count() > 0; // salvo status payloadOk MService.PayloadOk = answ; } await Task.Delay(1); return answ; } protected async Task DoVerifyActiv() { MService.PayloadOk = false; await Task.Delay(1); await VerifyActiv(); } protected async Task ForceReset() { string nextPage = NavMan.ToBaseRelativePath(NavMan.Uri); // tengo solo parte finale... if (nextPage.Contains("/")) { nextPage = nextPage.Substring(nextPage.LastIndexOf("/") + 1); } await Task.Delay(1); await DoVerifyActiv(); NavMan.NavigateTo($"{baseUrl}ForceReset?nextPage={nextPage}", true); } protected override async Task OnAfterRenderAsync(bool firstrender) { await Task.Delay(1); if (firstrender) { if (!NavMan.Uri.Contains("jumper")) { await checkUser(); // verifico attivazione dipendente... await checkUserLicense(); } } Log.Info($"MainLayout.OnAfterRenderAsync | firstrender: {firstrender}"); } protected override async Task OnInitializedAsync() { await DoVerifyActiv(); string baseUrl = configuration.GetValue("OptConf:BaseUrl") ?? "~/"; var versione = typeof(Program).Assembly.GetName().Version; currConfig = new ConfigDTO() { BaseUrl = baseUrl, Versione = $"{versione}" }; await ReloadData(); #if false if (!DipOk) { try { if (!NavMan.Uri.Contains("jumper")) { await checkUser(); // verifico attivazione dipendente... await checkUserLicense(); } } catch (Exception exc) { Log.Info($"MainLayout | No user data: reloading{Environment.NewLine}{exc}"); await Task.Delay(1500); NavMan.NavigateTo($"{baseUrl}Index"); } } #endif Log.Info($"MainLayout.OnInitialized | url: {baseUrl} | v:{versione}"); } protected async Task ReturnHome() { // svuoto cache await CDService.FlushRedisCache(); // ritorno in home NavMan.NavigateTo($"{currConfig.BaseUrl}Home", true); } protected async Task VerifyActiv() { // preliminarmente verifica shared info await checkSharedInfo(); // refresh attivazioni se necessario await checkActivations(); // verifico e salvo dati attivazione dipendente... userOk = await checkUserLicense(); } #endregion Protected Methods #region Private Fields private static Logger Log = LogManager.GetCurrentClassLogger(); private ConfigDTO currConfig = new ConfigDTO(); private string currIpv4 = ""; /// /// Info utente corrente x display /// private UserDTO? currUser = null; private bool dataLoaded = false; private string LocalNet = "10.74"; #endregion Private Fields #region Private Properties private string baseUrl { get => configuration.GetValue("OptConf:BaseUrl") ?? "~/"; } private bool DipOk { get => (currUser != null && currUser.IdxDip > 0); } [Inject] private IHttpContextAccessor httpContextAccessor { get; set; } = null!; /// /// Verifica se IP sia locale /// private bool isIpLocal { get { bool answ = false; if (!string.IsNullOrEmpty(currIpv4)) { answ = currIpv4.Contains(LocalNet); } return answ; } } private bool userOk { get; set; } = false; #endregion Private Properties #region Private Methods private async Task checkOtherData() { currIpv4 = await MService.getDevIpAsync(); if (string.IsNullOrEmpty(currIpv4)) { // ricalcolo e salvo... if (httpContextAccessor.HttpContext != null) { var remoteIp = $"{httpContextAccessor.HttpContext.Connection?.RemoteIpAddress}"; // provo a recuperare ipV4... currIpv4 = IpUtils.getLocalIpv4(remoteIp); await MService.setDevIpv4Async(currIpv4); } } } /// /// Verifica dati utente e pagina /// private async Task checkUser() { dataLoaded = false; // se idxDip NON valido if (!DipOk) { await tryDeviceLogin(); // verifico se ho dipendente... if (currUser != null && currUser.IdxDip > 0) { // segnalo update... await setupUserData(); } } else { dataLoaded = true; } } private async Task ReloadData() { var confNet = await CDService.ConfigGetKey("LocalNet"); LocalNet = confNet != null ? confNet.valore : "10"; } private async Task setupUserData() { if (DipOk) { // verifico o calcolo IP... await checkOtherData(); Log.Info($"MainLayout.setupUserData | idxDip: {currUser.IdxDip}"); dataLoaded = true; // segnalo update... await InvokeAsync(StateHasChanged); } } /// /// Prova login da dati Device (in LocalStorage) /// /// private async Task tryDeviceLogin() { // cerco in localstorage il devicesecret string devSecret = await MService.getDevSecretAsync(); await Task.Delay(50); if (!string.IsNullOrEmpty(devSecret)) { // cerco sul DB... var rigaDev = await CDService.DeviceBySecret(devSecret); // se trovato if (rigaDev != null) { // recupero dati dip e inizializzo message service var elencoDip = await CDService.DipendentiGetAll(); if (elencoDip != null) { var rigaDip = elencoDip.FirstOrDefault(x => x.IdxDipendente == rigaDev.IdxDipendente); if (rigaDip != null) { MService.RigaDip = rigaDip; currUser = new UserDTO() { IdxDip = rigaDip.IdxDipendente, Cognome = rigaDip.Cognome ?? "-", Nome = rigaDip.Nome ?? "...", Attivo = rigaDip.Attivo, AuthKey = rigaDip.AuthKey, Cf = rigaDip.Cf, DataNascita = rigaDip.DataNascita, Email = rigaDip.Email, Gruppo = rigaDip.Gruppo, Matricola = rigaDip.Matricola, Dominio = rigaDip.Dominio, Utente = rigaDip.Utente }; await setupUserData(); } } } else { NavMan.NavigateTo($"{baseUrl}RegNewDevice"); } } else { NavMan.NavigateTo($"{baseUrl}RegNewDevice"); } } #endregion Private Methods } }