Files
gpw_next/GPW.CORE.SMART/Components/Compo/CmpTop.razor.cs
T
Samuele Locatelli 2e91431129 Gestione base URL
2024-08-23 12:26:50 +02:00

358 lines
10 KiB
C#

using EgwCoreLib.Razor.Data;
using GPW.CORE.Data.DbModels;
using GPW.CORE.Smart.Data;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
namespace GPW.CORE.Smart.Components.Compo
{
public partial class CmpTop : IDisposable
{
#region Public Methods
public void Dispose()
{
MService.EA_DipUpdated -= MService_EA_DipUpdated;
GC.Collect();
}
#endregion Public Methods
#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!;
protected string Cognome { get; set; } = "";
[Inject]
protected IConfiguration configuration { get; set; } = null!;
protected string currDip
{
get => $"{Cognome} {Nome} [{idxDipendente}]";
}
protected string homeCss
{
get => isIpLocal ? "text-light" : "text-danger";
}
protected string homeMessage
{
get => isIpLocal ? "INT" : "EXT";
}
protected int idxDipendente { get; set; } = 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 IJSRuntime JSRuntime { get; set; } = null!;
[Inject]
protected LicenseService LicServ { get; set; } = null!;
[Inject]
protected MessageService MService { get; set; } = null!;
[Inject]
protected NavigationManager navManager { get; set; } = null!;
protected string Nome { get; set; } = "";
#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()
{
MService.EA_DipUpdated += MService_EA_DipUpdated;
// lettura dati base
await ReloadData();
// verifica attivazione
await VerifyActiv();
}
protected async Task ReturnHome()
{
// svuoto cache
await CDService.FlushRedisCache();
// ritorno in home
navManager.NavigateTo($"{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 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()
{
// se idxDip NON valido
if (MService.IdxDipendente <= 0)
{
await tryDeviceLogin();
// verifico se ho dipendente...
if (MService.IdxDipendente > 0)
{
// verifico o calcolo IP...
await checkOtherData();
}
}
else
{
if (idxDipendente <= 0)
{
await setupUserData();
}
}
}
private async void MService_EA_DipUpdated()
{
// lettura dati base
await ReloadData();
// verifica attivazione
await VerifyActiv();
}
private async Task ReloadData()
{
var confNet = await CDService.ConfigGetKey("LocalNet");
LocalNet = confNet != null ? confNet.valore : "10";
}
private async Task setupUserData()
{
if (MService.RigaDip != null && MService.IdxDipendente > 0)
{
idxDipendente = MService.IdxDipendente;
Cognome = MService.RigaDip?.Cognome ?? "ND";
Nome = MService.RigaDip?.Nome ?? "-";
// verifico o calcolo IP...
await checkOtherData();
// 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);
MService.RigaDip = rigaDip;
await setupUserData();
MService.RigaDip = rigaDip;
}
}
else
{
navManager.NavigateTo($"{baseUrl}/RegNewDevice");
}
}
else
{
navManager.NavigateTo($"{baseUrl}/RegNewDevice");
}
}
#endregion Private Methods
}
}