diff --git a/LiMan.Api/Controllers/EnrollerController.cs b/LiMan.Api/Controllers/EnrollerController.cs index f431a45..87f87d3 100644 --- a/LiMan.Api/Controllers/EnrollerController.cs +++ b/LiMan.Api/Controllers/EnrollerController.cs @@ -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 /// - /// 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 /// - /// Codice cliente/Installazione - /// Codice Applicazione + /// ID richiesta + /// passcode associato /// [HttpGet("{id}")] - public async Task Get(string id = "NA", string CodApp = "Updater") + public async Task 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; + } + + /// + /// Richiesta di un record con codice TOTP per l'enroll di un app client + /// POST api/enroller/getNewCode + /// + [HttpPost("getEnrollRec")] + public async Task GetEnrollRec([FromBody] Dictionary 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 /// private static Logger Log = LogManager.GetCurrentClassLogger(); + /// + /// Generatore pseudocasuale + /// + private Random rnd = new Random(); + #endregion Private Fields } } \ No newline at end of file diff --git a/LiMan.Api/Data/ApiDataService.cs b/LiMan.Api/Data/ApiDataService.cs index c6ad613..950342f 100644 --- a/LiMan.Api/Data/ApiDataService.cs +++ b/LiMan.Api/Data/ApiDataService.cs @@ -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(); } + /// + /// Crea record richiesta enroll (univoco rispetto richieste correnti) + /// + /// Dati da associare alla richeista + /// + public async Task EnrollReqCreate(Dictionary 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; + } + + /// + /// Elimino una richiesta enroll (anche se già approvata...) + /// + /// ID record + public async Task 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; + } + + /// + /// Recupera una richiesta dato suo ID x verificare approvazione e dati associati... + /// + /// ID record + public async Task 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(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; + } + /// /// Elenco richeiste enroll attive al momento /// @@ -365,6 +478,39 @@ namespace LiMan.APi.Data return dbResult; } + /// + /// Elimino eventuali richieste non approvate e scadute + /// + public async Task EnrollReqPurgeInvalid() + { + var reqPurged = dbController.EnrollReqPurgeInvalid(); + // svuota eventuale cache redis... + await FlushRedisCachePattern("Enroll"); + return await Task.FromResult(reqPurged); + } + + /// + /// Upsert record richiesta enroll + /// + /// + /// + public async Task 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; + } + /// /// Esegue aggiunta file dato ticket e list uploadResult /// @@ -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 } /// - /// Refresh globale cache redis + /// Refresh globale cache Redis /// /// public async Task FlushRedisCache() @@ -420,6 +566,23 @@ namespace LiMan.APi.Data return answ; } + /// + /// Refresh cache Redis dato redPattern + /// + /// Pattern da eliminare + /// + public async Task 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; + } + /// /// invalida tutta la cache in caso di update /// @@ -961,7 +1124,7 @@ namespace LiMan.APi.Data #region Protected Methods /// - /// Esegue flush memoria redis dato pattern + /// Esegue flush memoria redis dato redPattern /// /// /// diff --git a/LiMan.Api/Resources/ChangeLog.html b/LiMan.Api/Resources/ChangeLog.html index 65904f9..a01c5c1 100644 --- a/LiMan.Api/Resources/ChangeLog.html +++ b/LiMan.Api/Resources/ChangeLog.html @@ -1,6 +1,6 @@ License Manager -

Versione: 1.1.2501.0309

+

Versione: 1.1.2501.0311


Note di rilascio:
  • diff --git a/LiMan.Api/Resources/VersNum.txt b/LiMan.Api/Resources/VersNum.txt index fe971aa..3f505b4 100644 --- a/LiMan.Api/Resources/VersNum.txt +++ b/LiMan.Api/Resources/VersNum.txt @@ -1 +1 @@ -1.1.2501.0309 +1.1.2501.0311 diff --git a/LiMan.Api/Resources/manifest.xml b/LiMan.Api/Resources/manifest.xml index ef4f429..c99c341 100644 --- a/LiMan.Api/Resources/manifest.xml +++ b/LiMan.Api/Resources/manifest.xml @@ -1,6 +1,6 @@ - 1.1.2501.0309 + 1.1.2501.0311 https://nexus.steamware.net/repository/SWS/LiMan/stable/LAST/LiMan.UI.zip https://nexus.steamware.net/repository/SWS/LiMan/stable/LAST/ChangeLog.html false diff --git a/LiMan.DB/Controllers/DbController.cs b/LiMan.DB/Controllers/DbController.cs index e2f70a4..6ef1a29 100644 --- a/LiMan.DB/Controllers/DbController.cs +++ b/LiMan.DB/Controllers/DbController.cs @@ -739,11 +739,10 @@ namespace LiMan.DB.Controllers //Log.Info("Dispose di GWMSController"); } - /// /// Elimino una richiesta enroll (anche se già approvata...) /// - /// record da eliminare + /// ID record da eliminare public bool EnrollReqDelete(int idReq) { bool fatto = false; @@ -766,36 +765,23 @@ namespace LiMan.DB.Controllers } return fatto; } + /// - /// Elimino eventuali richieste non approvate e scadute + /// Elenco Enroll Attivi (non completati/cancellati) /// - /// record da eliminare - public bool EnrollReqPurgeInvalid(int idReq) + /// + public List EnrollReqGetActive() { - bool fatto = false; + List dbResult = new List(); using (LMDbContext localDbCtx = new LMDbContext(_configuration)) { - List item2del = new List(); - - // 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; } /// @@ -814,20 +800,20 @@ namespace LiMan.DB.Controllers } return dbResult; } + /// - /// Elenco Enroll Attivi (non completati/cancellati) + /// Recupero richiesta da ID (oppure empty...) /// + /// /// - public List EnrollReqGetActive() + public EnrollRequestModel EnrollReqGetById(int idReq) { - List dbResult = new List(); + 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; } + /// + /// Elimino eventuali richieste non approvate e scadute + /// + public bool EnrollReqPurgeInvalid() + { + bool fatto = false; + using (LMDbContext localDbCtx = new LMDbContext(_configuration)) + { + List item2del = new List(); + // 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; + } + /// /// Upsert record richiesta enroll /// /// /// - 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) { diff --git a/LiMan.Transfer/Resources/ChangeLog.html b/LiMan.Transfer/Resources/ChangeLog.html index 1138aa4..7b81544 100644 --- a/LiMan.Transfer/Resources/ChangeLog.html +++ b/LiMan.Transfer/Resources/ChangeLog.html @@ -1,6 +1,6 @@ License Manager -

    Versione: 1.1.2412.3111

    +

    Versione: 1.1.2501.0311


    Note di rilascio:
      diff --git a/LiMan.Transfer/Resources/VersNum.txt b/LiMan.Transfer/Resources/VersNum.txt index 111d0aa..3f505b4 100644 --- a/LiMan.Transfer/Resources/VersNum.txt +++ b/LiMan.Transfer/Resources/VersNum.txt @@ -1 +1 @@ -1.1.2412.3111 +1.1.2501.0311 diff --git a/LiMan.Transfer/Resources/manifest.xml b/LiMan.Transfer/Resources/manifest.xml index 583952b..c3f9902 100644 --- a/LiMan.Transfer/Resources/manifest.xml +++ b/LiMan.Transfer/Resources/manifest.xml @@ -1,6 +1,6 @@ - 1.1.2412.3111 + 1.1.2501.0311 https://nexus.steamware.net/repository/SWS/LiMan/stable/LAST/LiMan.Transfer.zip https://nexus.steamware.net/repository/SWS/LiMan/stable/LAST/ChangeLog.html false diff --git a/LiMan.UI/LiMan.UI.csproj b/LiMan.UI/LiMan.UI.csproj index 2b5fcff..614066d 100644 --- a/LiMan.UI/LiMan.UI.csproj +++ b/LiMan.UI/LiMan.UI.csproj @@ -2,7 +2,7 @@ net6.0 - 1.1.2412.3111 + 1.1.2501.0311 LiMan.UI LiMan.UI diff --git a/LiMan.UI/Resources/ChangeLog.html b/LiMan.UI/Resources/ChangeLog.html index 26dd0da..a01c5c1 100644 --- a/LiMan.UI/Resources/ChangeLog.html +++ b/LiMan.UI/Resources/ChangeLog.html @@ -1,6 +1,6 @@ License Manager -

      Versione: 1.1.2412.3111

      +

      Versione: 1.1.2501.0311


      Note di rilascio:
      • diff --git a/LiMan.UI/Resources/VersNum.txt b/LiMan.UI/Resources/VersNum.txt index 111d0aa..3f505b4 100644 --- a/LiMan.UI/Resources/VersNum.txt +++ b/LiMan.UI/Resources/VersNum.txt @@ -1 +1 @@ -1.1.2412.3111 +1.1.2501.0311 diff --git a/LiMan.UI/Resources/manifest.xml b/LiMan.UI/Resources/manifest.xml index 9ab1cc4..c99c341 100644 --- a/LiMan.UI/Resources/manifest.xml +++ b/LiMan.UI/Resources/manifest.xml @@ -1,6 +1,6 @@ - 1.1.2412.3111 + 1.1.2501.0311 https://nexus.steamware.net/repository/SWS/LiMan/stable/LAST/LiMan.UI.zip https://nexus.steamware.net/repository/SWS/LiMan/stable/LAST/ChangeLog.html false