Files
gpw_next/GPW.CORE.Smart8/Components/Layout/MainLayout.razor.cs
T
Samuele Locatelli 15be702f29 Pulizia + publish
2025-03-21 19:31:25 +01:00

359 lines
11 KiB
C#

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 Fields
protected string currIpv4 = "";
#endregion Protected Fields
#region Protected Properties
protected string baseUrl
{
get => configuration.GetValue<string>("OptConf:BaseUrl") ?? "~/";
}
[Inject]
protected CoreSmartDataService CDService { get; set; } = null!;
[Inject]
protected IConfiguration configuration { get; set; } = null!;
protected bool DipOk
{
get => (currUser != null && currUser.IdxDip > 0);
}
/// <summary>
/// Verifica se IP sia locale
/// </summary>
protected bool isIpLocal
{
get
{
bool answ = false;
if (!string.IsNullOrEmpty(currIpv4))
{
answ = currIpv4.Contains(LocalNet);
}
return answ;
}
}
[Inject]
protected LicenseService LicServ { get; set; } = null!;
[Inject]
protected MessageService MService { get; set; } = null!;
[Inject]
protected NavigationManager navManager { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
/// <summary>
/// Verifica informazioni condivise applicazione
/// </summary>
/// <returns></returns>
protected async Task<bool> 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;
}
/// <summary>
/// Verifica informazioni condivise applicazione
/// </summary>
/// <returns></returns>
protected async Task<bool> 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<AnagKeyValueModel>? rawAkvList = await CDService.AKVList();
if (rawAkvList != null)
{
LicServ.AKVList = rawAkvList;
allOk = LicServ.InitAkv();
}
}
await Task.Delay(1);
return allOk;
}
/// <summary>
/// Verifica info specifiche utente
/// </summary>
/// <returns></returns>
protected async Task<bool> 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 = navManager.ToBaseRelativePath(navManager.Uri);
// tengo solo parte finale...
if (nextPage.Contains("/"))
{
nextPage = nextPage.Substring(nextPage.LastIndexOf("/") + 1);
}
await Task.Delay(1);
await DoVerifyActiv();
navManager.NavigateTo($"{baseUrl}ForceReset?nextPage={nextPage}", true);
}
protected override async Task OnAfterRenderAsync(bool firstrender)
{
await Task.Delay(1);
if (firstrender)
{
if (!navManager.Uri.Contains("jumper"))
{
await checkUser();
// verifico attivazione dipendente...
await checkUserLicense();
}
}
}
protected override async Task OnInitializedAsync()
{
string baseUrl = configuration.GetValue<string>("OptConf:BaseUrl") ?? "~/";
var versione = typeof(Program).Assembly.GetName().Version;
currConfig = new ConfigDTO()
{
BaseUrl = baseUrl,
Versione = $"{versione}"
};
await ReloadData();
base.OnInitialized();
Log.Info($"MainLayout.OnInitialized | url: {baseUrl} | v:{versione}");
}
protected async Task ReturnHome()
{
// svuoto cache
await CDService.FlushRedisCache();
// ritorno in home
navManager.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();
/// <summary>
/// Info utente corrente x display
/// </summary>
private UserDTO? currUser = null;
private bool dataLoaded = false;
private string LocalNet = "10.74";
#endregion Private Fields
#region Private Properties
[Inject]
private IHttpContextAccessor httpContextAccessor { get; set; } = null!;
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);
}
}
}
/// <summary>
/// Verifica dati utente e pagina
/// </summary>
private async Task checkUser()
{
dataLoaded = false;
// se idxDip NON valido
if (!DipOk || MService.IdxDipendente <= 0)
{
await tryDeviceLogin();
// verifico se ho dipendente...
if (currUser != null && currUser.IdxDip > 0)
//if (currUser!=null && currUser.IdxDip > 0 || MService.IdxDipendente > 0)
{
// segnalo update...
await setupUserData();
}
}
#if false
else
{
if (!DipOk)
{
await setupUserData();
}
}
#endif
}
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);
}
}
/// <summary>
/// Prova login da dati Device (in LocalStorage)
/// </summary>
/// <returns></returns>
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
{
navManager.NavigateTo($"{baseUrl}RegNewDevice");
}
}
else
{
navManager.NavigateTo($"{baseUrl}RegNewDevice");
}
}
#endregion Private Methods
}
}