Files
gpw_next/GPW.CORE.ADM/Components/Compo/LoginDisplay.razor.cs
T
2024-10-12 16:08:01 +02:00

393 lines
12 KiB
C#

using GPW.CORE.Data;
using GPW.CORE.Data.DbModels;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Routing;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Identity;
using GPW.CORE.Data.Services;
namespace GPW.CORE.ADM.Components.Compo
{
public partial class LoginDisplay : IDisposable
{
#region Public Methods
public void Dispose()
{
GDataServ.mPipeTimb.EA_NewMessage -= MPipeTimb_EA_NewMessage;
GDataServ.mPipeRich.EA_NewMessage -= MPipeRich_EA_NewMessage;
AppMServ.EA_HideSearch -= AppMServ_EA_HideSearch;
AppMServ.EA_HideSearch -= AppMServ_EA_ShowSearch;
AppMServ.EA_PageUpdated -= AppMServ_EA_PageUpdated;
}
#endregion Public Methods
#region Protected Properties
[Inject]
protected MessageService AppMServ { get; set; } = null!;
[Inject]
protected NavigationManager NavMan { get; set; } = null!;
[Inject]
protected AuthenticationStateProvider AuthenticationStateProvider { get; set; } = null!;
[Inject]
protected AppAuthService AuthServ { get; set; } = null!;
[Inject]
protected AppAuthService AuthService { get; set; } = null!;
[Inject]
protected IConfiguration config { get; set; } = null!;
protected string cssActive { get => AppMServ.IsActive ? "text-success" : "text-danger"; }
protected string cssLicOk { get => AppMServ.PayloadOk ? "text-success" : "text-danger"; }
[Inject]
protected GpwDataService GDataServ { get; set; } = null!;
[Inject]
protected LicenseService LicServ { get; set; } = null!;
protected List<string> ListLingue
{
get => AuthServ.Lingue;
}
protected string searchVal
{
get => AppMServ.SearchVal;
set => AppMServ.SearchVal = value;
}
protected string UserLang
{
get => AppMServ.UserLang;
set => AppMServ.UserLang = value;
}
#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();
}
}
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 = GDataServ.CodApp;
// effettuo lettura dati preliminari da AKV (eventualmente in cache...)
List<AnagKeyValueModel>? rawAkvList = await GDataServ.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 = AppMServ.IsActive;
if (answ)
{
var rawActList = LicServ.ActivList;
// recupero hash utente
string hashImpiego = AppMServ.HashDip;
// confronto con elenco attivazioni dello shared service...
var foundRec = rawActList.Where(x => x.CodImpiego == hashImpiego);
answ = foundRec.Count() > 0;
// salvo status payloadOk
AppMServ.PayloadOk = answ;
}
await Task.Delay(1);
return answ;
}
protected async Task DoReloadUtente()
{
AppMServ.RigaDip = null;
await Task.Delay(1);
await LoadUtente();
// recupero dati conf lingua...
await ReloadLang();
// verifico attivazione dipendente...
await checkUserLicense();
}
protected async Task DoVerifyActiv()
{
AppMServ.PayloadOk = false;
// svuoto oggetto activList...
LicServ.ActivList = new List<LiManObj.AttivazioneDTO>();
await LicServ.resetActivList();
await Task.Delay(50);
await VerifyActiv();
}
protected async Task LoadUtente()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
// verifico se ho utente loggato...
if (user.Identity != null && user.Identity.IsAuthenticated)
{
userName = $"{user.Identity.Name}";
// cerco su DB
DipendentiModel? rigaDip = ListaDip.Where(x => $"{x.Dominio.Trim()}\\{x.Utente.Trim()}".ToLower() == userName.Trim().ToLower()).FirstOrDefault();
if (rigaDip != null)
{
// salvo riga info dipendente
AppMServ.RigaDip = rigaDip;
userName = $"{rigaDip.Cognome} {rigaDip.Nome}";
idxDipendente = rigaDip.IdxDipendente;
// leggo e salvo in MsgService il menù utente...
var listMenu = await AuthService.MenuUtente(rigaDip.Utente, UserLang);
AppMServ.ListMenu = listMenu;
}
}
else
{
userName = "N.A.";
}
}
protected override async Task OnInitializedAsync()
{
// aggiungo gestione notifica messaggi timbrature...
GDataServ.mPipeTimb.EA_NewMessage += MPipeTimb_EA_NewMessage;
GDataServ.mPipeRich.EA_NewMessage += MPipeRich_EA_NewMessage;
AppMServ.EA_HideSearch += AppMServ_EA_HideSearch;
AppMServ.EA_ShowSearch += AppMServ_EA_ShowSearch;
AppMServ.EA_PageUpdated += AppMServ_EA_PageUpdated;
// leggo lingue...
UserLang = await AppMServ.UserLangGet();
// leggo lista dip
ListaDip = await GDataServ.DipendentiGetAll();
numTot = ListaDip.Count(x => x.Attivo == true);
//effettuo eventuale recupero dati dipendente
if (AppMServ.RigaDip == null)
{
await LoadUtente();
}
// verifica attivazione
await VerifyActiv();
// verifica rpesenza personale
await VerifPresenze(false);
}
protected void resetSearch()
{
searchVal = "";
}
protected async Task VerifyActiv()
{
// preliminarmente verifica shared info
await checkSharedInfo();
// refresh attivazioni se necessario
await checkActivations();
// verifico e salvo dati attivazione dipendente...
await checkUserLicense();
// verifica admin
checkAdmin();
}
#endregion Protected Methods
#region Private Fields
private int idxDipendente = 0;
private bool isAdmin = false;
private List<DipendentiModel> ListaDip = new List<DipendentiModel>();
private List<StatsDayPresModel> ListaPres = new List<StatsDayPresModel>();
private List<string> ListIdxAdmin = new List<string>();
private int numPres = 0;
private int numTot = 0;
private string PageIcon = "";
private string PageTitle = "";
private bool showSearch = false;
private string userName = "";
#endregion Private Fields
#region Private Properties
private string lblDisplay { get; set; } = "";
[Inject]
private NavigationManager NavManager { get; set; } = null!;
#endregion Private Properties
#region Private Methods
private void AppMServ_EA_HideSearch()
{
showSearch = false;
}
private void AppMServ_EA_PageUpdated()
{
PageTitle = AppMServ.PageName;
PageIcon = AppMServ.PageIcon;
InvokeAsync(StateHasChanged);
}
private void AppMServ_EA_ShowSearch()
{
showSearch = true;
}
private void checkAdmin()
{
// leggo elenco admin
string rawData = config.GetValue<string>("ServerConf:AdmList") ?? "";
if (!string.IsNullOrEmpty(rawData))
{
ListIdxAdmin = rawData.Split(',').ToList();
}
isAdmin = ListIdxAdmin.Contains($"{idxDipendente}");
}
private async void MPipeRich_EA_NewMessage(object? sender, EventArgs e)
{
await processMessage(e);
}
private async void MPipeTimb_EA_NewMessage(object? sender, EventArgs e)
{
await processMessage(e);
}
private async Task processMessage(EventArgs e)
{
PubSubEventArgs currArgs = (PubSubEventArgs)e;
// conversione on-the-fly List<string> --> allarmi
if (!string.IsNullOrEmpty(currArgs.newMessage))
{
try
{
TimbratureModel? timbData = JsonConvert.DeserializeObject<TimbratureModel>(currArgs.newMessage);
if (timbData != null)
{
// solo se admin
if (isAdmin)
{
updateDisplay(timbData);
await InvokeAsync(StateHasChanged);
await ResetMessage(6);
}
}
}
catch
{ }
}
}
private async Task ReloadLang()
{
UserLang = await AppMServ.UserLangGet();
}
/// <summary>
/// Chiama reset messaggio dopo un periodo di sec richiesto...
/// </summary>
/// <returns></returns>
private async Task ResetMessage(int numSec)
{
// chiudo visualizzazione stato update
await Task.Delay(numSec * 1000);
lblDisplay = "";
await VerifPresenze(true);
await InvokeAsync(StateHasChanged);
}
private async void SaveLang()
{
await AppMServ.UserLangSet(UserLang);
// vado a pagina reload...
NavMan.NavigateTo("ForceReset", true);
}
private void updateDisplay(TimbratureModel timbData)
{
string tipo = timbData.Entrata ?? true ? "IN" : "OUT";
string dipData = $"Dip: {timbData.IdxDipendente}";
//cerco dip...
var currDip = ListaDip.Find(x => x.IdxDipendente == timbData.IdxDipendente);
if (currDip != null)
{
dipData = $"{currDip.Cognome} {currDip.Nome} ({timbData.IdxDipendente})";
}
lblDisplay = $"{tipo} | {dipData} | {timbData.DataOra:HH:mm:ss}";
}
private async Task VerifPresenze(bool resetCache)
{
if (resetCache)
{
await GDataServ.FlushRedisCache();
}
ListaPres = await GDataServ.PresenzeDay(DateTime.Now);
numPres = ListaPres.Sum(x => x.IsPresent);
}
#endregion Private Methods
}
}