Files
magman/MagMan.UI/Components/UserMan.razor.cs
T
2024-01-31 13:02:39 +01:00

346 lines
11 KiB
C#

using MagMan.Core.Services;
using MagMan.Data.Admin;
using MagMan.Data.Admin.Services;
using MagMan.Data.Tenant.DbModels;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Identity;
using Microsoft.Data.SqlClient;
using Microsoft.JSInterop;
namespace MagMan.UI.Components
{
public partial class UserMan : IDisposable
{
#region Public Methods
public List<string> ConvertClaim(List<System.Security.Claims.Claim> ClaimList)
{
List<string> answ = new List<string>();
#if false
var pUpd = Task.Run(async () =>
{
await refreshLists();
});
pUpd.Wait();
// ciclo sui claims
foreach (var claim in ClaimList)
{
string claimStr = claim.ToString();
// verifico che in OGNI claim ci siaun valore ammissibile...
if (claimStr.Contains(":"))
{
// splitto chiave/valore
var splitClaim = claimStr.Split(':');
switch (splitClaim[0])
{
case "PlantId":
if (plantList != null)
{
var plantRec = plantList
.Where(x => x.PlantId.ToString() == splitClaim[1].Trim())
.FirstOrDefault();
if (plantRec != null)
{
answ.Add($"{splitClaim[0]}: {plantRec.PlantDesc}");
}
else
{
answ.Add($"{claim}");
}
}
break;
case "SupplierId":
if (suppList != null)
{
var plantRec = suppList
.Where(x => x.SupplierId.ToString() == splitClaim[1].Trim())
.FirstOrDefault();
if (plantRec != null)
{
answ.Add($"{splitClaim[0]}: {plantRec.SupplierDesc}");
}
else
{
answ.Add($"{claim}");
}
}
break;
case "TransporterId":
if (transpList != null)
{
var plantRec = transpList
.Where(x => x.TransporterId.ToString() == splitClaim[1].Trim())
.FirstOrDefault();
if (plantRec != null)
{
answ.Add($"{splitClaim[0]}: {plantRec.TransporterDesc}");
}
else
{
answ.Add($"{claim}");
}
}
break;
case "None":
default:
break;
}
}
}
#endif
return answ;
}
public void Dispose()
{
AppMService.EA_SearchUpdated -= AppMService_EA_SearchUpdated;
}
/// <summary>
/// Recupera elenco utenti
/// </summary>
public async Task GetUsers()
{
// clear any error messages
strError = "";
UsersAll = await MTService.UserDataGetFilt(currSearch);
// filtro visualizzazione x tipo SE richeisto
if (FiltUserRole != "0")
{
UsersAll = UsersAll.Where(x => x.Roles.Contains(FiltUserRole)).ToList();
}
totalCount = UsersAll.Count;
UsersList = UsersAll
.Skip(numRecord * (currPage - 1)).Take(numRecord)
.ToList();
}
public string ShowClaims(List<System.Security.Claims.Claim> ClaimList)
{
//string answ = string.Join(",", ClaimList);
string answ = string.Join(",", ConvertClaim(ClaimList));
return answ;
}
public string ShowRoles(List<string> RoleList)
{
string answ = string.Join(",", RoleList);
return answ;
}
#endregion Public Methods
#region Protected Properties
[Inject]
protected MessageService AppMService { get; set; } = null!;
[Inject]
protected MTAdminService MTService { get; set; } = null!;
protected int totalCount { get; set; } = 0;
#endregion Protected Properties
#region Protected Methods
protected void DoSelect(UserData? selItem)
{
#if false
if (selItem != null)
{
ProjDbId = selItem.ProjDbId;
}
else
{
ProjDbId = 0;
}
E_ProjSel.InvokeAsync(selItem);
#endif
}
protected override void OnInitialized()
{
currSearch = "";
AppMService.EA_SearchUpdated += AppMService_EA_SearchUpdated;
}
protected void SetNumRec(int newNum)
{
numRecord = newNum;
currPage = 1;
InvokeAsync(ReloadData);
}
protected void SetPage(int newNum)
{
currPage = newNum;
DoSelect(null);
InvokeAsync(ReloadData);
}
#endregion Protected Methods
#region Private Fields
private string currSearch = "";
/// <summary>
/// Elenco ROLES da mostrare in dropdown durante editing (1 solo? usare DB?!?)
/// </summary>
private List<string> RolesList = new List<string>() { "Undef", "User", "Admin", "SuperAdmin" };
// To enable showing the Popup
private bool ShowPopup = false;
// To hold any possible errors
private string strError = "";
/// <summary>
/// Collezione utenti (totale)
/// </summary>
private List<UserData> UsersAll = new List<UserData>();
/// <summary>
/// Collezione utenti
/// </summary>
private List<UserData> UsersList = new List<UserData>();
#endregion Private Fields
#region Private Properties
private string _filtUserRole { get; set; } = "0";
private int currPage { get; set; } = 1;
/// <summary>
/// Gestione filtraggio dati
/// </summary>
private string FiltUserRole
{
get
{
return _filtUserRole;
}
set
{
if (_filtUserRole != value)
{
_filtUserRole = value;
var pUpd = Task.Run(async () => await ReloadData());
pUpd.Wait();
}
}
}
private bool isLoading { get; set; } = false;
private int numRecord { get; set; } = 10;
#endregion Private Properties
#region Private Methods
private async void AppMService_EA_SearchUpdated()
{
currSearch = AppMService.SearchVal;
await ReloadData();
}
private async Task DeleteUser(IdentityUser _IdentityUser)
{
#if false
// Close the Popup
ShowPopup = false;
if (!await JSRuntime.InvokeAsync<bool>("confirm", "Sicuro di voler eliminare l'utente selezionato??"))
return;
// Get the user
var user = await _UserManager.FindByIdAsync(_IdentityUser.Id);
if (user != null)
{
// Delete the user
await _UserManager.DeleteAsync(user);
}
#endif
// Refresh Users
await GetUsers();
}
private void EditUser(IdentityUser _IdentityUser)
{
#if false
// selezione come current user
objUser = _IdentityUser;
var pUpd = Task.Run(async () =>
{
// Get the user
var user = await _UserManager.FindByIdAsync(objUser.Id);
if (user != null)
{
// salvo role
var UserRoles = await _UserManager.GetRolesAsync(user);
if (UserRoles != null)
{
CurrentUserRole = UserRoles.FirstOrDefault();
}
else
{
CurrentUserRole = UNDEF_ROLE;
}
// salvo claim
var UserClaims = await _UserManager.GetClaimsAsync(user);
if (UserClaims != null && UserClaims.Count > 0)
{
var CurrentUserClaim = UserClaims.FirstOrDefault();
CurrentUserClaimType = CurrentUserClaim.Type;
CurrentUserClaimVal = CurrentUserClaim.Value;
}
else
{
CurrentUserClaimType = STD_CLAIM;
CurrentUserClaimVal = "0";
}
string baseUrl = Configuration["BaseUrl"];
string baseAppPath = Configuration["BaseAppPath"];
string redirPage = Configuration["QrRedirPage"];
if (!string.IsNullOrEmpty(baseAppPath))
{
if (baseUrl.EndsWith("/"))
{
baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
}
baseUrl = $"{baseUrl}{baseAppPath}";
}
qrCodeVal = $"{baseUrl}Identity/Account/LogIn?uid={user.Id}&uem={user.Email}&pag={redirPage}";
}
});
pUpd.Wait();
// Open the Popup
ShowPopup = true;
#endif
}
private async Task ReloadData()
{
isLoading = true;
await GetUsers();
isLoading = false;
}
#endregion Private Methods
}
}