Files
2025-04-17 08:31:03 +02:00

152 lines
4.4 KiB
C#

using Amazon.Runtime.Internal.Endpoints.StandardLibrary;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.JSInterop;
using MP.AppAuth.Services;
using MP.SPEC.Data;
using MP.SPEC.Extensions;
using System;
namespace MP.SPEC.Components
{
public partial class CmpTop : IDisposable
{
#region Public Methods
public void Dispose()
{
MService.EA_PageUpdated -= OnPageUpdate;
NavManager.LocationChanged -= NavManager_LocationChanged;
}
public void OnPageUpdate()
{
PageName = MService.PageName;
PageIcon = MService.PageIcon;
InvokeAsync(() =>
{
StateHasChanged();
});
}
#endregion Public Methods
#region Protected Properties
[Inject]
protected AppAuthService AAService { get; set; } = null!;
[Inject]
protected AuthenticationStateProvider AuthStateProv { get; set; } = null!;
[Inject]
protected IJSRuntime JSRuntime { get; set; } = null!;
[Inject]
protected MpDataService MDService { get; set; } = null!;
[Inject]
protected MsgServiceSpec MService { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
protected async Task FlushCache()
{
await MDService.FlushRedisCache();
await ForceReload();
// rimando a pagina corrente
NavManager.NavigateTo(NavManager.Uri, true);
}
protected override async Task OnInitializedAsync()
{
await ForceReload();
MService.EA_PageUpdated += OnPageUpdate;
NavManager.LocationChanged += NavManager_LocationChanged;
SetPageData();
}
#endregion Protected Methods
#region Private Fields
private string PageIcon = "";
private string PageName = "";
private string userName = "";
#endregion Private Fields
#region Private Properties
[Inject]
private NavigationManager NavManager { get; set; } = null!;
#endregion Private Properties
#region Private Methods
private async Task ForceReload()
{
var authState = await AuthStateProv.GetAuthenticationStateAsync();
var user = authState.User;
if (user.Identity != null && user.Identity.IsAuthenticated)
{
userName = $"{user.Identity.Name}";
// salvo username in msgService
var userData = userName.Split(@"\");
if (userData.Length > 1)
{
MService.DomainName = userData[0];
MService.UserName = userData[1];
// recupero diritti e salvo...
var UserRightList = await AAService.DirittiGetByUser(MService.UserName);
if (UserRightList != null)
{
MService.UserRight = UserRightList.Select(x => x.Funzione).ToList();
}
}
}
else
{
userName = "N.A.";
}
}
private void NavManager_LocationChanged(object? sender, Microsoft.AspNetCore.Components.Routing.LocationChangedEventArgs e)
{
SetPageData();
}
private void SetPageData()
{
// update testo pagina da URI
string pageUri = NavManager.Page().ToLower();
// se home/index/vuoto... home!
if (string.IsNullOrEmpty(pageUri) || pageUri == "index" || pageUri == "home")
{
MService.PageIcon = "fa-solid fa-house";
MService.PageName = "Home";
}
else
{
// verifica pagina x permessi
if (MService.ElencoLink != null)
{
var recLink = MService.ElencoLink.FirstOrDefault(x => x.NavigateUrl.ToLower() == pageUri);
if (recLink != null)
{
MService.PageIcon = recLink.Icona;
MService.PageName = recLink.Testo;
}
}
}
}
#endregion Private Methods
}
}