Continuo metodi x gestione Enroll apps

This commit is contained in:
Samuele Locatelli
2025-01-03 11:16:52 +01:00
parent 5c1c4fd79f
commit 8e2ea9e05e
13 changed files with 279 additions and 67 deletions
+41 -10
View File
@@ -1,8 +1,13 @@
using LiMan.APi.Data;
using Core;
using LiMan.APi.Data;
using LiMan.DB.DBModels;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace LiMan.APi.Controllers
@@ -31,19 +36,40 @@ namespace LiMan.APi.Controllers
#region Public Methods
/// <summary>
/// Recupera conteggio del numero di task di enroll attivi al momento
/// GET api/enroller
/// Recupera record di enroll di una richiesta x ricavarne ID licenza da applicare
/// </summary>
/// <param name="id">Codice cliente/Installazione</param>
/// <param name="CodApp">Codice Applicazione</param>
/// <param name="id">ID richiesta</param>
/// <param name="passcode">passcode associato</param>
/// <returns></returns>
[HttpGet("{id}")]
public async Task<int> Get(string id = "NA", string CodApp = "Updater")
public async Task<EnrollRequestModel> Get(string id, int passcode)
{
var resList = await dataService.EnrollReqGetActive();
int result = resList.Count;
await dataService.recordCall(id, CodApp, $"GET:api/enroller");
return result;
string CodInst = "NA";
string CodApp = "Updater";
int reqId = 0;
int.TryParse(id, out reqId);
EnrollRequestModel reqRec = await dataService.EnrollReqGetById(reqId);
// solo se il passcode è corretto restituisco record, altrimenti fake one...
if (reqRec != null && reqRec.Passcode != passcode)
{
reqRec = new EnrollRequestModel() { IdReq = reqId };
}
await dataService.recordCall(CodInst, CodApp, $"GET:api/enroller/{id}");
return reqRec;
}
/// <summary>
/// Richiesta di un record con codice TOTP per l'enroll di un app client
/// POST api/enroller/getNewCode
/// </summary>
[HttpPost("getEnrollRec")]
public async Task<EnrollRequestModel> GetEnrollRec([FromBody] Dictionary<string, string> MachineInfo)
{
string CodInst = "NA";
string CodApp = "Updater";
var newRec = await dataService.EnrollReqCreate(MachineInfo);
await dataService.recordCall(CodInst, CodApp, $"GET:api/enroller/GetEnrollRec");
return newRec;
}
#endregion Public Methods
@@ -64,6 +90,11 @@ namespace LiMan.APi.Controllers
/// </summary>
private static Logger Log = LogManager.GetCurrentClassLogger();
/// <summary>
/// Generatore pseudocasuale
/// </summary>
private Random rnd = new Random();
#endregion Private Fields
}
}
+173 -10
View File
@@ -296,13 +296,13 @@ namespace LiMan.APi.Data
{
bool taskDone = false;
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
Stopwatch sw = new Stopwatch();
sw.Start();
taskDone = dbController.AttivazioniTryRefresh(MasterKey, ParamDict);
await InvalidateAllCache();
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
sw.Stop();
TimeSpan ts = sw.Elapsed;
Log.Trace($"Effettuata scrittura + rilettura da DB per AttivazioniTryRefresh: {ts.TotalMilliseconds} ms");
return await Task.FromResult(taskDone);
@@ -317,6 +317,119 @@ namespace LiMan.APi.Data
dbController.Dispose();
}
/// <summary>
/// Crea record richiesta enroll (univoco rispetto richieste correnti)
/// </summary>
/// <param name="MachineInfo">Dati da associare alla richeista</param>
/// <returns></returns>
public async Task<EnrollRequestModel> EnrollReqCreate(Dictionary<string, string> MachineInfo)
{
Stopwatch sw = new Stopwatch();
sw.Start();
// svuoto elenco richieste scaduteù
var cleanDone = await EnrollReqPurgeInvalid();
// prendo elenco attive x evitare duplicazioni codici TOTP...
var resList = await EnrollReqGetActive();
int totpCode = rnd.Next(1, 100000000);
// verifico che non sia preesistente...
if (resList.Count > 0)
{
// cerco se fosse già presente...
while (resList.Where(x => x.Passcode == totpCode).Any())
{
// genero nuovo...
await Task.Delay(rnd.Next(50));
totpCode = rnd.Next(1, 100000000);
}
}
// serializzo i dati della richiesta..
string reqPayload = JsonConvert.SerializeObject(MachineInfo);
// preparo il record da registrare...
EnrollRequestModel newReq = new EnrollRequestModel()
{
DtReq = DateTime.Now,
Passcode = totpCode,
ReqPayload = reqPayload
};
var dbRec = await EnrollReqUpsert(newReq);
sw.Stop();
TimeSpan ts = sw.Elapsed;
Log.Trace($"Effettuata EnrollReqCreate: {ts.TotalMilliseconds} ms");
// restituisce elenco
return dbRec;
}
/// <summary>
/// Elimino una richiesta enroll (anche se già approvata...)
/// </summary>
/// <param name="idReq">ID record</param>
public async Task<bool> EnrollReqDelete(int idReq)
{
bool fatto = false;
// inserimento!
Stopwatch sw = new Stopwatch();
sw.Start();
fatto = dbController.EnrollReqDelete(idReq);
// svuota eventuale cache redis...
await FlushRedisCachePattern("Enroll");
sw.Stop();
TimeSpan ts = sw.Elapsed;
Log.Trace($"Effettuata EnrollReqDelete: {ts.TotalMilliseconds} ms");
return fatto;
}
/// <summary>
/// Recupera una richiesta dato suo ID x verificare approvazione e dati associati...
/// </summary>
/// <param name="idReq">ID record</param>
public async Task<EnrollRequestModel> EnrollReqGetById(int idReq)
{
string source = "DB";
EnrollRequestModel dbResult = new EnrollRequestModel();
try
{
string currKey = $"{Const.rKeyConfig}:Enroll:ById:{idReq}";
Stopwatch sw = new Stopwatch();
sw.Start();
string? rawData = await redisDb.StringGetAsync(currKey);
if (!string.IsNullOrEmpty(rawData))
{
source = "REDIS";
var tempResult = JsonConvert.DeserializeObject<EnrollRequestModel>(rawData);
if (tempResult == null)
{
dbResult = new EnrollRequestModel();
}
else
{
dbResult = tempResult;
}
}
else
{
dbResult = dbController.EnrollReqGetById(idReq);
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
await redisDb.StringSetAsync(currKey, rawData, FastCache);
}
if (dbResult == null)
{
dbResult = new EnrollRequestModel();
}
sw.Stop();
TimeSpan ts = sw.Elapsed;
Log.Debug($"EnrollReqGetById | {source} in: {ts.TotalMilliseconds} ms");
}
catch (Exception exc)
{
Log.Error($"Error during EnrollReqGetById:{Environment.NewLine}{exc}");
}
return dbResult;
}
/// <summary>
/// Elenco richeiste enroll attive al momento
/// </summary>
@@ -365,6 +478,39 @@ namespace LiMan.APi.Data
return dbResult;
}
/// <summary>
/// Elimino eventuali richieste non approvate e scadute
/// </summary>
public async Task<bool> EnrollReqPurgeInvalid()
{
var reqPurged = dbController.EnrollReqPurgeInvalid();
// svuota eventuale cache redis...
await FlushRedisCachePattern("Enroll");
return await Task.FromResult(reqPurged);
}
/// <summary>
/// Upsert record richiesta enroll
/// </summary>
/// <param name="newRec"></param>
/// <returns></returns>
public async Task<EnrollRequestModel> EnrollReqUpsert(EnrollRequestModel newRec)
{
int recId = 0;
// inserimento!
Stopwatch sw = new Stopwatch();
sw.Start();
recId = dbController.EnrollReqUpsert(newRec);
await FlushRedisCachePattern("Enroll");
var dbResult = await EnrollReqGetById(recId);
// svuota eventuale cache redis...
sw.Stop();
TimeSpan ts = sw.Elapsed;
Log.Trace($"Effettuata EnrollReqUpsert: {ts.TotalMilliseconds} ms");
// restituisce risultato
return dbResult;
}
/// <summary>
/// Esegue aggiunta file dato ticket e list uploadResult
/// </summary>
@@ -376,11 +522,11 @@ namespace LiMan.APi.Data
{
bool fatto = false;
// inserimento!
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
Stopwatch sw = new Stopwatch();
sw.Start();
fatto = dbController.FileAdd(idxTicket, baseDir, fileUploaded);
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
sw.Stop();
TimeSpan ts = sw.Elapsed;
Log.Trace($"Effettuata inserimento con FileAdd: {ts.TotalMilliseconds} ms");
// restituisce elenco
@@ -405,7 +551,7 @@ namespace LiMan.APi.Data
}
/// <summary>
/// Refresh globale cache redis
/// Refresh globale cache Redis
/// </summary>
/// <returns></returns>
public async Task<bool> FlushRedisCache()
@@ -420,6 +566,23 @@ namespace LiMan.APi.Data
return answ;
}
/// <summary>
/// Refresh cache Redis dato redPattern
/// </summary>
/// <param name="pattern">Pattern da eliminare</param>
/// <returns></returns>
public async Task<bool> FlushRedisCachePattern(string pattern)
{
Stopwatch sw = new Stopwatch();
sw.Start();
await Task.Delay(1);
RedisValue redPattern = new RedisValue($"{Const.rKeyConfig}:{pattern}*");
bool answ = await ExecFlushRedisPattern(redPattern);
sw.Stop();
Log.Debug($"FlushRedisCachePattern in {sw.Elapsed.TotalMilliseconds} ms");
return answ;
}
/// <summary>
/// invalida tutta la cache in caso di update
/// </summary>
@@ -961,7 +1124,7 @@ namespace LiMan.APi.Data
#region Protected Methods
/// <summary>
/// Esegue flush memoria redis dato pattern
/// Esegue flush memoria redis dato redPattern
/// </summary>
/// <param name="pattern"></param>
/// <returns></returns>
+1 -1
View File
@@ -1,6 +1,6 @@
<body>
<i>License Manager</i>
<h4>Versione: 1.1.2501.0309</h4>
<h4>Versione: 1.1.2501.0311</h4>
<br /> Note di rilascio:
<ul>
<li>
+1 -1
View File
@@ -1 +1 @@
1.1.2501.0309
1.1.2501.0311
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<item>
<version>1.1.2501.0309</version>
<version>1.1.2501.0311</version>
<url>https://nexus.steamware.net/repository/SWS/LiMan/stable/LAST/LiMan.UI.zip</url>
<changelog>https://nexus.steamware.net/repository/SWS/LiMan/stable/LAST/ChangeLog.html</changelog>
<mandatory>false</mandatory>
+55 -37
View File
@@ -739,11 +739,10 @@ namespace LiMan.DB.Controllers
//Log.Info("Dispose di GWMSController");
}
/// <summary>
/// Elimino una richiesta enroll (anche se già approvata...)
/// </summary>
/// <param name="delRec">record da eliminare</param>
/// <param name="idReq">ID record da eliminare</param>
public bool EnrollReqDelete(int idReq)
{
bool fatto = false;
@@ -766,36 +765,23 @@ namespace LiMan.DB.Controllers
}
return fatto;
}
/// <summary>
/// Elimino eventuali richieste non approvate e scadute
/// Elenco Enroll Attivi (non completati/cancellati)
/// </summary>
/// <param name="delRec">record da eliminare</param>
public bool EnrollReqPurgeInvalid(int idReq)
/// <returns></returns>
public List<EnrollRequestModel> EnrollReqGetActive()
{
bool fatto = false;
List<EnrollRequestModel> dbResult = new List<EnrollRequestModel>();
using (LMDbContext localDbCtx = new LMDbContext(_configuration))
{
List<EnrollRequestModel> item2del = new List<EnrollRequestModel>();
// cerco in primis richieste NON associate a licenza
var list2check = localDbCtx
.DbSetEnrollReq
.Where(x => x.IdxLic == 0)
.ToList();
var list2del = list2check
.Where(x => x.IsScaduta)
.ToList();
if (list2del != null)
{
localDbCtx
.DbSetEnrollReq
.RemoveRange(list2del);
// salvo
int numDone = localDbCtx.SaveChanges();
fatto = numDone != 0;
}
dbResult = localDbCtx
.DbSetEnrollReq
.Where(x => x.DtAppr == null)
.OrderByDescending(x => x.DtReq)
.ToList();
}
return fatto;
return dbResult;
}
/// <summary>
@@ -814,20 +800,20 @@ namespace LiMan.DB.Controllers
}
return dbResult;
}
/// <summary>
/// Elenco Enroll Attivi (non completati/cancellati)
/// Recupero richiesta da ID (oppure empty...)
/// </summary>
/// <param name="idReq"></param>
/// <returns></returns>
public List<EnrollRequestModel> EnrollReqGetActive()
public EnrollRequestModel EnrollReqGetById(int idReq)
{
List<EnrollRequestModel> dbResult = new List<EnrollRequestModel>();
EnrollRequestModel dbResult = new EnrollRequestModel();
using (LMDbContext localDbCtx = new LMDbContext(_configuration))
{
dbResult = localDbCtx
.DbSetEnrollReq
.Where(x => x.DtAppr == null)
.OrderByDescending(x => x.DtReq)
.ToList();
.FirstOrDefault(x => x.IdReq==idReq);
}
return dbResult;
}
@@ -852,14 +838,44 @@ namespace LiMan.DB.Controllers
return dbResult;
}
/// <summary>
/// Elimino eventuali richieste non approvate e scadute
/// </summary>
public bool EnrollReqPurgeInvalid()
{
bool fatto = false;
using (LMDbContext localDbCtx = new LMDbContext(_configuration))
{
List<EnrollRequestModel> item2del = new List<EnrollRequestModel>();
// cerco in primis richieste NON associate a licenza
var list2check = localDbCtx
.DbSetEnrollReq
.Where(x => x.IdxLic == 0)
.ToList();
var list2del = list2check
.Where(x => x.IsScaduta)
.ToList();
if (list2del != null)
{
localDbCtx
.DbSetEnrollReq
.RemoveRange(list2del);
// salvo
int numDone = localDbCtx.SaveChanges();
fatto = numDone != 0;
}
}
return fatto;
}
/// <summary>
/// Upsert record richiesta enroll
/// </summary>
/// <param name="newRec"></param>
/// <returns></returns>
public bool EnrollReqUpsert(EnrollRequestModel newRec)
public int EnrollReqUpsert(EnrollRequestModel newRec)
{
bool answ = false;
int answ = 0;
using (LMDbContext localDbCtx = new LMDbContext(_configuration))
{
try
@@ -878,15 +894,17 @@ namespace LiMan.DB.Controllers
currData.IdxLic = newRec.IdxLic;
// segno aggiornato
localDbCtx.Entry(currData).State = EntityState.Modified;
localDbCtx.SaveChanges();
answ = newRec.IdReq;
}
else
{
localDbCtx
var result = localDbCtx
.DbSetEnrollReq
.Add(newRec);
localDbCtx.SaveChanges();
answ = newRec.IdReq;
}
localDbCtx.SaveChanges();
answ = true;
}
catch (Exception exc)
{
+1 -1
View File
@@ -1,6 +1,6 @@
<body>
<i>License Manager</i>
<h4>Versione: 1.1.2412.3111</h4>
<h4>Versione: 1.1.2501.0311</h4>
<br />
Note di rilascio:
<ul>
+1 -1
View File
@@ -1 +1 @@
1.1.2412.3111
1.1.2501.0311
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<item>
<version>1.1.2412.3111</version>
<version>1.1.2501.0311</version>
<url>https://nexus.steamware.net/repository/SWS/LiMan/stable/LAST/LiMan.Transfer.zip</url>
<changelog>https://nexus.steamware.net/repository/SWS/LiMan/stable/LAST/ChangeLog.html</changelog>
<mandatory>false</mandatory>
+1 -1
View File
@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Version>1.1.2412.3111</Version>
<Version>1.1.2501.0311</Version>
<RootNamespace>LiMan.UI</RootNamespace>
<AssemblyName>LiMan.UI</AssemblyName>
</PropertyGroup>
+1 -1
View File
@@ -1,6 +1,6 @@
<body>
<i>License Manager</i>
<h4>Versione: 1.1.2412.3111</h4>
<h4>Versione: 1.1.2501.0311</h4>
<br /> Note di rilascio:
<ul>
<li>
+1 -1
View File
@@ -1 +1 @@
1.1.2412.3111
1.1.2501.0311
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<item>
<version>1.1.2412.3111</version>
<version>1.1.2501.0311</version>
<url>https://nexus.steamware.net/repository/SWS/LiMan/stable/LAST/LiMan.UI.zip</url>
<changelog>https://nexus.steamware.net/repository/SWS/LiMan/stable/LAST/ChangeLog.html</changelog>
<mandatory>false</mandatory>