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(); } } #endregion Protected Methods /// /// Valore CustomerID filtrato da claim /// protected int ClaimCustomerId = -1; #region Private Fields private int customerID = -1; private List? CustomersList = null; #endregion Private Fields [Inject] protected AuthenticationStateProvider AuthenticationStateProvider { get; set; } = null!; /// /// Recupero Claims dell'utente... /// /// https://docs.microsoft.com/it-it/aspnet/core/blazor/security/?view=aspnetcore-6.0 /// /// 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; } } } }