6e0579e315
- ricostruzione servizi lettura dati utente x login
667 lines
22 KiB
C#
667 lines
22 KiB
C#
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using StackExchange.Redis;
|
|
using System.Diagnostics;
|
|
using System.Security.Claims;
|
|
using WebDoorCreator.Core;
|
|
using WebDoorCreator.Data.User;
|
|
|
|
namespace WebDoorCreator.UI.Data
|
|
{
|
|
public class WDCUserService
|
|
{
|
|
#region Public Constructors
|
|
|
|
public WDCUserService(IConnectionMultiplexer redisConnMult, UserManager<IdentityUser> userManager)
|
|
{
|
|
_userManager = userManager;
|
|
// Conf cache
|
|
redisConn = redisConnMult;
|
|
redisDb = this.redisConn.GetDatabase();
|
|
|
|
// json serializer... FIX errore loop circolare https://www.ryadel.com/en/jsonserializationexception-self-referencing-loop-detected-error-fix-entity-framework-asp-net-core/
|
|
JSSettings = new JsonSerializerSettings()
|
|
{
|
|
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
|
|
};
|
|
// chiudo log
|
|
Log.Info("Avviata classe WDCUserService");
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Events
|
|
|
|
public event Action EA_CurrLanguage = null!;
|
|
|
|
public event Action EA_UserClaims = null!;
|
|
|
|
public event Action EA_UserCurrCompany = null!;
|
|
|
|
public event Action EA_UserId = null!;
|
|
|
|
public event Action EA_UserRole = null!;
|
|
|
|
#endregion Public Events
|
|
|
|
#region Public Properties
|
|
|
|
public string? currLanguage
|
|
{
|
|
get => _currLanguage;
|
|
set
|
|
{
|
|
if (_currLanguage != value)
|
|
{
|
|
_currLanguage = value;
|
|
reportCurrentLanguage();
|
|
}
|
|
}
|
|
}
|
|
|
|
public List<string>? userClaims
|
|
{
|
|
get => _userClaims;
|
|
set
|
|
{
|
|
if (_userClaims != value)
|
|
{
|
|
_userClaims = value;
|
|
reportUserClaims();
|
|
}
|
|
}
|
|
}
|
|
|
|
public int userCurrComp
|
|
{
|
|
get => _userCurrComp;
|
|
set
|
|
{
|
|
if (_userCurrComp != value)
|
|
{
|
|
_userCurrComp = value;
|
|
reportUserCurrCompany();
|
|
}
|
|
}
|
|
}
|
|
|
|
public string userId
|
|
{
|
|
get => _userId;
|
|
set
|
|
{
|
|
if (_userId != value)
|
|
{
|
|
_userId = value;
|
|
reportUserId();
|
|
}
|
|
}
|
|
}
|
|
|
|
public Dictionary<string, string> UserPref { get; set; } = new Dictionary<string, string>();
|
|
|
|
public string userRole
|
|
{
|
|
get => _userRole;
|
|
set
|
|
{
|
|
if (_userRole != value)
|
|
{
|
|
_userRole = value;
|
|
reportUserRole();
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Effettua reset dati utente...
|
|
/// </summary>
|
|
/// <param name="resetAll">Indica reset completo (anche dati elenco di base)</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> ResetUserDataCache(bool resetAll)
|
|
{
|
|
bool fatto = false;
|
|
RedisValue pattern = new RedisValue($"{Constants.rKeyUsersAll}");
|
|
if (resetAll)
|
|
{
|
|
fatto = await ExecFlushRedisPattern(pattern);
|
|
}
|
|
// ora resetto il resto
|
|
pattern = new RedisValue($"{Constants.rKeyUsersData}:*");
|
|
fatto = fatto && await ExecFlushRedisPattern(pattern);
|
|
pattern = new RedisValue($"{Constants.rKeyUsersAll}:*");
|
|
fatto = fatto && await ExecFlushRedisPattern(pattern);
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Dati utente (TUTTI) da REDIS o DB
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<IdentityUser>> UserDataGetAll()
|
|
{
|
|
string source = "DB";
|
|
List<IdentityUser> dataResult = new List<IdentityUser>();
|
|
// cerco da cache
|
|
string currKey = $"{Constants.rKeyUsersAll}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<IdentityUser>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dataResult = new List<IdentityUser>();
|
|
}
|
|
else
|
|
{
|
|
dataResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dataResult = await _userManager.Users.ToListAsync();
|
|
rawData = JsonConvert.SerializeObject(dataResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dataResult == null)
|
|
{
|
|
dataResult = new List<IdentityUser>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"UserDataGetAll | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dataResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Dati utente (singolo da UID)
|
|
/// </summary>
|
|
/// <param name="UserId"></param>
|
|
/// <returns></returns>
|
|
public async Task<UserData> UserDataGetById(string UserId)
|
|
{
|
|
string source = "FULL";
|
|
UserData dataResult = new UserData();
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
// uso metodi helper protected di base...
|
|
var userRaw = await UserIdentityById(UserId);
|
|
var userIdent = new IdentityUser
|
|
{
|
|
Id = userRaw.Id,
|
|
UserName = userRaw.UserName,
|
|
Email = userRaw.Email,
|
|
PhoneNumber = userRaw.PhoneNumber,
|
|
PasswordHash = "*****",
|
|
EmailConfirmed = userRaw.EmailConfirmed
|
|
};
|
|
// cerco ruoli & claims
|
|
var UserRoles = await UserRolesById(userIdent);
|
|
var UserClaims = await UserClaimsById(userIdent);
|
|
// compongo output
|
|
dataResult = new UserData()
|
|
{
|
|
Identity = userIdent,
|
|
Roles = UserRoles,
|
|
Claims = UserClaims
|
|
};
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"UserDataGetById | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dataResult;
|
|
}
|
|
/// <summary>
|
|
/// Dati utente (singolo x UserName)
|
|
/// </summary>
|
|
/// <param name="UserName"></param>
|
|
/// <returns></returns>
|
|
public async Task<UserData> UserDataGetByName(string UserName)
|
|
{
|
|
string source = "FULL";
|
|
UserData dataResult = new UserData();
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
// uso metodi helper protected di base...
|
|
var userRaw = await UserIdentityByName(UserName);
|
|
var userIdent = new IdentityUser
|
|
{
|
|
Id = userRaw.Id,
|
|
UserName = userRaw.UserName,
|
|
Email = userRaw.Email,
|
|
PhoneNumber = userRaw.PhoneNumber,
|
|
PasswordHash = "*****",
|
|
EmailConfirmed = userRaw.EmailConfirmed
|
|
};
|
|
// cerco ruoli & claims
|
|
var UserRoles = await UserRolesById(userIdent);
|
|
var UserClaims = await UserClaimsById(userIdent);
|
|
// compongo output
|
|
dataResult = new UserData()
|
|
{
|
|
Identity = userIdent,
|
|
Roles = UserRoles,
|
|
Claims = UserClaims
|
|
};
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"UserDataGetByName | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dataResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Dati utente (filtrati da searchVal)
|
|
/// </summary>
|
|
/// <param name="searchVal"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<UserData>> UserDataGetFilt(string searchVal)
|
|
{
|
|
string source = "FULL";
|
|
List<UserData> dataResult = new List<UserData>();
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
// uso metodi helper protected di base...
|
|
List<IdentityUser> RawList = await UserIdentityBySearch(searchVal);
|
|
// conversione
|
|
var user = RawList.Select(x => new IdentityUser
|
|
{
|
|
Id = x.Id,
|
|
UserName = x.UserName,
|
|
Email = x.Email,
|
|
PhoneNumber = x.PhoneNumber,
|
|
PasswordHash = "*****",
|
|
EmailConfirmed = x.EmailConfirmed,
|
|
LockoutEnabled = x.LockoutEnabled,
|
|
LockoutEnd = x.LockoutEnd,
|
|
AccessFailedCount = x.AccessFailedCount,
|
|
ConcurrencyStamp = x.ConcurrencyStamp,
|
|
NormalizedEmail = x.NormalizedEmail,
|
|
NormalizedUserName = x.NormalizedUserName,
|
|
PhoneNumberConfirmed = x.PhoneNumberConfirmed,
|
|
SecurityStamp = x.SecurityStamp,
|
|
TwoFactorEnabled = x.TwoFactorEnabled
|
|
}).ToList();
|
|
foreach (var item in user)
|
|
{
|
|
// cerco ruoli & claims
|
|
var UserRoles = await UserRolesById(item);
|
|
var UserClaims = await UserClaimsById(item);
|
|
|
|
var newItem = new UserData()
|
|
{
|
|
Identity = item,
|
|
Roles = UserRoles,
|
|
Claims = UserClaims
|
|
};
|
|
dataResult.Add(newItem);
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"UserDataGetFilt | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dataResult;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Protected Methods
|
|
|
|
protected void reportCurrentLanguage()
|
|
{
|
|
if (EA_CurrLanguage != null)
|
|
{
|
|
EA_CurrLanguage?.Invoke();
|
|
}
|
|
}
|
|
|
|
protected void reportUserClaims()
|
|
{
|
|
if (EA_UserClaims != null)
|
|
{
|
|
EA_UserClaims?.Invoke();
|
|
}
|
|
}
|
|
|
|
protected void reportUserCurrCompany()
|
|
{
|
|
if (EA_UserCurrCompany != null)
|
|
{
|
|
EA_UserCurrCompany?.Invoke();
|
|
}
|
|
}
|
|
|
|
protected void reportUserId()
|
|
{
|
|
if (EA_UserId != null)
|
|
{
|
|
EA_UserId?.Invoke();
|
|
}
|
|
}
|
|
|
|
protected void reportUserRole()
|
|
{
|
|
if (EA_UserRole != null)
|
|
{
|
|
EA_UserRole?.Invoke();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Dati utente CLAIMS da REDIS o DB
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
protected async Task<List<Claim>> UserClaimsById(IdentityUser UserIdent)
|
|
{
|
|
string source = "DB";
|
|
List<Claim> dataResult = new List<Claim>();
|
|
// cerco da cache
|
|
string currKey = $"{Constants.rKeyUsersData}:Claims:{UserIdent.Id}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<Claim>>(rawData, new ClaimConverter());
|
|
if (tempResult != null)
|
|
{
|
|
dataResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var rawResult = await _userManager.GetClaimsAsync(UserIdent);
|
|
dataResult = rawResult.ToList();
|
|
rawData = JsonConvert.SerializeObject(dataResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dataResult == null)
|
|
{
|
|
dataResult = new List<Claim>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"UserClaimsById | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dataResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Dati utente IDENTITY da REDIS o DB da UID
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
protected async Task<IdentityUser> UserIdentityById(string UserId)
|
|
{
|
|
string source = "DB";
|
|
IdentityUser dataResult = new IdentityUser();
|
|
// cerco da cache
|
|
string currKey = $"{Constants.rKeyUsersData}:Identity:{UserId}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<IdentityUser>(rawData);
|
|
if (tempResult != null)
|
|
{
|
|
dataResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dataResult = await _userManager.FindByIdAsync(UserId);
|
|
rawData = JsonConvert.SerializeObject(dataResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dataResult == null)
|
|
{
|
|
dataResult = new IdentityUser();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"UserIdentityById | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dataResult;
|
|
}
|
|
/// <summary>
|
|
/// Dati utente IDENTITY da REDIS o DB per NAME
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
protected async Task<IdentityUser> UserIdentityByName(string uName)
|
|
{
|
|
string source = "DB";
|
|
IdentityUser dataResult = new IdentityUser();
|
|
// cerco da cache
|
|
string currKey = $"{Constants.rKeyUsersData}:UN:{uName}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<IdentityUser>(rawData);
|
|
if (tempResult != null)
|
|
{
|
|
dataResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dataResult = await _userManager.FindByNameAsync(uName);
|
|
rawData = JsonConvert.SerializeObject(dataResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dataResult == null)
|
|
{
|
|
dataResult = new IdentityUser();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"UserIdentityByName | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dataResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Dati utente IDENTITY in modalità SEARCH da REDIS o DB
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
protected async Task<List<IdentityUser>> UserIdentityBySearch(string searchVal)
|
|
{
|
|
string source = "DB";
|
|
List<IdentityUser> dataResult = new List<IdentityUser>();
|
|
// cerco da cache
|
|
string currKey = $"{Constants.rKeyUsersDataSearch}:{searchVal}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<IdentityUser>>(rawData);
|
|
if (tempResult != null)
|
|
{
|
|
dataResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var allData = await UserDataGetAll();
|
|
if (!string.IsNullOrEmpty(searchVal))
|
|
{
|
|
dataResult = allData
|
|
.Where(x => x.NormalizedEmail.Contains(searchVal.ToUpper()) || x.NormalizedUserName.Contains(searchVal.ToUpper())).ToList();
|
|
}
|
|
else
|
|
{
|
|
dataResult = allData;
|
|
}
|
|
rawData = JsonConvert.SerializeObject(dataResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dataResult == null)
|
|
{
|
|
dataResult = new List<IdentityUser>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"UserIdentityBySearch | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dataResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Dati utente ROLES da REDIS o DB
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
protected async Task<List<string>> UserRolesById(IdentityUser UserIdent)
|
|
{
|
|
string source = "DB";
|
|
List<string> dataResult = new List<string>();
|
|
// cerco da cache
|
|
string currKey = $"{Constants.rKeyUsersData}:Roles:{UserIdent.Id}";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
string? rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<string>>(rawData);
|
|
if (tempResult != null)
|
|
{
|
|
dataResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var rawResult = await _userManager.GetRolesAsync(UserIdent);
|
|
dataResult = rawResult.ToList();
|
|
rawData = JsonConvert.SerializeObject(dataResult, JSSettings);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (dataResult == null)
|
|
{
|
|
dataResult = new List<string>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"UserRolesById | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dataResult;
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Private Fields
|
|
|
|
private static JsonSerializerSettings? JSSettings;
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
private readonly UserManager<IdentityUser> _userManager;
|
|
|
|
/// <summary>
|
|
/// Durata cache lunga IN SECONDI
|
|
/// </summary>
|
|
private int cacheTtlLong = 60 * 5;
|
|
|
|
/// <summary>
|
|
/// Durata cache breve IN SECONDI
|
|
/// </summary>
|
|
private int cacheTtlShort = 60 * 1;
|
|
|
|
/// <summary>
|
|
/// Oggetto per connessione a REDIS
|
|
/// </summary>
|
|
private IConnectionMultiplexer redisConn;
|
|
|
|
/// <summary>
|
|
/// Oggetto DB redis da impiegare x chiamate R/W
|
|
/// </summary>
|
|
private IDatabase redisDb = null!;
|
|
|
|
private Random rnd = new Random();
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Properties
|
|
|
|
private string? _currLanguage { get; set; } = null;
|
|
|
|
private List<string>? _userClaims { get; set; } = null;
|
|
|
|
private int _userCurrComp { get; set; }
|
|
|
|
private string _userId { get; set; } = "";
|
|
|
|
private string _userRole { get; set; } = "";
|
|
|
|
/// <summary>
|
|
/// Durata cache breve (1 min circa + perturbazione percentuale +/-10%)
|
|
/// </summary>
|
|
private TimeSpan FastCache
|
|
{
|
|
get => TimeSpan.FromSeconds(cacheTtlShort * rnd.Next(900, 1100) / 1000);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Durata cache lunga (+ perturbazione percentuale +/-10%)
|
|
/// </summary>
|
|
private TimeSpan LongCache
|
|
{
|
|
get => TimeSpan.FromSeconds(cacheTtlLong * rnd.Next(900, 1100) / 1000);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Durata cache molto breve (10 sec circa + perturbazione percentuale +/-10%)
|
|
/// </summary>
|
|
private TimeSpan UltraFastCache
|
|
{
|
|
get => TimeSpan.FromSeconds(cacheTtlShort / 6 * rnd.Next(900, 1100) / 1000);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Durata cache lunga (+ perturbazione percentuale +/-10%)
|
|
/// </summary>
|
|
private TimeSpan UltraLongCache
|
|
{
|
|
get => TimeSpan.FromSeconds(cacheTtlLong * 10 * rnd.Next(900, 1100) / 1000);
|
|
}
|
|
|
|
#endregion Private Properties
|
|
|
|
#region Private Methods
|
|
|
|
/// <summary>
|
|
/// Esegue flush memoria redis dato pattern
|
|
/// </summary>
|
|
/// <param name="pattern"></param>
|
|
/// <returns></returns>
|
|
private async Task<bool> ExecFlushRedisPattern(RedisValue pattern)
|
|
{
|
|
bool answ = false;
|
|
var listEndpoints = redisConn.GetEndPoints();
|
|
foreach (var endPoint in listEndpoints)
|
|
{
|
|
//var server = redisConnAdmin.GetServer(listEndpoints[0]);
|
|
var server = redisConn.GetServer(endPoint);
|
|
if (server != null)
|
|
{
|
|
var keyList = server.Keys(redisDb.Database, pattern);
|
|
foreach (var item in keyList)
|
|
{
|
|
await redisDb.KeyDeleteAsync(item);
|
|
}
|
|
answ = true;
|
|
}
|
|
}
|
|
|
|
return answ;
|
|
}
|
|
|
|
#endregion Private Methods
|
|
|
|
//= -1;
|
|
}
|
|
} |