275 lines
7.3 KiB
C#
275 lines
7.3 KiB
C#
using Microsoft.AspNetCore.Identity;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using StackExchange.Redis;
|
|
using WebDoorCreator.Data.DbModels;
|
|
|
|
namespace WebDoorCreator.UI.Data
|
|
{
|
|
public class WDCUserService
|
|
{
|
|
#region Public Constructors
|
|
|
|
/// <summary>
|
|
/// Classe gestione dati utente corrente (da impiegare scoped)
|
|
/// </summary>
|
|
/// <param name="redisConnMult"></param>
|
|
/// <param name="userManager"></param>
|
|
public WDCUserService(IConnectionMultiplexer redisConnMult, UserManager<IdentityUser> userManager)
|
|
{
|
|
Log.Info("WDCUserService starting...");
|
|
_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("WDCUserService started!");
|
|
}
|
|
|
|
#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 List<CompanyModel> UserCompaniesList { get; set; } = new List<CompanyModel>();
|
|
|
|
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 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 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; } = 0;
|
|
|
|
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
|
|
}
|
|
} |