Files
magman/MagMan.UI/Components/CmpSelCliente.razor.cs
2024-02-05 15:02:19 +01:00

137 lines
4.2 KiB
C#

using k8s.KubeConfigModels;
using MagMan.Core.Services;
using MagMan.Data.Admin.DbModels;
using MagMan.Data.Admin.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
namespace MagMan.UI.Components
{
public partial class CmpSelCliente
{
#region Protected Properties
[Inject]
protected MessageService AppMService { get; set; } = null!;
protected int CustomerID
{
get => customerID;
set
{
if (customerID != value)
{
customerID = value;
InvokeAsync(() => AppMService.ClientIdSet(value));
// gestione KeyNum
if (CustomersList != null)
{
var currRec = CustomersList.Find(x => x.CustomerID == customerID);
if (currRec != null)
{
int mKeyNum = currRec.MainKey;
InvokeAsync(() => AppMService.KeyNumSet(mKeyNum));
AppMService.KeyNum = mKeyNum;
}
}
AppMService.CustomerID = value;
InvokeAsync(StateHasChanged);
}
}
}
[Inject]
protected MTAdminService MTService { get; set; } = null!;
protected bool selDisabled = false;
#endregion Protected Properties
#region Protected Methods
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender || CustomerID < 0)
{
CustomerID = await AppMService.ClientIdGet();
}
// verifico sia valido...
bool hasCust = CustomersList != null && CustomersList.Where(x => x.CustomerID == CustomerID).Any();
if (!hasCust && ClaimCustomerId > 0)
{
CustomerID = ClaimCustomerId;
}
}
protected override async Task OnInitializedAsync()
{
await ReloadData();
}
protected async Task ReloadData()
{
await GetClaimsData();
var rawList = await MTService.CustomerGetAll();
// se ho un plantId valido --> altrimenti non abilitato
if (ClaimCustomerId == 0)
{
CustomersList = rawList;
}
else if (ClaimCustomerId > 0)
{
CustomersList = rawList.Where(x => x.CustomerID == ClaimCustomerId).ToList();
}
else
{
CustomersList = new List<CustomerModel>();
}
}
#endregion Protected Methods
/// <summary>
/// Valore CustomerID filtrato da claim
/// </summary>
protected int ClaimCustomerId = -1;
#region Private Fields
private int customerID = -1;
private List<CustomerModel>? CustomersList = null;
#endregion Private Fields
[Inject]
protected AuthenticationStateProvider AuthenticationStateProvider { get; set; } = null!;
/// <summary>
/// Recupero Claims dell'utente...
///
/// https://docs.microsoft.com/it-it/aspnet/core/blazor/security/?view=aspnetcore-6.0
/// </summary>
/// <returns></returns>
private async Task GetClaimsData()
{
// recupero auth
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
// se autenticato --> controllo i claims
if (user.Identity != null && user.Identity.IsAuthenticated)
{
// cerco il claim PlantId...
var custClaim = user.FindFirst(c => c.Type == "CustomerID")?.Value;
int.TryParse(custClaim, out ClaimCustomerId);
// verifico se sia SuperAdmin --> disabilito selezione altrimenti
selDisabled = !user.IsInRole("SuperAdmin");
}
else
{
ClaimCustomerId = -1;
}
}
}
}