using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Newtonsoft.Json; using NLog; using NLog.Fluent; using Org.BouncyCastle.Asn1.X500; using StackExchange.Redis; using System.Diagnostics; using WebDoorCreator.Core; using WebDoorCreator.Data.DbModels; using WebDoorCreator.Data.User; namespace WebDoorCreator.UI.Data { public class WDCUserService { #region Public Constructors private static Logger Log = LogManager.GetCurrentClassLogger(); /// /// Durata cache lunga IN SECONDI /// private int cacheTtlLong = 60 * 5; /// /// Durata cache breve IN SECONDI /// private int cacheTtlShort = 60 * 1; /// /// Oggetto per connessione a REDIS /// private IConnectionMultiplexer redisConn; /// /// Oggetto DB redis da impiegare x chiamate R/W /// private IDatabase redisDb = null!; private static JsonSerializerSettings? JSSettings; /// /// Durata cache breve (1 min circa + perturbazione percentuale +/-10%) /// private TimeSpan FastCache { get => TimeSpan.FromSeconds(cacheTtlShort * rnd.Next(900, 1100) / 1000); } /// /// Durata cache lunga (+ perturbazione percentuale +/-10%) /// private TimeSpan LongCache { get => TimeSpan.FromSeconds(cacheTtlLong * rnd.Next(900, 1100) / 1000); } /// /// Durata cache molto breve (10 sec circa + perturbazione percentuale +/-10%) /// private TimeSpan UltraFastCache { get => TimeSpan.FromSeconds(cacheTtlShort / 6 * rnd.Next(900, 1100) / 1000); } /// /// Durata cache lunga (+ perturbazione percentuale +/-10%) /// private TimeSpan UltraLongCache { get => TimeSpan.FromSeconds(cacheTtlLong * 10 * rnd.Next(900, 1100) / 1000); } private Random rnd = new Random(); /// /// Esegue flush memoria redis dato pattern /// /// /// private async Task 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; } public WDCUserService(IConnectionMultiplexer redisConnMult, UserManager 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? 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 UserPref { get; set; } = new Dictionary(); public string userRole { get => _userRole; set { if (_userRole != value) { _userRole = value; reportUserRole(); } } } #endregion Public Properties #region Public Methods /// /// Dati utente (TUTTI) da REDIS o DB /// /// public async Task> UserDataGetAll() { string source = "DB"; List dbResult = new List(); // 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>(rawData); if (tempResult == null) { dbResult = new List(); } else { dbResult = tempResult; } } else { dbResult = await _userManager.Users.ToListAsync(); rawData = JsonConvert.SerializeObject(dbResult, JSSettings); await redisDb.StringSetAsync(currKey, rawData, LongCache); } if (dbResult == null) { dbResult = new List(); } stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; Log.Debug($"UserDataGetAll | {source} in: {ts.TotalMilliseconds} ms"); return dbResult; } /// /// Dati utente (filtrati da UserId) /// /// /// public async Task> UserDataGetFilt(string searchVal) { // Collezione utenti List RawList = new List(); List UsersList = new List(); #if false // recupero utenti da obj _userManager var allData = _userManager.Users.ToList(); #endif var allData = await UserDataGetAll(); if (!string.IsNullOrEmpty(searchVal)) { RawList = allData.Where(x => x.NormalizedEmail.Contains(searchVal.ToUpper()) || x.NormalizedUserName.Contains(searchVal.ToUpper())).ToList(); } else { RawList = allData; } var user = RawList.Select(x => new IdentityUser { Id = x.Id, UserName = x.UserName, Email = x.Email, PhoneNumber = x.PhoneNumber, PasswordHash = "*****", EmailConfirmed = x.EmailConfirmed }).ToList(); foreach (var item in user) { var UserRoles = await _userManager.GetRolesAsync(item); var UserClaims = await _userManager.GetClaimsAsync(item); var newItem = new UserData() { Identity = item, Roles = UserRoles.ToList(), Claims = UserClaims.ToList() }; UsersList.Add(newItem); } return await Task.FromResult(UsersList); } /// /// Dati utente (singolo x UserId) /// /// /// public async Task> UserDataGetById(string UserId) { // Collezione utenti List RawList = new List(); List UsersList = new List(); // recupero utenti da obj _userManager var allData = _userManager.Users.ToList(); if (!string.IsNullOrEmpty(UserId)) { RawList = allData.Where(x => x.NormalizedEmail.Contains(UserId.ToUpper()) || x.NormalizedUserName.Contains(UserId.ToUpper())).ToList(); } else { RawList = allData; } var user = RawList.Select(x => new IdentityUser { Id = x.Id, UserName = x.UserName, Email = x.Email, PhoneNumber = x.PhoneNumber, PasswordHash = "*****", EmailConfirmed = x.EmailConfirmed }).ToList(); foreach (var item in user) { var UserRoles = await _userManager.GetRolesAsync(item); var UserClaims = await _userManager.GetClaimsAsync(item); var newItem = new UserData() { Identity = item, Roles = UserRoles.ToList(), Claims = UserClaims.ToList() }; UsersList.Add(newItem); } return await Task.FromResult(UsersList); } #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(); } } #endregion Protected Methods #region Private Fields private readonly UserManager _userManager; #endregion Private Fields #region Private Properties private string? _currLanguage { get; set; } = null; private List? _userClaims { get; set; } = null; private int _userCurrComp { get; set; } private string _userId { get; set; } = ""; private string _userRole { get; set; } = ""; #endregion Private Properties //= -1; } }