5d72d21fb1
Update gestione paginazione (visibile solo dove serve) + fix calcolo di alcuni intems male filtrati
366 lines
10 KiB
C#
366 lines
10 KiB
C#
using LiMan.UI.Data;
|
|
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.Extensions.Configuration;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Security.Claims;
|
|
using System.Threading.Tasks;
|
|
using System;
|
|
using LiMan.DB.DBModels;
|
|
using Microsoft.JSInterop;
|
|
using StackExchange.Redis;
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
using System.Data;
|
|
using NLog.LayoutRenderers;
|
|
using NLog.Filters;
|
|
|
|
namespace LiMan.UI.Components
|
|
{
|
|
public partial class UserMan : IDisposable
|
|
{
|
|
#region Public Methods
|
|
|
|
public void Dispose()
|
|
{
|
|
AppMService.EA_SearchUpdated -= AppMService_EA_SearchUpdated;
|
|
}
|
|
|
|
public string ShowRoles(List<AuthClaimModel> ClaimList)
|
|
{
|
|
string answ = "";
|
|
foreach (var item in ClaimList)
|
|
{
|
|
answ += $"{item.RoleNav.Ruolo}, ";
|
|
}
|
|
if (answ.Length > 2)
|
|
{
|
|
answ = answ.Substring(0, answ.Length - 2);
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Protected Properties
|
|
|
|
[Inject]
|
|
protected MessageService AppMService { get; set; } = null!;
|
|
|
|
[Inject]
|
|
protected AuthenticationStateProvider AutheStateProvider { get; set; } = null!;
|
|
|
|
[Inject]
|
|
protected LiManDataService DataService { get; set; } = null!;
|
|
|
|
[Inject]
|
|
protected IJSRuntime JSRuntime { get; set; } = null!;
|
|
|
|
protected int totalCount { get; set; } = 0;
|
|
|
|
protected List<AuthClaimModel> UserClaims { get; set; } = new List<AuthClaimModel>();
|
|
|
|
#endregion Protected Properties
|
|
|
|
#region Protected Methods
|
|
|
|
/// <summary>
|
|
/// Recupera elenco utenti
|
|
/// </summary>
|
|
protected async Task GetUsers()
|
|
{
|
|
// clear any error messages
|
|
strError = "";
|
|
|
|
UsersAll = await DataService.AuthUserAll();
|
|
|
|
// filtro visualizzazione x tipo SE richeisto
|
|
if (FiltUserRole > 0)
|
|
{
|
|
UsersAll = UsersAll.Where(x => x.Claims.Any(c => c.RoleID == FiltUserRole)).ToList();
|
|
}
|
|
// se c'è search,,,
|
|
if (!string.IsNullOrEmpty(currSearch))
|
|
{
|
|
UsersAll = UsersAll
|
|
.Where(x => x.Cognome.Contains(currSearch, StringComparison.CurrentCultureIgnoreCase)
|
|
|| x.Nome.Contains(currSearch, StringComparison.CurrentCultureIgnoreCase)
|
|
|| x.Username.Contains(currSearch, StringComparison.CurrentCultureIgnoreCase))
|
|
.ToList();
|
|
}
|
|
|
|
totalCount = UsersAll.Count;
|
|
|
|
UsersList = UsersAll
|
|
.Skip(numRecord * (currPage - 1)).Take(numRecord)
|
|
.ToList();
|
|
}
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
currSearch = "";
|
|
string rawConf = Configuration["RuntimeOpt:MultiRoleEnab"];
|
|
if (rawConf != null)
|
|
{
|
|
bool.TryParse(rawConf, out MultiRoleEnab);
|
|
}
|
|
AppMService.EA_SearchUpdated += AppMService_EA_SearchUpdated;
|
|
// lettura dati
|
|
await GetUsers();
|
|
await refreshLists();
|
|
await GetUserClaims();
|
|
}
|
|
|
|
protected async Task SetNumRec(int newNum)
|
|
{
|
|
currPage = 1;
|
|
numRecord = newNum;
|
|
currPage = 1;
|
|
await InvokeAsync(ReloadData);
|
|
}
|
|
|
|
protected async Task SetPage(int newNum)
|
|
{
|
|
currPage = newNum;
|
|
EditUser(null);
|
|
await InvokeAsync(ReloadData);
|
|
}
|
|
|
|
protected bool UserHasClaim(string ruolo)
|
|
{
|
|
bool answ = false;
|
|
if (UserClaims != null && UserClaims.Count > 0)
|
|
{
|
|
answ = UserClaims.Where(x => x.RoleNav.Ruolo == ruolo).Any();
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Private Fields
|
|
|
|
private const string ADMIN_ROLE = "Admin";
|
|
|
|
private const string UNDEF_ROLE = "Undef";
|
|
|
|
private string currSearch = "";
|
|
|
|
/// <summary>
|
|
/// User corrente
|
|
/// </summary>
|
|
private AuthUserModel? currUser = null;
|
|
|
|
/// <summary>
|
|
/// Abilitazione gestione ROLES multipli
|
|
/// </summary>
|
|
private bool MultiRoleEnab = true;
|
|
|
|
private string qrCodeVal = "";
|
|
|
|
/// <summary>
|
|
/// Elenco ROLES da mostrare in dropdown durante editing (usare DB?)
|
|
/// </summary>
|
|
private List<AuthRoleModel> RolesList = new List<AuthRoleModel>();
|
|
|
|
// To enable showing the Popup
|
|
private bool ShowPopup = false;
|
|
|
|
// To hold any possible errors
|
|
private string strError = "";
|
|
|
|
private string userName = "";
|
|
|
|
/// <summary>
|
|
/// Collezione utenti (totale)
|
|
/// </summary>
|
|
private List<AuthUserModel> UsersAll = new List<AuthUserModel>();
|
|
|
|
/// <summary>
|
|
/// Collezione utenti
|
|
/// </summary>
|
|
private List<AuthUserModel> UsersList = new List<AuthUserModel>();
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Properties
|
|
|
|
private int _filtUserRole { get; set; } = 0;
|
|
|
|
[Inject]
|
|
private IConfiguration Configuration { get; set; } = null!;
|
|
|
|
/// <summary>
|
|
/// Ruolo di default (User!!!)
|
|
/// </summary>
|
|
private string CurrentUserRole { get; set; } = "User";
|
|
|
|
private int currPage { get; set; } = 1;
|
|
|
|
/// <summary>
|
|
/// Ruoli correnti utente
|
|
/// </summary>
|
|
private string[] CurrUserRoles { get; set; } = new string[] { "User" };
|
|
|
|
/// <summary>
|
|
/// Gestione filtraggio dati
|
|
/// </summary>
|
|
private int 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 NewRoleId { get; set; } = 0;
|
|
|
|
private int numRecord { get; set; } = 10;
|
|
|
|
private List<AuthRoleModel> Roles2Add
|
|
{
|
|
get
|
|
{
|
|
List<AuthRoleModel> currRoles = new List<AuthRoleModel>();
|
|
foreach (var item in RolesList)
|
|
{
|
|
// solo se l'utente corrente lo possiede...
|
|
if (UserClaims.Where(x => x.RoleID == item.RoleID).Any())
|
|
{
|
|
// verifico no ci sia già...
|
|
if (!currUser.Claims.Where(x => x.RoleID == item.RoleID).Any())
|
|
{
|
|
currRoles.Add(item);
|
|
}
|
|
}
|
|
}
|
|
return currRoles;
|
|
}
|
|
}
|
|
|
|
#endregion Private Properties
|
|
|
|
#region Private Methods
|
|
|
|
private async Task AddSelRole()
|
|
{
|
|
if (NewRoleId > 0)
|
|
{
|
|
int userId = currUser.UserID;
|
|
// creo nuovo ruolo...
|
|
AuthClaimModel newClaim = new AuthClaimModel()
|
|
{
|
|
ClaimID = 0,
|
|
UserID = currUser.UserID,
|
|
RoleID = NewRoleId,
|
|
DtIns = DateTime.Now,
|
|
DtMod = DateTime.Now
|
|
};
|
|
await DataService.AuthClaimUpsert(newClaim);
|
|
// Refresh Users
|
|
await ReloadData();
|
|
currUser = UsersAll.Find(x => x.UserID == userId);
|
|
}
|
|
}
|
|
|
|
private async void AppMService_EA_SearchUpdated()
|
|
{
|
|
currSearch = AppMService.SearchVal;
|
|
await ReloadData();
|
|
}
|
|
|
|
private async Task ClosePopup()
|
|
{
|
|
// Close the Popup
|
|
ShowPopup = false;
|
|
// Refresh Users
|
|
await GetUsers();
|
|
}
|
|
|
|
private async Task DeleteRole(AuthClaimModel currClaim)
|
|
{
|
|
if (!await JSRuntime.InvokeAsync<bool>("confirm", "Sicuro di voler eliminare il Ruolo utente selezionato?"))
|
|
return;
|
|
|
|
int userId = currUser.UserID;
|
|
await DataService.AuthClaimRemove(currClaim);
|
|
// Refresh Users
|
|
await ReloadData();
|
|
currUser = UsersAll.Find(x => x.UserID == userId);
|
|
}
|
|
|
|
private async Task DeleteUser(AuthUserModel _currRec)
|
|
{
|
|
if (!await JSRuntime.InvokeAsync<bool>("confirm", "Sicuro di voler eliminare i Ruoli per l'utente selezionato? Il record rimarrà ma inattivo."))
|
|
return;
|
|
|
|
await DataService.AuthRoleResetUser(_currRec.UserID);
|
|
|
|
// Close the Popup
|
|
ShowPopup = false;
|
|
// Refresh Users
|
|
await ReloadData();
|
|
}
|
|
|
|
private void EditUser(AuthUserModel _currRec)
|
|
{
|
|
// Open the Popup
|
|
currUser = _currRec;
|
|
NewRoleId = 0;
|
|
ShowPopup = true;
|
|
}
|
|
|
|
private async Task GetUserClaims()
|
|
{
|
|
var authState = await AutheStateProvider.GetAuthenticationStateAsync();
|
|
var user = authState.User;
|
|
|
|
if (user.Identity.IsAuthenticated)
|
|
{
|
|
userName = $"{user.Identity.Name}";
|
|
}
|
|
UserClaims = await DataService.AuthClaimByUserName(userName);
|
|
}
|
|
|
|
private async Task refreshLists()
|
|
{
|
|
RolesList = await DataService.AuthRolesGetAll();
|
|
}
|
|
|
|
private async Task ReloadData()
|
|
{
|
|
isLoading = true;
|
|
await GetUsers();
|
|
isLoading = false;
|
|
}
|
|
|
|
private async Task SaveUser()
|
|
{
|
|
try
|
|
{
|
|
// chiamo update!
|
|
await DataService.AuthUserUpsert(currUser);
|
|
ShowPopup = false;
|
|
// Refresh Users
|
|
await GetUsers();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
strError = ex.GetBaseException().Message;
|
|
}
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |